From 70ab119de8cb158dfc0f2db9fc0ed60f4fbb1de9 Mon Sep 17 00:00:00 2001 From: JWM Date: Mon, 18 Nov 2024 11:42:38 +0100 Subject: [PATCH] Use unittest.mock.patch.dict to set envvar --- tests/test_coverity.py | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/tests/test_coverity.py b/tests/test_coverity.py index fdedf931..26432915 100644 --- a/tests/test_coverity.py +++ b/tests/test_coverity.py @@ -2,7 +2,7 @@ import os from io import StringIO from pathlib import Path -from unittest import TestCase +from unittest import TestCase, mock from unittest.mock import patch from mlx.warnings import WarningsPlugin, warnings_wrapper @@ -19,19 +19,12 @@ def ordered(obj): else: return obj - +@mock.patch.dict(os.environ, {"MIN_COV_WARNINGS": "1", "MAX_COV_WARNINGS": "2"}) class TestCoverityWarnings(TestCase): def setUp(self): - os.environ["MIN_COV_WARNINGS"] = "1" - os.environ["MAX_COV_WARNINGS"] = "2" self.warnings = WarningsPlugin(verbose=True) self.warnings.activate_checker_name('coverity') - def tearDown(self): - for var in ('MIN_POLY_WARNINGS', 'MAX_POLY_WARNINGS'): - if var in os.environ: - del os.environ[var] - def test_no_warning_normal_text(self): dut = 'This should not be treated as warning' self.warnings.check(dut) @@ -79,9 +72,9 @@ def test_code_quality_without_config(self): str(TEST_IN_DIR / 'defects.txt'), ]) self.assertEqual(8, retval) - with open(out_file) as file: + with open(out_file) as file: cq_out = json.load(file) - with open(ref_file) as file: + with open(ref_file) as file: cq_ref = json.load(file) self.assertEqual(ordered(cq_out), ordered(cq_ref)) @@ -95,8 +88,8 @@ def test_code_quality_with_config(self): str(TEST_IN_DIR / 'defects.txt'), ]) self.assertEqual(3, retval) - with open(out_file) as file: + with open(out_file) as file: cq_out = json.load(file) - with open(ref_file) as file: + with open(ref_file) as file: cq_ref = json.load(file) self.assertEqual(ordered(cq_out), ordered(cq_ref))