]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/bootstrap_test.py
Auto merge of #44251 - kennytm:osx-backtrace, r=alexcrichton
[rust.git] / src / bootstrap / bootstrap_test.py
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.
4 #
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.
10
11 """Bootstrap tests"""
12
13 import os
14 import doctest
15 import unittest
16 import tempfile
17 import hashlib
18 import sys
19
20 from shutil import rmtree
21
22 import bootstrap
23
24
25 class Stage0DataTestCase(unittest.TestCase):
26     """Test Case for stage0_data"""
27     def setUp(self):
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")
33
34     def tearDown(self):
35         rmtree(self.rust_root)
36
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)
42
43
44 class VerifyTestCase(unittest.TestCase):
45     """Test Case for verify"""
46     def setUp(self):
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"
52
53         with open(self.src, "w") as src:
54             src.write(content)
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:
58             bad.write("Hello!")
59
60     def tearDown(self):
61         rmtree(self.container)
62
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))
66
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))
70
71
72 class ProgramOutOfDate(unittest.TestCase):
73     """Test if a program is out of date"""
74     def setUp(self):
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",
81                                              ".rustc-stamp")
82
83     def tearDown(self):
84         rmtree(self.container)
85
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))
91
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))
97
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))
103
104
105 if __name__ == '__main__':
106     SUITE = unittest.TestSuite()
107     TEST_LOADER = unittest.TestLoader()
108     SUITE.addTest(doctest.DocTestSuite(bootstrap))
109     SUITE.addTests([
110         TEST_LOADER.loadTestsFromTestCase(Stage0DataTestCase),
111         TEST_LOADER.loadTestsFromTestCase(VerifyTestCase),
112         TEST_LOADER.loadTestsFromTestCase(ProgramOutOfDate)])
113
114     RUNNER = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
115     result = RUNNER.run(SUITE)
116     sys.exit(0 if result.wasSuccessful() else 1)