def test_add(self): entry_path = os.path.join(self.temp_dir, "entry") data = "test\n" with open(entry_path, "w") as entry: print(data, end="", file=entry) checksum_file = ChecksumFile( self.config, self.temp_dir, "MD5SUMS", hashlib.md5) checksum_file.add("entry") self.assertEqual( {"entry": hashlib.md5(data).hexdigest()}, checksum_file.entries)
def test_add(self): entry_path = os.path.join(self.temp_dir, "entry") data = b"test\n" with mkfile(entry_path, mode="wb") as entry: entry.write(data) checksum_file = ChecksumFile(self.config, self.temp_dir, "MD5SUMS", hashlib.md5) checksum_file.add("entry") self.assertEqual({"entry": hashlib.md5(data).hexdigest()}, checksum_file.entries)
def test_add_existing(self): # Attempting to add an existing file has no effect. (Use .remove() # first to overwrite an existing checksum.) entry_path = os.path.join(self.temp_dir, "entry") data = "test\n" with open(entry_path, "w") as entry: print(data, end="", file=entry) checksum_file = ChecksumFile( self.config, self.temp_dir, "MD5SUMS", hashlib.md5) checksum_file.entries["entry"] = "" checksum_file.add("entry") self.assertEqual("", checksum_file.entries["entry"])
def test_add_existing(self): # Attempting to add an existing file that is not newer than the # checksums file has no effect. (Use .remove() first to overwrite # an existing checksum.) entry_path = os.path.join(self.temp_dir, "entry") data = "test\n" with mkfile(entry_path) as entry: entry.write(data) checksum_file = ChecksumFile(self.config, self.temp_dir, "MD5SUMS", hashlib.md5) checksum_file.entries["entry"] = "" checksum_file.add("entry") self.assertEqual("", checksum_file.entries["entry"])
def test_add_updated_mtime(self): # Adding an existing file with an mtime newer than that of the # checksums file causes its checksum to be updated. path = os.path.join(self.temp_dir, "entry") with mkfile(path) as entry: pass checksum_file = ChecksumFile(self.config, self.temp_dir, "MD5SUMS", hashlib.md5, sign=False) checksum_file.add("entry") checksum_file.write() self.rewind_mtime(checksum_file.path) with mkfile(path) as entry: print("mtime", end="", file=entry) checksum_file.add("entry") self.assertEqual( hashlib.md5(b"mtime").hexdigest(), checksum_file.entries["entry"])
def test_write(self): checksum_file = ChecksumFile( self.config, self.temp_dir, "MD5SUMS", hashlib.md5, sign=False) for name in "1", "2": entry_path = os.path.join(self.temp_dir, name) with open(entry_path, "w") as entry: print(name, end="", file=entry) checksum_file.add(name) checksum_file.write() with open(checksum_file.path) as md5sums: self.assertEqual(dedent("""\ %s *1 %s *2 """) % (hashlib.md5("1").hexdigest(), hashlib.md5("2").hexdigest()), md5sums.read()) self.assertEqual( 0, subprocess.call( ["md5sum", "-c", "--status", "MD5SUMS"], cwd=self.temp_dir))
def test_add_updated_ctime(self): # Adding an existing file with a ctime newer than that of the # checksums file causes its checksum to be updated. path = os.path.join(self.temp_dir, "entry") with mkfile(path) as entry: print("ctime", end="", file=entry) checksum_file = ChecksumFile(self.config, self.temp_dir, "MD5SUMS", hashlib.md5, sign=False) checksum_file.entries["entry"] = "" checksum_file.changed = True checksum_file.write() # We can simulate a ctime change by rewinding the mtime of both # entry and the checksums file. self.rewind_mtime(checksum_file.path) self.rewind_mtime(path) checksum_file.add("entry") self.assertEqual( hashlib.md5(b"ctime").hexdigest(), checksum_file.entries["entry"])
def test_write(self): checksum_file = ChecksumFile(self.config, self.temp_dir, "MD5SUMS", hashlib.md5, sign=False) for name in "1", "2": entry_path = os.path.join(self.temp_dir, name) with mkfile(entry_path) as entry: print(name, end="", file=entry) checksum_file.add(name) checksum_file.write() with open(checksum_file.path) as md5sums: expected = dedent("""\ %s *1 %s *2 """) % (hashlib.md5(b"1").hexdigest(), hashlib.md5(b"2").hexdigest()) self.assertEqual(expected, md5sums.read()) self.assertEqual( 0, subprocess.call(["md5sum", "-c", "--status", "MD5SUMS"], cwd=self.temp_dir))