3 from __future__ import absolute_import, division, print_function
11 from shutil import rmtree
16 class VerifyTestCase(unittest.TestCase):
17 """Test Case for verify"""
19 self.container = tempfile.mkdtemp()
20 self.src = os.path.join(self.container, "src.txt")
21 self.sums = os.path.join(self.container, "sums")
22 self.bad_src = os.path.join(self.container, "bad.txt")
23 content = "Hello world"
25 with open(self.src, "w") as src:
27 with open(self.sums, "w") as sums:
28 sums.write(hashlib.sha256(content.encode("utf-8")).hexdigest())
29 with open(self.bad_src, "w") as bad:
33 rmtree(self.container)
35 def test_valid_file(self):
36 """Check if the sha256 sum of the given file is valid"""
37 self.assertTrue(bootstrap.verify(self.src, self.sums, False))
39 def test_invalid_file(self):
40 """Should verify that the file is invalid"""
41 self.assertFalse(bootstrap.verify(self.bad_src, self.sums, False))
44 class ProgramOutOfDate(unittest.TestCase):
45 """Test if a program is out of date"""
47 self.container = tempfile.mkdtemp()
48 os.mkdir(os.path.join(self.container, "stage0"))
49 self.build = bootstrap.RustBuild()
50 self.build.date = "2017-06-15"
51 self.build.build_dir = self.container
52 self.rustc_stamp_path = os.path.join(self.container, "stage0",
54 self.key = self.build.date + str(None)
57 rmtree(self.container)
59 def test_stamp_path_does_not_exists(self):
60 """Return True when the stamp file does not exists"""
61 if os.path.exists(self.rustc_stamp_path):
62 os.unlink(self.rustc_stamp_path)
63 self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
65 def test_dates_are_different(self):
66 """Return True when the dates are different"""
67 with open(self.rustc_stamp_path, "w") as rustc_stamp:
68 rustc_stamp.write("2017-06-14None")
69 self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
71 def test_same_dates(self):
72 """Return False both dates match"""
73 with open(self.rustc_stamp_path, "w") as rustc_stamp:
74 rustc_stamp.write("2017-06-15None")
75 self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
78 if __name__ == '__main__':
79 SUITE = unittest.TestSuite()
80 TEST_LOADER = unittest.TestLoader()
81 SUITE.addTest(doctest.DocTestSuite(bootstrap))
83 TEST_LOADER.loadTestsFromTestCase(VerifyTestCase),
84 TEST_LOADER.loadTestsFromTestCase(ProgramOutOfDate)])
86 RUNNER = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
87 result = RUNNER.run(SUITE)
88 sys.exit(0 if result.wasSuccessful() else 1)