Clean-up return statements

Also increase coverage of main function

Change-Id: I9f92c9c6d2141fd601f45b99facd24f53931b27e
This commit is contained in:
Stanislav Kudriashev
2014-10-08 11:57:12 +03:00
committed by Matt Odden
parent dbc021c9ee
commit e441afd8aa
2 changed files with 58 additions and 10 deletions

View File

@@ -78,16 +78,11 @@ def check_function_decl(line, report):
def starts_multiline(line):
m = re.search("[^<]<<\s*(?P<token>\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__":

View File

@@ -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)