Skip to content

Preserve leading whitespace in YAML literal blocks - #958

Open
NgoQuocViet2001 wants to merge 1 commit into
mitsuhiko:masterfrom
NgoQuocViet2001:fix-yaml-literal-block-indent
Open

NgoQuocViet2001 wants to merge 1 commit into
mitsuhiko:masterfrom
NgoQuocViet2001:fix-yaml-literal-block-indent

Conversation

@NgoQuocViet2001

Copy link
Copy Markdown

With INSTA_YAML_BLOCK_STYLE=1, a multiline string whose first line starts with whitespace is written as a literal block without an indentation indicator. YAML takes the indentation from that first line, so the leading whitespace is lost on read. If a later line is less indented, the file no longer parses: with description => " return x;\n }", the next run prints "Failed to parse snapshot file; a new snapshot will be generated" and the assertion fails even though the contents match.

This writes the indentation indicator when the first non-empty line starts with a space or tab, so description: |- becomes description: |2-. The vendored parser already reads the indicator, and output for all other strings is unchanged. With the variable set, assert_yaml_snapshot! uses the same emitter, so its output for such strings changes from |- to |2- and those snapshots need to be accepted once. Falling back to the quoted style for these strings would also work if you prefer that.

Ran:

  • cargo test -p insta --lib --locked: 80 passed. Both new emitter tests fail on master.
  • cargo test -p insta --all-features --locked: passes except the three test_globs_follow_links* tests, which fail the same way on master in this checkout.
  • cargo clippy -p insta --all-targets --all-features -- --deny warnings and cargo fmt --all -- --check: clean.

Checked: a crate using with_settings!({description => ...}) with INSTA_YAML_BLOCK_STYLE=1 fails on master (the parse warning above, and an INSTA_REQUIRE_FULL_MATCH=1 mismatch for a uniformly indented description) and passes with this change.

With INSTA_YAML_BLOCK_STYLE=1, a multiline string whose first line
starts with a space or tab was written as a literal block without an
indentation indicator. The parser then takes the indentation from that
first line, so the leading whitespace was dropped on read, or the
snapshot failed to parse when a later line was less indented.

Write the indentation indicator (e.g. `|2-`) in that case. Output for
other strings is unchanged. With the variable set, assert_yaml_snapshot!
uses the same emitter, so its output for such strings changes from `|-`
to `|2-` and those snapshots need to be accepted once.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant