test(formatted): add regression tests for quoting scalar values - #266
Merged
Merged
Conversation
Add TestQuoteScalars, a table test covering how quote expressions treat each scalar type. yaml.v3 already double-quotes non-string scalars on quote paths while keeping their original text, so ints, floats, hex/octal ints, .inf/.NaN, bools, and timestamps all become strings without any type coercion in yam. Until now only a single float fixture (testdata/format/quotes.yaml) covered quoting. Explicitly tagged values (!!float 5.12) keep their tag and gain quotes (!!float "5.12"), so quoting never overrides a type the author spelled out. These rows fail if non-string tags are rewritten to !!str, as proposed in chainguard-dev#125. Also covers plain and already-quoted strings, YAML 1.1 bool words, anchored scalars, line comments, nulls, block literals, multiple quote expressions, any-index and specific-index sequence paths, and paths that should not match: a sibling key, a same-depth path under a different parent (the repro in chainguard-dev#100), a deeper path, and a path to a sequence rather than its items. Each row checks the formatted text, that the decoded value has the expected type, and that formatting the output again changes nothing. To test: go test ./pkg/yam/formatted -run TestQuoteScalars -v 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
stevebeattie
marked this pull request as ready for review
October 1, 2026 17:21
stevebeattie
enabled auto-merge (squash)
October 1, 2026 17:25
This was referenced Oct 1, 2026
egibs
approved these changes
Oct 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This adds
TestQuoteScalars, a table-driven regression test for how quoteexpressions (
--quote/quote:in.yam.yaml) handle each kind of YAMLscalar. Only tests change; formatter behavior stays the same.
yaml.v3 already does the right thing here. When a scalar on a quote path isn't
explicitly tagged, it drops the implicit tag and writes the value
double-quoted with its original text, so
5.12,42,0x1F,.inf,trueand
2024-01-01all become strings with no type coercion in yam. Until nowonly a single float fixture (
testdata/format/quotes.yaml) covered quoting,and it didn't check other types, explicit tags, or the decoded type. That's
how #125 came to propose a fix that wasn't needed.
What's covered
Each row checks three things: the exact formatted text, the decoded value
(so a change that keeps the text but changes the type fails), and that
formatting the output a second time changes nothing.
float, int, hex and octal ints,
.inf,.NaN, exponent floats,1_000,true, timestamps, and block literals.yes), single-quotedstrings, and already double-quoted strings (left unchanged).
!!float 5.12becomes!!float "5.12", andthe same for
!!int,!!booland!!str. Quoting should never overridea type the author wrote out.
Nulls stay null.
.v[]quotes everysequence item;
.v[1]quotes only that item.same-depth path under a different parent (the mistake in the yam --quotes doesn't work for floats (and probably others) #100 repro),
a deeper path, and a path to a sequence rather than its items.
The block-literal row deliberately pins today's behavior: quoting a
|scalar flattens it to one escaped line. A comment on the row says so.
Verification
go test ./...andgo test -race ./pkg/yam/formattedpass on main.(
!!float "5.12"→!!str "5.12",!!int "42"→!!str "42").the row written for it. Before this test, the first five passed the
entire repo suite:
"null"|=changed to=)gofmt -l .is clean.golangci-lint run --new-from-rev=origin/mainreports 0 issues.
Related: #100, #125
🤖 Generated with Claude Code