changeset 385:ea73723be05e

treesum: unit tests with an existing .treesum file: - ignore them - FIX: checksums where existing .treesum files were not ignored properly - accept them with (implicitly) changing the algorithm
author Franz Glasner <fzglas.hg@dom66.de>
date Sat, 17 May 2025 16:53:16 +0200
parents f66afecac253
children f045d46e9f3d
files cutils/treesum.py tests/test_treesum.py
diffstat 2 files changed, 56 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/cutils/treesum.py	Sat May 17 15:14:30 2025 +0200
+++ b/cutils/treesum.py	Sat May 17 16:53:16 2025 +0200
@@ -490,7 +490,7 @@
             if not isinstance(f, (tuple, list)):
                 raise TypeError(
                     "items in `fnmatch_filters' must be tuples or lists")
-            if f[0] not in ("exclude", "include"):
+            if f[0] not in ("exclude", "include", "accept-treesum"):
                 raise ValueError(
                     "every kind of every item in `fnmatch_filters' must be"
                     " \"include\", \"exclude\" or \"accept-treesum\""
--- a/tests/test_treesum.py	Sat May 17 15:14:30 2025 +0200
+++ b/tests/test_treesum.py	Sat May 17 16:53:16 2025 +0200
@@ -141,6 +141,61 @@
         self.assertEqual(110, info.size)
         self.assertEqual("SHA1", info.algorithm)
 
+    def test_gen_and_info_P_utf8_with_treesum(self):
+        digest_file = os.path.join(
+            TMPDIR,
+            "%d__test_gen_and_info_P_native.info" % (os.getpid(),))
+        rel_tmpdir = os.path.relpath(TMPDIR, DATADIR)
+        gen_opts = cutils.treesum.gen_generate_opts(
+            directories=[DATADIR],
+            algorithm="SHA1",
+            fnmatch_filters=[("exclude", "path:%s" % (rel_tmpdir,))],
+            generator="full",
+            grouping_separator="_",
+            output=digest_file,
+            output_style="tagged",
+            print_size=True,
+            utf8=True)
+        info_opts = cutils.treesum.gen_info_opts(digest_files=[digest_file],
+                                                 last=True)
+        cutils.treesum.generate_treesum(gen_opts)
+        cutils.treesum.print_treesum_digestfile_infos(info_opts)
+        info = cutils.treesum.TreesumInfo.collect_last_from_file(digest_file)
+        self.assertEqual(
+            b"\x78\xdd\xcd\xd2\xbe\xa5\x2c\x8c\xc9\x1e\x3a\xd4\x26\xda\x35\x2e\xa6\xc2\x0a\x06",    # noqa: E501 line too long
+            info.digest)
+        self.assertEqual(2620, info.size)
+        self.assertEqual("SHA1", info.algorithm)
+
+    def test_gen_and_info_P_utf8_accept_treesum(self):
+        digest_file = os.path.join(
+            TMPDIR,
+            "%d__test_gen_and_info_P_native.info" % (os.getpid(),))
+        rel_tmpdir = os.path.relpath(TMPDIR, DATADIR)
+        gen_opts = cutils.treesum.gen_generate_opts(
+            directories=[DATADIR],
+            algorithm="SHA1",
+            fnmatch_filters=[("exclude", "path:%s" % (rel_tmpdir,)),
+                             ("accept-treesum", "glob:*.treesum")],
+            generator="full",
+            grouping_separator="_",
+            output=digest_file,
+            output_style="tagged",
+            print_size=True,
+            utf8=True)
+        info_opts = cutils.treesum.gen_info_opts(digest_files=[digest_file],
+                                                 last=True)
+        cutils.treesum.generate_treesum(gen_opts)
+        cutils.treesum.print_treesum_digestfile_infos(info_opts)
+        info = cutils.treesum.TreesumInfo.collect_last_from_file(digest_file)
+        self.assertEqual(
+            b"\x69\x6f\xe2\x51\xbe\x94\xbe\xcc\x76\xa5\x91\x24\x1d\x46\x83\xbb\x44\x36\xc7\x9b\x5b\x7b\x62\xb3\xe0\x4a\x0e\xdc\x7e\xc4\x07\xcb",    # noqa: E501 line too long
+            info.digest)
+        # accepts the size within the .treesum file
+        self.assertEqual(67, info.size)
+        # accepts uses the digest algorithm from the .treesum file
+        self.assertEqual("SHA256", info.algorithm)
+
 
 if __name__ == "__main__":
     sys.exit(unittest.main(buffer=True))