From b8af8be3fe422cba8ab95d596d55320b1df7af26 Mon Sep 17 00:00:00 2001 From: pascalholthaus <94793111+pascalholthaus@users.noreply.github.com> Date: Thu, 31 Mar 2022 10:48:59 +0200 Subject: [PATCH] Add: Return value for successful Linting. (#128) * Add really simple exit code handling. * Add tests for sys exit * Use only CheckCVSSFormat in test_runner_run_error * Changed assertEqual to assertTrue,assertFalse --- tests/test_runner.py | 28 ++++++++++++++++++++++++++++ troubadix/runner.py | 4 +++- troubadix/troubadix.py | 4 +++- 3 files changed, 34 insertions(+), 2 deletions(-) diff --git a/tests/test_runner.py b/tests/test_runner.py index ba79aed5..fe10af48 100644 --- a/tests/test_runner.py +++ b/tests/test_runner.py @@ -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, @@ -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, diff --git a/troubadix/runner.py b/troubadix/runner.py index 8840f1b1..9aa57183 100644 --- a/troubadix/runner.py +++ b/troubadix/runner.py @@ -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 ) @@ -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() diff --git a/troubadix/troubadix.py b/troubadix/troubadix.py index 38d4e993..7c2a365c 100644 --- a/troubadix/troubadix.py +++ b/troubadix/troubadix.py @@ -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.")