0ba60a07dc linked Homebrew's Vulkan loader and KosmicKrisp in <target>/debug/lib, ../lib from <target>/debug/deps. Under the pinned nightly's build-dir layout no deps directory is made: each test binary is <target>/.../build/<crate>/<hash>/out/<bin>, whose ../lib is build/<crate>/<hash>/lib, so the links served no test (batch 329's Mac leg, m3-max-58423, had no deps directory at all). The check now asks nextest where the binaries are: cargo nextest list with the run's own arguments, which builds them, lists each binary's path (binary-path, --list-type binaries-only, JSON), and the loader and KosmicKrisp are linked in ../lib from each binary's directory before the run. That holds under either layout. A file Homebrew lacks is named and not linked, as before. A list that fails, or that names no binary, fails the check. tools.rs's reader of cargo metadata's target_directory becomes json_strings, every string value of a key, which reads nextest's list too. The test that pinned the Makefile lines goes: the check no longer runs make for this. The new tests read a list and lay out the directories of both layouts.
197 lines
7.0 KiB
Rust
197 lines
7.0 KiB
Rust
//! What `dist` and `check` share: the external programs they run ([`Tools`]), Cargo's profiles
|
|
//! and target directory.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use crate::util::{Res, repo_root};
|
|
|
|
/// A Cargo profile.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub struct Profile(pub String);
|
|
|
|
impl Profile {
|
|
/// The directory under the target dir the profile writes to (`dev` writes to `debug`).
|
|
pub fn dir(&self) -> &str {
|
|
match self.0.as_str() {
|
|
"dev" | "test" => "debug",
|
|
"bench" => "release",
|
|
other => other,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The external programs xtask runs: Cargo, `make`, `xcrun` tools and `codesign`. Tests record
|
|
/// the calls instead.
|
|
pub trait Tools {
|
|
/// Runs `program` with `args`, failing on a non-zero exit.
|
|
fn run(&mut self, program: &str, args: &[String]) -> Res<()>;
|
|
/// Runs `program` with `args` and returns its trimmed stdout.
|
|
fn output(&mut self, program: &str, args: &[String]) -> Res<String>;
|
|
}
|
|
|
|
/// The real tools, run in the repository's root.
|
|
pub struct System;
|
|
|
|
impl Tools for System {
|
|
fn run(&mut self, program: &str, args: &[String]) -> Res<()> {
|
|
println!("$ {program} {}", args.join(" "));
|
|
let status = std::process::Command::new(program)
|
|
.args(args)
|
|
.current_dir(repo_root())
|
|
.status()
|
|
.map_err(|e| format!("{program}: {e}"))?;
|
|
if status.success() {
|
|
Ok(())
|
|
} else {
|
|
Err(format!("{program} {} failed ({status})", args.join(" ")))
|
|
}
|
|
}
|
|
|
|
fn output(&mut self, program: &str, args: &[String]) -> Res<String> {
|
|
let mut cmd = std::process::Command::new(program);
|
|
cmd.args(args).current_dir(repo_root());
|
|
crate::util::run_capture(&mut cmd).map(|s| s.trim().to_owned())
|
|
}
|
|
}
|
|
|
|
/// Cargo's target directory, as Cargo itself resolves it (`CARGO_TARGET_DIR`, relative to the
|
|
/// directory Cargo runs in, or `build.target-dir` from a config file): `cargo metadata`'s
|
|
/// `target_directory`, run where [`System`] runs Cargo.
|
|
pub fn target_dir(tools: &mut dyn Tools) -> Res<PathBuf> {
|
|
let cargo = std::env::var("CARGO").unwrap_or_else(|_| "cargo".to_owned());
|
|
let args = ["metadata", "--format-version", "1", "--no-deps"].map(str::to_owned);
|
|
let json = tools.output(&cargo, &args)?;
|
|
parse_target_directory(&json)
|
|
.ok_or_else(|| "cargo metadata printed no target_directory".to_owned())
|
|
}
|
|
|
|
/// The `target_directory` string of `cargo metadata`'s JSON, unescaped.
|
|
pub fn parse_target_directory(json: &str) -> Option<PathBuf> {
|
|
json_strings(json, "target_directory")
|
|
.into_iter()
|
|
.next()
|
|
.map(PathBuf::from)
|
|
}
|
|
|
|
/// Every string value of `key` in `json`, unescaped, in their order: what xtask reads of Cargo's
|
|
/// and nextest's JSON needs no more of its structure. A key inside a string is escaped there, so
|
|
/// it is not matched; a value of the key that is not a string is passed over.
|
|
pub fn json_strings(json: &str, key: &str) -> Vec<String> {
|
|
let pattern = format!("\"{key}\":");
|
|
let mut out = Vec::new();
|
|
let mut rest = json;
|
|
while let Some(at) = rest.find(&pattern) {
|
|
rest = rest[at + pattern.len()..].trim_start();
|
|
if let Some((value, after)) = json_string(rest) {
|
|
out.push(value);
|
|
rest = after;
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
/// The JSON string `text` starts with, unescaped, and the text after it.
|
|
fn json_string(text: &str) -> Option<(String, &str)> {
|
|
let body = text.strip_prefix('"')?;
|
|
let mut chars = body.char_indices();
|
|
let mut out = String::new();
|
|
loop {
|
|
match chars.next()? {
|
|
(at, '"') => return Some((out, &body[at + 1..])),
|
|
(_, '\\') => match chars.next()?.1 {
|
|
'u' => {
|
|
let hex: String = chars.by_ref().take(4).map(|(_, c)| c).collect();
|
|
out.push(char::from_u32(u32::from_str_radix(&hex, 16).ok()?)?);
|
|
}
|
|
'n' => out.push('\n'),
|
|
't' => out.push('\t'),
|
|
'r' => out.push('\r'),
|
|
'b' => out.push('\u{8}'),
|
|
'f' => out.push('\u{c}'),
|
|
c => out.push(c),
|
|
},
|
|
(_, c) => out.push(c),
|
|
}
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
/// Records every call; `output` answers with `answer`.
|
|
#[derive(Default)]
|
|
struct Log {
|
|
calls: Vec<(String, Vec<String>)>,
|
|
answer: String,
|
|
}
|
|
|
|
impl Tools for Log {
|
|
fn run(&mut self, program: &str, args: &[String]) -> Res<()> {
|
|
self.calls.push((program.to_owned(), args.to_vec()));
|
|
Ok(())
|
|
}
|
|
fn output(&mut self, program: &str, args: &[String]) -> Res<String> {
|
|
self.calls.push((program.to_owned(), args.to_vec()));
|
|
Ok(self.answer.clone())
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn profile_dir_maps_dev_to_debug() {
|
|
assert_eq!(Profile("dev".into()).dir(), "debug");
|
|
assert_eq!(Profile("test".into()).dir(), "debug");
|
|
assert_eq!(Profile("bench".into()).dir(), "release");
|
|
assert_eq!(Profile("release".into()).dir(), "release");
|
|
assert_eq!(Profile("checked".into()).dir(), "checked");
|
|
}
|
|
|
|
#[test]
|
|
fn parse_target_directory_reads_cargo_metadata() {
|
|
let json = r#"{"packages":[],"target_directory":"/a/b \"c\"é","version":1}"#;
|
|
assert_eq!(
|
|
parse_target_directory(json),
|
|
Some(PathBuf::from("/a/b \"c\"\u{e9}"))
|
|
);
|
|
assert_eq!(parse_target_directory("{}"), None);
|
|
}
|
|
|
|
/// nextest's list names each test binary by its `binary-path`; the key's other values, a
|
|
/// non-string one and an escaped key inside a string, are passed over.
|
|
#[test]
|
|
fn json_strings_reads_every_string_value_of_a_key() {
|
|
let json = r#"{"rust-suites":{"a":{"binary-path":"/t/build/a/1/out/a-1","note":"\"binary-path\":\"/no\""},"b":{"binary-path":null},"c":{"binary-path": "/t/d\u00e9ps/c-2\n"}}}"#;
|
|
assert_eq!(
|
|
json_strings(json, "binary-path"),
|
|
["/t/build/a/1/out/a-1", "/t/d\u{e9}ps/c-2\n"]
|
|
);
|
|
assert!(json_strings(json, "binary-id").is_empty());
|
|
}
|
|
|
|
#[test]
|
|
fn target_dir_asks_cargo_metadata() {
|
|
let mut log = Log {
|
|
answer: r#"{"target_directory":"/w/t"}"#.to_owned(),
|
|
..Log::default()
|
|
};
|
|
assert_eq!(target_dir(&mut log).unwrap(), PathBuf::from("/w/t"));
|
|
assert_eq!(
|
|
log.calls[0].1,
|
|
["metadata", "--format-version", "1", "--no-deps"].map(str::to_owned)
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn target_dir_matches_cargo_for_a_relative_env_dir() {
|
|
// The real System: cargo resolves a relative CARGO_TARGET_DIR against the directory it
|
|
// runs in, which System makes repo_root() whatever the xtask's cwd.
|
|
let got = target_dir(&mut System).unwrap();
|
|
let want = match std::env::var_os("CARGO_TARGET_DIR").map(PathBuf::from) {
|
|
Some(p) if p.is_absolute() => p,
|
|
Some(p) => repo_root().join(p),
|
|
None => got.clone(),
|
|
};
|
|
assert_eq!(got, want);
|
|
}
|
|
}
|