Skip to content

Commit

Permalink
test: Create xtask test command
Browse files Browse the repository at this point in the history
  • Loading branch information
Flix committed Sep 3, 2023
1 parent 8e85ac7 commit aa59098
Show file tree
Hide file tree
Showing 7 changed files with 301 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[alias]
xtask = "run --package xtask -- "

[env]
CARGO_WORKSPACE_DIR = { value = "", relative = true }
200 changes: 200 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[workspace]
members = ["crates/*"]
members = ["crates/*", "xtask"]
resolver = "2"

[profile.dev.build-override]
Expand Down
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions xtask/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
4 changes: 4 additions & 0 deletions xtask/README.md
Original file line number Diff line number Diff line change
@@ -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.
82 changes: 82 additions & 0 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -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::<Vec<_>>().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(())
}

0 comments on commit aa59098

Please sign in to comment.