Commits

Vinay Sajip committed d6ac6fb

Firmed up copy_file checking and added a test.

  • Participants
  • Parent commits e7d9b8d

Comments (0)

Files changed (2)

File distlib/util.py

 
         return os.stat(source).st_mtime > os.stat(target).st_mtime
 
-    def copy_file(self, infile, outfile, check=False):
+    def copy_file(self, infile, outfile, check=True):
         """Copy a file respecting dry-run and force flags.
         """
-        assert not os.path.isdir(outfile)
         self.ensure_dir(os.path.dirname(outfile))
         logger.info('Copying %s to %s', infile, outfile)
         if not self.dry_run:
-            if not check:
-                msg = None
-            if os.path.islink(outfile):
-                msg = '%s is a symlink' % outfile
-            elif os.path.exists(outfile) and not os.path.isfile(outfile):
-                msg = '%s is a non-regular file' % outfile
+            msg = None
+            if check:
+                if os.path.islink(outfile):
+                    msg = '%s is a symlink' % outfile
+                elif os.path.exists(outfile) and not os.path.isfile(outfile):
+                    msg = '%s is a non-regular file' % outfile
             if msg:
                 raise ValueError(msg + ' which would be overwritten')
             shutil.copyfile(infile, outfile)

File tests/test_util.py

         self.fileop.ensure_dir(path)
         return set(written), set([path])
 
+    def test_copy_check(self):
+        srcpath = os.path.join(self.workdir, 'file1')
+        self.fileop.write_text_file(srcpath, 'test', 'utf-8')
+        dstpath = os.path.join(self.workdir, 'file2')
+        os.mkdir(dstpath)
+        self.assertRaises(ValueError, self.fileop.copy_file, srcpath,
+                          dstpath)
+        os.rmdir(dstpath)
+        if os.name == 'posix':      # symlinks available
+            linkpath = os.path.join(self.workdir, 'file3')
+            self.fileop.write_text_file(linkpath, 'linkdest', 'utf-8')
+            os.symlink(linkpath, dstpath)
+            self.assertRaises(ValueError, self.fileop.copy_file, srcpath,
+                              dstpath)
+
     def test_commit(self):
         # will assert if record isn't set
         self.assertRaises(AssertionError, self.fileop.commit)