1
0
Fork 0
forked from mirror/grapevine

add complement wrapper xtask script

This commit is contained in:
Benjamin Lee 2024-06-20 19:06:57 -07:00
parent 5a299fef90
commit 2176a1a190
No known key found for this signature in database
GPG key ID: FB9624E2885D55A4
10 changed files with 292 additions and 0 deletions

2
.cargo/config.toml Normal file
View file

@ -0,0 +1,2 @@
[alias]
xtask = "run --package xtask --"

126
Cargo.lock generated
View file

@ -285,6 +285,15 @@ dependencies = [
"rustc-demangle",
]
[[package]]
name = "backtrace-ext"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "537beee3be4a18fb023b570f80e3ae28003db9167a751266b259926e25539d50"
dependencies = [
"backtrace",
]
[[package]]
name = "base64"
version = "0.21.7"
@ -1260,6 +1269,12 @@ version = "2.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f518f335dce6725a761382244631d86cf0ccb2863413590b31338feb467f9c3"
[[package]]
name = "is_ci"
version = "1.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7655c9839580ee829dfacba1d1278c2b7883e50a277ff7541299489d6bdfdc45"
[[package]]
name = "itertools"
version = "0.12.1"
@ -1476,6 +1491,37 @@ version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c8640c5d730cb13ebd907d8d04b52f55ac9a2eec55b440c8892f40d56c76c1d"
[[package]]
name = "miette"
version = "7.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4edc8853320c2a0dab800fbda86253c8938f6ea88510dc92c5f1ed20e794afc1"
dependencies = [
"backtrace",
"backtrace-ext",
"cfg-if",
"miette-derive",
"owo-colors",
"supports-color",
"supports-hyperlinks",
"supports-unicode",
"terminal_size",
"textwrap",
"thiserror",
"unicode-width",
]
[[package]]
name = "miette-derive"
version = "7.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcf09caffaac8068c346b6df2a7fc27a177fd20b39421a39ce0a211bde679a6c"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "mime"
version = "0.3.17"
@ -1709,6 +1755,12 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b15813163c1d831bf4a13c3610c05c0d03b39feb07f7e09fa234dac9b15aaf39"
[[package]]
name = "owo-colors"
version = "4.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "caff54706df99d2a78a5a4e3455ff45448d81ef1bb63c22cd14052ca0e993a3f"
[[package]]
name = "parking_lot"
version = "0.12.3"
@ -2731,6 +2783,12 @@ version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c5e1a9a646d36c3599cd173a41282daf47c44583ad367b8e6837255952e5c67"
[[package]]
name = "smawk"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7c388c1b5e93756d0c740965c41e8822f866621d41acbdf6336a6a168f8840c"
[[package]]
name = "socket2"
version = "0.5.7"
@ -2794,6 +2852,27 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]]
name = "supports-color"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9829b314621dfc575df4e409e79f9d6a66a3bd707ab73f23cb4aa3a854ac854f"
dependencies = [
"is_ci",
]
[[package]]
name = "supports-hyperlinks"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2c0a1e5168041f5f3ff68ff7d95dcb9c8749df29f6e7e89ada40dd4c9de404ee"
[[package]]
name = "supports-unicode"
version = "3.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b7401a30af6cb5818bb64852270bb722533397edcfc7344954a38f420819ece2"
[[package]]
name = "syn"
version = "2.0.66"
@ -2827,6 +2906,17 @@ dependencies = [
"windows-sys 0.48.0",
]
[[package]]
name = "textwrap"
version = "0.16.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "23d434d3f8967a09480fb04132ebe0a3e088c173e6d0ee7897abbdf4eab0f8b9"
dependencies = [
"smawk",
"unicode-linebreak",
"unicode-width",
]
[[package]]
name = "thiserror"
version = "1.0.61"
@ -3330,6 +3420,12 @@ version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "unicode-linebreak"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b09c83c3c29d37506a3e260c08c03743a6bb66a9cd432c6934ab501a190571f"
[[package]]
name = "unicode-normalization"
version = "0.1.23"
@ -3339,6 +3435,12 @@ dependencies = [
"tinyvec",
]
[[package]]
name = "unicode-width"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d"
[[package]]
name = "unsafe-libyaml"
version = "0.2.11"
@ -3720,6 +3822,30 @@ version = "2.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "213b7324336b53d2414b2db8537e56544d981803139155afa84f76eeebb7a546"
[[package]]
name = "xshell"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6db0ab86eae739efd1b054a8d3d16041914030ac4e01cd1dca0cf252fd8b6437"
dependencies = [
"xshell-macros",
]
[[package]]
name = "xshell-macros"
version = "0.2.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9d422e8e38ec76e2f06ee439ccc765e9c6a9638b9e7c9f2e8255e4d41e8bd852"
[[package]]
name = "xtask"
version = "0.1.0"
dependencies = [
"clap",
"miette",
"xshell",
]
[[package]]
name = "yap"
version = "0.12.0"

View file

@ -1,3 +1,5 @@
[workspace]
members = ["xtask"]
[workspace.package]
license = "Apache-2.0"
@ -98,6 +100,7 @@ hyper-util = { version = "0.1.4", features = ["client", "client-legacy", "servic
image = { version = "0.25.1", default-features = false, features = ["jpeg", "png", "gif"] }
jsonwebtoken = "9.3.0"
lru-cache = "0.1.2"
miette = { version = "7.2.0", features = ["fancy"] }
nix = { version = "0.29", features = ["resource"] }
num_cpus = "1.16.0"
once_cell = "1.19.0"
@ -136,6 +139,7 @@ tracing-opentelemetry = "0.24.0"
tracing-subscriber = { version = "0.3.18", features = ["env-filter", "json"] }
trust-dns-resolver = "0.23.2"
xdg = "2.5.2"
xshell = "0.2.6"
[package]
name = "grapevine"

View file

@ -86,6 +86,7 @@ let
"Cargo.lock"
"Cargo.toml"
"src"
"xtask"
];
};

View file

@ -1,6 +1,8 @@
# Keep sorted
{ default
, engage
, complement
, go
, inputs
, jq
, lychee
@ -34,6 +36,10 @@ mkShell {
markdownlint-cli
mdbook
toolchain
# TODO: don't pollute the devshell with these
go
complement
]
++
default.nativeBuildInputs

14
xtask/Cargo.toml Normal file
View file

@ -0,0 +1,14 @@
[package]
name = "xtask"
version = "0.1.0"
edition = "2021"
license.workspace = true
rust-version.workspace = true
[dependencies]
clap.workspace = true
miette.workspace = true
xshell.workspace = true
[lints]
workspace = true

32
xtask/src/complement.rs Normal file
View file

@ -0,0 +1,32 @@
use std::path::PathBuf;
use miette::{IntoDiagnostic, Result, WrapErr};
use xshell::{cmd, Shell};
mod docker;
mod test2json;
use self::{docker::load_docker_image, test2json::run_complement};
#[derive(clap::Args)]
pub(crate) struct Args;
#[allow(clippy::needless_pass_by_value)]
pub(crate) fn main(_args: Args) -> Result<()> {
let sh = Shell::new().unwrap();
let toplevel = get_toplevel_path(&sh)
.wrap_err("failed to determine repository root directory")?;
let docker_image = load_docker_image(&sh, &toplevel).wrap_err(
"failed to build and load complement-grapevine docker image",
)?;
run_complement(&sh, &docker_image)
.wrap_err("failed to run complement tests")?;
Ok(())
}
/// Returns the path to the repository root
fn get_toplevel_path(sh: &Shell) -> Result<PathBuf> {
let path =
cmd!(sh, "git rev-parse --show-toplevel").read().into_diagnostic()?;
Ok(path.into())
}

View file

@ -0,0 +1,58 @@
//! Functions for working with docker images and containers.
use std::path::Path;
use miette::{miette, IntoDiagnostic, LabeledSpan, Result, WrapErr};
use xshell::{cmd, Shell};
/// Build the 'grapevine-complement' OCI image and load it into the docker
/// daemon.
pub(crate) fn load_docker_image(sh: &Shell, toplevel: &Path) -> Result<String> {
// > i would Not trust that parser as far as i can throw it
// - @jade_:matrix.org, 2024-06-19
//
// So we're not even gonna try to escape the arbitrary top level path
// correctly for a flake installable reference. Instead we're just gonna cd
// into toplevel before running nix commands.
let _pushd_guard = sh.push_dir(toplevel);
let installable = ".#complement-grapevine-oci-image";
cmd!(sh, "nix-build-and-cache just {installable} -- --no-link")
.run()
.into_diagnostic()
.wrap_err("error building complement-grapevine-oci-image")?;
let oci_image_path = cmd!(sh, "nix path-info {installable}")
.read()
.into_diagnostic()
.wrap_err(
"error getting nix store path for complement-grapevine-oci-image",
)?;
// Instead of building the image with a fixed tag, we let nix choose the tag
// based on the input hash, and then determine the image/tag it used by
// parsing the 'docker load' output. This is to avoid a race condition
// between multiple concurrent 'xtask complement' invocations, which might
// otherwise assign the same tag to different images.
let load_output = cmd!(sh, "docker image load --input {oci_image_path}")
.read()
.into_diagnostic()
.wrap_err("error loading complement-grapevine docker image")?;
let expected_prefix = "Loaded image: ";
let docker_image = load_output
.strip_prefix(expected_prefix)
.ok_or_else(|| {
// Miette doesn't support inclusive ranges.
// <https://github.com/zkat/miette/pull/385>
#[allow(clippy::range_plus_one)]
let span = 0..(expected_prefix.len().min(load_output.len()) + 1);
let label =
LabeledSpan::at(span, format!("Expected {expected_prefix:?}"));
miette!(
labels = vec![label],
"failed to parse 'docker image load' output"
)
.with_source_code(load_output.clone())
})?
.to_owned();
Ok(docker_image)
}

View file

@ -0,0 +1,19 @@
//! Functions for working with the go [`test2json`][test2json] tool.
//!
//! [test2json]: https://pkg.go.dev/cmd/test2json@go1.22.4
use miette::{IntoDiagnostic, Result};
use xshell::{cmd, Shell};
/// Runs complement test suite
pub(crate) fn run_complement(sh: &Shell, docker_image: &str) -> Result<()> {
// TODO: handle SIG{INT,TERM}
// TODO: XTASK_PATH variable, so that we don't need to pollute devshell with
// go
cmd!(sh, "go tool test2json complement.test -test.v=test2json")
.env("COMPLEMENT_BASE_IMAGE", docker_image)
.env("COMPLEMENT_SPAWN_HS_TIMEOUT", "5")
.env("COMPLEMENT_ALWAYS_PRINT_SERVER_LOGS", "1")
.run()
.into_diagnostic()
}

30
xtask/src/main.rs Normal file
View file

@ -0,0 +1,30 @@
mod complement;
use std::process::ExitCode;
use clap::{Parser, Subcommand};
#[derive(Parser)]
struct Args {
#[clap(subcommand)]
command: Command,
}
#[derive(Subcommand)]
enum Command {
Complement(complement::Args),
}
fn main() -> ExitCode {
let args = Args::parse();
let result = match args.command {
Command::Complement(args) => complement::main(args),
};
let Err(e) = result else {
return ExitCode::SUCCESS;
};
// Include a leading newline because sometimes an error will occur in
// the middle of displaying a progress indicator.
eprintln!("\n{e:?}");
ExitCode::FAILURE
}