Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 47 additions & 6 deletions packages/asnkitrt-c/tests/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<PathBuf> = 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());
Expand Down
4 changes: 2 additions & 2 deletions tests/vectors/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 12 additions & 2 deletions tests/vectors/record.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
)

Expand Down
Binary file removed tests/vectors/specs/a1607596889b181a.asni
Binary file not shown.
Loading