From 4dc3f2af6369f0090898b8d0d27cd0cf2d849d18 Mon Sep 17 00:00:00 2001 From: Dylan Baker Date: Wed, 2 Jun 2021 15:39:06 -0700 Subject: run_unittests.py: Use mock for monkey patching it's what mock is for afterall --- run_unittests.py | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) (limited to 'run_unittests.py') diff --git a/run_unittests.py b/run_unittests.py index a0beb48..39b3496 100755 --- a/run_unittests.py +++ b/run_unittests.py @@ -1608,28 +1608,26 @@ class InternalTests(unittest.TestCase): self.assertIsInstance(kwargs['input'], str) self.assertEqual(kwargs['input'], 'foo') - # With Meson 0.1 it should trigger the "introduced" warning but not the "deprecated" warning - mesonbuild.mesonlib.project_meson_versions[''] = '0.1' - sys.stdout = io.StringIO() - _(None, mock.Mock(subproject=''), [], {'input': 'foo'}) - self.assertRegex(sys.stdout.getvalue(), r'WARNING:.*introduced.*input arg in testfunc') - self.assertNotRegex(sys.stdout.getvalue(), r'WARNING:.*deprecated.*input arg in testfunc') - - # With Meson 1.5 it shouldn't trigger any warning - mesonbuild.mesonlib.project_meson_versions[''] = '1.5' - sys.stdout = io.StringIO() - _(None, mock.Mock(subproject=''), [], {'input': 'foo'}) - self.assertNotRegex(sys.stdout.getvalue(), r'WARNING:.*') - self.assertNotRegex(sys.stdout.getvalue(), r'WARNING:.*') - - # With Meson 2.0 it should trigger the "deprecated" warning but not the "introduced" warning - mesonbuild.mesonlib.project_meson_versions[''] = '2.0' - sys.stdout = io.StringIO() - _(None, mock.Mock(subproject=''), [], {'input': 'foo'}) - self.assertRegex(sys.stdout.getvalue(), r'WARNING:.*deprecated.*input arg in testfunc') - self.assertNotRegex(sys.stdout.getvalue(), r'WARNING:.*introduced.*input arg in testfunc') - - sys.stdout = sys.__stdout__ + with mock.patch('sys.stdout', io.StringIO()) as out: + # With Meson 0.1 it should trigger the "introduced" warning but not the "deprecated" warning + mesonbuild.mesonlib.project_meson_versions[''] = '0.1' + _(None, mock.Mock(subproject=''), [], {'input': 'foo'}) + self.assertRegex(out.getvalue(), r'WARNING:.*introduced.*input arg in testfunc') + self.assertNotRegex(out.getvalue(), r'WARNING:.*deprecated.*input arg in testfunc') + + with mock.patch('sys.stdout', io.StringIO()) as out: + # With Meson 1.5 it shouldn't trigger any warning + mesonbuild.mesonlib.project_meson_versions[''] = '1.5' + _(None, mock.Mock(subproject=''), [], {'input': 'foo'}) + self.assertNotRegex(out.getvalue(), r'WARNING:.*') + self.assertNotRegex(out.getvalue(), r'WARNING:.*') + + with mock.patch('sys.stdout', io.StringIO()) as out: + # With Meson 2.0 it should trigger the "deprecated" warning but not the "introduced" warning + mesonbuild.mesonlib.project_meson_versions[''] = '2.0' + _(None, mock.Mock(subproject=''), [], {'input': 'foo'}) + self.assertRegex(out.getvalue(), r'WARNING:.*deprecated.*input arg in testfunc') + self.assertNotRegex(out.getvalue(), r'WARNING:.*introduced.*input arg in testfunc') @unittest.skipIf(is_tarball(), 'Skipping because this is a tarball release') -- cgit v1.1