From 0cfc866c0d1dd3e0ba163f32697ab4acc63be1ac Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Apr 01 2019 08:06:48 +0000 Subject: [PATCH 1/2] use context manager for open in CLI --- diff --git a/cli/koji_cli/commands.py b/cli/koji_cli/commands.py index 8efe4de..a03487c 100644 --- a/cli/koji_cli/commands.py +++ b/cli/koji_cli/commands.py @@ -989,9 +989,8 @@ def anon_handle_mock_config(goptions, session, args): name = "%(tag_name)s-repo_%(repoid)s" % opts output = koji.genMockConfig(name, arch, **opts) if options.ofile: - fo = open(options.ofile, 'w') - fo.write(output) - fo.close() + with open(options.ofile, 'w') as fo: + fo.write(output) else: print(output) @@ -1553,13 +1552,11 @@ def handle_prune_signed_copies(options, session, args): #(with the modification that we check to see if the build was latest within #the last N days) if options.ignore_tag_file: - fo = open(options.ignore_tag_file) - options.ignore_tag.extend([line.strip() for line in fo.readlines()]) - fo.close() + with open(options.ignore_tag_file) as fo: + options.ignore_tag.extend([line.strip() for line in fo.readlines()]) if options.protect_tag_file: - fo = open(options.protect_tag_file) - options.protect_tag.extend([line.strip() for line in fo.readlines()]) - fo.close() + with open(options.protect_tag_file) as fo: + options.protect_tag.extend([line.strip() for line in fo.readlines()]) if options.debug: options.verbose = True cutoff_ts = time.time() - options.days * 24 * 3600 @@ -6756,7 +6753,8 @@ def anon_handle_download_logs(options, session, args): full_filename = os.path.normpath(os.path.join(task_log_dir, FAIL_LOG)) koji.ensuredir(os.path.dirname(full_filename)) sys.stdout.write("Writing: %s\n" % full_filename) - open(full_filename, 'w').write(content) + with open(full_filename, 'w') as fo: + fo.write(content) def download_log(task_log_dir, task_id, filename, blocksize=102400, volume=None): # Create directories only if there is any log file to write to From 04fb07f767d89b2815266e779d12a0e8a357f1bc Mon Sep 17 00:00:00 2001 From: Tomas Kopecek Date: Apr 01 2019 09:21:43 +0000 Subject: [PATCH 2/2] fix test for opening mock file --- diff --git a/tests/test_cli/test_mock_config.py b/tests/test_cli/test_mock_config.py index 78fe928..38f5e22 100644 --- a/tests/test_cli/test_mock_config.py +++ b/tests/test_cli/test_mock_config.py @@ -241,8 +241,9 @@ config_opts['macros']['%distribution'] = 'Koji Testing' @mock.patch('sys.stdout', new_callable=six.StringIO) @mock.patch('koji.genMockConfig') @mock.patch('koji_cli.commands.activate_session') + @mock.patch('koji_cli.commands.open') def test_handle_mock_config_target_option( - self, activate_session_mock, gen_config_mock, stdout, stderr): + self, openf, activate_session_mock, gen_config_mock, stdout, stderr): """Test anon_handle_mock_config with target option""" arguments = [] arch = "x86_64" @@ -299,11 +300,11 @@ config_opts['macros']['%distribution'] = 'Koji Testing' # --latest and -o (output) test opts['repoid'] = 'latest' arguments.extend(['--latest', '-o', '/tmp/mock.out']) - with mock.patch('koji_cli.commands.open', create=True) as openf_mock: - anon_handle_mock_config(options, session, arguments) - openf_mock.assert_called_with('/tmp/mock.out', 'w') - handle = openf_mock() - handle.write.assert_called_once_with(self.mock_output) + fobj = mock.MagicMock() + openf.return_value.__enter__.return_value = fobj + anon_handle_mock_config(options, session, arguments) + openf.assert_called_with('/tmp/mock.out', 'w') + fobj.write.assert_called_once_with(self.mock_output) gen_config_mock.assert_called_with( self.progname, arch, **opts)