Skip to content

Commit 1fdf035

Browse files
authored
When in manual lint/unit, allow user to skip amending (#218)
Allow the user to ignore amending changes when running linters manually. We wouldn't want this on `smartpush`, but it's not unreasonable in `lint` itself. Closes #209 Signed-off-by: Phil Dibowitz <phil@ipom.com>
1 parent 9cf11e8 commit 1fdf035

3 files changed

Lines changed: 29 additions & 12 deletions

File tree

lib/sugarjar/commands/checks.rb

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@ def lint
2222
exit(1)
2323
end
2424
end
25-
exit(1) unless run_check('lint')
25+
exit(1) unless run_check('lint', false)
2626
end
2727

2828
def unit
2929
assert_in_repo!
30-
exit(1) unless run_check('unit')
30+
exit(1) unless run_check('unit', false)
3131
end
3232

3333
def get_checks_from_command(type)
@@ -71,7 +71,13 @@ def get_checks(type)
7171
@checks[type]
7272
end
7373

74-
def run_check(type)
74+
# autorun is true when we're running from push, and false when someone
75+
# ran 'lint' or 'unit' directly
76+
#
77+
# In the case of a autorun, if a linter changes the code, we require
78+
# either the user amend, or bail out. If it's a manual run, then we
79+
# allow them to just go on.
80+
def run_check(type, autorun)
7581
repo_root = SugarJar::Util.repo_root
7682
Dir.chdir repo_root do
7783
checks = get_checks(type)
@@ -81,6 +87,7 @@ def run_check(type)
8187

8288
checks.each do |check|
8389
SugarJar::Log.debug("Running #{type} #{check}")
90+
skip_redo = false
8491

8592
short = check.split.first
8693
if short.include?('/')
@@ -104,10 +111,15 @@ def run_check(type)
104111
)
105112
puts git('diff').stdout
106113
loop do
107-
$stdout.print(
108-
"\nWould you like to\n\t[q]uit and inspect\n\t[a]mend the " +
109-
"changes to the current commit and re-run\n > ",
110-
)
114+
options = [
115+
'[q]uit and inspect',
116+
'[a]mend the changes to the current commit and re-run',
117+
]
118+
options << '[i]gnore the changes and keep going' unless autorun
119+
120+
msg = "\nWould you like to\n\t" + options.join("\n\t") + "\n > "
121+
122+
$stdout.print(msg)
111123
ans = $stdin.gets.strip
112124
case ans
113125
when /^q/
@@ -118,9 +130,14 @@ def run_check(type)
118130
# break here, if we get out of this loop we 'redo', assuming
119131
# the user chose this option
120132
break
133+
when /^i/
134+
unless autorun
135+
skip_redo = true
136+
break
137+
end
121138
end
122139
end
123-
redo
140+
redo unless skip_redo
124141
end
125142

126143
if s.error?

lib/sugarjar/commands/push.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def _smartpush(remote, branch, force)
4343
def run_prepush
4444
@repo_config['on_push']&.each do |item|
4545
SugarJar::Log.debug("Running on_push check type #{item}")
46-
unless run_check(item)
46+
unless run_check(item, true)
4747
SugarJar::Log.info("[prepush]: #{item} #{color('failed', :red)}.")
4848
return false
4949
end

spec/commands/checks_spec.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@
126126
expect($stdin).to receive(:gets).and_return("a\n")
127127
expect(sj).to receive(:qamend).with('-a')
128128
expect(sj).to receive(:dirty?).and_return(false)
129-
sj.run_check('lint')
129+
sj.run_check('lint', true)
130130
end
131131

132132
it 'quits if linter autocorrects and user says no' do
@@ -150,7 +150,7 @@
150150
raise SystemExit, 1
151151
end
152152
expect do
153-
sj.run_check('lint')
153+
sj.run_check('lint', true)
154154
end.to raise_error(SystemExit)
155155
end
156156

@@ -169,7 +169,7 @@
169169
expect(Mixlib::ShellOut).to receive(:new).with(cmd).and_return(so)
170170
expect(so).to receive(:run_command).and_return(so)
171171
expect(sj).to receive(:dirty?).and_return(false) if type == 'lint'
172-
expect(sj.run_check(type)).to eq(false)
172+
expect(sj.run_check(type, true)).to eq(false)
173173
end
174174
end
175175
end

0 commit comments

Comments
 (0)