3 from __future__ import absolute_import, division, print_function
11 from shutil import rmtree
16 class Stage0DataTestCase(unittest.TestCase):
17 """Test Case for stage0_data"""
19 self.rust_root = tempfile.mkdtemp()
20 os.mkdir(os.path.join(self.rust_root, "src"))
21 with open(os.path.join(self.rust_root, "src",
22 "stage0.txt"), "w") as stage0:
23 stage0.write("#ignore\n\ndate: 2017-06-15\nrustc: beta\ncargo: beta\nrustfmt: beta")
26 rmtree(self.rust_root)
28 def test_stage0_data(self):
29 """Extract data from stage0.txt"""
30 expected = {"date": "2017-06-15", "rustc": "beta", "cargo": "beta", "rustfmt": "beta"}
31 data = bootstrap.stage0_data(self.rust_root)
32 self.assertDictEqual(data, expected)
35 class VerifyTestCase(unittest.TestCase):
36 """Test Case for verify"""
38 self.container = tempfile.mkdtemp()
39 self.src = os.path.join(self.container, "src.txt")
40 self.sums = os.path.join(self.container, "sums")
41 self.bad_src = os.path.join(self.container, "bad.txt")
42 content = "Hello world"
44 with open(self.src, "w") as src:
46 with open(self.sums, "w") as sums:
47 sums.write(hashlib.sha256(content.encode("utf-8")).hexdigest())
48 with open(self.bad_src, "w") as bad:
52 rmtree(self.container)
54 def test_valid_file(self):
55 """Check if the sha256 sum of the given file is valid"""
56 self.assertTrue(bootstrap.verify(self.src, self.sums, False))
58 def test_invalid_file(self):
59 """Should verify that the file is invalid"""
60 self.assertFalse(bootstrap.verify(self.bad_src, self.sums, False))
63 class ProgramOutOfDate(unittest.TestCase):
64 """Test if a program is out of date"""
66 self.container = tempfile.mkdtemp()
67 os.mkdir(os.path.join(self.container, "stage0"))
68 self.build = bootstrap.RustBuild()
69 self.build.date = "2017-06-15"
70 self.build.build_dir = self.container
71 self.rustc_stamp_path = os.path.join(self.container, "stage0",
75 rmtree(self.container)
77 def test_stamp_path_does_not_exists(self):
78 """Return True when the stamp file does not exists"""
79 if os.path.exists(self.rustc_stamp_path):
80 os.unlink(self.rustc_stamp_path)
81 self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path))
83 def test_dates_are_different(self):
84 """Return True when the dates are different"""
85 with open(self.rustc_stamp_path, "w") as rustc_stamp:
86 rustc_stamp.write("2017-06-14")
87 self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path))
89 def test_same_dates(self):
90 """Return False both dates match"""
91 with open(self.rustc_stamp_path, "w") as rustc_stamp:
92 rustc_stamp.write("2017-06-15")
93 self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path))
96 if __name__ == '__main__':
97 SUITE = unittest.TestSuite()
98 TEST_LOADER = unittest.TestLoader()
99 SUITE.addTest(doctest.DocTestSuite(bootstrap))
101 TEST_LOADER.loadTestsFromTestCase(Stage0DataTestCase),
102 TEST_LOADER.loadTestsFromTestCase(VerifyTestCase),
103 TEST_LOADER.loadTestsFromTestCase(ProgramOutOfDate)])
105 RUNNER = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
106 result = RUNNER.run(SUITE)
107 sys.exit(0 if result.wasSuccessful() else 1)