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 f1e5198
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
Empty file added TestFile.py
Empty file.
13 changes: 11 additions & 2 deletions bears/java/CheckstyleBear.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@
"geosoft": "http://geosoft.no/development/geosoft_checks.xml"}


def invalid_configuration(checkstyle_configs, use_spaces, indent_size):
return (checkstyle_configs == 'google' and
(not use_spaces or indent_size != 2))


def known_checkstyle_or_path(setting):
if str(setting) in known_checkstyles.keys():
return str(setting)
Expand Down Expand Up @@ -43,8 +48,9 @@ def setup_dependencies(self):
"checkstyle.jar")

def create_arguments(
self, filename, file, config_file,
checkstyle_configs: known_checkstyle_or_path="google"):
self, filename, file, config_file,
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 +70,9 @@ def create_arguments(
- geosoft - The Java style followed by GeoSoft. More info at
<http://geosoft.no/development/javastyle.html>
"""
if invalid_configuration(checkstyle_configs, use_spaces, indent_size):
raise ValueError('Invalid configuration! Cannot proceed.')

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 f1e5198

Please sign in to comment.