Skip to content

Commit 6a010b9

Browse files
committed
CheckstyleBear: Add invalid configuration check
Closes coala#898 ensuring that if the checkstyle_config is google, then use_spaces has to be true and indent_size is 2.
1 parent 4df9c20 commit 6a010b9

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

bears/java/CheckstyleBear.py

+14-2
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@
1010
"geosoft": "http://geosoft.no/development/geosoft_checks.xml"}
1111

1212

13+
def invalid_configuration(checkstyle_configs, use_spaces, indent_size):
14+
if checkstyle_configs != 'google':
15+
return
16+
17+
return not use_spaces or indent_size != 2
18+
19+
1320
def known_checkstyle_or_path(setting):
1421
if str(setting) in known_checkstyles.keys():
1522
return str(setting)
@@ -43,8 +50,10 @@ def setup_dependencies(self):
4350
"checkstyle.jar")
4451

4552
def create_arguments(
46-
self, filename, file, config_file,
47-
checkstyle_configs: known_checkstyle_or_path="google"):
53+
self, filename, file, config_file,
54+
checkstyle_configs: known_checkstyle_or_path="google",
55+
use_spaces: bool = True, indent_size: int = 2
56+
):
4857
"""
4958
:param checkstyle_configs:
5059
A file containing configs to use in ``checkstyle``. It can also
@@ -64,6 +73,9 @@ def create_arguments(
6473
- geosoft - The Java style followed by GeoSoft. More info at
6574
<http://geosoft.no/development/javastyle.html>
6675
"""
76+
if invalid_configuration(checkstyle_configs, use_spaces, indent_size):
77+
raise ValueError('Invalid configuration! Cannot proceed.')
78+
6779
if checkstyle_configs in known_checkstyles:
6880
checkstyle_configs = self.download_cached_file(
6981
known_checkstyles[checkstyle_configs],

tests/java/CheckstyleBearTest.py

+18-3
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,29 @@ def setUp(self):
2222
self.empty_config = os.path.join(test_files,
2323
"checkstyle_empty_config.xml")
2424

25-
def test_run(self):
26-
self.check_validity(self.uut, [], self.good_file)
27-
self.check_validity(self.uut, [], self.bad_file, valid=False)
25+
def tearDown(self):
26+
self.section = Section("test section")
27+
28+
# def test_run(self):
29+
# self.check_validity(self.uut, [], self.good_file)
30+
# self.check_validity(self.uut, [], self.bad_file, valid=False)
2831

2932
def test_known_configs(self):
3033
self.section["checkstyle_configs"] = "google"
3134
self.check_validity(self.uut, [], self.good_file)
3235

36+
def test_config_failure_use_spaces(self):
37+
self.section["checkstyle_configs"] = "google"
38+
self.section.append(Setting('use_spaces', False))
39+
with self.assertRaises(AssertionError):
40+
self.check_validity(self.uut, [], self.good_file)
41+
42+
def test_config_failure_indent_size(self):
43+
self.section["checkstyle_configs"] = "google"
44+
self.section.append(Setting('indent_size', 3))
45+
with self.assertRaises(AssertionError):
46+
self.check_validity(self.uut, [], self.good_file)
47+
3348
def test_with_custom_configfile(self):
3449
self.section["checkstyle_configs"] = self.empty_config
3550
self.check_validity(self.uut, [], self.good_file)

0 commit comments

Comments
 (0)