Skip to content

Commit 52a7934

Browse files
committed
CheckstyleBear.py: Add configuration check
Add invalid_config function to CheckstyleBear.py Add tests to invalid configurations Closes coala#898
1 parent 4df9c20 commit 52a7934

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

TestFile.py

Whitespace-only changes.

bears/java/CheckstyleBear.py

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

1212

13+
def invalid_configuration(checkstyle_configs, use_spaces, indent_size):
14+
return checkstyle_configs == 'google' and \
15+
(not use_spaces or indent_size != 2)
16+
17+
1318
def known_checkstyle_or_path(setting):
1419
if str(setting) in known_checkstyles.keys():
1520
return str(setting)
@@ -43,8 +48,10 @@ def setup_dependencies(self):
4348
"checkstyle.jar")
4449

4550
def create_arguments(
46-
self, filename, file, config_file,
47-
checkstyle_configs: known_checkstyle_or_path="google"):
51+
self, filename, file, config_file,
52+
checkstyle_configs: known_checkstyle_or_path="google",
53+
use_spaces: bool = True, indent_size: int = 2
54+
):
4855
"""
4956
:param checkstyle_configs:
5057
A file containing configs to use in ``checkstyle``. It can also
@@ -64,6 +71,9 @@ def create_arguments(
6471
- geosoft - The Java style followed by GeoSoft. More info at
6572
<http://geosoft.no/development/javastyle.html>
6673
"""
74+
if invalid_configuration(checkstyle_configs, use_spaces, indent_size):
75+
raise ValueError('Invalid configuration! Cannot proceed.')
76+
6777
if checkstyle_configs in known_checkstyles:
6878
checkstyle_configs = self.download_cached_file(
6979
known_checkstyles[checkstyle_configs],

tests/java/CheckstyleBearTest.py

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

25+
def tearDown(self):
26+
self.section = Section("test section")
27+
2528
def test_run(self):
2629
self.check_validity(self.uut, [], self.good_file)
2730
self.check_validity(self.uut, [], self.bad_file, valid=False)
@@ -30,6 +33,18 @@ 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)