diff --git a/bashate/bashate.py b/bashate/bashate.py index 28df02b..6cb3bd4 100644 --- a/bashate/bashate.py +++ b/bashate/bashate.py @@ -78,16 +78,11 @@ def check_function_decl(line, report): def starts_multiline(line): m = re.search("[^<]<<\s*(?P\w+)", line) - if m: - return m.group('token') - else: - return False + return m.group('token') if m else False def end_of_multiline(line, token): - if token: - return re.search("^%s\s*$" % token, line) is not None - return False + return token and re.search("^%s\s*$" % token, line) def check_arithmetic(line, report): @@ -238,11 +233,10 @@ def main(): print("bashate: %s" % e) return 1 - if run.ERRORS > 0: + if run.ERRORS: print("%d bashate error(s) found" % run.ERRORS) return 1 - else: - return 0 + return 0 if __name__ == "__main__": diff --git a/bashate/tests/test_bashate.py b/bashate/tests/test_bashate.py index 2ab009d..5e5b866 100644 --- a/bashate/tests/test_bashate.py +++ b/bashate/tests/test_bashate.py @@ -29,6 +29,60 @@ class TestBashate(base.TestCase): super(TestBashate, self).setUp() self.run = bashate.BashateRun() + @mock.patch('argparse.ArgumentParser') + @mock.patch('bashate.bashate.BashateRun') + def test_main_no_files(self, m_bashaterun, m_argparser): + m_opts = mock.MagicMock() + m_opts.files = [] + m_parser_obj = mock.MagicMock() + m_parser_obj.parse_args = mock.Mock(return_value=m_opts) + m_argparser.return_value = m_parser_obj + + m_run_obj = mock.MagicMock() + m_run_obj.ERRORS = 0 + m_bashaterun.return_value = m_run_obj + + result = bashate.main() + + m_parser_obj.print_usage.assert_called_once_with() + expected_return = 1 + self.assertEqual(expected_return, result) + + @mock.patch('argparse.ArgumentParser') + @mock.patch('bashate.bashate.BashateRun') + def test_main_return_one_on_errors(self, m_bashaterun, m_argparser): + m_opts = mock.MagicMock() + m_opts.files = 'not_a_file' + m_parser_obj = mock.MagicMock() + m_parser_obj.parse_args = mock.Mock(return_value=m_opts) + m_argparser.return_value = m_parser_obj + + m_run_obj = mock.MagicMock() + m_run_obj.ERRORS = 1 + m_bashaterun.return_value = m_run_obj + + result = bashate.main() + expected_return = 1 + self.assertEqual(expected_return, result) + + @mock.patch('argparse.ArgumentParser') + @mock.patch('bashate.bashate.BashateRun') + def test_main_return_one_on_ioerror(self, m_bashaterun, m_argparser): + m_opts = mock.MagicMock() + m_opts.files = 'not_a_file' + m_parser_obj = mock.MagicMock() + m_parser_obj.parse_args = mock.Mock(return_value=m_opts) + m_argparser.return_value = m_parser_obj + + m_run_obj = mock.MagicMock() + m_run_obj.ERRORS = 0 + m_run_obj.check_files = mock.Mock(side_effect=IOError) + m_bashaterun.return_value = m_run_obj + + result = bashate.main() + expected_return = 1 + self.assertEqual(expected_return, result) + def test_multi_ignore_with_slash(self): self.run.register_ignores('E001|E011') bashate.check_no_trailing_whitespace("if ", self.run)