Skip to content

Commit

Permalink
CheckstyleBear.py: Add configuration check
Browse files Browse the repository at this point in the history
Add invalid_config function to CheckstyleBear.py
Add tests to invalid configurations

Closes coala#898
  • Loading branch information
tylfin committed Oct 25, 2016
1 parent 4df9c20 commit 376019a
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
13 changes: 12 additions & 1 deletion bears/java/CheckstyleBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,13 @@
"geosoft": "http://geosoft.no/development/geosoft_checks.xml"}


def invalid_configuration(checkstyle_configs, use_spaces, indent_size):
if (checkstyle_configs is 'google' and
(not use_spaces or indent_size != 2)):
raise Exception('Google checkstyle config does not support ' +
'use_spaces=False or indent_size != 2')


def known_checkstyle_or_path(setting):
if str(setting) in known_checkstyles.keys():
return str(setting)
Expand Down Expand Up @@ -44,7 +51,8 @@ def setup_dependencies(self):

def create_arguments(
self, filename, file, config_file,
checkstyle_configs: known_checkstyle_or_path="google"):
checkstyle_configs: known_checkstyle_or_path="google",
use_spaces: bool=True, indent_size: int = 2):
"""
:param checkstyle_configs:
A file containing configs to use in ``checkstyle``. It can also
Expand All @@ -64,6 +72,9 @@ def create_arguments(
- geosoft - The Java style followed by GeoSoft. More info at
<http://geosoft.no/development/javastyle.html>
"""
# Check for invalid configurations based on selected checkstyle_configs
invalid_configuration(checkstyle_configs, use_spaces, indent_size)

if checkstyle_configs in known_checkstyles:
checkstyle_configs = self.download_cached_file(
known_checkstyles[checkstyle_configs],
Expand Down
12 changes: 12 additions & 0 deletions tests/java/CheckstyleBearTest.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,18 @@ def test_known_configs(self):
self.section["checkstyle_configs"] = "google"
self.check_validity(self.uut, [], self.good_file)

def test_config_failure_use_spaces(self):
self.section["checkstyle_configs"] = "google"
self.section.append(Setting('use_spaces', False))
with self.assertRaises(AssertionError):
self.check_validity(self.uut, [], self.good_file)

def test_config_failure_indent_size(self):
self.section["checkstyle_configs"] = "google"
self.section.append(Setting('indent_size', 3))
with self.assertRaises(AssertionError):
self.check_validity(self.uut, [], self.good_file)

def test_with_custom_configfile(self):
self.section["checkstyle_configs"] = self.empty_config
self.check_validity(self.uut, [], self.good_file)
Expand Down

0 comments on commit 376019a

Please sign in to comment.