Mercurial > hgrepos > Python > apps > py-cutils
comparison tests/test_shasum.py @ 101:5de499711a92
Begin some unittests for shasum
| author | Franz Glasner <fzglas.hg@dom66.de> |
|---|---|
| date | Fri, 29 Apr 2022 09:27:05 +0200 |
| parents | |
| children | 83dd2506f8f8 |
comparison
equal
deleted
inserted
replaced
| 100:f95918115c6b | 101:5de499711a92 |
|---|---|
| 1 # -*- coding: utf-8 -*- | |
| 2 r"""Unit tests | |
| 3 | |
| 4 """ | |
| 5 | |
| 6 from __future__ import absolute_import, print_function | |
| 7 | |
| 8 import io | |
| 9 import os | |
| 10 import sys | |
| 11 import unittest | |
| 12 | |
| 13 from _test_setup import DATADIR | |
| 14 | |
| 15 from cutils import shasum | |
| 16 | |
| 17 | |
| 18 PY2 = sys.version_info[0] <= 2 | |
| 19 | |
| 20 | |
| 21 class ChangedDir(object): | |
| 22 | |
| 23 """Context manager to temporarily change the directory""" | |
| 24 | |
| 25 def __init__(self, path): | |
| 26 self._new_dir = path | |
| 27 self._prev_dir = None | |
| 28 | |
| 29 def __enter__(self): | |
| 30 self._prev_dir = os.getcwd() | |
| 31 os.chdir(self._new_dir) | |
| 32 | |
| 33 def __exit__(self, *args, **kwds): | |
| 34 if self._prev_dir is not None: | |
| 35 os.chdir(self._prev_dir) | |
| 36 self._prev_dir = None | |
| 37 | |
| 38 | |
| 39 class SignifyTests(unittest.TestCase): | |
| 40 | |
| 41 def test_empty(self): | |
| 42 destfile = io.StringIO() | |
| 43 opts = shasum.gen_opts(algorithm="SHA256", | |
| 44 dest=destfile, | |
| 45 files=[os.path.join(DATADIR, "empty")]) | |
| 46 shasum.shasum(opts) | |
| 47 self.assertTrue( | |
| 48 destfile.getvalue().startswith( | |
| 49 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *")) | |
| 50 | |
| 51 def test_empty_mmap(self): | |
| 52 destfile = io.StringIO() | |
| 53 opts = shasum.gen_opts(algorithm="SHA256", | |
| 54 dest=destfile, | |
| 55 files=[os.path.join(DATADIR, "empty")], | |
| 56 mmap=True) | |
| 57 shasum.shasum(opts) | |
| 58 self.assertTrue( | |
| 59 destfile.getvalue().startswith( | |
| 60 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *")) | |
| 61 | |
| 62 def test_empty_no_mmap(self): | |
| 63 destfile = io.StringIO() | |
| 64 opts = shasum.gen_opts(algorithm="SHA256", | |
| 65 dest=destfile, | |
| 66 files=[os.path.join(DATADIR, "empty")], | |
| 67 mmap=False) | |
| 68 shasum.shasum(opts) | |
| 69 self.assertTrue( | |
| 70 destfile.getvalue().startswith( | |
| 71 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *")) | |
| 72 | |
| 73 | |
| 74 if __name__ == "__main__": | |
| 75 sys.exit(unittest.main()) |
