Skip to content

Commit

Permalink
Add: Return value for successful Linting. (#128)
Browse files Browse the repository at this point in the history
* Add really simple exit code handling.

* Add tests for sys exit

* Use only CheckCVSSFormat in test_runner_run_error

* Changed assertEqual to assertTrue,assertFalse
  • Loading branch information
pascalholthaus authored Mar 31, 2022
1 parent c4f685d commit b8af8be
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
28 changes: 28 additions & 0 deletions tests/test_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,20 @@ def test_runner_run_ok(self):
nasl_file = _here / "plugins" / "test.nasl"
content = nasl_file.read_text(encoding="latin1")

# Check sys exit 1
included_plugins = [
"CheckMissingDescExit",
]
runner = Runner(
n_jobs=1,
included_plugins=included_plugins,
term=self._term,
)
with redirect_stdout(io.StringIO()) as _:
sys_exit = runner.run([nasl_file])
self.assertFalse(sys_exit)

# Test update_date
runner = Runner(
n_jobs=1,
term=self._term,
Expand Down Expand Up @@ -109,6 +123,20 @@ def test_runner_run_error(self):
nasl_file = _here / "plugins" / "fail.nasl"
content = nasl_file.read_text(encoding="latin1")

# Check sys exit 1
included_plugins = [
"CheckCVSSFormat",
]
runner = Runner(
n_jobs=1,
included_plugins=included_plugins,
term=self._term,
)
with redirect_stdout(io.StringIO()) as _:
sys_exit = runner.run([nasl_file])
self.assertTrue(sys_exit)

# Test update_date
runner = Runner(
n_jobs=1,
term=self._term,
Expand Down
4 changes: 3 additions & 1 deletion troubadix/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def __init__(
verbose: int = 0,
statistic: bool = True,
log_file: Path = None,
) -> None:
) -> bool:
self.plugins = Plugins(
excluded_plugins, included_plugins, update_date=update_date
)
Expand Down Expand Up @@ -231,6 +231,8 @@ def run(
self._report_info(f"Time elapsed: {datetime.datetime.now() - start}")
if self.statistic:
self._report_statistic()
# Return true if error exist
return len(self.result_counts.result_counts) > 0

def check_file(self, file_path: Path) -> FileResults:
file_name = file_path.resolve()
Expand Down
4 changes: 3 additions & 1 deletion troubadix/troubadix.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,9 @@ def main(args=None):
)
sys.exit(1)
info(f"Start linting {len(parsed_args.files)} files ... ")
runner.run(parsed_args.files)
# Return exit with 1 if error exist
if runner.run(parsed_args.files):
sys.exit(1)
else:
warning("No files given/found.")

Expand Down

0 comments on commit b8af8be

Please sign in to comment.