diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 05f31b3..7eebbb2 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -34,6 +34,31 @@ cargo clippy --all-targets --all-features cargo test ``` +#### Snapshot tests + +Some tests use [`insta`](https://insta.rs) snapshots instead of inline `assert_eq!`. +We reach for snapshots when the expected value is large or tedious to maintain by hand: + +- Parsed ASTs (`tests/parse.rs`) — snapshotted as JSON with source locations redacted, + so these tests stay focused on structure. Location correctness is asserted separately + in the validator tests. +- Long compiled HTML output (the bigger cases in `tests/compiler.rs`). +- Multi-line rendered diagnostics (`src/diagnostic/format/tests/default.rs`). + +Short, behavior-defining checks stay as plain `assert_eq!` — they read as documentation +and force you to think about the exact expected value. + +When a snapshot test fails (or you add a new one), review and accept the change with +[`cargo-insta`](https://insta.rs/docs/cli/) (`cargo install cargo-insta`): + +```sh +cargo insta test --review # run tests, then interactively review pending snapshots +cargo insta accept # accept all pending snapshots without review +``` + +Always eyeball the diff before accepting — a snapshot is only as trustworthy as the +review that produced it. Committed snapshots live in `**/snapshots/*.snap`. + ### Run HSML provides several subcommands: diff --git a/Cargo.lock b/Cargo.lock index 9da5104..ea85f7d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,6 +122,15 @@ version = "2.11.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c4512299f36f043ab09a583e57bceb5a5aab7a73db1805848e8fef3c9e8c78b3" +[[package]] +name = "block-buffer" +version = "0.10.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3078c7629b62d3f0439517fa394996acacc5cbc91c5a20d8c658e77abd503a71" +dependencies = [ + "generic-array", +] + [[package]] name = "bstr" version = "1.12.1" @@ -206,6 +215,26 @@ version = "1.0.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1d07550c9036bf2ae0c684c4297d503f838287c83c53686d05370d0e139ae570" +[[package]] +name = "console" +version = "0.16.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d64e8af5551369d19cf50138de61f1c42074ab970f74e99be916646777f8fc87" +dependencies = [ + "encode_unicode", + "libc", + "windows-sys", +] + +[[package]] +name = "cpufeatures" +version = "0.2.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "59ed5838eebb26a2bb2e58f6d5b5316989ae9d08bab10e0e6d103e656d1b0280" +dependencies = [ + "libc", +] + [[package]] name = "crossbeam-deque" version = "0.8.6" @@ -231,6 +260,16 @@ version = "0.8.21" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28" +[[package]] +name = "crypto-common" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78c8292055d1c1df0cce5d180393dc8cce0abec0a7102adb6c7b1eef6016d60a" +dependencies = [ + "generic-array", + "typenum", +] + [[package]] name = "dashmap" version = "5.5.3" @@ -250,6 +289,16 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6184e33543162437515c2e2b48714794e37845ec9851711914eec9d308f6ebe8" +[[package]] +name = "digest" +version = "0.10.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ed9a281f7bc9b7576e61468ba615a66a5c8cfdff42420a70aa82701a3b1e292" +dependencies = [ + "block-buffer", + "crypto-common", +] + [[package]] name = "displaydoc" version = "0.2.5" @@ -261,6 +310,12 @@ dependencies = [ "syn", ] +[[package]] +name = "encode_unicode" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34aa73646ffb006b8f5147f3dc182bd4bcb190227ce861fc4a4844bf8e3cb2c0" + [[package]] name = "equivalent" version = "1.0.2" @@ -389,6 +444,16 @@ dependencies = [ "slab", ] +[[package]] +name = "generic-array" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "85649ca51fd72272d7821adaf274ad91c288277713d9c18820d8499a7ff69e9a" +dependencies = [ + "typenum", + "version_check", +] + [[package]] name = "getrandom" version = "0.4.2" @@ -450,6 +515,7 @@ dependencies = [ "clap", "html5ever", "ignore", + "insta", "js-sys", "markup5ever_rcdom", "nom", @@ -618,6 +684,21 @@ dependencies = [ "serde_core", ] +[[package]] +name = "insta" +version = "1.48.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "86f0f8fee8c926415c58d6ae43a08523a26faccb2323f5e6b644fe7dd4ef6b82" +dependencies = [ + "console", + "once_cell", + "pest", + "pest_derive", + "serde", + "similar", + "tempfile", +] + [[package]] name = "is_terminal_polyfill" version = "1.70.2" @@ -837,6 +918,49 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "pest" +version = "2.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e0848c601009d37dfa3430c4666e147e49cdcf1b92ecd3e63657d8a5f19da662" +dependencies = [ + "memchr", + "ucd-trie", +] + +[[package]] +name = "pest_derive" +version = "2.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "11f486f1ea21e6c10ed15d5a7c77165d0ee443402f0780849d1768e7d9d6fe77" +dependencies = [ + "pest", + "pest_generator", +] + +[[package]] +name = "pest_generator" +version = "2.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8040c4647b13b210a963c1ed407c1ff4fdfa01c31d6d2a098218702e6664f94f" +dependencies = [ + "pest", + "pest_meta", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "pest_meta" +version = "2.8.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "89815c69d36021a140146f26659a81d6c2afa33d216d736dd4be5381a7362220" +dependencies = [ + "pest", + "sha2", +] + [[package]] name = "phf" version = "0.13.1" @@ -1124,12 +1248,29 @@ dependencies = [ "syn", ] +[[package]] +name = "sha2" +version = "0.10.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a7507d819769d01a365ab707794a4084392c824f54a7a6a7862f8c3d0892b283" +dependencies = [ + "cfg-if", + "cpufeatures", + "digest", +] + [[package]] name = "shlex" version = "1.3.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0fda2ff0d084019ba4d7c6f371c95d8fd75ce3524c3cb8fb653a3023f6323e64" +[[package]] +name = "similar" +version = "2.7.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bbbb5d9659141646ae647b42fe094daf6c6192d1620870b449d9557f748b2daa" + [[package]] name = "siphasher" version = "1.0.3" @@ -1359,6 +1500,18 @@ dependencies = [ "once_cell", ] +[[package]] +name = "typenum" +version = "1.20.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b6f5e870be6c3b371b77fe0ee0bafb859fa4964b4404c27de1d380043c4dda20" + +[[package]] +name = "ucd-trie" +version = "0.1.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2896d95c02a80c6d6a5d6e953d479f5ddf2dfdb6a244441010e373ac0fb88971" + [[package]] name = "unicode-ident" version = "1.0.24" @@ -1402,6 +1555,12 @@ version = "0.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" +[[package]] +name = "version_check" +version = "0.9.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a" + [[package]] name = "wait-timeout" version = "0.2.1" diff --git a/Cargo.toml b/Cargo.toml index cc8a312..436675b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -51,5 +51,6 @@ predicates = "=3.1.4" tempfile = "=3.27.0" [dev-dependencies] +insta = { version = "=1.48.0", features = ["json", "redactions"] } js-sys = "=0.3.99" wasm-bindgen-test = "=0.3.72" diff --git a/src/diagnostic/format/tests/default.rs b/src/diagnostic/format/tests/default.rs index decfa24..0a916f3 100644 --- a/src/diagnostic/format/tests/default.rs +++ b/src/diagnostic/format/tests/default.rs @@ -19,16 +19,7 @@ fn it_should_format_warning_with_source_context() { let source = "div#foo#bar"; let output = DefaultFormatter.format(&[diag], Some(source)); - assert_eq!( - output, - "\ -warning[W001]: Duplicate attribute 'id' is not allowed - --> example.hsml:1:8 - | -1 | div#foo#bar - | ^ -" - ); + insta::assert_snapshot!(output); } #[test] @@ -47,16 +38,7 @@ fn it_should_format_error_without_code() { let source = "123invalid"; let output = DefaultFormatter.format(&[diag], Some(source)); - assert_eq!( - output, - "\ -error: parse error - --> :1:1 - | -1 | 123invalid - | ^ -" - ); + insta::assert_snapshot!(output); } #[test] @@ -96,16 +78,7 @@ fn it_should_format_warning() { let source = "h1.text-red.text-red Hello"; let output = DefaultFormatter.format(&[diag], Some(source)); - assert_eq!( - output, - "\ -warning[W002]: Duplicate class 'text-red' - --> test.hsml:1:12 - | -1 | h1.text-red.text-red Hello - | ^ -" - ); + insta::assert_snapshot!(output); } #[test] @@ -151,14 +124,5 @@ fn it_should_underline_span_range() { let source = "h1.foo.foo Hello"; let output = DefaultFormatter.format(&[diag], Some(source)); - assert_eq!( - output, - "\ -warning[W002]: Duplicate class 'foo' - --> test.hsml:1:7 - | -1 | h1.foo.foo Hello - | ^^^^ -" - ); + insta::assert_snapshot!(output); } diff --git a/src/diagnostic/format/tests/gitlab.rs b/src/diagnostic/format/tests/gitlab.rs index a2de9d1..05350e7 100644 --- a/src/diagnostic/format/tests/gitlab.rs +++ b/src/diagnostic/format/tests/gitlab.rs @@ -21,15 +21,8 @@ fn it_should_format_warning_as_codeclimate_issue() { let output = GitlabFormatter.format(&[diag], None); let parsed: serde_json::Value = serde_json::from_str(&output).unwrap(); - let arr = parsed.as_array().unwrap(); - assert_eq!(arr.len(), 1); - assert_eq!(arr[0]["description"], "Duplicate class 'foo'"); - assert_eq!(arr[0]["check_name"], "W002"); - assert_eq!(arr[0]["severity"], "minor"); - assert_eq!(arr[0]["location"]["path"], "example.hsml"); - assert_eq!(arr[0]["location"]["lines"]["begin"], 1); - assert!(arr[0]["fingerprint"].is_string()); + insta::assert_json_snapshot!(parsed); } #[test] @@ -47,10 +40,8 @@ fn it_should_format_error_as_major_severity() { let output = GitlabFormatter.format(&[diag], None); let parsed: serde_json::Value = serde_json::from_str(&output).unwrap(); - let arr = parsed.as_array().unwrap(); - assert_eq!(arr[0]["severity"], "major"); - assert_eq!(arr[0]["check_name"], "hsml"); + insta::assert_json_snapshot!(parsed); } #[test] diff --git a/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_error_without_code.snap b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_error_without_code.snap new file mode 100644 index 0000000..e6422d3 --- /dev/null +++ b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_error_without_code.snap @@ -0,0 +1,9 @@ +--- +source: src/diagnostic/format/tests/default.rs +expression: output +--- +error: parse error + --> :1:1 + | +1 | 123invalid + | ^ diff --git a/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_warning.snap b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_warning.snap new file mode 100644 index 0000000..4e1072d --- /dev/null +++ b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_warning.snap @@ -0,0 +1,9 @@ +--- +source: src/diagnostic/format/tests/default.rs +expression: output +--- +warning[W002]: Duplicate class 'text-red' + --> test.hsml:1:12 + | +1 | h1.text-red.text-red Hello + | ^ diff --git a/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_warning_with_source_context.snap b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_warning_with_source_context.snap new file mode 100644 index 0000000..24d1231 --- /dev/null +++ b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_format_warning_with_source_context.snap @@ -0,0 +1,9 @@ +--- +source: src/diagnostic/format/tests/default.rs +expression: output +--- +warning[W001]: Duplicate attribute 'id' is not allowed + --> example.hsml:1:8 + | +1 | div#foo#bar + | ^ diff --git a/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_underline_span_range.snap b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_underline_span_range.snap new file mode 100644 index 0000000..e98fac1 --- /dev/null +++ b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__default__it_should_underline_span_range.snap @@ -0,0 +1,9 @@ +--- +source: src/diagnostic/format/tests/default.rs +expression: output +--- +warning[W002]: Duplicate class 'foo' + --> test.hsml:1:7 + | +1 | h1.foo.foo Hello + | ^^^^ diff --git a/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__gitlab__it_should_format_error_as_major_severity.snap b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__gitlab__it_should_format_error_as_major_severity.snap new file mode 100644 index 0000000..7f4f1b9 --- /dev/null +++ b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__gitlab__it_should_format_error_as_major_severity.snap @@ -0,0 +1,18 @@ +--- +source: src/diagnostic/format/tests/gitlab.rs +expression: parsed +--- +[ + { + "check_name": "hsml", + "description": "parse error", + "fingerprint": "f1051314d06cb78c", + "location": { + "lines": { + "begin": 1 + }, + "path": "test.hsml" + }, + "severity": "major" + } +] diff --git a/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__gitlab__it_should_format_warning_as_codeclimate_issue.snap b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__gitlab__it_should_format_warning_as_codeclimate_issue.snap new file mode 100644 index 0000000..9d4191d --- /dev/null +++ b/src/diagnostic/format/tests/snapshots/hsml__diagnostic__format__tests__gitlab__it_should_format_warning_as_codeclimate_issue.snap @@ -0,0 +1,18 @@ +--- +source: src/diagnostic/format/tests/gitlab.rs +expression: parsed +--- +[ + { + "check_name": "W002", + "description": "Duplicate class 'foo'", + "fingerprint": "3b3deaac0f93890d", + "location": { + "lines": { + "begin": 1 + }, + "path": "example.hsml" + }, + "severity": "minor" + } +] diff --git a/src/parser/attribute/tests/node.rs b/src/parser/attribute/tests/node.rs index 2033e74..f186945 100644 --- a/src/parser/attribute/tests/node.rs +++ b/src/parser/attribute/tests/node.rs @@ -1,7 +1,6 @@ use crate::parser::{ HsmlNode, HsmlProcessContext, Span, attribute::node::{AttributeNode, attribute_node, attribute_nodes}, - comment::node::CommentNode, }; #[test] @@ -117,15 +116,8 @@ fn it_should_return_attribute_nodes_with_dev_comments() { ) .unwrap(); - assert_eq!( - attribute_nodes, - vec![ - HsmlNode::Comment(CommentNode::new_without_location(" comment 1", true)), - HsmlNode::Attribute(AttributeNode::new_without_location("key", Some("value"))), - HsmlNode::Comment(CommentNode::new_without_location(" comment 2", true)), - HsmlNode::Attribute(AttributeNode::new_without_location(":key2", Some("value2"))), - ] - ); + // Structure (the comment/attribute interleaving) is snapshotted; locations redacted. + insta::assert_json_snapshot!(attribute_nodes, { ".**.location" => "[location]" }); assert_eq!(*rest.fragment(), "\n"); } diff --git a/src/parser/attribute/tests/snapshots/hsml__parser__attribute__tests__node__it_should_return_attribute_nodes_with_dev_comments.snap b/src/parser/attribute/tests/snapshots/hsml__parser__attribute__tests__node__it_should_return_attribute_nodes_with_dev_comments.snap new file mode 100644 index 0000000..beb92c6 --- /dev/null +++ b/src/parser/attribute/tests/snapshots/hsml__parser__attribute__tests__node__it_should_return_attribute_nodes_with_dev_comments.snap @@ -0,0 +1,30 @@ +--- +source: src/parser/attribute/tests/node.rs +expression: attribute_nodes +--- +[ + { + "type": "comment", + "text": " comment 1", + "isDev": true, + "location": "[location]" + }, + { + "type": "attribute", + "key": "key", + "value": "value", + "location": "[location]" + }, + { + "type": "comment", + "text": " comment 2", + "isDev": true, + "location": "[location]" + }, + { + "type": "attribute", + "key": ":key2", + "value": "value2", + "location": "[location]" + } +] diff --git a/src/parser/tag/tests/node.rs b/src/parser/tag/tests/node.rs index b0be186..1ff5ae5 100644 --- a/src/parser/tag/tests/node.rs +++ b/src/parser/tag/tests/node.rs @@ -1,10 +1,8 @@ use crate::parser::{ HsmlProcessContext, Span, - class::node::ClassNode, error::ErrorCode, id::node::IdNode, tag::node::{TagNode, tag_node}, - text::node::TextNode, }; #[test] @@ -26,27 +24,8 @@ fn it_should_return_tag_node_with_piped_text() { ) .unwrap(); - assert_eq!( - tag, - TagNode::without_location( - "p", - vec![], - Some(vec![ - ClassNode::new_without_location("text-lg"), - ClassNode::new_without_location("font-medium"), - ]), - None, - Some(TextNode { - text: String::from( - r#""Tailwind CSS is the only framework that I've seen scale -on large teams. It's easy to customize, adapts to any design, -and the build size is tiny.""# - ), - is_block: true, - }), - None, - ) - ); + // Structure is snapshotted; source locations are redacted (asserted separately). + insta::assert_json_snapshot!(tag, { ".**.location" => "[location]" }); assert_eq!(*rest.fragment(), "\n figcaption.font-medium"); } diff --git a/src/parser/tag/tests/snapshots/hsml__parser__tag__tests__node__it_should_return_tag_node_with_piped_text.snap b/src/parser/tag/tests/snapshots/hsml__parser__tag__tests__node__it_should_return_tag_node_with_piped_text.snap new file mode 100644 index 0000000..8b898e1 --- /dev/null +++ b/src/parser/tag/tests/snapshots/hsml__parser__tag__tests__node__it_should_return_tag_node_with_piped_text.snap @@ -0,0 +1,25 @@ +--- +source: src/parser/tag/tests/node.rs +expression: tag +--- +{ + "tag": "p", + "location": "[location]", + "ids": [], + "classes": [ + { + "name": "text-lg", + "location": "[location]" + }, + { + "name": "font-medium", + "location": "[location]" + } + ], + "attributes": null, + "text": { + "text": "\"Tailwind CSS is the only framework that I've seen scale\non large teams. It's easy to customize, adapts to any design,\nand the build size is tiny.\"", + "isBlock": true + }, + "children": null +} diff --git a/tests/compiler.rs b/tests/compiler.rs index b7f4ab0..0150ce8 100644 --- a/tests/compiler.rs +++ b/tests/compiler.rs @@ -75,10 +75,7 @@ fn it_should_compile_parsed_content() { let html_content = compile(&ast, &HsmlCompileOptions::default()).unwrap(); - assert_eq!( - html_content, - r#"

"Tailwind CSS is the only framework that I've seen scale -on large teams. It's easy to customize, adapts to any design, -and the build size is tiny."
{{ status.spoilerText }}
{{ `${$t('status.filter_hidden_phrase')}: ${filterPhrase}` }}

"Tailwind CSS is the only framework that I've seen scale +on large teams. It's easy to customize, adapts to any design, +and the build size is tiny."
{{ status.spoilerText }}
{{ `${$t('status.filter_hidden_phrase')}: ${filterPhrase}` }}