Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ jobs:
- run: rustup show active-toolchain || rustup toolchain install
- run: cargo fmt --all --check
- run: cargo clippy --locked --workspace --all-targets -- -D warnings
# snapshot-diff goes on crates.io: it builds without its schemars feature too.
- run: cargo clippy --locked -p snapshot-diff --no-default-features --all-targets -- -D warnings

# The CLI changed since its last release without a version bump: the docs would describe a CLI nobody can download.
# Not needed by deploy, so it never holds back the server.
Expand Down
26 changes: 25 additions & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Release the visualci CLI: static binaries for Linux (musl) and macOS, attached to a GitHub release v<version>.
# Release the visualci CLI: static binaries for Linux (musl) and macOS, attached to a GitHub release v<version>; and
# publish the snapshot-diff crate (crates/snapshot-diff) to crates.io when its version isn't there yet.
# A release happens by itself once CI passes on main with a workspace version (Cargo.toml) that has no release yet, so
# a version bump is all it takes and the docs never describe a CLI nobody can download. The cli-version job in ci.yml
# fails when the CLI changed since its last release without a bump. Pushing a v* tag by hand still works too.
Expand Down Expand Up @@ -95,3 +96,26 @@ jobs:
run: |
gh release create "${{ needs.version.outputs.tag }}" visualci-*.tar.gz* -R "${{ github.repository }}" \
--target "${{ needs.version.outputs.sha }}" --generate-notes

# snapshot-diff on crates.io (https://crates.io/crates/snapshot-diff), once per version in its Cargo.toml. Needs the
# CARGO_REGISTRY_TOKEN secret (a crates.io token for snapshot-diff, 1Password crates-io-snapshot-diff-token).
snapshot-diff:
if: github.event_name == 'push' || (github.event.workflow_run.conclusion == 'success' && github.event.workflow_run.event == 'push')
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
ref: ${{ github.event.workflow_run.head_sha || github.sha }}
- env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
run: |
version=$(sed -n 's/^version = "\(.*\)"/\1/p' crates/snapshot-diff/Cargo.toml | head -1)
code=$(curl -s -o /dev/null -w '%{http_code}' -A 'snapshot-diff release (github.com/DocSpring/visualci)' \
"https://crates.io/api/v1/crates/snapshot-diff/$version")
if [ "$code" = 200 ]; then
echo "snapshot-diff $version is already on crates.io"
elif [ -z "$CARGO_REGISTRY_TOKEN" ]; then
echo "::warning::snapshot-diff $version isn't on crates.io, and there's no CARGO_REGISTRY_TOKEN secret to publish it with"
else
cargo publish --locked -p snapshot-diff
fi
23 changes: 17 additions & 6 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ resolver = "3"
members = ["crates/*", "server", "server/migration"]

[workspace.package]
version = "0.2.10"
version = "0.2.11"
edition = "2024"
license = "Apache-2.0"
repository = "https://github.com/DocSpring/visualci"
Expand Down Expand Up @@ -32,3 +32,4 @@ sha2 = "0.10"
similar = "2"
thiserror = "2"
visualci-core = { path = "crates/visualci-core" }
snapshot-diff = { path = "crates/snapshot-diff", version = "0.1.0" }
25 changes: 25 additions & 0 deletions crates/snapshot-diff/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
[package]
name = "snapshot-diff"
description = "Normalized diffs for text, JSON and HTML snapshots: canonical forms, ignore rules for volatile values, JSON changes by path, and unified diffs"
version = "0.1.0"
edition.workspace = true
license.workspace = true
repository.workspace = true
documentation = "https://docs.rs/snapshot-diff"
readme = "README.md"
keywords = ["snapshot", "testing", "diff", "json", "html"]
categories = ["development-tools::testing", "text-processing"]

[features]
# `IgnoreRules` derives schemars' JsonSchema, for configuration files that embed it.
schemars = ["dep:schemars"]

[dependencies]
regex.workspace = true
schemars = { workspace = true, optional = true }
serde.workspace = true
serde_json.workspace = true
similar.workspace = true

[package.metadata.docs.rs]
all-features = true
27 changes: 27 additions & 0 deletions crates/snapshot-diff/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# snapshot-diff

Normalized diffs for text snapshots: plain text, JSON, HTML and serialized component trees. Snapshot tests compare a
value with the one saved last time; this crate makes that comparison ignore what doesn't matter and say clearly what
does.

- **Normalization:** JSON with sorted keys and two-space indents, HTML with sorted attributes and collapsed whitespace,
text with normalized line endings. A change in key order, attribute order or whitespace isn't a change.
- **Ignore rules:** JSON paths (`$.generated_at`, `$.items[*].id`, `$..request_id`) and regular expressions whose values
are replaced with `[ignored]` before comparing, for timestamps, IDs and other values that change on every run.
- **JSON changes by path:** `items[3].price: 10 → 12`, `items[4]: added`.
- **Unified diffs** of the normalized text, for a failing test's message.

```rust
use snapshot_diff::{normalize, unified, Format, IgnoreRules};

let rules = IgnoreRules { json_paths: vec!["$.created_at".into()], ..Default::default() }.compile().unwrap();
let base = normalize(Format::Json, r#"{"total": 65, "created_at": "2026-01-01"}"#, &rules);
let head = normalize(Format::Json, r#"{"created_at": "2026-12-02", "total": 71}"#, &rules);
println!("{}", unified(&base.text, &head.text, 3));
```

The `schemars` feature derives `JsonSchema` for `IgnoreRules`, for configuration files that embed the rules.

Used by [VisualCI](https://visualci.com). For image diffs, see [blazediff](https://crates.io/crates/blazediff).

Licensed under Apache-2.0.
Original file line number Diff line number Diff line change
@@ -1,6 +1,24 @@
//! Text snapshots: plain text, JSON, HTML and serialized component trees. Each format is normalized before it's
//! compared, so a change in key order, attribute order or whitespace isn't a change, and a project's ignore rules
//! blank out volatile values (timestamps, IDs) first.
//! Normalized diffs for text snapshots: plain text, JSON, HTML and serialized component trees.
//!
//! Each format is normalized before it's compared, so a change in key order, attribute order or whitespace isn't a
//! change, and ignore rules blank out volatile values (timestamps, IDs) first:
//!
//! - [`normalize`]: a snapshot in its canonical form (JSON with sorted keys and two-space indents, HTML with sorted
//! attributes and collapsed whitespace, text with normalized line endings), after the [`IgnoreRules`].
//! - [`json_changes`]: what changed between two JSON values, by path (`items[3].price: 10 → 12`).
//! - [`unified`]: a unified diff of two normalized snapshots, for a test failure's message.
//!
//! ```
//! use snapshot_diff::{normalize, unified, CompiledRules, Format, IgnoreRules};
//!
//! let rules = IgnoreRules { json_paths: vec!["$.created_at".into()], ..Default::default() }.compile().unwrap();
//! let base = normalize(Format::Json, r#"{"total": 65, "created_at": "2026-01-01"}"#, &rules);
//! let head = normalize(Format::Json, r#"{"created_at": "2026-12-02", "total": 71}"#, &rules);
//! assert!(unified(&base.text, &head.text, 3).contains("- \"total\": 65"));
//! # let _: CompiledRules = rules;
//! ```
//!
//! Used by [VisualCI](https://visualci.com).

use std::fmt::Write as _;

Expand Down Expand Up @@ -74,7 +92,8 @@ impl Format {
}

/// Rules for values that change on every run.
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, schemars::JsonSchema)]
#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct IgnoreRules {
/// JSON paths whose values are ignored: `$.generated_at`, `$.items[*].id`, `$..request_id`, `$["odd key"]`.
Expand Down
2 changes: 1 addition & 1 deletion crates/visualci-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ globset.workspace = true
hex.workspace = true
image.workspace = true
lopdf.workspace = true
regex.workspace = true
schemars.workspace = true
serde.workspace = true
serde_json.workspace = true
sha2.workspace = true
similar.workspace = true
snapshot-diff = { workspace = true, features = ["schemars"] }
thiserror.workspace = true
5 changes: 4 additions & 1 deletion crates/visualci-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ pub mod config;
pub mod diff;
pub mod image_hash;
pub mod pdf;
pub mod snapshot_text;
/// Text snapshots: the `snapshot-diff` crate.
pub mod snapshot_text {
pub use snapshot_diff::*;
}
pub mod text;

/// Hex SHA-256 of some bytes.
Expand Down
Loading