Skip to content

Commit

Permalink
[target-spec-miette] expose fixtures as an optional feature
Browse files Browse the repository at this point in the history
This is really useful for testing in downstream crates.
  • Loading branch information
sunshowers committed Dec 26, 2024
1 parent 3aa74ec commit 50a8a3f
Show file tree
Hide file tree
Showing 30 changed files with 76 additions and 23 deletions.
18 changes: 9 additions & 9 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ jobs:
run: git diff --exit-code
# cargo hack might cause changes to Cargo.lock which can cause the git diff above to fail. Put
# this at the end.
- name: Clippy feature powerset for target-spec
run: cargo hack --feature-powerset --package target-spec clippy
- name: target-spec-powerset clippy
run: just target-spec-powerset clippy

build:
name: Build and test core crates
Expand All @@ -57,6 +57,10 @@ jobs:
with:
toolchain: ${{ matrix.rust-version }}
- uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2
- name: Install tools
uses: taiki-e/install-action@8484225d9734e230a8bf38421a4ffec1cc249372 # v2.46.20
with:
tool: cargo-hack,just,nextest

# Build all packages we care about one by one to ensure feature unification
# doesn't happen.
Expand All @@ -71,14 +75,10 @@ jobs:
run: cargo build --all-targets --package determinator
- name: Build hakari
run: cargo build --all-targets --package hakari
- name: Install latest nextest release
uses: taiki-e/install-action@nextest
- name: Run tests for core crates
run: cargo nextest run --package target-spec --package guppy-summaries --package guppy --package determinator --package hakari
- name: Install cargo-hack
uses: taiki-e/install-action@cargo-hack
- name: Check feature powerset for target-spec
run: cargo hack --feature-powerset --package target-spec nextest run
- name: target-spec-powerset nextest run
run: just target-spec-powerset nextest run

build-all-features:
name: Build and test (all features)
Expand All @@ -98,7 +98,7 @@ jobs:
- uses: Swatinem/rust-cache@82a92a6e8fbeee089604da2575dc567ae9ddeaab # v2
- name: Build
# Build all targets to ensure examples are built as well.
# Exclude cargo-compare so that it only runs on the cfg-expr version below.
# Exclude cargo-compare: it runs as part of test-extended below.
run: cargo test --no-run --all-targets --all-features --workspace --exclude cargo-compare
- name: Run doctests
run: cargo test --doc --all-features --workspace --exclude cargo-compare
Expand Down
3 changes: 3 additions & 0 deletions Cargo.lock

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

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ members = [
[workspace.dependencies]
ahash = "0.8.11"
cfg-expr = "0.17.2"
datatest-stable = "0.3.0"
datatest-stable = { version = "0.3.0", features = ["include-dir"] }
guppy-workspace-hack = "0.1.0"
include_dir = "0.7.4"
insta = "1.41.1"
miette = "7.4.0"

Expand All @@ -32,7 +33,10 @@ miette = "7.4.0"
rust-version = "1.78"

[workspace.lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = ['cfg(doc_cfg)', 'cfg(guppy_nightly)'] }
unexpected_cfgs = { level = "warn", check-cfg = [
'cfg(doc_cfg)',
'cfg(guppy_nightly)',
] }

[patch.crates-io.guppy-workspace-hack]
path = "workspace-hack"
Expand Down
4 changes: 4 additions & 0 deletions Justfile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ nightly arg1 *args:
bootstrap arg1 *args:
RUSTC_BOOTSTRAP=1 cargo {{arg1}} {{args}} --all-features --all-targets --config .cargo/nightly-config.toml

# Run `cargo hack --feature-powerset` on target-spec crates
target-spec-powerset *args:
cargo hack --feature-powerset -p target-spec -p target-spec-miette {{args}}

# Build docs for crates and direct dependencies
rustdoc:
@# Ignore clap since we currently depend on both clap 2, 3, and 4 -- we
Expand Down
13 changes: 10 additions & 3 deletions target-spec-miette/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,26 @@ all-features = true
rustdoc-args = ["--cfg=doc_cfg"]

[dependencies]
target-spec = { version = "3.3.1", path = "../target-spec" }
miette.workspace = true
guppy-workspace-hack.workspace = true
include_dir = { workspace = true, optional = true }
miette.workspace = true
target-spec = { version = "3.3.1", path = "../target-spec" }

[dev-dependencies]
datatest-stable.workspace = true
insta.workspace = true
miette = { workspace = true, features = ["fancy"] }
target-spec = { version = "3.3.1", path = "../target-spec", features = ["custom"] }
target-spec = { version = "3.3.1", path = "../target-spec", features = [
"custom",
] }

[lints]
workspace = true

[[test]]
name = "datatest-snapshot"
harness = false
required-features = ["fixtures"]

[features]
fixtures = ["dep:include_dir"]
5 changes: 5 additions & 0 deletions target-spec-miette/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@ Integrate [target-spec](https://crates.io/crates/target-spec) errors with [miett
This crate has implementations of `Diagnostic` for the various kinds of errors that target-spec
produces. This can be used to pretty-print errors returned by target-spec.

### Features

- `fixtures`: Include [a set of fixtures](crate::fixtures) for testing
downstream users against. This feature is disabled by default.

### Minimum supported Rust version

The minimum supported Rust version (MSRV) is **Rust 1.78**.
Expand Down
27 changes: 27 additions & 0 deletions target-spec-miette/src/fixtures.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// Copyright (c) The cargo-guppy Contributors
// SPDX-License-Identifier: MIT OR Apache-2.0

//! Fixtures for target-spec-miette.
//!
//! This module contains test fixtures for the target-spec-miette crate,
//! typically around testing invalid inputs of various kinds.
//!
//! These fixtures can be used in downstream crates to verify that bad inputs
//! are gracefully handled, particularly by showing good error messages. (Good
//! error messages are part of why this library exists in the first place).
//!
//! The target-spec-miette library itself uses them via the
//! [`datatest-stable`](https://docs.rs/datatest-stable) crate.
//!
//! The exact contents of each directory are not part of the semver guarantees,
//! so using this module is only recommended in tests, and with a checked-in
//! `Cargo.lock`. A minor update to target-spec-miette may require that your
//! tests also be updated.
/// A set of invalid custom target JSON specifications.
pub static CUSTOM_INVALID: include_dir::Dir<'static> =
include_dir::include_dir!("target-spec-miette/tests/fixtures/custom-invalid");

/// A set of invalid `cfg` expressions.
pub static EXPR_INVALID: include_dir::Dir<'static> =
include_dir::include_dir!("target-spec-miette/tests/fixtures/expr-invalid");
7 changes: 7 additions & 0 deletions target-spec-miette/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
//! This crate has implementations of `Diagnostic` for the various kinds of errors that target-spec
//! produces. This can be used to pretty-print errors returned by target-spec.
//!
//! ## Features
//!
//! - `fixtures`: Include [a set of fixtures](crate::fixtures) for testing
//! downstream users against. This feature is disabled by default.
//!
//! ## Minimum supported Rust version
//!
//! The minimum supported Rust version (MSRV) is **Rust 1.78**.
Expand All @@ -17,6 +22,8 @@
#![forbid(unsafe_code)]
#![cfg_attr(doc_cfg, feature(doc_cfg, doc_auto_cfg))]

#[cfg(feature = "fixtures")]
pub mod fixtures;
mod imp;

pub use imp::*;
5 changes: 1 addition & 4 deletions target-spec-miette/tests/datatest-snapshot/custom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,9 @@ use datatest_stable::Utf8Path;
use target_spec::TargetFeatures;
use target_spec_miette::IntoMietteDiagnostic;

pub(crate) const CUSTOM_INVALID_PATH: &str =
"tests/datatest-snapshot/snapshots/custom-invalid/input";

pub(crate) fn custom_invalid(path: &Utf8Path, contents: String) -> datatest_stable::Result<()> {
let (_guard, insta_prefix) =
bind_insta_settings(path, "../datatest-snapshot/snapshots/custom-invalid/output");
bind_insta_settings(path, "../datatest-snapshot/snapshots/custom-invalid");

let error = target_spec::Platform::new_custom("my-target", &contents, TargetFeatures::none())
.expect_err("expected input to fail");
Expand Down
4 changes: 1 addition & 3 deletions target-spec-miette/tests/datatest-snapshot/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ use crate::helpers::bind_insta_settings;
use datatest_stable::Utf8Path;
use target_spec_miette::IntoMietteDiagnostic;

pub(crate) const EXPR_INVALID_PATH: &str = "tests/datatest-snapshot/snapshots/expr-invalid/input";

pub(crate) fn expr_invalid(path: &Utf8Path, contents: String) -> datatest_stable::Result<()> {
let (_guard, insta_prefix) =
bind_insta_settings(path, "../datatest-snapshot/snapshots/expr-invalid/output");
bind_insta_settings(path, "../datatest-snapshot/snapshots/expr-invalid");

let error = target_spec::TargetSpec::new(contents.trim_end().to_owned())
.expect_err("expected input to fail");
Expand Down
4 changes: 2 additions & 2 deletions target-spec-miette/tests/datatest-snapshot/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ mod expr;
mod helpers;

datatest_stable::harness! {
{ test = custom::custom_invalid, root = custom::CUSTOM_INVALID_PATH, pattern = r"^.*/*" },
{ test = expr::expr_invalid, root = expr::EXPR_INVALID_PATH, pattern = r"^.*/*" },
{ test = custom::custom_invalid, root = &target_spec_miette::fixtures::CUSTOM_INVALID, pattern = r"^.*/*" },
{ test = expr::expr_invalid, root = &target_spec_miette::fixtures::EXPR_INVALID, pattern = r"^.*/*" },
}
1 change: 1 addition & 0 deletions workspace-hack/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ clap = { version = "4.5.22", features = ["derive"] }
clap_builder = { version = "4.5.22", default-features = false, features = ["color", "help", "std", "suggestions", "usage"] }
console = { version = "0.15.8" }
getrandom = { version = "0.2.12", default-features = false, features = ["std"] }
include_dir = { version = "0.7.4", features = ["glob"] }
indexmap = { version = "1.9.3", default-features = false, features = ["std"] }
log = { version = "0.4.22", default-features = false, features = ["std"] }
miette = { version = "7.4.0", features = ["fancy"] }
Expand Down

0 comments on commit 50a8a3f

Please sign in to comment.