1 # Copyright 2015-2016 The Rust Project Developers. See the COPYRIGHT
2 # file at the top-level directory of this distribution and at
3 # http://rust-lang.org/COPYRIGHT.
5 # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 # option. This file may not be copied, modified, or distributed
9 # except according to those terms.
20 from shutil import rmtree
25 class Stage0DataTestCase(unittest.TestCase):
26 """Test Case for stage0_data"""
28 self.rust_root = tempfile.mkdtemp()
29 os.mkdir(os.path.join(self.rust_root, "src"))
30 with open(os.path.join(self.rust_root, "src",
31 "stage0.txt"), "w") as stage0:
32 stage0.write("#ignore\n\ndate: 2017-06-15\nrustc: beta\ncargo: beta")
35 rmtree(self.rust_root)
37 def test_stage0_data(self):
38 """Extract data from stage0.txt"""
39 expected = {"date": "2017-06-15", "rustc": "beta", "cargo": "beta"}
40 data = bootstrap.stage0_data(self.rust_root)
41 self.assertDictEqual(data, expected)
44 class VerifyTestCase(unittest.TestCase):
45 """Test Case for verify"""
47 self.container = tempfile.mkdtemp()
48 self.src = os.path.join(self.container, "src.txt")
49 self.sums = os.path.join(self.container, "sums")
50 self.bad_src = os.path.join(self.container, "bad.txt")
51 content = "Hello world"
53 with open(self.src, "w") as src:
55 with open(self.sums, "w") as sums:
56 sums.write(hashlib.sha256(content.encode("utf-8")).hexdigest())
57 with open(self.bad_src, "w") as bad:
61 rmtree(self.container)
63 def test_valid_file(self):
64 """Check if the sha256 sum of the given file is valid"""
65 self.assertTrue(bootstrap.verify(self.src, self.sums, False))
67 def test_invalid_file(self):
68 """Should verify that the file is invalid"""
69 self.assertFalse(bootstrap.verify(self.bad_src, self.sums, False))
72 class ProgramOutOfDate(unittest.TestCase):
73 """Test if a program is out of date"""
75 self.container = tempfile.mkdtemp()
76 os.mkdir(os.path.join(self.container, "stage0"))
77 self.build = bootstrap.RustBuild()
78 self.build.date = "2017-06-15"
79 self.build.build_dir = self.container
80 self.rustc_stamp_path = os.path.join(self.container, "stage0",
84 rmtree(self.container)
86 def test_stamp_path_does_not_exists(self):
87 """Return True when the stamp file does not exists"""
88 if os.path.exists(self.rustc_stamp_path):
89 os.unlink(self.rustc_stamp_path)
90 self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path))
92 def test_dates_are_different(self):
93 """Return True when the dates are different"""
94 with open(self.rustc_stamp_path, "w") as rustc_stamp:
95 rustc_stamp.write("2017-06-14")
96 self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path))
98 def test_same_dates(self):
99 """Return False both dates match"""
100 with open(self.rustc_stamp_path, "w") as rustc_stamp:
101 rustc_stamp.write("2017-06-15")
102 self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path))
105 if __name__ == '__main__':
106 SUITE = unittest.TestSuite()
107 TEST_LOADER = unittest.TestLoader()
108 SUITE.addTest(doctest.DocTestSuite(bootstrap))
110 TEST_LOADER.loadTestsFromTestCase(Stage0DataTestCase),
111 TEST_LOADER.loadTestsFromTestCase(VerifyTestCase),
112 TEST_LOADER.loadTestsFromTestCase(ProgramOutOfDate)])
114 RUNNER = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
115 result = RUNNER.run(SUITE)
116 sys.exit(0 if result.wasSuccessful() else 1)