From 338c5658c97d11c5895f5e5b83f3decfeff74e7d Mon Sep 17 00:00:00 2001
From: Ian Wienand <iwienand@redhat.com>
Date: Tue, 22 Aug 2017 11:14:04 +1000
Subject: [PATCH] Don't mock sysv.args
Python 3.6's argparse() must do some more introspection than previous
versions because it fails with a mocked sys.argv; for reference:
File "/usr/lib64/python3.6/argparse.py", line 1622, in __init__
prog = _os.path.basename(_sys.argv[0])
File ".tox/py36/lib64/python3.6/posixpath.py", line 144, in basename
p = os.fspath(p)
Convert main() to take an args argument, which is passed in for unit
tests, but remains sys.argv for __main__ calls
Change-Id: I2e0415dab7c90825188928d60073b8135c4bde48
(cherry picked from commit 83c32e66fd174ae1830161c9e10b7513b09dfd4a)
---
bashate/bashate.py | 7 +++++--
bashate/tests/test_bashate.py | 19 ++++++++++---------
2 files changed, 15 insertions(+), 11 deletions(-)
diff --git a/bashate/bashate.py b/bashate/bashate.py
index b1180ca..04e0149 100644
--- a/bashate/bashate.py
+++ b/bashate/bashate.py
@@ -371,7 +371,10 @@ class BashateRun(object):
report.print_error(MESSAGES['E004'].msg, line)
-def main():
+def main(args=None):
+
+ if args is None:
+ args = sys.argv[1:]
parser = argparse.ArgumentParser(
description='A bash script style checker')
@@ -384,7 +387,7 @@ def main():
help='Rules to always error (rather than warn)')
parser.add_argument('-v', '--verbose', action='store_true', default=False)
parser.add_argument('-s', '--show', action='store_true', default=False)
- opts = parser.parse_args()
+ opts = parser.parse_args(args)
if opts.show:
messages.print_messages()
diff --git a/bashate/tests/test_bashate.py b/bashate/tests/test_bashate.py
index 3039b1e..2f72d50 100644
--- a/bashate/tests/test_bashate.py
+++ b/bashate/tests/test_bashate.py
@@ -34,37 +34,38 @@ class TestBashate(base.TestCase):
self.run = bashate.BashateRun()
@mock.patch('bashate.bashate.BashateRun')
- @mock.patch('sys.argv')
- def test_main_no_files(self, m_bashaterun, m_argv):
+ def test_main_no_files(self, m_bashaterun):
m_run_obj = mock.MagicMock()
m_run_obj.error_count = 0
+ m_run_obj.warning_count = 0
m_bashaterun.return_value = m_run_obj
- result = bashate.main()
+ result = bashate.main([])
expected_return = 1
self.assertEqual(expected_return, result)
@mock.patch('bashate.bashate.BashateRun')
- @mock.patch('sys.argv')
- def test_main_return_one_on_errors(self, m_bashaterun, m_argv):
+ def test_main_return_one_on_errors(self, m_bashaterun):
m_run_obj = mock.MagicMock()
m_run_obj.warning_count = 1
m_run_obj.error_count = 1
m_bashaterun.return_value = m_run_obj
- result = bashate.main()
+ result = bashate.main([])
expected_return = 1
self.assertEqual(expected_return, result)
@mock.patch('bashate.bashate.BashateRun')
- @mock.patch('sys.argv')
- def test_main_return_one_on_ioerror(self, m_bashaterun, m_argv):
+ def test_main_return_one_on_ioerror(self, m_bashaterun):
m_run_obj = mock.MagicMock()
m_run_obj.error_count = 0
m_run_obj.check_files = mock.Mock(side_effect=IOError)
m_bashaterun.return_value = m_run_obj
- result = bashate.main()
+ result = bashate.main(['--verbose',
+ '/path/to/fileA', '/path/to/fileB'])
+ m_run_obj.check_files.assert_called_with(['/path/to/fileA',
+ '/path/to/fileB'], True)
expected_return = 1
self.assertEqual(expected_return, result)