]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/bootstrap_test.py
Rollup merge of #82598 - GuillaumeGomez:rustdoc-rustc-pass, r=jyn514
[rust.git] / src / bootstrap / bootstrap_test.py
1 """Bootstrap tests"""
2
3 from __future__ import absolute_import, division, print_function
4 import os
5 import doctest
6 import unittest
7 import tempfile
8 import hashlib
9 import sys
10
11 from shutil import rmtree
12
13 import bootstrap
14
15
16 class Stage0DataTestCase(unittest.TestCase):
17     """Test Case for stage0_data"""
18     def setUp(self):
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")
24
25     def tearDown(self):
26         rmtree(self.rust_root)
27
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)
33
34
35 class VerifyTestCase(unittest.TestCase):
36     """Test Case for verify"""
37     def setUp(self):
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"
43
44         with open(self.src, "w") as src:
45             src.write(content)
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:
49             bad.write("Hello!")
50
51     def tearDown(self):
52         rmtree(self.container)
53
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))
57
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))
61
62
63 class ProgramOutOfDate(unittest.TestCase):
64     """Test if a program is out of date"""
65     def setUp(self):
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",
72                                              ".rustc-stamp")
73         self.key = self.build.date + str(None)
74
75     def tearDown(self):
76         rmtree(self.container)
77
78     def test_stamp_path_does_not_exists(self):
79         """Return True when the stamp file does not exists"""
80         if os.path.exists(self.rustc_stamp_path):
81             os.unlink(self.rustc_stamp_path)
82         self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
83
84     def test_dates_are_different(self):
85         """Return True when the dates are different"""
86         with open(self.rustc_stamp_path, "w") as rustc_stamp:
87             rustc_stamp.write("2017-06-14None")
88         self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
89
90     def test_same_dates(self):
91         """Return False both dates match"""
92         with open(self.rustc_stamp_path, "w") as rustc_stamp:
93             rustc_stamp.write("2017-06-15None")
94         self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
95
96
97 if __name__ == '__main__':
98     SUITE = unittest.TestSuite()
99     TEST_LOADER = unittest.TestLoader()
100     SUITE.addTest(doctest.DocTestSuite(bootstrap))
101     SUITE.addTests([
102         TEST_LOADER.loadTestsFromTestCase(Stage0DataTestCase),
103         TEST_LOADER.loadTestsFromTestCase(VerifyTestCase),
104         TEST_LOADER.loadTestsFromTestCase(ProgramOutOfDate)])
105
106     RUNNER = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
107     result = RUNNER.run(SUITE)
108     sys.exit(0 if result.wasSuccessful() else 1)