]>
Commit | Line | Data |
---|---|---|
3b2f2976 XL |
1 | # Copyright 2015-2016 The Rust Project Developers. See the COPYRIGHT |
2 | # file at the top-level directory of this distribution and at | |
3 | # http://rust-lang.org/COPYRIGHT. | |
4 | # | |
5 | # Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | |
6 | # http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | |
7 | # <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | |
8 | # option. This file may not be copied, modified, or distributed | |
9 | # except according to those terms. | |
10 | ||
11 | """Bootstrap tests""" | |
12 | ||
abe05a73 | 13 | from __future__ import absolute_import, division, print_function |
3b2f2976 XL |
14 | import os |
15 | import doctest | |
16 | import unittest | |
17 | import tempfile | |
18 | import hashlib | |
ea8adc8c | 19 | import sys |
3b2f2976 XL |
20 | |
21 | from shutil import rmtree | |
22 | ||
23 | import bootstrap | |
24 | ||
25 | ||
26 | class Stage0DataTestCase(unittest.TestCase): | |
27 | """Test Case for stage0_data""" | |
28 | def setUp(self): | |
29 | self.rust_root = tempfile.mkdtemp() | |
30 | os.mkdir(os.path.join(self.rust_root, "src")) | |
31 | with open(os.path.join(self.rust_root, "src", | |
32 | "stage0.txt"), "w") as stage0: | |
33 | stage0.write("#ignore\n\ndate: 2017-06-15\nrustc: beta\ncargo: beta") | |
34 | ||
35 | def tearDown(self): | |
36 | rmtree(self.rust_root) | |
37 | ||
38 | def test_stage0_data(self): | |
39 | """Extract data from stage0.txt""" | |
40 | expected = {"date": "2017-06-15", "rustc": "beta", "cargo": "beta"} | |
41 | data = bootstrap.stage0_data(self.rust_root) | |
42 | self.assertDictEqual(data, expected) | |
43 | ||
44 | ||
45 | class VerifyTestCase(unittest.TestCase): | |
46 | """Test Case for verify""" | |
47 | def setUp(self): | |
48 | self.container = tempfile.mkdtemp() | |
49 | self.src = os.path.join(self.container, "src.txt") | |
50 | self.sums = os.path.join(self.container, "sums") | |
51 | self.bad_src = os.path.join(self.container, "bad.txt") | |
52 | content = "Hello world" | |
53 | ||
54 | with open(self.src, "w") as src: | |
55 | src.write(content) | |
56 | with open(self.sums, "w") as sums: | |
57 | sums.write(hashlib.sha256(content.encode("utf-8")).hexdigest()) | |
58 | with open(self.bad_src, "w") as bad: | |
59 | bad.write("Hello!") | |
60 | ||
61 | def tearDown(self): | |
62 | rmtree(self.container) | |
63 | ||
64 | def test_valid_file(self): | |
65 | """Check if the sha256 sum of the given file is valid""" | |
66 | self.assertTrue(bootstrap.verify(self.src, self.sums, False)) | |
67 | ||
68 | def test_invalid_file(self): | |
69 | """Should verify that the file is invalid""" | |
70 | self.assertFalse(bootstrap.verify(self.bad_src, self.sums, False)) | |
71 | ||
72 | ||
73 | class ProgramOutOfDate(unittest.TestCase): | |
74 | """Test if a program is out of date""" | |
75 | def setUp(self): | |
76 | self.container = tempfile.mkdtemp() | |
77 | os.mkdir(os.path.join(self.container, "stage0")) | |
78 | self.build = bootstrap.RustBuild() | |
79 | self.build.date = "2017-06-15" | |
80 | self.build.build_dir = self.container | |
81 | self.rustc_stamp_path = os.path.join(self.container, "stage0", | |
82 | ".rustc-stamp") | |
83 | ||
84 | def tearDown(self): | |
85 | rmtree(self.container) | |
86 | ||
87 | def test_stamp_path_does_not_exists(self): | |
88 | """Return True when the stamp file does not exists""" | |
89 | if os.path.exists(self.rustc_stamp_path): | |
90 | os.unlink(self.rustc_stamp_path) | |
91 | self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path)) | |
92 | ||
93 | def test_dates_are_different(self): | |
94 | """Return True when the dates are different""" | |
95 | with open(self.rustc_stamp_path, "w") as rustc_stamp: | |
96 | rustc_stamp.write("2017-06-14") | |
97 | self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path)) | |
98 | ||
99 | def test_same_dates(self): | |
100 | """Return False both dates match""" | |
101 | with open(self.rustc_stamp_path, "w") as rustc_stamp: | |
102 | rustc_stamp.write("2017-06-15") | |
103 | self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path)) | |
104 | ||
105 | ||
106 | if __name__ == '__main__': | |
107 | SUITE = unittest.TestSuite() | |
108 | TEST_LOADER = unittest.TestLoader() | |
109 | SUITE.addTest(doctest.DocTestSuite(bootstrap)) | |
110 | SUITE.addTests([ | |
111 | TEST_LOADER.loadTestsFromTestCase(Stage0DataTestCase), | |
112 | TEST_LOADER.loadTestsFromTestCase(VerifyTestCase), | |
113 | TEST_LOADER.loadTestsFromTestCase(ProgramOutOfDate)]) | |
114 | ||
ea8adc8c XL |
115 | RUNNER = unittest.TextTestRunner(stream=sys.stdout, verbosity=2) |
116 | result = RUNNER.run(SUITE) | |
117 | sys.exit(0 if result.wasSuccessful() else 1) |