]> git.lizzy.rs Git - rust.git/blob - src/bootstrap/bootstrap_test.py
Rollup merge of #107740 - oli-obk:lock_tcx, r=petrochenkov
[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 VerifyTestCase(unittest.TestCase):
17     """Test Case for verify"""
18     def setUp(self):
19         self.container = tempfile.mkdtemp()
20         self.src = os.path.join(self.container, "src.txt")
21         self.bad_src = os.path.join(self.container, "bad.txt")
22         content = "Hello world"
23
24         self.expected = hashlib.sha256(content.encode("utf-8")).hexdigest()
25
26         with open(self.src, "w") as src:
27             src.write(content)
28         with open(self.bad_src, "w") as bad:
29             bad.write("Hello!")
30
31     def tearDown(self):
32         rmtree(self.container)
33
34     def test_valid_file(self):
35         """Check if the sha256 sum of the given file is valid"""
36         self.assertTrue(bootstrap.verify(self.src, self.expected, False))
37
38     def test_invalid_file(self):
39         """Should verify that the file is invalid"""
40         self.assertFalse(bootstrap.verify(self.bad_src, self.expected, False))
41
42
43 class ProgramOutOfDate(unittest.TestCase):
44     """Test if a program is out of date"""
45     def setUp(self):
46         self.container = tempfile.mkdtemp()
47         os.mkdir(os.path.join(self.container, "stage0"))
48         self.build = bootstrap.RustBuild()
49         self.build.date = "2017-06-15"
50         self.build.build_dir = self.container
51         self.rustc_stamp_path = os.path.join(self.container, "stage0",
52                                              ".rustc-stamp")
53         self.key = self.build.date + str(None)
54
55     def tearDown(self):
56         rmtree(self.container)
57
58     def test_stamp_path_does_not_exist(self):
59         """Return True when the stamp file does not exist"""
60         if os.path.exists(self.rustc_stamp_path):
61             os.unlink(self.rustc_stamp_path)
62         self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
63
64     def test_dates_are_different(self):
65         """Return True when the dates are different"""
66         with open(self.rustc_stamp_path, "w") as rustc_stamp:
67             rustc_stamp.write("2017-06-14None")
68         self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
69
70     def test_same_dates(self):
71         """Return False both dates match"""
72         with open(self.rustc_stamp_path, "w") as rustc_stamp:
73             rustc_stamp.write("2017-06-15None")
74         self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
75
76
77 if __name__ == '__main__':
78     SUITE = unittest.TestSuite()
79     TEST_LOADER = unittest.TestLoader()
80     SUITE.addTest(doctest.DocTestSuite(bootstrap))
81     SUITE.addTests([
82         TEST_LOADER.loadTestsFromTestCase(VerifyTestCase),
83         TEST_LOADER.loadTestsFromTestCase(ProgramOutOfDate)])
84
85     RUNNER = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
86     result = RUNNER.run(SUITE)
87     sys.exit(0 if result.wasSuccessful() else 1)