def test_copy_single_file_into_dir(self): with self.getNewDestination() as dest: os.mkdir(dest) p = PyCopier(source=__file__, destination=dest) p.execute() assert p.getCopiedDataBytes() == os.path.getsize(__file__) assert filecmp.cmp(__file__, os.path.join(dest, os.path.basename(__file__)), shallow=False)
def test_cleaning_directory(self): p = PyCopier(source=None, destination=None, ignoreErrorOnCopy=False) with _TestDirectory() as t: t.create() # count files numFiles = 0 for path, dirs, files in os.walk(t.sourceDirectory): numFiles += len(files) p._cleanDestinationDirectory(t.sourceDirectory, []) # nothing raised... nothing copied assert p.getPurgedFileCount() == numFiles
def test_zero_length_copy(self): with _TestDirectory() as t: t.create() with self.getNewDestination() as dest: PyCopier(t.sourceDirectory, dest, zeroLengthFiles=True).execute() assert t.checkMatch(dest, zeroLengthFiles=True)
def test_permission_copy(self): with _TestDirectory() as t: t.create() with self.getNewDestination() as dest: PyCopier(t.sourceDirectory, dest, copyPermissions=True).execute() assert t.checkMatch(dest, checkPermissions=True)
def test_generic_copy_weird_buffer_size(self): for bufferSize in [1024, 4096, 8192, 1, 2]: with _TestDirectory() as t: t.create() with self.getNewDestination() as dest: PyCopier(t.sourceDirectory, dest, bufferSize=bufferSize).execute() assert t.checkMatch(dest)
def test_generic_copy_various_worker_count(self): for numWorkers in range(1, 16): with _TestDirectory() as t: t.create() with self.getNewDestination() as dest: PyCopier(t.sourceDirectory, dest, numWorkers=numWorkers).execute() assert t.checkMatch(dest)
def test_copy_single_file_into_dir_and_purge(self): with self.getNewDestination() as dest: os.mkdir(dest) TEST_PATH = os.path.join(dest, 'test') with open(TEST_PATH, 'wb') as f: f.write(b'abc' * 10) p = PyCopier(source=__file__, destination=dest, purgeDestination=True) p.execute() assert p.getCopiedDataBytes() == os.path.getsize(__file__) assert filecmp.cmp(__file__, os.path.join(dest, os.path.basename(__file__)), shallow=False) assert not os.path.exists(TEST_PATH)
def test_copy_single_file_with_move(self): with open(__file__, 'rb') as f: fileData = f.read() try: with self.getNewDestination() as dest: p = PyCopier(source=__file__, destination=dest, move=True) p.execute() assert p.getCopiedDataBytes() == len(fileData) with open(dest, 'rb') as f: newFileData = f.read() assert fileData == newFileData assert not os.path.exists(__file__) finally: with open(__file__, 'wb') as f: f.write(fileData)
def _dirCmpMatch(self, dirCmpObject, shallow, checkPermissions, zeroLengthFiles, move, ignoreExtraRight): if move: self.create() try: if dirCmpObject.left_list != dirCmpObject.right_list: err = True if ignoreExtraRight: if not dirCmpObject.left_only and dirCmpObject.right_only: err = False if err: print("Match failure (missing/extra files)") print("Left: %s" % dirCmpObject.left_list) print("Right: %s" % dirCmpObject.right_list) dirCmpObject.report() return False for file in dirCmpObject.common_files: left = os.path.join(dirCmpObject.left, file) right = os.path.join(dirCmpObject.right, file) if zeroLengthFiles: if os.path.getsize(right) != 0: print("%s should be size 0" % right) return False else: if not filecmp.cmp(left, right, shallow=shallow): print("Match failure: \n%s\n%s\n don't match" % (left, right)) return False if checkPermissions: leftStat = os.stat(left) rightStat = os.stat(right) if not PyCopier.statMatch(leftStat, rightStat): print( "Permission mismatch: \n%s\n%s\n don't match" % (left, right)) return False for name, dirCmp in dirCmpObject.subdirs.items(): # move must always be False in nested scenarios, since we would have already recreated the src if not self._dirCmpMatch(dirCmp, shallow, checkPermissions, zeroLengthFiles, move=False, ignoreExtraRight=ignoreExtraRight): return False return True finally: if move: # delete src. self.__exit__(None, None, None)
def test_purge_destination(self): with _TestDirectory() as t: t.create() with self.getNewDestination() as dest: os.mkdir(dest) fileThatShouldBePurged = os.path.join(dest, 'test_tmp') with open(fileThatShouldBePurged, 'w') as f: f.write('test') PyCopier(t.sourceDirectory, dest, purgeDestination=False).execute() assert os.path.exists(fileThatShouldBePurged) assert t.checkMatch(dest, ignoreExtraRight=True) PyCopier(t.sourceDirectory, dest, purgeDestination=True).execute() assert not os.path.exists(fileThatShouldBePurged) assert t.checkMatch(dest)
def test_quiet_param(self): with _TestDirectory() as t: t.create() output = io.StringIO() with self.getNewDestination() as dest: with contextlib.redirect_stdout( output), contextlib.redirect_stderr(output): PyCopier(t.sourceDirectory, dest, quiet=True).execute() assert t.checkMatch(dest) assert output.getvalue( ) == '', "Nothing should go to output if quiet"
def test_skip_same_looking_file_doesnt_happen_if_meta_doesnt_match(self): p = PyCopier(source=None, destination=None, skipSameLookingFiles=True) with self.getNewDestination() as dest: shutil.copy(__file__, dest) p._copyFile(__file__, dest) assert p.getSkippedCopiesCount() == 0 assert p.getCopiedDataBytes() == os.path.getsize(__file__)
def test_errors_ignored_on_copy(self): p = PyCopier(source=None, destination=None, ignoreErrorOnCopy=True) with self.getNewDestination() as dest: p._copyFile("/fake/file", dest) # nothing raised... nothing copied assert p.getSkippedCopiesCount() == 0 assert p.getCopiedDataBytes() == 0
def test_skip_same_looking_file(self): p = PyCopier(source=None, destination=None, skipSameLookingFiles=True) with self.getNewDestination() as dest: shutil.copy2(__file__, dest) p._copyFile(__file__, dest) assert p.getSkippedCopiesCount() == 1 assert p.getCopiedDataBytes() == 0
def test_generic_copy_file(self): p = PyCopier(source=None, destination=None) with self.getNewDestination() as dest: p._copyFile(__file__, dest) assert os.path.isfile(dest) assert filecmp.cmp(__file__, dest, shallow=False) assert p.getCopiedDataBytes() == os.path.getsize(__file__) assert p.getSkippedCopiesCount() == 0
def test_copy_single_file(self): with self.getNewDestination() as dest: p = PyCopier(source=__file__, destination=dest) p.execute() assert p.getCopiedDataBytes() == os.path.getsize(__file__) assert filecmp.cmp(__file__, dest, shallow=False)
def test_move(self): with _TestDirectory() as t: t.create() with self.getNewDestination() as dest: PyCopier(t.sourceDirectory, dest, move=True).execute() assert t.checkMatch(dest, move=True)