def testCmp(self): path1 = '/some/local/path' path2 = '/other/local/path' self.mox.StubOutWithMock(filelib.os.path, 'exists') self.mox.StubOutWithMock(filelib.filecmp, 'cmp') # Set up the test replay script. # Run 1, both exist, are different. filelib.os.path.exists(path1).AndReturn(True) filelib.os.path.exists(path2).AndReturn(True) filelib.filecmp.cmp(path1, path2).AndReturn(True) # Run 2, both exist, are different. filelib.os.path.exists(path1).AndReturn(True) filelib.os.path.exists(path2).AndReturn(True) filelib.filecmp.cmp(path1, path2).AndReturn(False) # Run 3, second file missing. filelib.os.path.exists(path1).AndReturn(True) filelib.os.path.exists(path2).AndReturn(False) self.mox.ReplayAll() # Run the test verification. self.assertTrue(filelib.Cmp(path1, path2)) self.assertFalse(filelib.Cmp(path1, path2)) self.assertFalse(filelib.Cmp(path1, path2)) self.mox.VerifyAll()
def testCmp(self): gs_path = 'gs://bucket/some/path' local_path = '/some/local/path' http_path = 'http://host.domain/some/path' result = 'TheResult' self.mox.StubOutWithMock(gslib, 'Cmp') self.mox.StubOutWithMock(filelib, 'Cmp') # Set up the test replay script. # Run 1, two local files. filelib.Cmp(local_path, local_path + '.1').AndReturn(result) # Run 2, local and GS. gslib.Cmp(local_path, gs_path).AndReturn(result) # Run 4, GS and GS gslib.Cmp(gs_path, gs_path + '.1').AndReturn(result) # Run 7, local and HTTP self.mox.ReplayAll() # Run the test verification. self.assertEquals(result, urilib.Cmp(local_path, local_path + '.1')) self.assertEquals(result, urilib.Cmp(local_path, gs_path)) self.assertEquals(result, urilib.Cmp(gs_path, gs_path + '.1')) self.assertRaises(urilib.NotSupportedBetweenTypes, urilib.Cmp, local_path, http_path) self.mox.VerifyAll()
def Cmp(uri1, uri2): """Return True if paths hold identical files. If either file is missing then always return False. Args: uri1: URI to a file. uri2: URI to a file. Returns: True if files are the same, False otherwise. Raises: NotSupportedBetweenTypes if Cmp cannot be done between the two URIs provided. """ uri_type1 = GetUriType(uri1) uri_type2 = GetUriType(uri2) uri_types = set([uri_type1, uri_type2]) if TYPE_GS in uri_types: # GS only supported between other GS files or local files. if len(uri_types) == 1 or TYPE_LOCAL in uri_types: return gslib.Cmp(uri1, uri2) if TYPE_LOCAL in uri_types and len(uri_types) == 1: return filelib.Cmp(uri1, uri2) raise NotSupportedBetweenTypes(uri_type1, uri_type2)
def testIntegrationScript(self): dir1 = None dir2 = None try: dir1 = utils.CreateTmpDir('filelib_unittest1-') dir2 = utils.CreateTmpDir('filelib_unittest2-') self._SetUpTempdir(dir1) dir1_file1 = os.path.join(dir1, self.FILE1) dir1_file2 = os.path.join(dir1, self.FILE2) dir1_subfile = os.path.join(dir1, self.SUBFILE) dir1_top_files = [dir1_file1, dir1_file2] dir1_deep_files = dir1_top_files + [dir1_subfile] dir2_file1 = os.path.join(dir2, self.FILE1) dir2_file2 = os.path.join(dir2, self.FILE2) dir2_subdir = os.path.join(dir2, self.SUBDIR) dir2_subfile = os.path.join(dir2, self.SUBFILE) dir2_top_files = [dir2_file1, dir2_file2] dir2_deep_files = dir2_top_files + [dir2_subfile] # Test Exists. for dir1_path in dir1_deep_files: self.assertTrue(filelib.Exists(dir1_path)) for dir2_path in dir2_deep_files: self.assertFalse(filelib.Exists(dir2_path)) # Test ListFiles with various options. self.assertEqual(set(dir1_top_files), set(filelib.ListFiles(dir1))) self.assertEqual(set(dir1_deep_files), set(filelib.ListFiles(dir1, recurse=True))) self.assertEqual(sorted(dir1_deep_files), filelib.ListFiles(dir1, recurse=True, sort=True)) self.assertEqual(set([dir1_file1, dir1_subfile]), set(filelib.ListFiles(dir1, recurse=True, filepattern=self.FILE_GLOB))) # Test CopyFiles from dir1 to dir2. self.assertEqual(set(dir2_deep_files), set(filelib.CopyFiles(dir1, dir2))) for dir2_path in dir2_deep_files: self.assertTrue(filelib.Exists(dir2_path)) # Test Cmp. self.assertTrue(filelib.Cmp(dir1_file1, dir2_file1)) self.assertTrue(filelib.Cmp(dir2_file2, dir1_file2)) self.assertFalse(filelib.Cmp(dir1_file2, dir2_file1)) # Test RemoveDirContents. filelib.RemoveDirContents(dir2_subdir) self.assertTrue(filelib.Exists(dir2_subdir, as_dir=True)) self.assertFalse(filelib.Exists(dir2_subfile)) filelib.RemoveDirContents(dir2) self.assertTrue(filelib.Exists(dir2, as_dir=True)) for dir2_path in dir2_deep_files: self.assertFalse(filelib.Exists(dir2_path)) filelib.RemoveDirContents(dir1) self.assertTrue(filelib.Exists(dir1, as_dir=True)) for dir1_path in dir1_deep_files: self.assertFalse(filelib.Exists(dir1_path)) finally: for d in (dir1, dir2): if d and os.path.isdir(d): shutil.rmtree(d)