def testPreserveTarMtimesFalse(self): with archive.TarFileWriter(self.tempfile, preserve_tar_mtimes=False) as f: input_tar_path = self.data_files.Rlocation( "rules_pkg/tests/testdata/tar_test.tar") f.add_tar(input_tar_path) for output_file in f.tar: self.assertEqual(output_file.mtime, 0)
def testPreserveTarMtimesTrueByDefault(self): with archive.TarFileWriter(self.tempfile) as f: input_tar_path = self.data_files.Rlocation( "rules_pkg/tests/testdata/tar_test.tar") f.add_tar(input_tar_path) input_tar = tarfile.open(input_tar_path, "r") for file_name in f.members: input_file = input_tar.getmember(file_name) output_file = f.tar.getmember(file_name) self.assertEqual(input_file.mtime, output_file.mtime)
def testChangingRootDirectory(self): with archive.TarFileWriter(self.tempfile, root_directory="root") as f: f.add_file("d", tarfile.DIRTYPE) f.add_file("d/f") f.add_file("a", tarfile.DIRTYPE) f.add_file("a/b", tarfile.DIRTYPE) f.add_file("a/b", tarfile.DIRTYPE) f.add_file("a/b/", tarfile.DIRTYPE) f.add_file("a/b/c/f") f.add_file("x/y/f") f.add_file("x", tarfile.DIRTYPE) content = [ { "name": "root", "mode": 0o755 }, { "name": "root/d", "mode": 0o755 }, { "name": "root/d/f" }, { "name": "root/a", "mode": 0o755 }, { "name": "root/a/b", "mode": 0o755 }, { "name": "root/a/b/c", "mode": 0o755 }, { "name": "root/a/b/c/f" }, { "name": "root/x", "mode": 0o755 }, { "name": "root/x/y", "mode": 0o755 }, { "name": "root/x/y/f" }, ] self.assertTarFileContent(self.tempfile, content)
def testAddingDirectoriesForFileManually(self): with archive.TarFileWriter(self.tempfile) as f: f.add_file("d", tarfile.DIRTYPE) f.add_file("d/f") f.add_file("a", tarfile.DIRTYPE) f.add_file("a/b", tarfile.DIRTYPE) f.add_file("a/b", tarfile.DIRTYPE) f.add_file("a/b/", tarfile.DIRTYPE) f.add_file("a/b/c/f") f.add_file("x/y/f") f.add_file("x", tarfile.DIRTYPE) content = [ { "name": ".", "mode": 0o755 }, { "name": "./d", "mode": 0o755 }, { "name": "./d/f" }, { "name": "./a", "mode": 0o755 }, { "name": "./a/b", "mode": 0o755 }, { "name": "./a/b/c", "mode": 0o755 }, { "name": "./a/b/c/f" }, { "name": "./x", "mode": 0o755 }, { "name": "./x/y", "mode": 0o755 }, { "name": "./x/y/f" }, ] self.assertTarFileContent(self.tempfile, content)
def assertSimpleFileContent(self, names): with archive.TarFileWriter(self.tempfile) as f: for n in names: f.add_file(n, content=n) # pylint: disable=g-complex-comprehension content = ([{ "name": "." }] + [{ "name": n, "size": len(n.encode("utf-8")), "data": n.encode("utf-8") } for n in names]) self.assertTarFileContent(self.tempfile, content)
def testAddingDirectoriesForFileSeparately(self): d_dir = os.path.join(os.environ["TEST_TMPDIR"], "d_dir") os.makedirs(d_dir) with open(os.path.join(d_dir, "dir_file"), "w"): pass a_dir = os.path.join(os.environ["TEST_TMPDIR"], "a_dir") os.makedirs(a_dir) with open(os.path.join(a_dir, "dir_file"), "w"): pass with archive.TarFileWriter(self.tempfile) as f: f.add_dir("d", d_dir) f.add_file("d/f") f.add_dir("a", a_dir) f.add_file("a/b/f") content = [ { "name": ".", "mode": 0o755 }, { "name": "./d", "mode": 0o755 }, { "name": "./d/dir_file" }, { "name": "./d/f" }, { "name": "./a", "mode": 0o755 }, { "name": "./a/dir_file" }, { "name": "./a/b", "mode": 0o755 }, { "name": "./a/b/f" }, ] self.assertTarFileContent(self.tempfile, content)
def testAddingDirectoriesForFile(self): with archive.TarFileWriter(self.tempfile) as f: f.add_file("d/f") content = [ { "name": ".", "mode": 0o755 }, { "name": "./d", "mode": 0o755 }, { "name": "./d/f" }, ] self.assertTarFileContent(self.tempfile, content)
def testMergeTar(self): content = [ { "name": "./a", "data": b"a" }, { "name": "./ab", "data": b"ab" }, ] for ext in [("." + comp if comp else "") for comp in archive.COMPRESSIONS]: with archive.TarFileWriter(self.tempfile) as f: datafile = self.data_files.Rlocation( "rules_pkg/tests/testdata/tar_test.tar" + ext) f.add_tar(datafile, name_filter=lambda n: n != "./b") self.assertTarFileContent(self.tempfile, content)
def testAddDir(self): # For some strange reason, ending slash is stripped by the test content = [ { "name": ".", "mode": 0o755 }, { "name": "./a", "mode": 0o755 }, { "name": "./a/b", "data": b"ab", "mode": 0o644 }, { "name": "./a/c", "mode": 0o755 }, { "name": "./a/c/d", "data": b"acd", "mode": 0o644 }, ] tempdir = os.path.join(os.environ["TEST_TMPDIR"], "test_dir") # Iterate over the `content` array to create the directory # structure it describes. for c in content: if "data" in c: # Create files is happening locally, so use native path sep. path_parts = c["name"][2:].split('/') p = os.path.join(tempdir, *path_parts) os.makedirs(os.path.dirname(p)) with open(p, "wb") as f: f.write(c["data"]) with archive.TarFileWriter(self.tempfile) as f: f.add_dir("./", tempdir, mode=0o644) self.assertTarFileContent(self.tempfile, content)
def testMergeTarRelocated(self): content = [ { "name": ".", "mode": 0o755 }, { "name": "./foo", "mode": 0o755 }, { "name": "./foo/a", "data": b"a" }, { "name": "./foo/ab", "data": b"ab" }, ] with archive.TarFileWriter(self.tempfile) as f: datafile = self.data_files.Rlocation( "rules_pkg/tests/testdata/tar_test.tar") f.add_tar(datafile, name_filter=lambda n: n != "./b", root="/foo") self.assertTarFileContent(self.tempfile, content)
def testDottedFiles(self): with archive.TarFileWriter(self.tempfile) as f: f.add_file("a") f.add_file("/b") f.add_file("./c") f.add_file("./.d") f.add_file("..e") f.add_file(".f") content = [{ "name": "." }, { "name": "./a" }, { "name": "/b" }, { "name": "./c" }, { "name": "./.d" }, { "name": "./..e" }, { "name": "./.f" }] self.assertTarFileContent(self.tempfile, content)
def testPortableMtime(self): with archive.TarFileWriter(self.tempfile, default_mtime="portable") as f: self.assertEqual(f.default_mtime, 946684800)
def testDefaultMtimeProvided(self): with archive.TarFileWriter(self.tempfile, default_mtime=1234) as f: self.assertEqual(f.default_mtime, 1234)
def testDefaultMtimeNotProvided(self): with archive.TarFileWriter(self.tempfile) as f: self.assertEqual(f.default_mtime, 0)
def testEmptyTarFile(self): with archive.TarFileWriter(self.tempfile): pass self.assertTarFileContent(self.tempfile, [])