~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/scripts/rust_is_available_test.py

Version: ~ [ linux-6.11.5 ] ~ [ linux-6.10.14 ] ~ [ linux-6.9.12 ] ~ [ linux-6.8.12 ] ~ [ linux-6.7.12 ] ~ [ linux-6.6.58 ] ~ [ linux-6.5.13 ] ~ [ linux-6.4.16 ] ~ [ linux-6.3.13 ] ~ [ linux-6.2.16 ] ~ [ linux-6.1.114 ] ~ [ linux-6.0.19 ] ~ [ linux-5.19.17 ] ~ [ linux-5.18.19 ] ~ [ linux-5.17.15 ] ~ [ linux-5.16.20 ] ~ [ linux-5.15.169 ] ~ [ linux-5.14.21 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.228 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.284 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.322 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.336 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.337 ] ~ [ linux-4.4.302 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.9 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 #!/usr/bin/env python3
  2 # SPDX-License-Identifier: GPL-2.0
  3 
  4 """Tests the `rust_is_available.sh` script.
  5 
  6 Some of the tests require the real programs to be available in `$PATH`
  7 under their canonical name (and with the expected versions).
  8 """
  9 
 10 import enum
 11 import os
 12 import pathlib
 13 import stat
 14 import subprocess
 15 import tempfile
 16 import unittest
 17 
 18 class TestRustIsAvailable(unittest.TestCase):
 19     @enum.unique
 20     class Expected(enum.Enum):
 21         SUCCESS = enum.auto()
 22         SUCCESS_WITH_WARNINGS = enum.auto()
 23         SUCCESS_WITH_EXTRA_OUTPUT = enum.auto()
 24         FAILURE = enum.auto()
 25 
 26     @classmethod
 27     def generate_executable(cls, content):
 28         path = pathlib.Path(cls.tempdir.name)
 29         name = str(len(tuple(path.iterdir())))
 30         path = path / name
 31         with open(path, "w") as file_:
 32             file_.write(content)
 33         os.chmod(path, os.stat(path).st_mode | stat.S_IXUSR)
 34         return path
 35 
 36     @classmethod
 37     def generate_clang(cls, stdout):
 38         return cls.generate_executable(f"""#!/usr/bin/env python3
 39 import sys
 40 if "-E" in " ".join(sys.argv):
 41     print({repr("Clang " + " ".join(cls.llvm_default_version.split(" ")))})
 42 else:
 43     print({repr(stdout)})
 44 """)
 45 
 46     @classmethod
 47     def generate_rustc(cls, stdout):
 48         return cls.generate_executable(f"""#!/usr/bin/env python3
 49 import sys
 50 if "--print sysroot" in " ".join(sys.argv):
 51     print({repr(cls.rust_default_sysroot)})
 52 else:
 53     print({repr(stdout)})
 54 """)
 55 
 56     @classmethod
 57     def generate_bindgen(cls, version_stdout, libclang_stderr, version_0_66_patched=False):
 58         if libclang_stderr is None:
 59             libclang_case = f"raise SystemExit({cls.bindgen_default_bindgen_libclang_failure_exit_code})"
 60         else:
 61             libclang_case = f"print({repr(libclang_stderr)}, file=sys.stderr)"
 62 
 63         if version_0_66_patched:
 64             version_0_66_case = "pass"
 65         else:
 66             version_0_66_case = "raise SystemExit(1)"
 67 
 68         return cls.generate_executable(f"""#!/usr/bin/env python3
 69 import sys
 70 if "rust_is_available_bindgen_libclang.h" in " ".join(sys.argv):
 71     {libclang_case}
 72 elif "rust_is_available_bindgen_0_66.h" in " ".join(sys.argv):
 73     {version_0_66_case}
 74 else:
 75     print({repr(version_stdout)})
 76 """)
 77 
 78     @classmethod
 79     def generate_bindgen_version(cls, stdout, version_0_66_patched=False):
 80         return cls.generate_bindgen(stdout, cls.bindgen_default_bindgen_libclang_stderr, version_0_66_patched)
 81 
 82     @classmethod
 83     def generate_bindgen_libclang_failure(cls):
 84         return cls.generate_bindgen(cls.bindgen_default_bindgen_version_stdout, None)
 85 
 86     @classmethod
 87     def generate_bindgen_libclang(cls, stderr):
 88         return cls.generate_bindgen(cls.bindgen_default_bindgen_version_stdout, stderr)
 89 
 90     @classmethod
 91     def setUpClass(cls):
 92         cls.tempdir = tempfile.TemporaryDirectory()
 93 
 94         cls.missing = pathlib.Path(cls.tempdir.name) / "missing"
 95 
 96         cls.nonexecutable = pathlib.Path(cls.tempdir.name) / "nonexecutable"
 97         with open(cls.nonexecutable, "w") as file_:
 98             file_.write("nonexecutable")
 99 
100         cls.unexpected_binary = "true"
101 
102         cls.rustc_default_version = subprocess.check_output(("scripts/min-tool-version.sh", "rustc")).decode().strip()
103         cls.bindgen_default_version = subprocess.check_output(("scripts/min-tool-version.sh", "bindgen")).decode().strip()
104         cls.llvm_default_version = subprocess.check_output(("scripts/min-tool-version.sh", "llvm")).decode().strip()
105         cls.rust_default_sysroot = subprocess.check_output(("rustc", "--print", "sysroot")).decode().strip()
106 
107         cls.bindgen_default_bindgen_version_stdout = f"bindgen {cls.bindgen_default_version}"
108         cls.bindgen_default_bindgen_libclang_failure_exit_code = 42
109         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"
110 
111         cls.default_rustc = cls.generate_rustc(f"rustc {cls.rustc_default_version}")
112         cls.default_bindgen =  cls.generate_bindgen(cls.bindgen_default_bindgen_version_stdout, cls.bindgen_default_bindgen_libclang_stderr)
113         cls.default_cc = cls.generate_clang(f"clang version {cls.llvm_default_version}")
114 
115     def run_script(self, expected, override_env):
116         env = {
117             "RUSTC": self.default_rustc,
118             "BINDGEN": self.default_bindgen,
119             "CC": self.default_cc,
120         }
121 
122         for key, value in override_env.items():
123             if value is None:
124                 del env[key]
125                 continue
126             env[key] = value
127 
128         result = subprocess.run("scripts/rust_is_available.sh", env=env, capture_output=True)
129 
130         # The script should never output anything to `stdout`.
131         self.assertEqual(result.stdout, b"")
132 
133         if expected == self.Expected.SUCCESS:
134             # When expecting a success, the script should return 0
135             # and it should not output anything to `stderr`.
136             self.assertEqual(result.returncode, 0)
137             self.assertEqual(result.stderr, b"")
138         elif expected == self.Expected.SUCCESS_WITH_EXTRA_OUTPUT:
139             # When expecting a success with extra output (that is not warnings,
140             # which is the common case), the script should return 0 and it
141             # should output at least something to `stderr` (the output should
142             # be checked further by the test).
143             self.assertEqual(result.returncode, 0)
144             self.assertNotEqual(result.stderr, b"")
145         elif expected == self.Expected.SUCCESS_WITH_WARNINGS:
146             # When expecting a success with warnings, the script should return 0
147             # and it should output at least the instructions to `stderr`.
148             self.assertEqual(result.returncode, 0)
149             self.assertIn(b"Please see Documentation/rust/quick-start.rst for details", result.stderr)
150         else:
151             # When expecting a failure, the script should return non-0
152             # and it should output at least the instructions to `stderr`.
153             self.assertNotEqual(result.returncode, 0)
154             self.assertIn(b"Please see Documentation/rust/quick-start.rst for details", result.stderr)
155 
156         # The output will generally be UTF-8 (i.e. unless the user has
157         # put strange values in the environment).
158         result.stderr = result.stderr.decode()
159 
160         return result
161 
162     def test_rustc_unset(self):
163         result = self.run_script(self.Expected.FAILURE, { "RUSTC": None })
164         self.assertIn("Environment variable 'RUSTC' is not set.", result.stderr)
165         self.assertIn("This script is intended to be called from Kbuild.", result.stderr)
166 
167     def test_bindgen_unset(self):
168         result = self.run_script(self.Expected.FAILURE, { "BINDGEN": None })
169         self.assertIn("Environment variable 'BINDGEN' is not set.", result.stderr)
170         self.assertIn("This script is intended to be called from Kbuild.", result.stderr)
171 
172     def test_cc_unset(self):
173         result = self.run_script(self.Expected.FAILURE, { "CC": None })
174         self.assertIn("Environment variable 'CC' is not set.", result.stderr)
175         self.assertIn("This script is intended to be called from Kbuild.", result.stderr)
176 
177     def test_rustc_missing(self):
178         result = self.run_script(self.Expected.FAILURE, { "RUSTC": self.missing })
179         self.assertIn(f"Rust compiler '{self.missing}' could not be found.", result.stderr)
180 
181     def test_bindgen_missing(self):
182         result = self.run_script(self.Expected.FAILURE, { "BINDGEN": self.missing })
183         self.assertIn(f"Rust bindings generator '{self.missing}' could not be found.", result.stderr)
184 
185     def test_rustc_nonexecutable(self):
186         result = self.run_script(self.Expected.FAILURE, { "RUSTC": self.nonexecutable })
187         self.assertIn(f"Running '{self.nonexecutable}' to check the Rust compiler version failed with", result.stderr)
188 
189     def test_rustc_unexpected_binary(self):
190         result = self.run_script(self.Expected.FAILURE, { "RUSTC": self.unexpected_binary })
191         self.assertIn(f"Running '{self.unexpected_binary}' to check the Rust compiler version did not return", result.stderr)
192 
193     def test_rustc_unexpected_name(self):
194         rustc = self.generate_rustc(f"unexpected {self.rustc_default_version} (a8314ef7d 2022-06-27)")
195         result = self.run_script(self.Expected.FAILURE, { "RUSTC": rustc })
196         self.assertIn(f"Running '{rustc}' to check the Rust compiler version did not return", result.stderr)
197 
198     def test_rustc_unexpected_version(self):
199         rustc = self.generate_rustc("rustc unexpected (a8314ef7d 2022-06-27)")
200         result = self.run_script(self.Expected.FAILURE, { "RUSTC": rustc })
201         self.assertIn(f"Running '{rustc}' to check the Rust compiler version did not return", result.stderr)
202 
203     def test_rustc_no_minor(self):
204         rustc = self.generate_rustc(f"rustc {'.'.join(self.rustc_default_version.split('.')[:2])} (a8314ef7d 2022-06-27)")
205         result = self.run_script(self.Expected.FAILURE, { "RUSTC": rustc })
206         self.assertIn(f"Running '{rustc}' to check the Rust compiler version did not return", result.stderr)
207 
208     def test_rustc_old_version(self):
209         rustc = self.generate_rustc("rustc 1.60.0 (a8314ef7d 2022-06-27)")
210         result = self.run_script(self.Expected.FAILURE, { "RUSTC": rustc })
211         self.assertIn(f"Rust compiler '{rustc}' is too old.", result.stderr)
212 
213     def test_bindgen_nonexecutable(self):
214         result = self.run_script(self.Expected.FAILURE, { "BINDGEN": self.nonexecutable })
215         self.assertIn(f"Running '{self.nonexecutable}' to check the Rust bindings generator version failed with", result.stderr)
216 
217     def test_bindgen_unexpected_binary(self):
218         result = self.run_script(self.Expected.FAILURE, { "BINDGEN": self.unexpected_binary })
219         self.assertIn(f"Running '{self.unexpected_binary}' to check the bindings generator version did not return", result.stderr)
220 
221     def test_bindgen_unexpected_name(self):
222         bindgen = self.generate_bindgen_version(f"unexpected {self.bindgen_default_version}")
223         result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
224         self.assertIn(f"Running '{bindgen}' to check the bindings generator version did not return", result.stderr)
225 
226     def test_bindgen_unexpected_version(self):
227         bindgen = self.generate_bindgen_version("bindgen unexpected")
228         result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
229         self.assertIn(f"Running '{bindgen}' to check the bindings generator version did not return", result.stderr)
230 
231     def test_bindgen_no_minor(self):
232         bindgen = self.generate_bindgen_version(f"bindgen {'.'.join(self.bindgen_default_version.split('.')[:2])}")
233         result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
234         self.assertIn(f"Running '{bindgen}' to check the bindings generator version did not return", result.stderr)
235 
236     def test_bindgen_old_version(self):
237         bindgen = self.generate_bindgen_version("bindgen 0.50.0")
238         result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
239         self.assertIn(f"Rust bindings generator '{bindgen}' is too old.", result.stderr)
240 
241     def test_bindgen_bad_version_0_66_0_and_0_66_1(self):
242         for version in ("0.66.0", "0.66.1"):
243             with self.subTest(version=version):
244                 bindgen = self.generate_bindgen_version(f"bindgen {version}")
245                 result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "BINDGEN": bindgen })
246                 self.assertIn(f"Rust bindings generator '{bindgen}' versions 0.66.0 and 0.66.1 may not", result.stderr)
247 
248     def test_bindgen_bad_version_0_66_0_and_0_66_1_patched(self):
249         for version in ("0.66.0", "0.66.1"):
250             with self.subTest(version=version):
251                 bindgen = self.generate_bindgen_version(f"bindgen {version}", True)
252                 result = self.run_script(self.Expected.SUCCESS, { "BINDGEN": bindgen })
253 
254     def test_bindgen_libclang_failure(self):
255         bindgen = self.generate_bindgen_libclang_failure()
256         result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
257         self.assertIn(f"Running '{bindgen}' to check the libclang version (used by the Rust", result.stderr)
258         self.assertIn(f"bindings generator) failed with code {self.bindgen_default_bindgen_libclang_failure_exit_code}. This may be caused by", result.stderr)
259 
260     def test_bindgen_libclang_unexpected_version(self):
261         bindgen = self.generate_bindgen_libclang("scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version unexpected [-W#pragma-messages], err: false")
262         result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
263         self.assertIn(f"Running '{bindgen}' to check the libclang version (used by the Rust", result.stderr)
264         self.assertIn("bindings generator) did not return an expected output. See output", result.stderr)
265 
266     def test_bindgen_libclang_old_version(self):
267         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")
268         result = self.run_script(self.Expected.FAILURE, { "BINDGEN": bindgen })
269         self.assertIn(f"libclang (used by the Rust bindings generator '{bindgen}') is too old.", result.stderr)
270 
271     def test_clang_matches_bindgen_libclang_different_bindgen(self):
272         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")
273         result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "BINDGEN": bindgen })
274         self.assertIn("version does not match Clang's. This may be a problem.", result.stderr)
275 
276     def test_clang_matches_bindgen_libclang_different_clang(self):
277         cc = self.generate_clang("clang version 999.0.0")
278         result = self.run_script(self.Expected.SUCCESS_WITH_WARNINGS, { "CC": cc })
279         self.assertIn("version does not match Clang's. This may be a problem.", result.stderr)
280 
281     def test_rustc_src_core_krustflags(self):
282         result = self.run_script(self.Expected.FAILURE, { "PATH": os.environ["PATH"], "RUSTC": "rustc", "KRUSTFLAGS": f"--sysroot={self.missing}" })
283         self.assertIn("Source code for the 'core' standard library could not be found", result.stderr)
284 
285     def test_rustc_src_core_rustlibsrc(self):
286         result = self.run_script(self.Expected.FAILURE, { "RUST_LIB_SRC": self.missing })
287         self.assertIn("Source code for the 'core' standard library could not be found", result.stderr)
288 
289     def test_success_cc_unknown(self):
290         result = self.run_script(self.Expected.SUCCESS_WITH_EXTRA_OUTPUT, { "CC": self.missing })
291         self.assertIn("unknown C compiler", result.stderr)
292 
293     def test_success_cc_multiple_arguments_ccache(self):
294         clang = self.generate_clang(f"""Ubuntu clang version {self.llvm_default_version}-1ubuntu1
295 Target: x86_64-pc-linux-gnu
296 Thread model: posix
297 InstalledDir: /usr/bin
298 """)
299         result = self.run_script(self.Expected.SUCCESS, { "CC": f"{clang} clang" })
300 
301     def test_success_rustc_version(self):
302         for rustc_stdout in (
303             f"rustc {self.rustc_default_version} (a8314ef7d 2022-06-27)",
304             f"rustc {self.rustc_default_version}-dev (a8314ef7d 2022-06-27)",
305             f"rustc {self.rustc_default_version}-1.60.0 (a8314ef7d 2022-06-27)",
306         ):
307             with self.subTest(rustc_stdout=rustc_stdout):
308                 rustc = self.generate_rustc(rustc_stdout)
309                 result = self.run_script(self.Expected.SUCCESS, { "RUSTC": rustc })
310 
311     def test_success_bindgen_version(self):
312         for bindgen_stdout in (
313             f"bindgen {self.bindgen_default_version}",
314             f"bindgen {self.bindgen_default_version}-dev",
315             f"bindgen {self.bindgen_default_version}-0.999.0",
316         ):
317             with self.subTest(bindgen_stdout=bindgen_stdout):
318                 bindgen = self.generate_bindgen_version(bindgen_stdout)
319                 result = self.run_script(self.Expected.SUCCESS, { "BINDGEN": bindgen })
320 
321     def test_success_bindgen_libclang(self):
322         for stderr in (
323             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",
324             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",
325             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",
326             f"""
327 /nix/store/dsd5gz46hdbdk2rfdimqddhq6m8m8fqs-bash-5.1-p16/bin/bash: warning: setlocale: LC_ALL: cannot change locale (c)
328 scripts/rust_is_available_bindgen_libclang.h:2:9: warning: clang version {self.llvm_default_version}  [-W#pragma-messages], err: false
329 """,
330             f"""
331 /nix/store/dsd5gz46hdbdk2rfdimqddhq6m8m8fqs-bash-5.1.0-p16/bin/bash: warning: setlocale: LC_ALL: cannot change locale (c)
332 /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
333 """
334         ):
335             with self.subTest(stderr=stderr):
336                 bindgen = self.generate_bindgen_libclang(stderr)
337                 result = self.run_script(self.Expected.SUCCESS, { "BINDGEN": bindgen })
338 
339     def test_success_clang_version(self):
340         for clang_stdout in (
341             f"clang version {self.llvm_default_version} (https://github.com/llvm/llvm-project.git 4a2c05b05ed07f1f620e94f6524a8b4b2760a0b1)",
342             f"clang version {self.llvm_default_version}-dev",
343             f"clang version {self.llvm_default_version}-2~ubuntu20.04.1",
344             f"Ubuntu clang version {self.llvm_default_version}-2~ubuntu20.04.1",
345         ):
346             with self.subTest(clang_stdout=clang_stdout):
347                 clang = self.generate_clang(clang_stdout)
348                 result = self.run_script(self.Expected.SUCCESS, { "CC": clang })
349 
350     def test_success_real_programs(self):
351         for cc in ["gcc", "clang"]:
352             with self.subTest(cc=cc):
353                 result = self.run_script(self.Expected.SUCCESS, {
354                     "PATH": os.environ["PATH"],
355                     "RUSTC": "rustc",
356                     "BINDGEN": "bindgen",
357                     "CC": cc,
358                 })
359 
360 if __name__ == "__main__":
361     unittest.main()

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

sflogo.php