From aa590981fb927b995281c2338c8afcfcdebe9d6d Mon Sep 17 00:00:00 2001 From: Flix Date: Sun, 3 Sep 2023 21:49:59 +0200 Subject: [PATCH] test: Create xtask test command --- .cargo/config.toml | 5 ++ Cargo.lock | 200 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 +- README.md | 3 +- xtask/Cargo.toml | 8 ++ xtask/README.md | 4 + xtask/src/main.rs | 82 +++++++++++++++++++ 7 files changed, 301 insertions(+), 3 deletions(-) create mode 100644 .cargo/config.toml create mode 100644 xtask/Cargo.toml create mode 100644 xtask/README.md create mode 100644 xtask/src/main.rs diff --git a/.cargo/config.toml b/.cargo/config.toml new file mode 100644 index 00000000..9aba8366 --- /dev/null +++ b/.cargo/config.toml @@ -0,0 +1,5 @@ +[alias] +xtask = "run --package xtask -- " + +[env] +CARGO_WORKSPACE_DIR = { value = "", relative = true } diff --git a/Cargo.lock b/Cargo.lock index ec01fe83..a763572c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -36,6 +36,54 @@ dependencies = [ "memchr", ] +[[package]] +name = "anstream" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b1f58811cfac344940f1a400b6e6231ce35171f614f26439e80f8c1465c5cc0c" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "15c4c2c83f81532e5845a733998b6971faca23490340a418e9b72a3ec9de12ea" + +[[package]] +name = "anstyle-parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "938874ff5980b03a87c5524b3ae5b59cf99b1d6bc836848df7bc5ada9643c333" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5ca11d4be1bab0c8bc8734a9aa7bf4ee8316d462a08c6ac5052f888fef5b494b" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "2.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58f54d10c6dfa51283a066ceab3ec1ab78d13fae00aa49243a45e4571fb79dfd" +dependencies = [ + "anstyle", + "windows-sys", +] + [[package]] name = "anyhow" version = "1.0.75" @@ -129,6 +177,79 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" +[[package]] +name = "clap" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6a13b88d2c62ff462f88e4a121f17a82c1af05693a2f192b5c38d14de73c19f6" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2bb9faaa7c2ef94b2743a21f5a29e6f0010dff4caa69ac8e9d6cf8b6fa74da08" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0862016ff20d69b84ef8247369fabf5c008a7417002411897d40ee1f4532b873" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.5.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "cd7cc57abe963c6d3b9d8be5b06ba7c8957a930305ca90304f24ef040aa6f961" + +[[package]] +name = "color-eyre" +version = "0.6.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5a667583cca8c4f8436db8de46ea8233c42a7d9ae424a82d338f2e4675229204" +dependencies = [ + "backtrace", + "color-spantrace", + "eyre", + "indenter", + "once_cell", + "owo-colors", + "tracing-error", +] + +[[package]] +name = "color-spantrace" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1ba75b3d9449ecdccb27ecbc479fdc0b87fa2dd43d2f8298f9bf0e59aacc8dce" +dependencies = [ + "once_cell", + "owo-colors", + "tracing-core", + "tracing-error", +] + +[[package]] +name = "colorchoice" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" + [[package]] name = "core-foundation" version = "0.9.3" @@ -403,6 +524,12 @@ version = "0.12.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888" +[[package]] +name = "heck" +version = "0.4.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" + [[package]] name = "hermit-abi" version = "0.3.2" @@ -680,6 +807,12 @@ dependencies = [ "vcpkg", ] +[[package]] +name = "owo-colors" +version = "3.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c1b04fb49957986fdce4d6ee7a65027d55d4b6d2265e5848bbb507b58ccfdb6f" + [[package]] name = "parking_lot" version = "0.12.1" @@ -992,6 +1125,15 @@ dependencies = [ "serde", ] +[[package]] +name = "sharded-slab" +version = "0.1.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "900fba806f70c630b0a382d0d825e17a0f19fcd059a2ade1ff237bcddf446b31" +dependencies = [ + "lazy_static", +] + [[package]] name = "signal-hook-registry" version = "1.4.1" @@ -1036,6 +1178,12 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "strsim" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623" + [[package]] name = "syn" version = "2.0.29" @@ -1080,6 +1228,16 @@ dependencies = [ "syn", ] +[[package]] +name = "thread_local" +version = "1.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fdd6f064ccff2d6567adcb3873ca630700f00b5ad3f060c25b5dcfd9a4ce152" +dependencies = [ + "cfg-if", + "once_cell", +] + [[package]] name = "tinyvec" version = "1.6.0" @@ -1184,6 +1342,28 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0955b8137a1df6f1a2e9a37d8a6656291ff0297c1a97c24e0d8425fe2312f79a" dependencies = [ "once_cell", + "valuable", +] + +[[package]] +name = "tracing-error" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d686ec1c0f384b1277f097b2f279a2ecc11afe8c133c1aabf036a27cb4cd206e" +dependencies = [ + "tracing", + "tracing-subscriber", +] + +[[package]] +name = "tracing-subscriber" +version = "0.3.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30a651bc37f915e81f087d86e62a18eec5f79550c7faff886f7090b4ea757c77" +dependencies = [ + "sharded-slab", + "thread_local", + "tracing-core", ] [[package]] @@ -1244,6 +1424,18 @@ dependencies = [ "percent-encoding", ] +[[package]] +name = "utf8parse" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" + +[[package]] +name = "valuable" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "830b7e5d4d90034032940e4ace0d9a9a057e7a45cd94e6c007832e39edb82f6d" + [[package]] name = "vcpkg" version = "0.2.15" @@ -1438,3 +1630,11 @@ dependencies = [ "cfg-if", "windows-sys", ] + +[[package]] +name = "xtask" +version = "0.1.0" +dependencies = [ + "clap", + "color-eyre", +] diff --git a/Cargo.toml b/Cargo.toml index f5046615..3afec825 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [workspace] -members = ["crates/*"] +members = ["crates/*", "xtask"] resolver = "2" [profile.dev.build-override] diff --git a/README.md b/README.md index a5c1f9fc..61522f08 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,6 @@ This is a [FHIR](https://www.hl7.org/fhir/) library in its early stages. The mod - [x] Create, Read, Update, Delete - [x] Search - [x] Paging - - [ ] Capabilities - [ ] Batch operations / Transactions - [ ] Operations - [ ] Patch @@ -64,7 +63,7 @@ For more examples, see the [tests](https://github.com/FlixCoder/fhir-sdk/blob/ma ## Testing -Simply set up the FHIR test server using `docker compose up -d` and run `cargo test --workspace` in the workspace root. +Simply set up the FHIR test server using `docker compose up -d` in the workspace root and then run `cargo xtask test`. ## Known Problems diff --git a/xtask/Cargo.toml b/xtask/Cargo.toml new file mode 100644 index 00000000..051f50fc --- /dev/null +++ b/xtask/Cargo.toml @@ -0,0 +1,8 @@ +[package] +edition = "2021" +name = "xtask" +version = "0.1.0" + +[dependencies] +clap = { version = "4.4.2", features = ["derive"] } +color-eyre = "0.6.2" diff --git a/xtask/README.md b/xtask/README.md new file mode 100644 index 00000000..18220e74 --- /dev/null +++ b/xtask/README.md @@ -0,0 +1,4 @@ +# XTask + +This is a pattern to manage common use-cases like testing multiple features. +It is a simple CLI tool that works kind of similar to a `Makefile` and executes certain tasks. diff --git a/xtask/src/main.rs b/xtask/src/main.rs new file mode 100644 index 00000000..3f2fb151 --- /dev/null +++ b/xtask/src/main.rs @@ -0,0 +1,82 @@ +//! XTask CLI tool. +#![allow(clippy::expect_used, clippy::print_stdout)] + +use std::{env, ffi::OsStr, path::PathBuf, process::Command, str::FromStr}; + +use clap::Parser; +use color_eyre::{eyre::bail, Result}; + +/// XTask pattern helper CLI tool. +#[derive(Debug, Parser)] +#[command(about)] +enum Cli { + /// Test command: run all tests with different feature selections. + Test, +} + +impl Cli { + /// Run the CLI. + pub fn run(self) -> Result<()> { + match self { + Self::Test => Self::run_test()?, + } + Ok(()) + } + + /// Run the test sub-command. + fn run_test() -> Result<()> { + let workspace_path = PathBuf::from_str(&env::var("CARGO_WORKSPACE_DIR")?)?; + + // Run all tests and doc-tests with default features. + let mut command = Command::new("cargo"); + command.args(["test", "--workspace"]).current_dir(&workspace_path); + run_command(command)?; + + // Run R4 tests. + let mut command = Command::new("cargo"); + command + .args([ + "test", + "--workspace", + "--no-default-features", + "--features", + "r4b,builders,client", + ]) + .current_dir(&workspace_path); + run_command(command)?; + + // Make sure the models compile without builders in all FHIR versions. + let mut command = Command::new("cargo"); + command.args(["check", "-p", "fhir-model", "--no-default-features", "--features", "r4b"]); + run_command(command)?; + let mut command = Command::new("cargo"); + command.args(["check", "-p", "fhir-model", "--no-default-features", "--features", "r5"]); + run_command(command)?; + + Ok(()) + } +} + +/// Print out the command being run and then run it, checking its exit code +/// afterwards. +fn run_command(mut command: Command) -> Result<()> { + println!( + "Running {} {}", + command.get_program().to_string_lossy(), + command.get_args().map(OsStr::to_string_lossy).collect::>().join(" ") + ); + + let exit_code = command.spawn()?.wait()?; + if !exit_code.success() { + bail!("Command failed!"); + } + + Ok(()) +} + +fn main() -> Result<()> { + color_eyre::install()?; + let cli = Cli::parse(); + cli.run()?; + Ok(()) +}