From 682c38b26854ecab96001adce25ebfe8d794ec16 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micka=C3=ABl=20Schoentgen?= Date: Sun, 6 Jan 2019 14:31:18 +0100 Subject: [PATCH] Fix ResourceWarning: unclosed file MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mickaƫl Schoentgen --- bandit/core/config.py | 3 ++- examples/hardcoded-tmp.py | 25 ++++++++++--------------- tests/unit/core/test_manager.py | 12 ++++++------ tests/unit/core/test_util.py | 14 ++++++-------- tests/unit/formatters/test_csv.py | 6 +++--- tests/unit/formatters/test_html.py | 18 +++++++++--------- tests/unit/formatters/test_json.py | 6 +++--- tests/unit/formatters/test_screen.py | 24 ++++++++++++------------ tests/unit/formatters/test_text.py | 23 ++++++++++++----------- tests/unit/formatters/test_xml.py | 6 +++--- tests/unit/formatters/test_yaml.py | 6 +++--- 11 files changed, 69 insertions(+), 74 deletions(-) diff --git a/bandit/core/config.py b/bandit/core/config.py index 7369f061b..d863b8f69 100644 --- a/bandit/core/config.py +++ b/bandit/core/config.py @@ -47,7 +47,8 @@ def __init__(self, config_file=None): config_file) try: - self._config = yaml.safe_load(f) + with f: + self._config = yaml.safe_load(f) self.validate(config_file) except yaml.YAMLError as err: LOG.error(err) diff --git a/examples/hardcoded-tmp.py b/examples/hardcoded-tmp.py index 9a9b8f240..c5befd38a 100644 --- a/examples/hardcoded-tmp.py +++ b/examples/hardcoded-tmp.py @@ -1,21 +1,16 @@ -f = open('/tmp/abc', 'w') -f.write('def') -f.close() +with open('/tmp/abc', 'w') as f: + f.write('def') # ok -f = open('/abc/tmp', 'w') -f.write('def') -f.close() +with open('/abc/tmp', 'w') as f: + f.write('def') -f = open('/var/tmp/123', 'w') -f.write('def') -f.close() +with open('/var/tmp/123', 'w') as f: + f.write('def') -f = open('/dev/shm/unit/test', 'w') -f.write('def') -f.close() +with open('/dev/shm/unit/test', 'w') as f: + f.write('def') # Negative test -f = open('/foo/bar', 'w') -f.write('def') -f.close() +with open('/foo/bar', 'w') as f: + f.write('def') diff --git a/tests/unit/core/test_manager.py b/tests/unit/core/test_manager.py index 5009ab71f..92ce77165 100644 --- a/tests/unit/core/test_manager.py +++ b/tests/unit/core/test_manager.py @@ -157,9 +157,9 @@ def test_output_results_invalid_format(self): conf_level = constants.LOW output_filename = os.path.join(temp_directory, "_temp_output") output_format = "invalid" - tmp_file = open(output_filename, 'w') - self.manager.output_results(lines, sev_level, conf_level, tmp_file, - output_format) + with open(output_filename, 'w') as tmp_file: + self.manager.output_results(lines, sev_level, conf_level, + tmp_file, output_format) self.assertTrue(os.path.isfile(output_filename)) def test_output_results_valid_format(self): @@ -170,9 +170,9 @@ def test_output_results_valid_format(self): conf_level = constants.LOW output_filename = os.path.join(temp_directory, "_temp_output.txt") output_format = "txt" - tmp_file = open(output_filename, 'w') - self.manager.output_results(lines, sev_level, conf_level, tmp_file, - output_format) + with open(output_filename, 'w') as tmp_file: + self.manager.output_results(lines, sev_level, conf_level, + tmp_file, output_format) self.assertTrue(os.path.isfile(output_filename)) @mock.patch('os.path.isdir') diff --git a/tests/unit/core/test_util.py b/tests/unit/core/test_util.py index 8d2833377..99c3c0659 100644 --- a/tests/unit/core/test_util.py +++ b/tests/unit/core/test_util.py @@ -28,8 +28,7 @@ def _touch(path): '''Create an empty file at ``path``.''' - newf = open(path, 'w') - newf.close() + open(path, 'w').close() class UtilTests(testtools.TestCase): @@ -225,10 +224,10 @@ def test_get_call_name3(self): # self.assertEqual(name, 'a.list[0]') def test_linerange(self): - self.test_file = open("./examples/jinja2_templating.py") - self.tree = ast.parse(self.test_file.read()) + with open("./examples/jinja2_templating.py") as test_file: + tree = ast.parse(test_file.read()) # Check linerange returns corrent number of lines - line = self.tree.body[8] + line = tree.body[8] lrange = b_utils.linerange(line) # line 9 should be three lines long @@ -287,9 +286,8 @@ def test_parse_ini_file(self): with tempfile.NamedTemporaryFile('r+') as t: for test in tests: - f = open(t.name, 'w') - f.write(test['content']) - f.close() + with open(t.name, 'w') as f: + f.write(test['content']) self.assertEqual(b_utils.parse_ini_file(t.name), test['expected']) diff --git a/tests/unit/formatters/test_csv.py b/tests/unit/formatters/test_csv.py index fbb41f19b..d7a20b8b5 100644 --- a/tests/unit/formatters/test_csv.py +++ b/tests/unit/formatters/test_csv.py @@ -48,9 +48,9 @@ def setUp(self): self.manager.results.append(self.issue) def test_report(self): - tmp_file = open(self.tmp_fname, 'w') - b_csv.report(self.manager, tmp_file, self.issue.severity, - self.issue.confidence) + with open(self.tmp_fname, 'w') as tmp_file: + b_csv.report(self.manager, tmp_file, self.issue.severity, + self.issue.confidence) with open(self.tmp_fname) as f: reader = csv.DictReader(f) diff --git a/tests/unit/formatters/test_html.py b/tests/unit/formatters/test_html.py index 928d0be02..221ce420e 100644 --- a/tests/unit/formatters/test_html.py +++ b/tests/unit/formatters/test_html.py @@ -41,9 +41,9 @@ def setUp(self): def test_report_with_skipped(self): self.manager.skipped = [('abc.py', 'File is bad')] - tmp_file = open(self.tmp_fname, 'w') - b_html.report( - self.manager, tmp_file, bandit.LOW, bandit.LOW) + with open(self.tmp_fname, 'w') as tmp_file: + b_html.report( + self.manager, tmp_file, bandit.LOW, bandit.LOW) with open(self.tmp_fname) as f: soup = bs4.BeautifulSoup(f.read(), 'html.parser') @@ -78,9 +78,9 @@ def test_report_contents(self, get_issue_list, get_code): [(issue_a, [issue_x, issue_y]), (issue_b, [issue_x]), (issue_c, [issue_y])]) - tmp_file = open(self.tmp_fname, 'w') - b_html.report( - self.manager, tmp_file, bandit.LOW, bandit.LOW) + with open(self.tmp_fname, 'w') as tmp_file: + b_html.report( + self.manager, tmp_file, bandit.LOW, bandit.LOW) with open(self.tmp_fname) as f: soup = bs4.BeautifulSoup(f.read(), 'html.parser') @@ -143,9 +143,9 @@ def test_escaping(self, get_issue_list, get_code): get_issue_list.return_value = {issue_a: [issue_x]} - tmp_file = open(self.tmp_fname, 'w') - b_html.report( - self.manager, tmp_file, bandit.LOW, bandit.LOW) + with open(self.tmp_fname, 'w') as tmp_file: + b_html.report( + self.manager, tmp_file, bandit.LOW, bandit.LOW) with open(self.tmp_fname) as f: contents = f.read() diff --git a/tests/unit/formatters/test_json.py b/tests/unit/formatters/test_json.py index 957c0473f..7d2ffc329 100644 --- a/tests/unit/formatters/test_json.py +++ b/tests/unit/formatters/test_json.py @@ -75,9 +75,9 @@ def test_report(self, get_issue_list): get_issue_list.return_value = collections.OrderedDict( [(self.issue, self.candidates)]) - tmp_file = open(self.tmp_fname, 'w') - b_json.report(self.manager, tmp_file, self.issue.severity, - self.issue.confidence) + with open(self.tmp_fname, 'w') as tmp_file: + b_json.report(self.manager, tmp_file, self.issue.severity, + self.issue.confidence) with open(self.tmp_fname) as f: data = json.loads(f.read()) diff --git a/tests/unit/formatters/test_screen.py b/tests/unit/formatters/test_screen.py index c044ef5e0..9b857585e 100644 --- a/tests/unit/formatters/test_screen.py +++ b/tests/unit/formatters/test_screen.py @@ -82,9 +82,9 @@ def test_no_issues(self, get_issue_list): get_issue_list.return_value = collections.OrderedDict() with mock.patch('bandit.formatters.screen.do_print') as m: - tmp_file = open(self.tmp_fname, 'w') - screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, - lines=5) + with open(self.tmp_fname, 'w') as tmp_file: + screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, + lines=5) self.assertIn('No issues identified.', '\n'.join([str(a) for a in m.call_args])) @@ -121,9 +121,9 @@ def test_report_nobaseline(self, get_issue_list): with mock.patch(output_str_fn) as output_str: output_str.return_value = 'ISSUE_OUTPUT_TEXT' - tmp_file = open(self.tmp_fname, 'w') - screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, - lines=5) + with open(self.tmp_fname, 'w') as tmp_file: + screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, + lines=5) calls = [mock.call(issue_a, '', lines=5), mock.call(issue_b, '', lines=5)] @@ -133,9 +133,9 @@ def test_report_nobaseline(self, get_issue_list): # Validate that we're outputting all of the expected fields and the # correct values with mock.patch('bandit.formatters.screen.do_print') as m: - tmp_file = open(self.tmp_fname, 'w') - screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, - lines=5) + with open(self.tmp_fname, 'w') as tmp_file: + screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, + lines=5) data = '\n'.join([str(a) for a in m.call_args[0][0]]) @@ -195,9 +195,9 @@ def test_report_baseline(self, get_issue_list): with mock.patch(output_str_fn) as output_str: output_str.return_value = 'ISSUE_OUTPUT_TEXT' - tmp_file = open(self.tmp_fname, 'w') - screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, - lines=5) + with open(self.tmp_fname, 'w') as tmp_file: + screen.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, + lines=5) calls = [mock.call(issue_a, '', lines=5), mock.call(issue_b, '', show_code=False, diff --git a/tests/unit/formatters/test_text.py b/tests/unit/formatters/test_text.py index c8a137050..5f69258f3 100644 --- a/tests/unit/formatters/test_text.py +++ b/tests/unit/formatters/test_text.py @@ -77,8 +77,9 @@ def test_no_issues(self, get_issue_list): self.manager.out_file = self.tmp_fname get_issue_list.return_value = collections.OrderedDict() - tmp_file = open(self.tmp_fname, 'w') - b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, lines=5) + with open(self.tmp_fname, 'w') as tmp_file: + b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, + lines=5) with open(self.tmp_fname) as f: data = f.read() @@ -117,9 +118,9 @@ def test_report_nobaseline(self, get_issue_list): with mock.patch(output_str_fn) as output_str: output_str.return_value = 'ISSUE_OUTPUT_TEXT' - tmp_file = open(self.tmp_fname, 'w') - b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, - lines=5) + with open(self.tmp_fname, 'w') as tmp_file: + b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, + lines=5) calls = [mock.call(issue_a, '', lines=5), mock.call(issue_b, '', lines=5)] @@ -128,9 +129,9 @@ def test_report_nobaseline(self, get_issue_list): # Validate that we're outputting all of the expected fields and the # correct values - tmp_file = open(self.tmp_fname, 'w') - b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, - lines=5) + with open(self.tmp_fname, 'w') as tmp_file: + b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, + lines=5) with open(self.tmp_fname) as f: data = f.read() @@ -182,9 +183,9 @@ def test_report_baseline(self, get_issue_list): with mock.patch(output_str_fn) as output_str: output_str.return_value = 'ISSUE_OUTPUT_TEXT' - tmp_file = open(self.tmp_fname, 'w') - b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, - lines=5) + with open(self.tmp_fname, 'w') as tmp_file: + b_text.report(self.manager, tmp_file, bandit.LOW, bandit.LOW, + lines=5) calls = [mock.call(issue_a, '', lines=5), mock.call(issue_b, '', show_code=False, diff --git a/tests/unit/formatters/test_xml.py b/tests/unit/formatters/test_xml.py index 68889b055..dc2bf46ca 100644 --- a/tests/unit/formatters/test_xml.py +++ b/tests/unit/formatters/test_xml.py @@ -69,9 +69,9 @@ def _xml_to_dict(self, t): return d def test_report(self): - tmp_file = open(self.tmp_fname, 'wb') - b_xml.report(self.manager, tmp_file, self.issue.severity, - self.issue.confidence) + with open(self.tmp_fname, 'wb') as tmp_file: + b_xml.report(self.manager, tmp_file, self.issue.severity, + self.issue.confidence) with open(self.tmp_fname) as f: data = self._xml_to_dict(ET.XML(f.read())) diff --git a/tests/unit/formatters/test_yaml.py b/tests/unit/formatters/test_yaml.py index 6997d9a9a..09b46e493 100644 --- a/tests/unit/formatters/test_yaml.py +++ b/tests/unit/formatters/test_yaml.py @@ -75,9 +75,9 @@ def test_report(self, get_issue_list): get_issue_list.return_value = collections.OrderedDict( [(self.issue, self.candidates)]) - tmp_file = open(self.tmp_fname, 'w') - b_json.report(self.manager, tmp_file, self.issue.severity, - self.issue.confidence) + with open(self.tmp_fname, 'w') as tmp_file: + b_json.report(self.manager, tmp_file, self.issue.severity, + self.issue.confidence) with open(self.tmp_fname) as f: data = yaml.load(f.read())