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
3 changes: 2 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ jobs:
- uses: tj-actions/changed-files@48d8f15b2aaa3d255ca5af3eba4870f807ce6b3c # v45
with:
# NB: this list must match all files that live within a nested Bazel module.
# Should match the /.bazelignore file as well.
# Should match the /REPO.bazel file as well.
files: |
angular-ngc/**
jest/**
Expand All @@ -61,6 +61,7 @@ jobs:
jest/**
nestjs/**
prisma/**
rust_insta_snapshot_test/**
rules_nodejs_to_rules_js_migration/**
ts_project_transpiler/**
# Just print the top-level directory names
Expand Down
5 changes: 4 additions & 1 deletion REPO.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ ignore_directories([
"**/node_modules",
"**/.pytest_cache",
"**/__pycache__",
# Nested projects that should be moved into the root workspace:
# Nested modules.
# TODO(alexeagle): some of these should move to the root
# NB: when updating this list, also add to .github/workflows/ci.yaml
"angular-ngc",
"bzlmod",
"check-npm-determinism",
Expand All @@ -21,6 +23,7 @@ ignore_directories([
"oci_java_image",
"prisma",
"rules_nodejs_to_rules_js_migration",
"rust_insta_snapshot_test",
"ts_project_transpiler",
"vercel_pkg",
])
1 change: 1 addition & 0 deletions rust_insta_snapshot_test/.bazelversion
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
8.5.0
2 changes: 2 additions & 0 deletions rust_insta_snapshot_test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bazel-*
target
11 changes: 11 additions & 0 deletions rust_insta_snapshot_test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
load("//:defs.bzl", "rust_snapshot_test")

rust_snapshot_test(
name = "test",
srcs = ["src/test.rs"],
crate_root = "src/test.rs",
snapshots_dir = "src/snapshots",
deps = [
"@crate_index//:insta",
],
)
239 changes: 239 additions & 0 deletions rust_insta_snapshot_test/Cargo.lock

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

12 changes: 12 additions & 0 deletions rust_insta_snapshot_test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "rust_insta_snapshot_test"
version = "0.0.0"
edition = "2024"
publish = false

[[test]]
name = "test"
path = "src/test.rs"

[dependencies]
insta = "1.46.0"
48 changes: 48 additions & 0 deletions rust_insta_snapshot_test/MODULE.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
"""bazelbuild/rules_rust - bzlmod example"""

module(
name = "snapshot_test_example",
version = "0.0.0",
)

bazel_dep(name = "bazel_lib", version = "3.1.0")
bazel_dep(name = "rules_rust", version = "0.68.1")
bazel_dep(name = "rules_shell", version = "0.6.1")
bazel_dep(name = "sed", version = "4.9.bcr.3")

rust_host_tools = use_extension("@rules_rust//rust:extensions.bzl", "rust_host_tools")
rust_host_tools.host_tools(
name = "rust_host_tools_nightly",
version = "nightly",
)
use_repo(
rust_host_tools,
"rust_host_tools_nightly",
)

crate = use_extension(
"@rules_rust//crate_universe:extensions.bzl",
"crate",
)
crate.from_cargo(
name = "crate_index",
cargo_lockfile = "//:Cargo.lock",
manifests = ["//:Cargo.toml"],
)
use_repo(crate, "crate_index")

bindeps = use_extension("@rules_rust//crate_universe:extension.bzl", "crate")
bindeps.spec(
artifact = "bin",
package = "cargo-insta",
version = "1.46.0",
)
bindeps.annotation(
crate = "cargo-insta",
gen_all_binaries = True,
)
bindeps.from_specs(
name = "bindeps",
host_tools = "@rust_host_tools_nightly",
)
use_repo(bindeps, "bindeps")
33 changes: 33 additions & 0 deletions rust_insta_snapshot_test/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# insta snapshot tests

An example for working with [insta](https://crates.io/crates/insta) snapshot tests under Bazel.

The `rust_snapshot_test` macro creates a `rust_test` which is run as a build action to create snapshots in the output tree, along with a `diff_test` to check whether any snapshots need to be applied. The diff test outputs a commands to run to review or accept the snapshot changes.

```starlark
rust_snapshot_test(
name = "test",
snapshots_dir = "src/snapshots",
srcs = ["src/test.rs"],
crate_root = "src/test.rs",
deps = [
"@crate_index//:insta",
],
)
```

Snapshots can be interactively reviewed using insta and written back to the source tree. For example, the following command runs `cargo insta review`.

```bash
bazel run //:test_review
```

Or, to accept all snapshot changes without reviewing them:

```bash
bazel run //:test_accept
```

## Limitations

* There isn't a way to remove unreferenced snapshots when reviewing, though this may be supported in the future (see https://github.com/mitsuhiko/insta/blob/3aa59d6f94d1b0c25d5953231019aa1eadbb4017/cargo-insta/src/cli.rs#L1079).
Loading