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.
13 from __future__ import absolute_import, division, print_function
21 from shutil import rmtree
26 class Stage0DataTestCase(unittest.TestCase):
27 """Test Case for stage0_data"""
29 self.rust_root = tempfile.mkdtemp()
30 os.mkdir(os.path.join(self.rust_root, "src"))
31 with open(os.path.join(self.rust_root, "src",
32 "stage0.txt"), "w") as stage0:
33 stage0.write("#ignore\n\ndate: 2017-06-15\nrustc: beta\ncargo: beta")
36 rmtree(self.rust_root)
38 def test_stage0_data(self):
39 """Extract data from stage0.txt"""
40 expected = {"date": "2017-06-15", "rustc": "beta", "cargo": "beta"}
41 data = bootstrap.stage0_data(self.rust_root)
42 self.assertDictEqual(data, expected)
45 class VerifyTestCase(unittest.TestCase):
46 """Test Case for verify"""
48 self.container = tempfile.mkdtemp()
49 self.src = os.path.join(self.container, "src.txt")
50 self.sums = os.path.join(self.container, "sums")
51 self.bad_src = os.path.join(self.container, "bad.txt")
52 content = "Hello world"
54 with open(self.src, "w") as src:
56 with open(self.sums, "w") as sums:
57 sums.write(hashlib.sha256(content.encode("utf-8")).hexdigest())
58 with open(self.bad_src, "w") as bad:
62 rmtree(self.container)
64 def test_valid_file(self):
65 """Check if the sha256 sum of the given file is valid"""
66 self.assertTrue(bootstrap.verify(self.src, self.sums, False))
68 def test_invalid_file(self):
69 """Should verify that the file is invalid"""
70 self.assertFalse(bootstrap.verify(self.bad_src, self.sums, False))
73 class ProgramOutOfDate(unittest.TestCase):
74 """Test if a program is out of date"""
76 self.container = tempfile.mkdtemp()
77 os.mkdir(os.path.join(self.container, "stage0"))
78 self.build = bootstrap.RustBuild()
79 self.build.date = "2017-06-15"
80 self.build.build_dir = self.container
81 self.rustc_stamp_path = os.path.join(self.container, "stage0",
85 rmtree(self.container)
87 def test_stamp_path_does_not_exists(self):
88 """Return True when the stamp file does not exists"""
89 if os.path.exists(self.rustc_stamp_path):
90 os.unlink(self.rustc_stamp_path)
91 self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path))
93 def test_dates_are_different(self):
94 """Return True when the dates are different"""
95 with open(self.rustc_stamp_path, "w") as rustc_stamp:
96 rustc_stamp.write("2017-06-14")
97 self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path))
99 def test_same_dates(self):
100 """Return False both dates match"""
101 with open(self.rustc_stamp_path, "w") as rustc_stamp:
102 rustc_stamp.write("2017-06-15")
103 self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path))
106 if __name__ == '__main__':
107 SUITE = unittest.TestSuite()
108 TEST_LOADER = unittest.TestLoader()
109 SUITE.addTest(doctest.DocTestSuite(bootstrap))
111 TEST_LOADER.loadTestsFromTestCase(Stage0DataTestCase),
112 TEST_LOADER.loadTestsFromTestCase(VerifyTestCase),
113 TEST_LOADER.loadTestsFromTestCase(ProgramOutOfDate)])
115 RUNNER = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
116 result = RUNNER.run(SUITE)
117 sys.exit(0 if result.wasSuccessful() else 1)