From 761d9196772bc150fd4923fb04367b919a521d59 Mon Sep 17 00:00:00 2001 From: Tim Burke Date: Tue, 30 Apr 2024 14:14:30 -0700 Subject: [PATCH] tests: Use mock.patch more Change-Id: I68974338f8e0284ed77960048a83f72855b93348 --- test/unit/common/test_utils.py | 30 +++++++++++------------------- 1 file changed, 11 insertions(+), 19 deletions(-) diff --git a/test/unit/common/test_utils.py b/test/unit/common/test_utils.py index bfb8cd0657..92c7457c6a 100644 --- a/test/unit/common/test_utils.py +++ b/test/unit/common/test_utils.py @@ -904,26 +904,18 @@ class TestUtils(unittest.TestCase): self.assertEqual(options['extra_args'], ['plugin_name']) def test_parse_options_errors(self): - orig_stdout = sys.stdout - orig_stderr = sys.stderr - stdo = StringIO() - stde = StringIO() - utils.sys.stdout = stdo - utils.sys.stderr = stde - self.assertRaises(SystemExit, utils.parse_options, once=True, - test_args=[]) - self.assertTrue('missing config' in stdo.getvalue()) + with mock.patch.object(utils.sys, 'stdout', StringIO()) as stdo: + self.assertRaises(SystemExit, utils.parse_options, once=True, + test_args=[]) + self.assertTrue('missing config' in stdo.getvalue()) - # verify conf file must exist, context manager will delete temp file - with NamedTemporaryFile() as f: - conf_file = f.name - self.assertRaises(SystemExit, utils.parse_options, once=True, - test_args=[conf_file]) - self.assertTrue('unable to locate' in stdo.getvalue()) - - # reset stdio - utils.sys.stdout = orig_stdout - utils.sys.stderr = orig_stderr + # verify conf file must exist -- context manager will delete + # temp file + with NamedTemporaryFile() as f: + conf_file = f.name + self.assertRaises(SystemExit, utils.parse_options, once=True, + test_args=[conf_file]) + self.assertTrue('unable to locate' in stdo.getvalue()) @with_tempdir def test_dump_recon_cache(self, testdir_base):