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))