Skip to content

Commit edd2d2b

Browse files
authored
Add snapshot testing setup (#1506)
1 parent 30d835d commit edd2d2b

File tree

5 files changed

+107
-0
lines changed

5 files changed

+107
-0
lines changed

.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+
src/snapshots

Cargo.lock

Lines changed: 41 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 @@ tera = "=1.20.0"
2121

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

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,28 @@ author: Blog post author (or on behalf of which team)
5454
release: true (to be only used for official posts about Rust releases announcements)
5555
---
5656
```
57+
58+
### Snapshot testing
59+
60+
If you're making changes to how the site is generated, you may want to check the impact your changes have on the output.
61+
For this purpose, there is a setup to do snapshot testing over the entire output directory.
62+
It's not run in CI, because the number of snapshots is too large.
63+
But you can run these tests locally as needed.
64+
65+
- Make sure you have [cargo-insta](https://insta.rs/docs/quickstart/) installed.
66+
67+
- Generate the good snapshots to compare against, usually based off the master branch:
68+
```sh
69+
cargo insta test --accept --include-ignored
70+
```
71+
Consider making a commit with these snapshots, so you can always check the diff of your changes with git:
72+
```sh
73+
git add --force src/snapshots # snapshots are ignored by default
74+
git commit --message "WIP add good snapshots"
75+
```
76+
Since we can't merge the snapshots to main, don't forget to drop this commit when opening a pull request.
77+
78+
- Compare the output of the branch you're working on with the good snapshots:
79+
```sh
80+
cargo insta test --review --include-ignored
81+
```

src/lib.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,3 +302,40 @@ pub fn main() -> eyre::Result<()> {
302302

303303
Ok(())
304304
}
305+
306+
#[test]
307+
fn snapshot() {
308+
main().unwrap();
309+
let timestamped_files = ["releases.json", "feed.xml"];
310+
let inexplicably_non_deterministic_files = ["images/2023-08-rust-survey-2022/experiences.png"];
311+
insta::glob!("..", "site/**/*", |path| {
312+
if path.is_dir() {
313+
return;
314+
}
315+
let path = path.display().to_string();
316+
if timestamped_files
317+
.into_iter()
318+
.chain(inexplicably_non_deterministic_files)
319+
.any(|f| path.ends_with(f))
320+
{
321+
// Skip troublesome files, e.g. they might contain timestamps.
322+
// If possible, they are tested separately below.
323+
return;
324+
}
325+
let content = fs::read(path).unwrap();
326+
// insta can't deal with non-utf8 strings?
327+
let content = String::from_utf8_lossy(&content).into_owned();
328+
insta::assert_snapshot!(content);
329+
});
330+
331+
// test files with timestamps filtered
332+
insta::with_settings!({filters => vec![
333+
(r"\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\+\d{2}:\d{2}", "(filtered timestamp)"),
334+
]}, {
335+
for file in timestamped_files {
336+
let content = fs::read(format!("site/{file}")).unwrap();
337+
let content = String::from_utf8_lossy(&content).into_owned();
338+
insta::assert_snapshot!(content);
339+
}
340+
});
341+
}

0 commit comments

Comments
 (0)