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