Skip to content

Commit d93bd7e

Browse files
committed
DO NOT MERGE - add snapshot tests for large refactors
This patch adds snapshot testing for essentially every file that's generated as part of the website. This should not be merged, because The number of snapshot files is massive and a new one would have to be added for every new post. However, this can be very useful to test refactors of the code that shouldn't change the output in any way. (Or if it does, these tests make it easier to see if the changes to the output are appropriate.) The process of using these tests would go something as follows: - install [cargo-insta](https://insta.rs/docs/quickstart/) - create a new branch, call it for example my-foo-refactor-testing-base - cherry pick this commit onto that branch - run `cargo insta test --accept` to generate the baseline snapshots - commit all these files and push the branch (it will not be merged) - create a new branch, e.g. my-foo-refactor, based on *-testing-base - do your refactoring - push your changes and create a PR against the *-testing-base branch Now you have a nice PR that's fully snapshot tested and can go through regular review cycles. Before merging, interactively rebase your refactor branch, drop the snapshot-related commits and re-target the PR to master.
1 parent 1f499a2 commit d93bd7e

File tree

5 files changed

+91
-1
lines changed

5 files changed

+91
-1
lines changed

.github/workflows/main.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ env:
1010
RUST_VERSION: 1.85.0
1111

1212
jobs:
13-
lint:
13+
lint_and_test:
1414
runs-on: ubuntu-latest
1515
steps:
1616
- uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4
@@ -22,6 +22,7 @@ jobs:
2222

2323
- run: cargo clippy --workspace -- -D warnings
2424
- run: cargo fmt --check --all
25+
- run: cargo test --all
2526

2627
build:
2728
runs-on: ubuntu-latest

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ static/styles/vendor.css
66
static/styles/app.css
77
static/styles/fonts.css
88
static/styles/noscript.css
9+
*.snap.new

Cargo.lock

Lines changed: 62 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,6 @@ chrono = "=0.4.40"
2121

2222
[workspace]
2323
members = ["serve"]
24+
25+
[dev-dependencies]
26+
insta = { version = "1.42.0", features = ["glob"] }

src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,3 +278,26 @@ pub fn main() -> eyre::Result<()> {
278278

279279
Ok(())
280280
}
281+
282+
#[test]
283+
fn snapshot() {
284+
main().unwrap();
285+
insta::glob!("..", "site/**/*", |path| {
286+
if path.is_dir() {
287+
return;
288+
}
289+
let path = path.display().to_string();
290+
let non_deterministic_files = ["releases.json", "feed.xml", "experiences.png"];
291+
if non_deterministic_files
292+
.into_iter()
293+
.any(|f| path.ends_with(f))
294+
{
295+
// Skip troublesome files, e.g. they might contain timestamps.
296+
return;
297+
}
298+
let content = fs::read(path).unwrap();
299+
// insta can't deal with non-utf8 strings?
300+
let content = String::from_utf8_lossy(&content).into_owned();
301+
insta::assert_snapshot!(content);
302+
});
303+
}

0 commit comments

Comments
 (0)