]> git.proxmox.com Git - rustc.git/blame - src/bootstrap/bootstrap_test.py
New upstream version 1.57.0+dfsg1
[rustc.git] / src / bootstrap / bootstrap_test.py
CommitLineData
3b2f2976
XL
1"""Bootstrap tests"""
2
abe05a73 3from __future__ import absolute_import, division, print_function
3b2f2976
XL
4import os
5import doctest
6import unittest
7import tempfile
8import hashlib
ea8adc8c 9import sys
3b2f2976
XL
10
11from shutil import rmtree
12
13import bootstrap
14
15
3b2f2976
XL
16class 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")
3b2f2976
XL
21 self.bad_src = os.path.join(self.container, "bad.txt")
22 content = "Hello world"
23
c295e0f8
XL
24 self.expected = hashlib.sha256(content.encode("utf-8")).hexdigest()
25
3b2f2976
XL
26 with open(self.src, "w") as src:
27 src.write(content)
3b2f2976
XL
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"""
c295e0f8 36 self.assertTrue(bootstrap.verify(self.src, self.expected, False))
3b2f2976
XL
37
38 def test_invalid_file(self):
39 """Should verify that the file is invalid"""
c295e0f8 40 self.assertFalse(bootstrap.verify(self.bad_src, self.expected, False))
3b2f2976
XL
41
42
43class 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")
5869c6ff 53 self.key = self.build.date + str(None)
3b2f2976
XL
54
55 def tearDown(self):
56 rmtree(self.container)
57
58 def test_stamp_path_does_not_exists(self):
59 """Return True when the stamp file does not exists"""
60 if os.path.exists(self.rustc_stamp_path):
61 os.unlink(self.rustc_stamp_path)
5869c6ff 62 self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
3b2f2976
XL
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:
5869c6ff
XL
67 rustc_stamp.write("2017-06-14None")
68 self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
3b2f2976
XL
69
70 def test_same_dates(self):
71 """Return False both dates match"""
72 with open(self.rustc_stamp_path, "w") as rustc_stamp:
5869c6ff
XL
73 rustc_stamp.write("2017-06-15None")
74 self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path, self.key))
3b2f2976
XL
75
76
77if __name__ == '__main__':
78 SUITE = unittest.TestSuite()
79 TEST_LOADER = unittest.TestLoader()
80 SUITE.addTest(doctest.DocTestSuite(bootstrap))
81 SUITE.addTests([
3b2f2976
XL
82 TEST_LOADER.loadTestsFromTestCase(VerifyTestCase),
83 TEST_LOADER.loadTestsFromTestCase(ProgramOutOfDate)])
84
ea8adc8c
XL
85 RUNNER = unittest.TextTestRunner(stream=sys.stdout, verbosity=2)
86 result = RUNNER.run(SUITE)
87 sys.exit(0 if result.wasSuccessful() else 1)