1 | n/a | """Tests for the md5sum script in the Tools directory.""" |
---|
2 | n/a | |
---|
3 | n/a | import os |
---|
4 | n/a | import unittest |
---|
5 | n/a | from test import support |
---|
6 | n/a | from test.support.script_helper import assert_python_ok, assert_python_failure |
---|
7 | n/a | |
---|
8 | n/a | from test.test_tools import scriptsdir, skip_if_missing |
---|
9 | n/a | |
---|
10 | n/a | skip_if_missing() |
---|
11 | n/a | |
---|
12 | n/a | class MD5SumTests(unittest.TestCase): |
---|
13 | n/a | @classmethod |
---|
14 | n/a | def setUpClass(cls): |
---|
15 | n/a | cls.script = os.path.join(scriptsdir, 'md5sum.py') |
---|
16 | n/a | os.mkdir(support.TESTFN) |
---|
17 | n/a | cls.fodder = os.path.join(support.TESTFN, 'md5sum.fodder') |
---|
18 | n/a | with open(cls.fodder, 'wb') as f: |
---|
19 | n/a | f.write(b'md5sum\r\ntest file\r\n') |
---|
20 | n/a | cls.fodder_md5 = b'd38dae2eb1ab346a292ef6850f9e1a0d' |
---|
21 | n/a | cls.fodder_textmode_md5 = b'a8b07894e2ca3f2a4c3094065fa6e0a5' |
---|
22 | n/a | |
---|
23 | n/a | @classmethod |
---|
24 | n/a | def tearDownClass(cls): |
---|
25 | n/a | support.rmtree(support.TESTFN) |
---|
26 | n/a | |
---|
27 | n/a | def test_noargs(self): |
---|
28 | n/a | rc, out, err = assert_python_ok(self.script) |
---|
29 | n/a | self.assertEqual(rc, 0) |
---|
30 | n/a | self.assertTrue( |
---|
31 | n/a | out.startswith(b'd41d8cd98f00b204e9800998ecf8427e <stdin>')) |
---|
32 | n/a | self.assertFalse(err) |
---|
33 | n/a | |
---|
34 | n/a | def test_checksum_fodder(self): |
---|
35 | n/a | rc, out, err = assert_python_ok(self.script, self.fodder) |
---|
36 | n/a | self.assertEqual(rc, 0) |
---|
37 | n/a | self.assertTrue(out.startswith(self.fodder_md5)) |
---|
38 | n/a | for part in self.fodder.split(os.path.sep): |
---|
39 | n/a | self.assertIn(part.encode(), out) |
---|
40 | n/a | self.assertFalse(err) |
---|
41 | n/a | |
---|
42 | n/a | def test_dash_l(self): |
---|
43 | n/a | rc, out, err = assert_python_ok(self.script, '-l', self.fodder) |
---|
44 | n/a | self.assertEqual(rc, 0) |
---|
45 | n/a | self.assertIn(self.fodder_md5, out) |
---|
46 | n/a | parts = self.fodder.split(os.path.sep) |
---|
47 | n/a | self.assertIn(parts[-1].encode(), out) |
---|
48 | n/a | self.assertNotIn(parts[-2].encode(), out) |
---|
49 | n/a | |
---|
50 | n/a | def test_dash_t(self): |
---|
51 | n/a | rc, out, err = assert_python_ok(self.script, '-t', self.fodder) |
---|
52 | n/a | self.assertEqual(rc, 0) |
---|
53 | n/a | self.assertTrue(out.startswith(self.fodder_textmode_md5)) |
---|
54 | n/a | self.assertNotIn(self.fodder_md5, out) |
---|
55 | n/a | |
---|
56 | n/a | def test_dash_s(self): |
---|
57 | n/a | rc, out, err = assert_python_ok(self.script, '-s', '512', self.fodder) |
---|
58 | n/a | self.assertEqual(rc, 0) |
---|
59 | n/a | self.assertIn(self.fodder_md5, out) |
---|
60 | n/a | |
---|
61 | n/a | def test_multiple_files(self): |
---|
62 | n/a | rc, out, err = assert_python_ok(self.script, self.fodder, self.fodder) |
---|
63 | n/a | self.assertEqual(rc, 0) |
---|
64 | n/a | lines = out.splitlines() |
---|
65 | n/a | self.assertEqual(len(lines), 2) |
---|
66 | n/a | self.assertEqual(*lines) |
---|
67 | n/a | |
---|
68 | n/a | def test_usage(self): |
---|
69 | n/a | rc, out, err = assert_python_failure(self.script, '-h') |
---|
70 | n/a | self.assertEqual(rc, 2) |
---|
71 | n/a | self.assertEqual(out, b'') |
---|
72 | n/a | self.assertGreater(err, b'') |
---|
73 | n/a | |
---|
74 | n/a | |
---|
75 | n/a | if __name__ == '__main__': |
---|
76 | n/a | unittest.main() |
---|