Skip to content

Commit 03c1f49

Browse files
committed
Teach Scalastyle hook to fail in exceptional conditions
1 parent e2b527d commit 03c1f49

File tree

2 files changed

+66
-23
lines changed

2 files changed

+66
-23
lines changed

lib/overcommit/hook/pre_commit/scalastyle.rb

+3-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,10 @@ class Scalastyle < Base
1212

1313
def run
1414
result = execute(command, args: applicable_files)
15-
output = result.stdout.chomp
15+
output = result.stdout.chomp + result.stderr.chomp
1616
messages = output.split("\n").grep(MESSAGE_REGEX)
17-
return :pass if result.success? && messages.empty?
17+
18+
return [:fail, output] unless result.success? || messages.any?
1819

1920
# example message:
2021
# error file=/path/to/file.scala message=Error message line=1 column=1

spec/overcommit/hook/pre_commit/scalastyle_spec.rb

+63-21
Original file line numberDiff line numberDiff line change
@@ -19,33 +19,33 @@
1919

2020
context 'with no errors or warnings' do
2121
before do
22-
result.stub(:stdout).and_return([
23-
'Processed 1 file(s)',
24-
'Found 0 errors',
25-
'Found 0 warnings',
26-
'Finished in 490 ms'
27-
].join("\n"))
22+
result.stub(stderr: '', stdout: normalize_indent(<<-OUT))
23+
Processed 1 file(s)
24+
Found 0 errors
25+
Found 0 warnings
26+
Finished in 490 ms
27+
OUT
2828
end
2929

3030
it { should pass }
3131
end
3232

3333
context 'and it reports a warning' do
3434
before do
35-
result.stub(:stdout).and_return([
36-
'warning file=file1.scala message=Use : Unit = for procedures line=1 column=15',
37-
'Processed 1 file(s)',
38-
'Found 0 errors',
39-
'Found 1 warnings',
40-
'Finished in 490 ms'
41-
].join("\n"))
35+
result.stub(stderr: '', stdout: normalize_indent(<<-OUT))
36+
warning file=file1.scala message=Use : Unit = for procedures line=1 column=15
37+
Processed 1 file(s)
38+
Found 0 errors
39+
Found 1 warnings
40+
Finished in 490 ms
41+
OUT
4242
end
4343

4444
it { should warn }
4545
end
4646
end
4747

48-
context 'when scalastyle exits unsucessfully' do
48+
context 'when scalastyle exits unsuccessfully' do
4949
let(:result) { double('result') }
5050

5151
before do
@@ -55,13 +55,55 @@
5555

5656
context 'and it reports an error' do
5757
before do
58-
result.stub(:stdout).and_return([
59-
'error file=file1.scala message=Use : Unit = for procedures line=1 column=15',
60-
'Processed 1 file(s)',
61-
'Found 1 errors',
62-
'Found 0 warnings',
63-
'Finished in 490 ms'
64-
].join("\n"))
58+
result.stub(stderr: '', stdout: normalize_indent(<<-OUT))
59+
error file=file1.scala message=Use : Unit = for procedures line=1 column=15
60+
Processed 1 file(s)
61+
Found 1 errors
62+
Found 0 warnings
63+
Finished in 490 ms
64+
OUT
65+
end
66+
67+
it { should fail_hook }
68+
end
69+
70+
context 'with a usage message' do
71+
before do
72+
result.stub(stderr: '', stdout: normalize_indent(<<-OUT))
73+
scalastyle 0.7.0
74+
Usage: scalastyle [options] <source directory>
75+
-c, --config FILE configuration file (required)
76+
-v, --verbose true|false verbose output
77+
-q, --quiet true|false be quiet
78+
--xmlOutput FILE write checkstyle format output to this file
79+
--xmlEncoding STRING encoding to use for the xml file
80+
--inputEncoding STRING encoding for the source files
81+
-w, --warnings true|false fail if there are warnings
82+
-e, --externalJar FILE jar containing custom rules
83+
OUT
84+
end
85+
86+
it { should fail_hook }
87+
end
88+
89+
context 'with a runtime error' do
90+
before do
91+
# rubocop:disable Metrics/LineLength
92+
result.stub(stdout: '', stderr: normalize_indent(<<-ERR))
93+
Exception in thread "main" java.io.FileNotFoundException: scalastyle-config.xml (No such file or directory)
94+
at java.io.FileInputStream.open0(Native Method)
95+
at java.io.FileInputStream.open(FileInputStream.java:195)
96+
at java.io.FileInputStream.<init>(FileInputStream.java:138)
97+
at java.io.FileInputStream.<init>(FileInputStream.java:93)
98+
at scala.xml.Source$.fromFile(XML.scala:22)
99+
at scala.xml.factory.XMLLoader$class.loadFile(XMLLoader.scala:50)
100+
at scala.xml.XML$.loadFile(XML.scala:60)
101+
at org.scalastyle.ScalastyleConfiguration$.readFromXml(ScalastyleConfiguration.scala:87)
102+
at org.scalastyle.Main$.execute(Main.scala:106)
103+
at org.scalastyle.Main$.main(Main.scala:95)
104+
at org.scalastyle.Main.main(Main.scala)
105+
ERR
106+
# rubocop:enable Metrics/LineLength
65107
end
66108

67109
it { should fail_hook }

0 commit comments

Comments
 (0)