[game-data-packager] 12/16: Add a test for HashedFile matching

Simon McVittie smcv at debian.org
Fri Jan 8 09:14:03 UTC 2016


This is an automated email from the git hooks/post-receive script.

smcv pushed a commit to branch master
in repository game-data-packager.

commit fd16b183b57ca24d501b1d9d74db7482b540cbb0
Author: Simon McVittie <smcv at debian.org>
Date:   Thu Jan 7 11:39:05 2016 +0000

    Add a test for HashedFile matching
---
 Makefile             |  1 +
 tests/hashed_file.py | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 90 insertions(+)

diff --git a/Makefile b/Makefile
index 4367045..e9639c7 100644
--- a/Makefile
+++ b/Makefile
@@ -106,6 +106,7 @@ clean:
 
 check:
 	LC_ALL=C $(PYFLAKES3) game_data_packager/*.py game_data_packager/*/*.py runtime/*.py tools/*.py || :
+	LC_ALL=C GDP_UNINSTALLED=1 PYTHONPATH=. $(PYTHON) tests/hashed_file.py
 	LC_ALL=C GDP_UNINSTALLED=1 PYTHONPATH=. $(PYTHON) tests/umod.py
 	LC_ALL=C GDP_UNINSTALLED=1 PYTHONPATH=. $(PYTHON) tools/check_syntax.py
 	LC_ALL=C GDP_UNINSTALLED=1 PYTHONPATH=. $(PYTHON) tools/check_equivalence.py
diff --git a/tests/hashed_file.py b/tests/hashed_file.py
new file mode 100755
index 0000000..2650ea1
--- /dev/null
+++ b/tests/hashed_file.py
@@ -0,0 +1,89 @@
+#!/usr/bin/python3
+# encoding=utf-8
+#
+# Copyright © 2016 Simon McVittie <smcv at debian.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation; either version 2
+# of the License, or (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
+#
+# You can find the GPL license text on a Debian system under
+# /usr/share/common-licenses/GPL-2.
+
+import hashlib
+import io
+import unittest
+
+from game_data_packager.build import HashedFile
+
+class HashedFileTestCase(unittest.TestCase):
+    def setUp(self):
+        hasher = hashlib.new('md5')
+        hasher.update(b'hello')
+        self.HELLO_MD5 = hasher.hexdigest()
+        hasher.update(b', world!')
+        self.HELLO_WORLD_MD5 = hasher.hexdigest()
+
+        hasher = hashlib.new('sha1')
+        hasher.update(b'hello')
+        self.HELLO_SHA1 = hasher.hexdigest()
+        hasher.update(b', world!')
+        self.HELLO_WORLD_SHA1 = hasher.hexdigest()
+
+        hasher = hashlib.new('sha256')
+        hasher.update(b'hello')
+        self.HELLO_SHA256 = hasher.hexdigest()
+        hasher.update(b', world!')
+        self.HELLO_WORLD_SHA256 = hasher.hexdigest()
+
+    def test_attrs(self):
+        hf = HashedFile('hello.txt')
+        self.assertIs(hf.have_hashes, False)
+
+        hf.md5 = self.HELLO_MD5
+        self.assertIs(hf.have_hashes, True)
+        self.assertEqual(hf.md5, self.HELLO_MD5)
+
+        with self.assertRaises(AssertionError):
+            hf.md5 = None
+
+        hf.sha1 = self.HELLO_SHA1
+        self.assertIs(hf.have_hashes, True)
+        self.assertEqual(hf.sha1, self.HELLO_SHA1)
+
+        hf.sha256 = self.HELLO_SHA256
+        self.assertIs(hf.have_hashes, True)
+        self.assertEqual(hf.sha256, self.HELLO_SHA256)
+
+    def test_matches(self):
+        first = HashedFile('hello.txt')
+        first.md5 = self.HELLO_MD5
+
+        second = HashedFile('hello.txt')
+        second.sha1 = self.HELLO_SHA1
+
+        with self.assertRaises(ValueError):
+            first.matches(second)
+
+        with self.assertRaises(ValueError):
+            second.matches(first)
+
+        second.md5 = self.HELLO_MD5
+        self.assertIs(first.matches(second), True)
+        self.assertIs(second.matches(first), True)
+
+        second = HashedFile('hello_world.txt')
+        second.md5 = self.HELLO_WORLD_MD5
+        self.assertIs(first.matches(second), False)
+        self.assertIs(second.matches(first), False)
+
+    def tearDown(self):
+        pass
+
+if __name__ == '__main__':
+    unittest.main(verbosity=2)

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-games/game-data-packager.git



More information about the Pkg-games-commits mailing list