comparison tests/test_shasum.py @ 102:83dd2506f8f8

FIX: Make tests work on Python 2
author Franz Glasner <f.glasner@feldmann-mg.com>
date Fri, 29 Apr 2022 10:19:27 +0200
parents 5de499711a92
children af8318191ed3
comparison
equal deleted inserted replaced
101:5de499711a92 102:83dd2506f8f8
7 7
8 import io 8 import io
9 import os 9 import os
10 import sys 10 import sys
11 import unittest 11 import unittest
12 try:
13 from StringIO import StringIO
14 except ImportError:
15 StringIO = None
12 16
13 from _test_setup import DATADIR 17 from _test_setup import DATADIR
14 18
15 from cutils import shasum 19 from cutils import shasum
16 20
17 21
18 PY2 = sys.version_info[0] <= 2 22 PY2 = sys.version_info[0] <= 2
23
24
25 def _memfile():
26 if StringIO:
27 return StringIO()
28 else:
29 return io.StringIO()
19 30
20 31
21 class ChangedDir(object): 32 class ChangedDir(object):
22 33
23 """Context manager to temporarily change the directory""" 34 """Context manager to temporarily change the directory"""
37 48
38 49
39 class SignifyTests(unittest.TestCase): 50 class SignifyTests(unittest.TestCase):
40 51
41 def test_empty(self): 52 def test_empty(self):
42 destfile = io.StringIO() 53 destfile = _memfile()
43 opts = shasum.gen_opts(algorithm="SHA256", 54 opts = shasum.gen_opts(algorithm="SHA256",
44 dest=destfile, 55 dest=destfile,
45 files=[os.path.join(DATADIR, "empty")]) 56 files=[os.path.join(DATADIR, "empty")])
46 shasum.shasum(opts) 57 shasum.shasum(opts)
47 self.assertTrue( 58 self.assertTrue(
48 destfile.getvalue().startswith( 59 destfile.getvalue().startswith(
49 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *")) 60 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *"))
50 61
51 def test_empty_mmap(self): 62 def test_empty_mmap(self):
52 destfile = io.StringIO() 63 destfile = _memfile()
53 opts = shasum.gen_opts(algorithm="SHA256", 64 opts = shasum.gen_opts(algorithm="SHA256",
54 dest=destfile, 65 dest=destfile,
55 files=[os.path.join(DATADIR, "empty")], 66 files=[os.path.join(DATADIR, "empty")],
56 mmap=True) 67 mmap=True)
57 shasum.shasum(opts) 68 shasum.shasum(opts)
58 self.assertTrue( 69 self.assertTrue(
59 destfile.getvalue().startswith( 70 destfile.getvalue().startswith(
60 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *")) 71 "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *"))
61 72
62 def test_empty_no_mmap(self): 73 def test_empty_no_mmap(self):
63 destfile = io.StringIO() 74 destfile = _memfile()
64 opts = shasum.gen_opts(algorithm="SHA256", 75 opts = shasum.gen_opts(algorithm="SHA256",
65 dest=destfile, 76 dest=destfile,
66 files=[os.path.join(DATADIR, "empty")], 77 files=[os.path.join(DATADIR, "empty")],
67 mmap=False) 78 mmap=False)
68 shasum.shasum(opts) 79 shasum.shasum(opts)