From 3bf797eeed54a6a6554f33261f3d5d7adb70e7cc Mon Sep 17 00:00:00 2001 From: Jeremiah Leary Date: Tue, 5 Mar 2024 08:05:51 -0600 Subject: [PATCH] Issue#17: Adding exit codes. --- docs/source/conf.py | 2 +- docs/source/exit_codes.rst | 21 ++++ docs/source/index.rst | 1 + elfws/cmd_line_args.py | 2 +- elfws/subcommand/create.py | 2 + elfws/subcommand/report.py | 6 +- elfws/subcommand/show.py | 4 +- elfws/subcommand/suppress.py | 2 + elfws/subcommand/version.py | 4 +- elfws/utils.py | 13 ++- tests/elfws/test_main.py | 96 ++++++++++++++++--- tests/junit/test_junit.py | 8 +- .../test_option.py | 8 +- 13 files changed, 146 insertions(+), 23 deletions(-) create mode 100644 docs/source/exit_codes.rst diff --git a/docs/source/conf.py b/docs/source/conf.py index 7c76b47..8dd0566 100644 --- a/docs/source/conf.py +++ b/docs/source/conf.py @@ -49,7 +49,7 @@ # Add any paths that contain custom static files (such as style sheets) here, # relative to this directory. They are copied after the builtin static files, # so a file named "default.css" will overwrite the builtin "default.css". -html_static_path = ['_static'] +#html_static_path = ['_static'] html_sidebars = { '**': [ diff --git a/docs/source/exit_codes.rst b/docs/source/exit_codes.rst new file mode 100644 index 0000000..9cecb37 --- /dev/null +++ b/docs/source/exit_codes.rst @@ -0,0 +1,21 @@ +Exit Codes +========== + +ELFWS provides the following exit codes to report execution status. + ++------+---------------------------------+ +| Code | Description | ++======+=================================+ +| 0 | Success | ++------+---------------------------------+ +| 1 | Suppression file error | ++------+---------------------------------+ +| 2 | No matching log parser | ++------+---------------------------------+ +| 3 | No command line arguments given | ++------+---------------------------------+ +| 4 | Could not write report file | ++------+---------------------------------+ +| 5 | Could not write junit file | ++------+---------------------------------+ + diff --git a/docs/source/index.rst b/docs/source/index.rst index c0eb6b0..c0061a0 100644 --- a/docs/source/index.rst +++ b/docs/source/index.rst @@ -13,6 +13,7 @@ Welcome to eda-log-file-warning-suppressor's documentation! overview installation usage + exit_codes evaluating_warnings suppression_rules theory_of_operation/theory_of_operation diff --git a/elfws/cmd_line_args.py b/elfws/cmd_line_args.py index 00526b7..8293d3d 100644 --- a/elfws/cmd_line_args.py +++ b/elfws/cmd_line_args.py @@ -33,7 +33,7 @@ def print_help_if_no_command_line_options_given(oParser): ''' if len(sys.argv) == 1: oParser.print_help() - sys.exit(1) + sys.exit(3) def add_file_arguments_to_parser(oParser): diff --git a/elfws/subcommand/create.py b/elfws/subcommand/create.py index c9e8f87..aaa8270 100644 --- a/elfws/subcommand/create.py +++ b/elfws/subcommand/create.py @@ -23,6 +23,8 @@ def create(cla): with open(cla.output_suppression_file, 'w') as file: yaml.dump(dSup, file) + sys.exit(0) + def create_suppression_dict(oWarnList): ''' diff --git a/elfws/subcommand/report.py b/elfws/subcommand/report.py index f2ce404..fc3fea6 100644 --- a/elfws/subcommand/report.py +++ b/elfws/subcommand/report.py @@ -28,11 +28,13 @@ def report(cla): build_section_5(oWarnList, lReport) build_summary(oWarnList, oSupList, lReport) - utils.write_file(cla.report_file, lReport) + utils.write_report_file(cla.report_file, lReport) if cla.junit: lJUnitFile = junit.generate_junit_xml_file(cla, oWarnList, oSupList) - utils.write_file(cla.junit, lJUnitFile) + utils.write_junit_file(cla.junit, lJUnitFile) + + sys.exit(0) def build_header(cla, lReport): diff --git a/elfws/subcommand/show.py b/elfws/subcommand/show.py index 04dce5e..5d085f0 100644 --- a/elfws/subcommand/show.py +++ b/elfws/subcommand/show.py @@ -20,4 +20,6 @@ def show(cla): if cla.junit: lJUnitFile = junit.generate_junit_xml_file(cla, oWarnList, oSupList) - utils.write_file(cla.junit, lJUnitFile) + utils.write_junit_file(cla.junit, lJUnitFile) + + sys.exit(0) diff --git a/elfws/subcommand/suppress.py b/elfws/subcommand/suppress.py index 06e439c..ed99332 100644 --- a/elfws/subcommand/suppress.py +++ b/elfws/subcommand/suppress.py @@ -21,6 +21,8 @@ def suppress(cla): display.results(cla.log_file, cla.suppression_file, oSupList, oWarnList) + sys.exit(0) + def extract_non_suppressed_warnings(oWarnList, oSupList): oReturn = warning_list.create() diff --git a/elfws/subcommand/version.py b/elfws/subcommand/version.py index 6f18df3..267546f 100644 --- a/elfws/subcommand/version.py +++ b/elfws/subcommand/version.py @@ -1,8 +1,10 @@ +import sys + from elfws import version def print_version(): print('EDA Log File Warning Suppressor (ELFWS) version ' + version.version) - exit(0) + sys.exit(0) diff --git a/elfws/utils.py b/elfws/utils.py index 3f55ed6..c51561d 100644 --- a/elfws/utils.py +++ b/elfws/utils.py @@ -299,7 +299,7 @@ def create_warning_list(lLogFile, sLogFileName): return mTool.extract_warnings(lLogFile) except AttributeError: print('ERROR: File ' + sLogFileName + ' is not recognized as a supported logfile.') - sys.exit(1) + sys.exit(2) def apply_suppression_rules_to_warnings(oWarnList, oSupList): @@ -378,7 +378,15 @@ def do_messages_match(oWarning, oSuppression): return False -def write_file(sFilename, lFile): +def write_junit_file(sFilename, lFile): + write_file(sFilename, lFile, 5) + + +def write_report_file(sFilename, lFile): + write_file(sFilename, lFile, 4) + + +def write_file(sFilename, lFile, iExitCode): ''' Writes a list of strings to a file. @@ -396,3 +404,4 @@ def write_file(sFilename, lFile): oFile.write(sLine + '\n') except PermissionError as err: print(err, "Could not write to file " + sFilename) + sys.exit(iExitCode) diff --git a/tests/elfws/test_main.py b/tests/elfws/test_main.py index 8e68a02..1dfa679 100644 --- a/tests/elfws/test_main.py +++ b/tests/elfws/test_main.py @@ -37,10 +37,14 @@ def tearDown(self): @mock.patch('sys.stdout') def test_version(self, mockStdout): sys.argv = ['elfws', 'version'] + try: __main__.main() - except SystemExit: - pass + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) + mockStdout.write.assert_has_calls([ mock.call('EDA Log File Warning Suppressor (ELFWS) version ' + version.version), mock.call('\n') @@ -52,7 +56,13 @@ def test_version(self, mockStdout): def test_show(self, mock_datetime, mock_stdout): mock_datetime.now.return_value = 'Some Date' sys.argv = ['elfws', 'show', sWarningFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lLogFile = utils.read_log_file('tests/elfws/show_output.txt') @@ -66,7 +76,13 @@ def test_show(self, mock_datetime, mock_stdout): @mock.patch('elfws.version.version', '0.1') def test_create_without_suppression_file(self): sys.argv = ['elfws', 'create', sWarningFile, sYamlFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lExpected = utils.read_log_file('tests/elfws/create_yaml_wo_suppression.yaml') lActual = utils.read_log_file(sYamlFile) @@ -76,7 +92,13 @@ def test_create_without_suppression_file(self): @mock.patch('elfws.version.version', '0.1') def test_create_with_suppression_file(self): sys.argv = ['elfws', 'create', sWarningFile, sYamlFile, '--suppression_file', sSupFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lExpected = utils.read_log_file('tests/elfws/create_yaml_with_suppression.yaml') lActual = utils.read_log_file(sYamlFile) @@ -89,7 +111,13 @@ def test_create_with_suppression_file(self): def test_suppress(self, mock_datetime, mock_stdout): mock_datetime.now.return_value = 'Some Date' sys.argv = ['elfws', 'suppress', sWarningFile, sSupFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lLogFile = utils.read_log_file('tests/elfws/suppress_output.txt') @@ -105,7 +133,13 @@ def test_suppress(self, mock_datetime, mock_stdout): def test_report(self, mock_datetime): mock_datetime.now.return_value = 'Some Date' sys.argv = ['elfws', 'report', sWarningFile, 'tests/subcommand/report/suppress_microsemi_designer_logfile.yaml', sReportFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lExpected = utils.read_log_file('tests/elfws/report_output.txt') lActual = utils.read_log_file(sReportFile) @@ -117,7 +151,13 @@ def test_report(self, mock_datetime): def test_report_all_warnings_suppressed(self, mock_datetime): mock_datetime.now.return_value = 'Some Date' sys.argv = ['elfws', 'report', sWarningFile, 'tests/elfws/suppress_all_microsemi_designer_warnings.yaml', sReportFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lExpected = utils.read_log_file('tests/elfws/report_output_all_warnings_suppressed.txt') lActual = utils.read_log_file(sReportFile) @@ -129,7 +169,13 @@ def test_report_all_warnings_suppressed(self, mock_datetime): def test_report_no_warnings(self, mock_datetime): mock_datetime.now.return_value = 'Some Date' sys.argv = ['elfws', 'report', 'tests/elfws/no_warnings/warning_messages.rpt', 'tests/elfws/no_warnings/suppression_file.yaml', sReportFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lExpected = utils.read_log_file('tests/elfws/no_warnings/report_output.txt') lActual = utils.read_log_file(sReportFile) @@ -141,7 +187,13 @@ def test_report_no_warnings(self, mock_datetime): def test_report_w_long_id(self, mock_datetime): mock_datetime.now.return_value = 'Some Date' sys.argv = ['elfws', 'report', 'tests/vendor/microsemi/designer/warning_messages_w_long_id.rpt', 'tests/subcommand/report/suppress_microsemi_designer_logfile_w_long_id.yaml', sReportFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lExpected = utils.read_log_file('tests/elfws/report_output_long_id.txt') lActual = utils.read_log_file(sReportFile) @@ -153,7 +205,13 @@ def test_report_w_long_id(self, mock_datetime): def test_report_w_investigate(self, mock_datetime): mock_datetime.now.return_value = 'Some Date' sys.argv = ['elfws', 'report', 'tests/vendor/microsemi/designer/warning_messages.log', 'tests/elfws/investigate/suppression.yaml', sReportFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lExpected = utils.read_log_file('tests/elfws/investigate/report_output.txt') lActual = utils.read_log_file(sReportFile) @@ -162,7 +220,13 @@ def test_report_w_investigate(self, mock_datetime): def test_report_w_junit_output(self): sys.argv = ['elfws', 'report', 'tests/vendor/microsemi/designer/warning_messages.log', 'tests/subcommand/report/suppress_for_junit_xml.yaml', sReportFile, '--junit', sXmlFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lExpected = utils.read_log_file('tests/subcommand/report/junit_output.xml') lActual = utils.read_log_file(sXmlFile) @@ -175,7 +239,13 @@ def test_report_w_junit_output(self): @mock.patch('sys.stdout') def test_show_w_junit_output(self, mockStdout): sys.argv = ['elfws', 'show', 'tests/vendor/microsemi/designer/warning_messages.log', '--junit', sXmlFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lExpected = utils.read_log_file('tests/subcommand/show/junit_output.xml') lActual = utils.read_log_file(sXmlFile) diff --git a/tests/junit/test_junit.py b/tests/junit/test_junit.py index 6260411..53fdbc4 100644 --- a/tests/junit/test_junit.py +++ b/tests/junit/test_junit.py @@ -26,7 +26,13 @@ def tearDown(self): @mock.patch('sys.stdout') def test_report_w_junit_output(self, mockStdout): sys.argv = ['elfws', 'show', sWarningFile, '--junit', sXmlFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lExpected = utils.read_log_file(os.path.join(os.path.dirname(__file__), 'junit_output.xml')) lActual = utils.read_log_file(sXmlFile) diff --git a/tests/option/suppress_in_json_if_unmatched/test_option.py b/tests/option/suppress_in_json_if_unmatched/test_option.py index aaf4b35..fb2db51 100644 --- a/tests/option/suppress_in_json_if_unmatched/test_option.py +++ b/tests/option/suppress_in_json_if_unmatched/test_option.py @@ -20,7 +20,13 @@ class test_arguments(unittest.TestCase): def test_report_w_junit_output(self, mock_datetime): mock_datetime.now.return_value = 'Some Date' sys.argv = ['elfws', 'report', os.path.join(os.path.dirname(__file__), 'warning_messages.log'), os.path.join(os.path.dirname(__file__), 'suppression.yaml'), sReportFile, '--junit', sXmlFile] - __main__.main() + + try: + __main__.main() + except SystemExit as e: + iExitStatus = e.args[0] + + self.assertEqual(iExitStatus, 0) lExpected = utils.read_log_file('tests/option/suppress_in_json_if_unmatched/expected.xml') lActual = utils.read_log_file(sXmlFile)