From d808bc296dc147ddf0d17e1816be7b84ba51675a Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 17 Mar 2023 10:24:40 -0500 Subject: Add tests for configure.py - Separate out functions so that each unit test doesn't create a file on disk - Add a few unit tests Notably, verifying that we generate valid toml relies on python 3.11 so we can use `tomllib`. --- src/bootstrap/bootstrap_test.py | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) (limited to 'src/bootstrap/bootstrap_test.py') diff --git a/src/bootstrap/bootstrap_test.py b/src/bootstrap/bootstrap_test.py index 06ca3ce21b3..6a4a64a1ed5 100644 --- a/src/bootstrap/bootstrap_test.py +++ b/src/bootstrap/bootstrap_test.py @@ -11,6 +11,7 @@ import sys from shutil import rmtree import bootstrap +import configure class VerifyTestCase(unittest.TestCase): @@ -74,12 +75,51 @@ class ProgramOutOfDate(unittest.TestCase): self.assertFalse(self.build.program_out_of_date(self.rustc_stamp_path, self.key)) +class GenerateAndParseConfig(unittest.TestCase): + """Test that we can serialize and deserialize a config.toml file""" + def serialize_and_parse(self, args): + from io import StringIO + + section_order, sections, targets = configure.parse_args(args) + buffer = StringIO() + configure.write_config_toml(buffer, section_order, targets, sections) + build = bootstrap.RustBuild() + build.config_toml = buffer.getvalue() + + try: + import tomllib + # Verify this is actually valid TOML. + tomllib.loads(build.config_toml) + except ImportError: + # too old a version of python + pass + return build + + def test_no_args(self): + build = self.serialize_and_parse([]) + self.assertEqual(build.get_toml("changelog-seen"), '2') + self.assertIsNone(build.get_toml("llvm.download-ci-llvm")) + + def test_set_section(self): + build = self.serialize_and_parse(["--set", "llvm.download-ci-llvm"]) + self.assertEqual(build.get_toml("download-ci-llvm", section="llvm"), 'true') + + def test_set_target(self): + build = self.serialize_and_parse(["--set", "target.x86_64-unknown-linux-gnu.cc=gcc"]) + self.assertEqual(build.get_toml("cc", section="target.x86_64-unknown-linux-gnu"), 'gcc') + + # Uncomment when #108928 is fixed. + # def test_set_top_level(self): + # build = self.serialize_and_parse(["--set", "profile=compiler"]) + # self.assertEqual(build.get_toml("profile"), 'compiler') + if __name__ == '__main__': SUITE = unittest.TestSuite() TEST_LOADER = unittest.TestLoader() SUITE.addTest(doctest.DocTestSuite(bootstrap)) SUITE.addTests([ TEST_LOADER.loadTestsFromTestCase(VerifyTestCase), + TEST_LOADER.loadTestsFromTestCase(GenerateAndParseConfig), TEST_LOADER.loadTestsFromTestCase(ProgramOutOfDate)]) RUNNER = unittest.TextTestRunner(stream=sys.stdout, verbosity=2) -- cgit 1.4.1-3-g733a5 From c7eccdaaee3cb5921d3d4cdb8dcb2639d4e7dec8 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Fri, 17 Mar 2023 10:39:40 -0500 Subject: Use python3.11 in CI to make sure toml is validated This also fixes a regression from https://github.com/rust-lang/rust/pull/106085 which stopped testing that we support python2 in PR CI. --- src/bootstrap/bootstrap_test.py | 3 +-- src/bootstrap/configure.py | 2 +- src/ci/docker/host-x86_64/mingw-check-tidy/Dockerfile | 5 ++++- src/ci/docker/host-x86_64/x86_64-gnu-llvm-14/Dockerfile | 4 +--- 4 files changed, 7 insertions(+), 7 deletions(-) (limited to 'src/bootstrap/bootstrap_test.py') diff --git a/src/bootstrap/bootstrap_test.py b/src/bootstrap/bootstrap_test.py index 6a4a64a1ed5..20bd71f06e9 100644 --- a/src/bootstrap/bootstrap_test.py +++ b/src/bootstrap/bootstrap_test.py @@ -91,8 +91,7 @@ class GenerateAndParseConfig(unittest.TestCase): # Verify this is actually valid TOML. tomllib.loads(build.config_toml) except ImportError: - # too old a version of python - pass + print("warning: skipping TOML validation, need at least python 3.11", file=sys.stderr) return build def test_no_args(self): diff --git a/src/bootstrap/configure.py b/src/bootstrap/configure.py index dc2271bdf27..abd28b4005d 100755 --- a/src/bootstrap/configure.py +++ b/src/bootstrap/configure.py @@ -251,7 +251,7 @@ def parse_args(args): if not found: unknown_args.append(arg) - + # Note: here and a few other places, we use [-1] to apply the *last* value # passed. But if option-checking is enabled, then the known_args loop will # also assert that options are only passed once. diff --git a/src/ci/docker/host-x86_64/mingw-check-tidy/Dockerfile b/src/ci/docker/host-x86_64/mingw-check-tidy/Dockerfile index 889a586b351..b5715024a84 100644 --- a/src/ci/docker/host-x86_64/mingw-check-tidy/Dockerfile +++ b/src/ci/docker/host-x86_64/mingw-check-tidy/Dockerfile @@ -1,6 +1,8 @@ FROM ubuntu:22.04 ARG DEBIAN_FRONTEND=noninteractive +# NOTE: intentionally uses python2 for x.py so we can test it still works. +# validate-toolstate only runs in our CI, so it's ok for it to only support python3. RUN apt-get update && apt-get install -y --no-install-recommends \ g++ \ make \ @@ -8,6 +10,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ file \ curl \ ca-certificates \ + python2.7 \ python3 \ python3-pip \ python3-pkg-resources \ @@ -30,4 +33,4 @@ RUN pip3 install --no-deps --no-cache-dir --require-hashes -r /tmp/reuse-require COPY host-x86_64/mingw-check/validate-toolstate.sh /scripts/ COPY host-x86_64/mingw-check/validate-error-codes.sh /scripts/ -ENV SCRIPT python3 ../x.py test --stage 0 src/tools/tidy tidyselftest +ENV SCRIPT python2.7 ../x.py test --stage 0 src/tools/tidy tidyselftest diff --git a/src/ci/docker/host-x86_64/x86_64-gnu-llvm-14/Dockerfile b/src/ci/docker/host-x86_64/x86_64-gnu-llvm-14/Dockerfile index a007bf183ee..dc8a4aac768 100644 --- a/src/ci/docker/host-x86_64/x86_64-gnu-llvm-14/Dockerfile +++ b/src/ci/docker/host-x86_64/x86_64-gnu-llvm-14/Dockerfile @@ -2,7 +2,6 @@ FROM ubuntu:22.04 ARG DEBIAN_FRONTEND=noninteractive -# NOTE: intentionally installs both python2 and python3 so we can test support for both. RUN apt-get update && apt-get install -y --no-install-recommends \ g++ \ gcc-multilib \ @@ -11,8 +10,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \ file \ curl \ ca-certificates \ - python2.7 \ - python3 \ + python3.11 \ git \ cmake \ sudo \ -- cgit 1.4.1-3-g733a5