fork/rust

git clone https://git.julia.blue/fork/rust

borsAuto merge of #71365 - Mark-Simulacrum:stable-next, r=Mark-Simulacrum4fb7144ed15

1.43.0
3.7 KiB107 linesraw
1"""Bootstrap tests"""
2
3from __future__ import absolute_import, division, print_function
4import os
5import doctest
6import unittest
7import tempfile
8import hashlib
9import sys
10
11from shutil import rmtree
12
13import bootstrap
14
15
16class 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
35class 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
63class 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
74    def tearDown(self):
75        rmtree(self.container)
76
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))
82
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))
88
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))
94
95
96if __name__ == '__main__':
97    SUITE = unittest.TestSuite()
98    TEST_LOADER = unittest.TestLoader()
99    SUITE.addTest(doctest.DocTestSuite(bootstrap))
100    SUITE.addTests([
101        TEST_LOADER.loadTestsFromTestCase(Stage0DataTestCase),
102        TEST_LOADER.loadTestsFromTestCase(VerifyTestCase),
103        TEST_LOADER.loadTestsFromTestCase(ProgramOutOfDate)])
104
105    RUNNER = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
106    result = RUNNER.run(SUITE)
107    sys.exit(0 if result.wasSuccessful() else 1)