fork/rust
git clone https://git.julia.blue/fork/rust
3bcad65fbf6
master
1"""Bootstrap tests 2 3Run these with `x test bootstrap`, or `python -m unittest src/bootstrap/bootstrap_test.py`.""" 4 5from __future__ import absolute_import, division, print_function 6import os 7import unittest 8import tempfile 9import hashlib 10import sys 11 12from shutil import rmtree 13 14# Allow running this from the top-level directory. 15bootstrap_dir = os.path.dirname(os.path.abspath(__file__)) 16# For the import below, have Python search in src/bootstrap first. 17sys.path.insert(0, bootstrap_dir) 18import bootstrap # noqa: E402 19import configure # noqa: E402 20 21def serialize_and_parse(configure_args, bootstrap_args=None): 22 from io import StringIO 23 24 if bootstrap_args is None: 25 bootstrap_args = bootstrap.FakeArgs() 26 27 section_order, sections, targets = configure.parse_args(configure_args) 28 buffer = StringIO() 29 configure.write_config_toml(buffer, section_order, targets, sections) 30 build = bootstrap.RustBuild(config_toml=buffer.getvalue(), args=bootstrap_args) 31 32 try: 33 import tomllib 34 # Verify this is actually valid TOML. 35 tomllib.loads(build.config_toml) 36 except ImportError: 37 print("warning: skipping TOML validation, need at least python 3.11", file=sys.stderr) 38 return build 39 40 41class VerifyTestCase(unittest.TestCase): 42 """Test Case for verify""" 43 def setUp(self): 44 self.container = tempfile.mkdtemp() 45 self.src = os.path.join(self.container, "src.txt") 46 self.bad_src = os.path.join(self.container, "bad.txt") 47 content = "Hello world" 48 49 self.expected = hashlib.sha256(content.encode("utf-8")).hexdigest() 50 51 with open(self.src, "w") as src: 52 src.write(content) 53 with open(self.bad_src, "w") as bad: 54 bad.write("Hello!") 55 56 def tearDown(self): 57 rmtree(self.container) 58 59 def test_valid_file(self): 60 """Check if the sha256 sum of the given file is valid""" 61 self.assertTrue(bootstrap.verify(self.src, self.expected, False)) 62 63 def test_invalid_file(self): 64 """Should verify that the file is invalid""" 65 self.assertFalse(bootstrap.verify(self.bad_src, self.expected, False)) 66 67 68class ProgramOutOfDate(unittest.TestCase): 69 """Test if a program is out of date""" 70 def setUp(self): 71 self.container = tempfile.mkdtemp() 72 os.mkdir(os.path.join(self.container, "stage0")) 73 self.build = bootstrap.RustBuild() 74 self.build.date = "2017-06-15" 75 self.build.build_dir = self.container 76 self.rustc_stamp_path = os.path.join(self.container, "stage0", 77 ".rustc-stamp") 78 self.key = self.build.date + str(None) 79 80 def tearDown(self): 81 rmtree(self.container) 82 83 def test_stamp_path_does_not_exist(self): 84 """Return True when the stamp file does not exist""" 85 if os.path.exists(self.rustc_stamp_path): 86 os.unlink(self.rustc_stamp_path) 87 self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path, self.key)) 88 89 def test_dates_are_different(self): 90 """Return True when the dates are different""" 91 with open(self.rustc_stamp_path, "w") as rustc_stamp: 92 rustc_stamp.write("2017-06-14None") 93 self.assertTrue(self.build.program_out_of_date(self.rustc_stamp_path, self.key)) 94 95 def test_same_dates(self): 96 """Return False both dates match""" 97 with open(self.rustc_stamp_path, "w") as rustc_stamp: 98 rustc_stamp.write("2017-06-15None") 99 self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path, self.key)) 100 101 102class GenerateAndParseConfig(unittest.TestCase): 103 """Test that we can serialize and deserialize a config.toml file""" 104 def test_no_args(self): 105 build = serialize_and_parse([]) 106 self.assertEqual(build.get_toml("change-id"), '115898') 107 self.assertEqual(build.get_toml("profile"), 'dist') 108 self.assertIsNone(build.get_toml("llvm.download-ci-llvm")) 109 110 def test_set_section(self): 111 build = serialize_and_parse(["--set", "llvm.download-ci-llvm"]) 112 self.assertEqual(build.get_toml("download-ci-llvm", section="llvm"), 'true') 113 114 def test_set_target(self): 115 build = serialize_and_parse(["--set", "target.x86_64-unknown-linux-gnu.cc=gcc"]) 116 self.assertEqual(build.get_toml("cc", section="target.x86_64-unknown-linux-gnu"), 'gcc') 117 118 def test_set_top_level(self): 119 build = serialize_and_parse(["--set", "profile=compiler"]) 120 self.assertEqual(build.get_toml("profile"), 'compiler') 121 122 def test_set_codegen_backends(self): 123 build = serialize_and_parse(["--set", "rust.codegen-backends=cranelift"]) 124 self.assertNotEqual(build.config_toml.find("codegen-backends = ['cranelift']"), -1) 125 build = serialize_and_parse(["--set", "rust.codegen-backends=cranelift,llvm"]) 126 self.assertNotEqual(build.config_toml.find("codegen-backends = ['cranelift', 'llvm']"), -1) 127 build = serialize_and_parse(["--enable-full-tools"]) 128 self.assertNotEqual(build.config_toml.find("codegen-backends = ['llvm']"), -1) 129 130 131class BuildBootstrap(unittest.TestCase): 132 """Test that we generate the appropriate arguments when building bootstrap""" 133 134 def build_args(self, configure_args=None, args=None, env=None): 135 if configure_args is None: 136 configure_args = [] 137 if args is None: 138 args = [] 139 if env is None: 140 env = {} 141 142 env = env.copy() 143 env["PATH"] = os.environ["PATH"] 144 145 parsed = bootstrap.parse_args(args) 146 build = serialize_and_parse(configure_args, parsed) 147 # Make these optional so that `python -m unittest` works when run manually. 148 build_dir = os.environ.get("BUILD_DIR") 149 if build_dir is not None: 150 build.build_dir = build_dir 151 build_platform = os.environ.get("BUILD_PLATFORM") 152 if build_platform is not None: 153 build.build = build_platform 154 return build.build_bootstrap_cmd(env), env 155 156 def test_cargoflags(self): 157 args, _ = self.build_args(env={"CARGOFLAGS": "--timings"}) 158 self.assertTrue("--timings" in args) 159 160 def test_warnings(self): 161 for toml_warnings in ['false', 'true', None]: 162 configure_args = [] 163 if toml_warnings is not None: 164 configure_args = ["--set", "rust.deny-warnings=" + toml_warnings] 165 166 _, env = self.build_args(configure_args, args=["--warnings=warn"]) 167 self.assertFalse("-Dwarnings" in env["RUSTFLAGS"]) 168 169 _, env = self.build_args(configure_args, args=["--warnings=deny"]) 170 self.assertTrue("-Dwarnings" in env["RUSTFLAGS"])