Mercurial > hgrepos > Python > apps > py-cutils
changeset 101:5de499711a92
Begin some unittests for shasum
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 29 Apr 2022 09:27:05 +0200 |
| parents | f95918115c6b |
| children | 83dd2506f8f8 |
| files | MANIFEST.in tests/_test_setup.py tests/data/empty tests/test_shasum.py |
| diffstat | 3 files changed, 90 insertions(+), 0 deletions(-) [+] |
line wrap: on
line diff
--- a/MANIFEST.in Fri Apr 29 04:57:08 2022 +0200 +++ b/MANIFEST.in Fri Apr 29 09:27:05 2022 +0200 @@ -1,2 +1,3 @@ include .hg* *.txt *.py +graft tests global-exclude *.pyc *.pyo
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/_test_setup.py Fri Apr 29 09:27:05 2022 +0200 @@ -0,0 +1,14 @@ +# -*- coding: utf-8 -*- +r"""Unit tests + +""" + +import os +import sys + + +TESTDIR = os.path.normpath(os.path.abspath(os.path.dirname(__file__))) +DATADIR = os.path.join(TESTDIR, "data") + + +sys.path.insert(0, os.path.join(TESTDIR, ".."))
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/test_shasum.py Fri Apr 29 09:27:05 2022 +0200 @@ -0,0 +1,75 @@ +# -*- coding: utf-8 -*- +r"""Unit tests + +""" + +from __future__ import absolute_import, print_function + +import io +import os +import sys +import unittest + +from _test_setup import DATADIR + +from cutils import shasum + + +PY2 = sys.version_info[0] <= 2 + + +class ChangedDir(object): + + """Context manager to temporarily change the directory""" + + def __init__(self, path): + self._new_dir = path + self._prev_dir = None + + def __enter__(self): + self._prev_dir = os.getcwd() + os.chdir(self._new_dir) + + def __exit__(self, *args, **kwds): + if self._prev_dir is not None: + os.chdir(self._prev_dir) + self._prev_dir = None + + +class SignifyTests(unittest.TestCase): + + def test_empty(self): + destfile = io.StringIO() + opts = shasum.gen_opts(algorithm="SHA256", + dest=destfile, + files=[os.path.join(DATADIR, "empty")]) + shasum.shasum(opts) + self.assertTrue( + destfile.getvalue().startswith( + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *")) + + def test_empty_mmap(self): + destfile = io.StringIO() + opts = shasum.gen_opts(algorithm="SHA256", + dest=destfile, + files=[os.path.join(DATADIR, "empty")], + mmap=True) + shasum.shasum(opts) + self.assertTrue( + destfile.getvalue().startswith( + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *")) + + def test_empty_no_mmap(self): + destfile = io.StringIO() + opts = shasum.gen_opts(algorithm="SHA256", + dest=destfile, + files=[os.path.join(DATADIR, "empty")], + mmap=False) + shasum.shasum(opts) + self.assertTrue( + destfile.getvalue().startswith( + "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *")) + + +if __name__ == "__main__": + sys.exit(unittest.main())
