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