#1376 use context manager for open in CLI
Merged 4 years ago by mikem. Opened 4 years ago by tkopecek.
tkopecek/koji fopen  into  master

file modified
+8 -10
@@ -989,9 +989,8 @@ 

          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 @@ 

      #(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 @@ 

          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

@@ -241,8 +241,9 @@ 

      @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 @@ 

          # --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)

  

no initial comment

1 new commit added

  • fix test for opening mock file
4 years ago

Commit c6f3b23 fixes this pull-request

Pull-Request has been merged by mikem

4 years ago