162306a36Sopenharmony_ci#!/usr/bin/env python3
262306a36Sopenharmony_ci# SPDX-License-Identifier: GPL-2.0
362306a36Sopenharmony_ci
462306a36Sopenharmony_ci"""Tests the `rust_is_available.sh` script.
562306a36Sopenharmony_ci
662306a36Sopenharmony_ciSome of the tests require the real programs to be available in `$PATH`
762306a36Sopenharmony_ciunder their canonical name (and with the expected versions).
862306a36Sopenharmony_ci"""
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ciimport enum
1162306a36Sopenharmony_ciimport os
1262306a36Sopenharmony_ciimport pathlib
1362306a36Sopenharmony_ciimport stat
1462306a36Sopenharmony_ciimport subprocess
1562306a36Sopenharmony_ciimport tempfile
1662306a36Sopenharmony_ciimport unittest
1762306a36Sopenharmony_ci
1862306a36Sopenharmony_ciclass TestRustIsAvailable(unittest.TestCase):
1962306a36Sopenharmony_ci    @enum.unique
2062306a36Sopenharmony_ci    class Expected(enum.Enum):
2162306a36Sopenharmony_ci        SUCCESS = enum.auto()
2262306a36Sopenharmony_ci        SUCCESS_WITH_WARNINGS = enum.auto()
2362306a36Sopenharmony_ci        SUCCESS_WITH_EXTRA_OUTPUT = enum.auto()
2462306a36Sopenharmony_ci        FAILURE = enum.auto()
2562306a36Sopenharmony_ci
2662306a36Sopenharmony_ci    @classmethod
2762306a36Sopenharmony_ci    def generate_executable(cls, content):
2862306a36Sopenharmony_ci        path = pathlib.Path(cls.tempdir.name)
2962306a36Sopenharmony_ci        name = str(len(tuple(path.iterdir())))
3062306a36Sopenharmony_ci        path = path / name
3162306a36Sopenharmony_ci        with open(path, "w") as file_:
3262306a36Sopenharmony_ci            file_.write(content)
3362306a36Sopenharmony_ci        os.chmod(path, os.stat(path).st_mode | stat.S_IXUSR)
3462306a36Sopenharmony_ci        return path
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci    @classmethod
3762306a36Sopenharmony_ci    def generate_clang(cls, stdout):
3862306a36Sopenharmony_ci        return cls.generate_executable(f"""#!/usr/bin/env python3
3962306a36Sopenharmony_ciimport sys
4062306a36Sopenharmony_ciif "-E" in " ".join(sys.argv):
4162306a36Sopenharmony_ci    print({repr("Clang " + " ".join(cls.llvm_default_version.split(" ")))})
4262306a36Sopenharmony_cielse:
4362306a36Sopenharmony_ci    print({repr(stdout)})
4462306a36Sopenharmony_ci""")
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_ci    @classmethod
4762306a36Sopenharmony_ci    def generate_rustc(cls, stdout):
4862306a36Sopenharmony_ci        return cls.generate_executable(f"""#!/usr/bin/env python3
4962306a36Sopenharmony_ciimport sys
5062306a36Sopenharmony_ciif "--print sysroot" in " ".join(sys.argv):
5162306a36Sopenharmony_ci    print({repr(cls.rust_default_sysroot)})
5262306a36Sopenharmony_cielse:
5362306a36Sopenharmony_ci    print({repr(stdout)})
5462306a36Sopenharmony_ci""")
5562306a36Sopenharmony_ci
5662306a36Sopenharmony_ci    @classmethod
5762306a36Sopenharmony_ci    def generate_bindgen(cls, version_stdout, libclang_stderr):
5862306a36Sopenharmony_ci        return cls.generate_executable(f"""#!/usr/bin/env python3
5962306a36Sopenharmony_ciimport sys
6062306a36Sopenharmony_ciif "rust_is_available_bindgen_libclang.h" in " ".join(sys.argv):
6162306a36Sopenharmony_ci    print({repr(libclang_stderr)}, file=sys.stderr)
6262306a36Sopenharmony_cielse:
6362306a36Sopenharmony_ci    print({repr(version_stdout)})
6462306a36Sopenharmony_ci""")
6562306a36Sopenharmony_ci
6662306a36Sopenharmony_ci    @classmethod
6762306a36Sopenharmony_ci    def generate_bindgen_version(cls, stdout):
6862306a36Sopenharmony_ci        return cls.generate_bindgen(stdout, cls.bindgen_default_bindgen_libclang_stderr)
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci    @classmethod
7162306a36Sopenharmony_ci    def generate_bindgen_libclang(cls, stderr):
7262306a36Sopenharmony_ci        return cls.generate_bindgen(cls.bindgen_default_bindgen_version_stdout, stderr)
7362306a36Sopenharmony_ci
7462306a36Sopenharmony_ci    @classmethod
7562306a36Sopenharmony_ci    def setUpClass(cls):
7662306a36Sopenharmony_ci        cls.tempdir = tempfile.TemporaryDirectory()
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci        cls.missing = pathlib.Path(cls.tempdir.name) / "missing"
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_ci        cls.nonexecutable = pathlib.Path(cls.tempdir.name) / "nonexecutable"
8162306a36Sopenharmony_ci        with open(cls.nonexecutable, "w") as file_:
8262306a36Sopenharmony_ci            file_.write("nonexecutable")
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci        cls.unexpected_binary = "true"
8562306a36Sopenharmony_ci
8662306a36Sopenharmony_ci        cls.rustc_default_version = subprocess.check_output(("scripts/min-tool-version.sh", "rustc")).decode().strip()
8762306a36Sopenharmony_ci        cls.bindgen_default_version = subprocess.check_output(("scripts/min-tool-version.sh", "bindgen")).decode().strip()
8862306a36Sopenharmony_ci        cls.llvm_default_version = subprocess.check_output(("scripts/min-tool-version.sh", "llvm")).decode().strip()
8962306a36Sopenharmony_ci        cls.rust_default_sysroot = subprocess.check_output(("rustc", "--print", "sysroot")).decode().strip()
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci        cls.bindgen_default_bindgen_version_stdout = f"bindgen {cls.bindgen_default_version}"
9262306a36Sopenharmony_ci        cls.bindgen_default_bindgen_libclang_stderr = f"scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {cls.llvm_default_version} [-W#pragma-messages], err: false"
9362306a36Sopenharmony_ci
9462306a36Sopenharmony_ci        cls.default_rustc = cls.generate_rustc(f"rustc {cls.rustc_default_version}")
9562306a36Sopenharmony_ci        cls.default_bindgen =  cls.generate_bindgen(cls.bindgen_default_bindgen_version_stdout, cls.bindgen_default_bindgen_libclang_stderr)
9662306a36Sopenharmony_ci        cls.default_cc = cls.generate_clang(f"clang version {cls.llvm_default_version}")
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_ci    def run_script(self, expected, override_env):
9962306a36Sopenharmony_ci        env = {
10062306a36Sopenharmony_ci            "RUSTC": self.default_rustc,
10162306a36Sopenharmony_ci            "BINDGEN": self.default_bindgen,
10262306a36Sopenharmony_ci            "CC": self.default_cc,
10362306a36Sopenharmony_ci        }
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci        for key, value in override_env.items():
10662306a36Sopenharmony_ci            if value is None:
10762306a36Sopenharmony_ci                del env[key]
10862306a36Sopenharmony_ci                continue
10962306a36Sopenharmony_ci            env[key] = value
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ci        result = subprocess.run("scripts/rust_is_available.sh", env=env, capture_output=True)
11262306a36Sopenharmony_ci
11362306a36Sopenharmony_ci        # The script should never output anything to `stdout`.
11462306a36Sopenharmony_ci        self.assertEqual(result.stdout, b"")
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_ci        if expected == self.Expected.SUCCESS:
11762306a36Sopenharmony_ci            # When expecting a success, the script should return 0
11862306a36Sopenharmony_ci            # and it should not output anything to `stderr`.
11962306a36Sopenharmony_ci            self.assertEqual(result.returncode, 0)
12062306a36Sopenharmony_ci            self.assertEqual(result.stderr, b"")
12162306a36Sopenharmony_ci        elif expected == self.Expected.SUCCESS_WITH_EXTRA_OUTPUT:
12262306a36Sopenharmony_ci            # When expecting a success with extra output (that is not warnings,
12362306a36Sopenharmony_ci            # which is the common case), the script should return 0 and it
12462306a36Sopenharmony_ci            # should output at least something to `stderr` (the output should
12562306a36Sopenharmony_ci            # be checked further by the test).
12662306a36Sopenharmony_ci            self.assertEqual(result.returncode, 0)
12762306a36Sopenharmony_ci            self.assertNotEqual(result.stderr, b"")
12862306a36Sopenharmony_ci        elif expected == self.Expected.SUCCESS_WITH_WARNINGS:
12962306a36Sopenharmony_ci            # When expecting a success with warnings, the script should return 0
13062306a36Sopenharmony_ci            # and it should output at least the instructions to `stderr`.
13162306a36Sopenharmony_ci            self.assertEqual(result.returncode, 0)
13262306a36Sopenharmony_ci            self.assertIn(b"Please see Documentation/rust/quick-start.rst for details", result.stderr)
13362306a36Sopenharmony_ci        else:
13462306a36Sopenharmony_ci            # When expecting a failure, the script should return non-0
13562306a36Sopenharmony_ci            # and it should output at least the instructions to `stderr`.
13662306a36Sopenharmony_ci            self.assertNotEqual(result.returncode, 0)
13762306a36Sopenharmony_ci            self.assertIn(b"Please see Documentation/rust/quick-start.rst for details", result.stderr)
13862306a36Sopenharmony_ci
13962306a36Sopenharmony_ci        # The output will generally be UTF-8 (i.e. unless the user has
14062306a36Sopenharmony_ci        # put strange values in the environment).
14162306a36Sopenharmony_ci        result.stderr = result.stderr.decode()
14262306a36Sopenharmony_ci
14362306a36Sopenharmony_ci        return result
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci    def test_rustc_unset(self):
14662306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "RUSTC": None })
14762306a36Sopenharmony_ci        self.assertIn("Environment variable 'RUSTC' is not set.", result.stderr)
14862306a36Sopenharmony_ci        self.assertIn("This script is intended to be called from Kbuild.", result.stderr)
14962306a36Sopenharmony_ci
15062306a36Sopenharmony_ci    def test_bindgen_unset(self):
15162306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": None })
15262306a36Sopenharmony_ci        self.assertIn("Environment variable 'BINDGEN' is not set.", result.stderr)
15362306a36Sopenharmony_ci        self.assertIn("This script is intended to be called from Kbuild.", result.stderr)
15462306a36Sopenharmony_ci
15562306a36Sopenharmony_ci    def test_cc_unset(self):
15662306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "CC": None })
15762306a36Sopenharmony_ci        self.assertIn("Environment variable 'CC' is not set.", result.stderr)
15862306a36Sopenharmony_ci        self.assertIn("This script is intended to be called from Kbuild.", result.stderr)
15962306a36Sopenharmony_ci
16062306a36Sopenharmony_ci    def test_rustc_missing(self):
16162306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "RUSTC": self.missing })
16262306a36Sopenharmony_ci        self.assertIn(f"Rust compiler '{self.missing}' could not be found.", result.stderr)
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci    def test_bindgen_missing(self):
16562306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": self.missing })
16662306a36Sopenharmony_ci        self.assertIn(f"Rust bindings generator '{self.missing}' could not be found.", result.stderr)
16762306a36Sopenharmony_ci
16862306a36Sopenharmony_ci    def test_rustc_nonexecutable(self):
16962306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "RUSTC": self.nonexecutable })
17062306a36Sopenharmony_ci        self.assertIn(f"Running '{self.nonexecutable}' to check the Rust compiler version failed with", result.stderr)
17162306a36Sopenharmony_ci
17262306a36Sopenharmony_ci    def test_rustc_unexpected_binary(self):
17362306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "RUSTC": self.unexpected_binary })
17462306a36Sopenharmony_ci        self.assertIn(f"Running '{self.unexpected_binary}' to check the Rust compiler version did not return", result.stderr)
17562306a36Sopenharmony_ci
17662306a36Sopenharmony_ci    def test_rustc_unexpected_name(self):
17762306a36Sopenharmony_ci        rustc = self.generate_rustc(f"unexpected {self.rustc_default_version} (a8314ef7d 2022-06-27)")
17862306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "RUSTC": rustc })
17962306a36Sopenharmony_ci        self.assertIn(f"Running '{rustc}' to check the Rust compiler version did not return", result.stderr)
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci    def test_rustc_unexpected_version(self):
18262306a36Sopenharmony_ci        rustc = self.generate_rustc("rustc unexpected (a8314ef7d 2022-06-27)")
18362306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "RUSTC": rustc })
18462306a36Sopenharmony_ci        self.assertIn(f"Running '{rustc}' to check the Rust compiler version did not return", result.stderr)
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci    def test_rustc_no_minor(self):
18762306a36Sopenharmony_ci        rustc = self.generate_rustc(f"rustc {'.'.join(self.rustc_default_version.split('.')[:2])} (a8314ef7d 2022-06-27)")
18862306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "RUSTC": rustc })
18962306a36Sopenharmony_ci        self.assertIn(f"Running '{rustc}' to check the Rust compiler version did not return", result.stderr)
19062306a36Sopenharmony_ci
19162306a36Sopenharmony_ci    def test_rustc_old_version(self):
19262306a36Sopenharmony_ci        rustc = self.generate_rustc("rustc 1.60.0 (a8314ef7d 2022-06-27)")
19362306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "RUSTC": rustc })
19462306a36Sopenharmony_ci        self.assertIn(f"Rust compiler '{rustc}' is too old.", result.stderr)
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_ci    def test_rustc_new_version(self):
19762306a36Sopenharmony_ci        rustc = self.generate_rustc("rustc 1.999.0 (a8314ef7d 2099-06-27)")
19862306a36Sopenharmony_ci        result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "RUSTC": rustc })
19962306a36Sopenharmony_ci        self.assertIn(f"Rust compiler '{rustc}' is too new. This may or may not work.", result.stderr)
20062306a36Sopenharmony_ci
20162306a36Sopenharmony_ci    def test_bindgen_nonexecutable(self):
20262306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": self.nonexecutable })
20362306a36Sopenharmony_ci        self.assertIn(f"Running '{self.nonexecutable}' to check the Rust bindings generator version failed with", result.stderr)
20462306a36Sopenharmony_ci
20562306a36Sopenharmony_ci    def test_bindgen_unexpected_binary(self):
20662306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": self.unexpected_binary })
20762306a36Sopenharmony_ci        self.assertIn(f"Running '{self.unexpected_binary}' to check the bindings generator version did not return", result.stderr)
20862306a36Sopenharmony_ci
20962306a36Sopenharmony_ci    def test_bindgen_unexpected_name(self):
21062306a36Sopenharmony_ci        bindgen = self.generate_bindgen_version(f"unexpected {self.bindgen_default_version}")
21162306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
21262306a36Sopenharmony_ci        self.assertIn(f"Running '{bindgen}' to check the bindings generator version did not return", result.stderr)
21362306a36Sopenharmony_ci
21462306a36Sopenharmony_ci    def test_bindgen_unexpected_version(self):
21562306a36Sopenharmony_ci        bindgen = self.generate_bindgen_version("bindgen unexpected")
21662306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
21762306a36Sopenharmony_ci        self.assertIn(f"Running '{bindgen}' to check the bindings generator version did not return", result.stderr)
21862306a36Sopenharmony_ci
21962306a36Sopenharmony_ci    def test_bindgen_no_minor(self):
22062306a36Sopenharmony_ci        bindgen = self.generate_bindgen_version(f"bindgen {'.'.join(self.bindgen_default_version.split('.')[:2])}")
22162306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
22262306a36Sopenharmony_ci        self.assertIn(f"Running '{bindgen}' to check the bindings generator version did not return", result.stderr)
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ci    def test_bindgen_old_version(self):
22562306a36Sopenharmony_ci        bindgen = self.generate_bindgen_version("bindgen 0.50.0")
22662306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
22762306a36Sopenharmony_ci        self.assertIn(f"Rust bindings generator '{bindgen}' is too old.", result.stderr)
22862306a36Sopenharmony_ci
22962306a36Sopenharmony_ci    def test_bindgen_new_version(self):
23062306a36Sopenharmony_ci        bindgen = self.generate_bindgen_version("bindgen 0.999.0")
23162306a36Sopenharmony_ci        result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "BINDGEN": bindgen })
23262306a36Sopenharmony_ci        self.assertIn(f"Rust bindings generator '{bindgen}' is too new. This may or may not work.", result.stderr)
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci    def test_bindgen_libclang_failure(self):
23562306a36Sopenharmony_ci        for env in (
23662306a36Sopenharmony_ci            { "LLVM_CONFIG_PATH": self.missing },
23762306a36Sopenharmony_ci            { "LIBCLANG_PATH": self.missing },
23862306a36Sopenharmony_ci            { "CLANG_PATH": self.missing },
23962306a36Sopenharmony_ci        ):
24062306a36Sopenharmony_ci            with self.subTest(env=env):
24162306a36Sopenharmony_ci                result = self.run_script(self.Expected.FAILURE, env | { "PATH": os.environ["PATH"], "BINDGEN": "bindgen" })
24262306a36Sopenharmony_ci                self.assertIn("Running 'bindgen' to check the libclang version (used by the Rust", result.stderr)
24362306a36Sopenharmony_ci                self.assertIn("bindings generator) failed with code ", result.stderr)
24462306a36Sopenharmony_ci
24562306a36Sopenharmony_ci    def test_bindgen_libclang_unexpected_version(self):
24662306a36Sopenharmony_ci        bindgen = self.generate_bindgen_libclang("scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version unexpected [-W#pragma-messages], err: false")
24762306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
24862306a36Sopenharmony_ci        self.assertIn(f"Running '{bindgen}' to check the libclang version (used by the Rust", result.stderr)
24962306a36Sopenharmony_ci        self.assertIn("bindings generator) did not return an expected output. See output", result.stderr)
25062306a36Sopenharmony_ci
25162306a36Sopenharmony_ci    def test_bindgen_libclang_old_version(self):
25262306a36Sopenharmony_ci        bindgen = self.generate_bindgen_libclang("scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version 10.0.0 [-W#pragma-messages], err: false")
25362306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
25462306a36Sopenharmony_ci        self.assertIn(f"libclang (used by the Rust bindings generator '{bindgen}') is too old.", result.stderr)
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_ci    def test_clang_matches_bindgen_libclang_different_bindgen(self):
25762306a36Sopenharmony_ci        bindgen = self.generate_bindgen_libclang("scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version 999.0.0 [-W#pragma-messages], err: false")
25862306a36Sopenharmony_ci        result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "BINDGEN": bindgen })
25962306a36Sopenharmony_ci        self.assertIn("version does not match Clang's. This may be a problem.", result.stderr)
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ci    def test_clang_matches_bindgen_libclang_different_clang(self):
26262306a36Sopenharmony_ci        cc = self.generate_clang("clang version 999.0.0")
26362306a36Sopenharmony_ci        result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "CC": cc })
26462306a36Sopenharmony_ci        self.assertIn("version does not match Clang's. This may be a problem.", result.stderr)
26562306a36Sopenharmony_ci
26662306a36Sopenharmony_ci    def test_rustc_src_core_krustflags(self):
26762306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "PATH": os.environ["PATH"], "RUSTC": "rustc", "KRUSTFLAGS": f"--sysroot={self.missing}" })
26862306a36Sopenharmony_ci        self.assertIn("Source code for the 'core' standard library could not be found", result.stderr)
26962306a36Sopenharmony_ci
27062306a36Sopenharmony_ci    def test_rustc_src_core_rustlibsrc(self):
27162306a36Sopenharmony_ci        result = self.run_script(self.Expected.FAILURE, { "RUST_LIB_SRC": self.missing })
27262306a36Sopenharmony_ci        self.assertIn("Source code for the 'core' standard library could not be found", result.stderr)
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci    def test_success_cc_unknown(self):
27562306a36Sopenharmony_ci        result = self.run_script(self.Expected.SUCCESS_WITH_EXTRA_OUTPUT, { "CC": self.missing })
27662306a36Sopenharmony_ci        self.assertIn("unknown C compiler", result.stderr)
27762306a36Sopenharmony_ci
27862306a36Sopenharmony_ci    def test_success_cc_multiple_arguments_ccache(self):
27962306a36Sopenharmony_ci        clang = self.generate_clang(f"""Ubuntu clang version {self.llvm_default_version}-1ubuntu1
28062306a36Sopenharmony_ciTarget: x86_64-pc-linux-gnu
28162306a36Sopenharmony_ciThread model: posix
28262306a36Sopenharmony_ciInstalledDir: /usr/bin
28362306a36Sopenharmony_ci""")
28462306a36Sopenharmony_ci        result = self.run_script(self.Expected.SUCCESS, { "CC": f"{clang} clang" })
28562306a36Sopenharmony_ci
28662306a36Sopenharmony_ci    def test_success_rustc_version(self):
28762306a36Sopenharmony_ci        for rustc_stdout in (
28862306a36Sopenharmony_ci            f"rustc {self.rustc_default_version} (a8314ef7d 2022-06-27)",
28962306a36Sopenharmony_ci            f"rustc {self.rustc_default_version}-dev (a8314ef7d 2022-06-27)",
29062306a36Sopenharmony_ci            f"rustc {self.rustc_default_version}-1.60.0 (a8314ef7d 2022-06-27)",
29162306a36Sopenharmony_ci        ):
29262306a36Sopenharmony_ci            with self.subTest(rustc_stdout=rustc_stdout):
29362306a36Sopenharmony_ci                rustc = self.generate_rustc(rustc_stdout)
29462306a36Sopenharmony_ci                result = self.run_script(self.Expected.SUCCESS, { "RUSTC": rustc })
29562306a36Sopenharmony_ci
29662306a36Sopenharmony_ci    def test_success_bindgen_version(self):
29762306a36Sopenharmony_ci        for bindgen_stdout in (
29862306a36Sopenharmony_ci            f"bindgen {self.bindgen_default_version}",
29962306a36Sopenharmony_ci            f"bindgen {self.bindgen_default_version}-dev",
30062306a36Sopenharmony_ci            f"bindgen {self.bindgen_default_version}-0.999.0",
30162306a36Sopenharmony_ci        ):
30262306a36Sopenharmony_ci            with self.subTest(bindgen_stdout=bindgen_stdout):
30362306a36Sopenharmony_ci                bindgen = self.generate_bindgen_version(bindgen_stdout)
30462306a36Sopenharmony_ci                result = self.run_script(self.Expected.SUCCESS, { "BINDGEN": bindgen })
30562306a36Sopenharmony_ci
30662306a36Sopenharmony_ci    def test_success_bindgen_libclang(self):
30762306a36Sopenharmony_ci        for stderr in (
30862306a36Sopenharmony_ci            f"scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {self.llvm_default_version} (https://github.com/llvm/llvm-project.git 4a2c05b05ed07f1f620e94f6524a8b4b2760a0b1) [-W#pragma-messages], err: false",
30962306a36Sopenharmony_ci            f"/home/jd/Documents/dev/kernel-module-flake/linux-6.1/outputs/dev/lib/modules/6.1.0-development/source/scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {self.llvm_default_version}  [-W#pragma-messages], err: false",
31062306a36Sopenharmony_ci            f"scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {self.llvm_default_version} (Fedora 13.0.0-3.fc35) [-W#pragma-messages], err: false",
31162306a36Sopenharmony_ci            f"""
31262306a36Sopenharmony_ci/nix/store/dsd5gz46hdbdk2rfdimqddhq6m8m8fqs-bash-5.1-p16/bin/bash: warning: setlocale: LC_ALL: cannot change locale (c)
31362306a36Sopenharmony_ciscripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {self.llvm_default_version}  [-W#pragma-messages], err: false
31462306a36Sopenharmony_ci""",
31562306a36Sopenharmony_ci            f"""
31662306a36Sopenharmony_ci/nix/store/dsd5gz46hdbdk2rfdimqddhq6m8m8fqs-bash-5.1.0-p16/bin/bash: warning: setlocale: LC_ALL: cannot change locale (c)
31762306a36Sopenharmony_ci/home/jd/Documents/dev/kernel-module-flake/linux-6.1/outputs/dev/lib/modules/6.1.0-development/source/scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {self.llvm_default_version} (Fedora 13.0.0-3.fc35) [-W#pragma-messages], err: false
31862306a36Sopenharmony_ci"""
31962306a36Sopenharmony_ci        ):
32062306a36Sopenharmony_ci            with self.subTest(stderr=stderr):
32162306a36Sopenharmony_ci                bindgen = self.generate_bindgen_libclang(stderr)
32262306a36Sopenharmony_ci                result = self.run_script(self.Expected.SUCCESS, { "BINDGEN": bindgen })
32362306a36Sopenharmony_ci
32462306a36Sopenharmony_ci    def test_success_clang_version(self):
32562306a36Sopenharmony_ci        for clang_stdout in (
32662306a36Sopenharmony_ci            f"clang version {self.llvm_default_version} (https://github.com/llvm/llvm-project.git 4a2c05b05ed07f1f620e94f6524a8b4b2760a0b1)",
32762306a36Sopenharmony_ci            f"clang version {self.llvm_default_version}-dev",
32862306a36Sopenharmony_ci            f"clang version {self.llvm_default_version}-2~ubuntu20.04.1",
32962306a36Sopenharmony_ci            f"Ubuntu clang version {self.llvm_default_version}-2~ubuntu20.04.1",
33062306a36Sopenharmony_ci        ):
33162306a36Sopenharmony_ci            with self.subTest(clang_stdout=clang_stdout):
33262306a36Sopenharmony_ci                clang = self.generate_clang(clang_stdout)
33362306a36Sopenharmony_ci                result = self.run_script(self.Expected.SUCCESS, { "CC": clang })
33462306a36Sopenharmony_ci
33562306a36Sopenharmony_ci    def test_success_real_programs(self):
33662306a36Sopenharmony_ci        for cc in ["gcc", "clang"]:
33762306a36Sopenharmony_ci            with self.subTest(cc=cc):
33862306a36Sopenharmony_ci                result = self.run_script(self.Expected.SUCCESS, {
33962306a36Sopenharmony_ci                    "PATH": os.environ["PATH"],
34062306a36Sopenharmony_ci                    "RUSTC": "rustc",
34162306a36Sopenharmony_ci                    "BINDGEN": "bindgen",
34262306a36Sopenharmony_ci                    "CC": cc,
34362306a36Sopenharmony_ci                })
34462306a36Sopenharmony_ci
34562306a36Sopenharmony_ciif __name__ == "__main__":
34662306a36Sopenharmony_ci    unittest.main()
347