From 5c8f0e33a5a2d238a601764d56d6450253966fc3 Mon Sep 17 00:00:00 2001 From: Matheus Xavier Date: Mon, 16 Nov 2026 22:47:07 +0100 Subject: [PATCH] Find the shared library where Cargo 1.100 puts it, and keep only the specifications of vectors Cargo 1.100 gives each crate a folder of its own, so the shared library is no longer next to the test that links C with it. The test asks Cargo where it is. tests/test_robust.py loads copies of an .asni file with a bit changed, and those that load were each recorded as a specification. record.py now removes a specification that no vector names. Signed-off-by: Matheus Xavier --- packages/asnkitrt-c/tests/c.rs | 53 +++++++++++++++++++--- tests/vectors/README.rst | 4 +- tests/vectors/record.py | 14 +++++- tests/vectors/specs/a1607596889b181a.asni | Bin 221 -> 0 bytes 4 files changed, 61 insertions(+), 10 deletions(-) delete mode 100644 tests/vectors/specs/a1607596889b181a.asni diff --git a/packages/asnkitrt-c/tests/c.rs b/packages/asnkitrt-c/tests/c.rs index 3690ec6d..d3a0088a 100644 --- a/packages/asnkitrt-c/tests/c.rs +++ b/packages/asnkitrt-c/tests/c.rs @@ -3,7 +3,7 @@ // SPDX-License-Identifier: MIT //! Compiles C with include/asnkitrt.h, links it with the shared library -//! that Cargo built for these tests, and runs it: examples/question.c on +//! that Cargo builds, and runs it: examples/question.c on //! examples/compiled/foo.asni, and tests/api.c. //! //! It needs a compiler of C, `cc` or the one `CC` names, and is for the @@ -12,18 +12,59 @@ #![cfg(unix)] use std::path::{Path, PathBuf}; -use std::process::{Command, Output}; +use std::process::{Command, Output, Stdio}; +use std::sync::OnceLock; fn manifest() -> &'static Path { Path::new(env!("CARGO_MANIFEST_DIR")) } +/// The folder of the shared library. Cargo is asked where it is, as it +/// is not always next to the test: since 1.100, Cargo gives each crate +/// a folder of its own. The library is built already, for the test. +fn libraries() -> &'static Path { + static LIBRARIES: OnceLock = OnceLock::new(); + + LIBRARIES.get_or_init(|| { + let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".into()); + let mut command = Command::new(&cargo); + + command + .args(["build", "--lib", "--message-format=json", "--manifest-path"]) + .arg(manifest().join("Cargo.toml")) + .stderr(Stdio::inherit()); + + // The profile of the test, to build nothing again. + if !cfg!(debug_assertions) { + command.arg("--release"); + } + + let output = command + .output() + .unwrap_or_else(|error| panic!("Can not run {cargo}: {error}")); + + assert!(output.status.success(), "{cargo} build failed"); + + // A path is between quotes in the messages, and the file is + // named as the library: libasnkitrt_c.so, or .dylib. + String::from_utf8_lossy(&output.stdout) + .split('"') + .map(Path::new) + .find(|path| { + path.file_stem().is_some_and(|stem| stem == "libasnkitrt_c") + && path + .extension() + .is_some_and(|extension| extension == "so" || extension == "dylib") + }) + .and_then(Path::parent) + .expect("the shared library in what Cargo built") + .to_path_buf() + }) +} + /// Compiles `source`, of this package, and runs it with `arguments`. fn compile_and_run(source: &str, arguments: &[PathBuf]) -> String { - // Where Cargo put the libraries for the tests: `deps/`, with the - // test. - let test = std::env::current_exe().expect("the path of the test"); - let libraries = test.parent().expect("the folder of the test"); + let libraries = libraries(); let name = Path::new(source).file_stem().expect("a file"); let output = Path::new(env!("CARGO_TARGET_TMPDIR")).join(name); let compiler = std::env::var("CC").unwrap_or_else(|_| "cc".into()); diff --git a/tests/vectors/README.rst b/tests/vectors/README.rst index c24e08a5..c79e4396 100644 --- a/tests/vectors/README.rst +++ b/tests/vectors/README.rst @@ -24,8 +24,8 @@ runtime for Python is held to them by ``tests/test_vectors.py``. decode, from the tests of the codecs of the runtime for Python, and has the runtime for Rust do each of them (``packages/asnkitrt-rs/examples/record_vectors.rs``). What it does -not support is left out. ``values.py`` says how a value is written as -JSON. +not support is left out, and so is a specification that no vector +names. ``values.py`` says how a value is written as JSON. ``specs/`` holds the compiled specifications (version 2 ``.asni`` files with no metadata, named by the first 16 hexadecimal digits of diff --git a/tests/vectors/record.py b/tests/vectors/record.py index e2da5e5a..a9d658e7 100644 --- a/tests/vectors/record.py +++ b/tests/vectors/record.py @@ -17,7 +17,9 @@ methods of a specification wrapped. Each time a test loads a specification, it is resolved and kept as an ``.asni`` file in ``specs``, named by the SHA-256 of its contents. Each time a test encodes or decodes, the type and what is -given (the value to encode, or the bytes to decode) are noted. +given (the value to encode, or the bytes to decode) are noted. A +specification that no vector names in the end is removed: some tests +only load, and some load files changed at random. The runtime for Rust then does each of these, with ``packages/asnkitrt-rs/examples/record_vectors.rs``, and what it gives @@ -238,10 +240,18 @@ def main(): for line in vectors: fout.write(line.encode("utf-8") + b"\n") + # The runtime for Rust read them all. Only those of a vector are of + # use to the others. + named = {vector["spec"] for vector in done if vector is not None} + + for digest in RECORDER.specs: + if digest not in named: + os.remove(os.path.join(specs_folder, digest + ".asni")) + print( "{} specifications, {} vectors, {} not written, {} not supported " "by the runtime for Rust.".format( - len(RECORDER.specs), len(vectors), RECORDER.skipped, unsupported + len(named), len(vectors), RECORDER.skipped, unsupported ) ) diff --git a/tests/vectors/specs/a1607596889b181a.asni b/tests/vectors/specs/a1607596889b181a.asni deleted file mode 100644 index 2cfe2398ef9c8478fca9dd47fe747be67882dea9..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 221 zcmZ<^_VZ+7C}MNTUC_kX#L&!??3SP3%FxW5=-9{uB%PWBni-l|5|f&FzO}G2e#`Lm z4e;@F_6z}vC4$8mki?i1ouTRzo4FI6T3C@KL56VU_&bNVhA0Gw1bO