Compare commits
1 commit
main
...
fix/mc-2nf
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
3a7da46afd |
2 changed files with 73 additions and 1 deletions
|
|
@ -1223,7 +1223,10 @@ func commentOutYamlKey(content, key string) (string, error) {
|
|||
continue
|
||||
}
|
||||
|
||||
if name, indent, ok := yamlKeyOnLine(line); ok && walk.step(name, indent) {
|
||||
name, indent, ok := yamlKeyOnLine(line)
|
||||
if !ok {
|
||||
walk.stepOver(line)
|
||||
} else if walk.step(name, indent) {
|
||||
if err := mappingValueUnsetRefusal(lines, i, indent, key); err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
|
@ -1386,6 +1389,31 @@ func (w *nestedKeyWalk) step(name string, indent int) bool {
|
|||
return false
|
||||
}
|
||||
|
||||
// stepOver advances the walk by one line that has content but declares no key
|
||||
// yamlKeyOnLine can read, a list item above all. A dotted key names mappings
|
||||
// only, so nothing nested under such a line can continue the match, and the
|
||||
// walk skips that subtree as it does one under a key that does not match. These
|
||||
// lines used to bypass the walk entirely, so unsetting `dolt.host` on
|
||||
//
|
||||
// dolt:
|
||||
// - port: 3307
|
||||
// host: 10.0.0.1
|
||||
//
|
||||
// read the item's `host:` as a direct child of `dolt:` and commented out a key
|
||||
// the file never set, reporting success (mc-2nfr1).
|
||||
//
|
||||
// The skip is armed only while none is. Narrowing an armed one would end the
|
||||
// skip over a section the key does not own partway through it, and widening
|
||||
// one would trust a line that need not end anything: yaml.v3 accepts a quoted
|
||||
// value continued at column 0, and the target can follow it.
|
||||
func (w *nestedKeyWalk) stepOver(line string) {
|
||||
trimmed := strings.TrimSpace(line)
|
||||
if trimmed == "" || strings.HasPrefix(trimmed, "#") || w.opaque >= 0 {
|
||||
return
|
||||
}
|
||||
w.opaque = lineIndent(line)
|
||||
}
|
||||
|
||||
// unsupportedUnsetShape reports the two shapes named in the changelog — a key
|
||||
// under a flow-style mapping, and a key whose value is a block scalar — in
|
||||
// whichever spelling the file uses. Both used to be silent: the flow-style one
|
||||
|
|
|
|||
|
|
@ -410,6 +410,44 @@ func TestUnsetRemovesADottedKeyInEveryShape(t *testing.T) {
|
|||
key: "dolt.host",
|
||||
survivors: map[string]string{"other.dolt.host": "should-survive"},
|
||||
},
|
||||
{
|
||||
// The key's own section spelled as a list of mappings. A dotted key
|
||||
// names mappings only, so nothing here is dolt.host, but the walk never
|
||||
// saw the `- ` line, read the item's `host:` as the section's direct
|
||||
// child, and commented it out with a success exit (mc-2nfr1).
|
||||
name: "section spelled as a list of mappings",
|
||||
seed: "dolt:\n - port: 3307\n host: 10.0.0.1\n - port: 3308\n host: 10.0.0.2\n",
|
||||
key: "dolt.host",
|
||||
},
|
||||
{
|
||||
// YAML lets the list sit at the section's own indentation.
|
||||
name: "section spelled as a list at its own indentation",
|
||||
seed: "dolt:\n- port: 3307\n host: 10.0.0.1\n",
|
||||
key: "dolt.host",
|
||||
},
|
||||
{
|
||||
// A list nested in a section the key does not own must not end the
|
||||
// skip over that section: the leaf name after it is still other.host.
|
||||
name: "list inside a section the key does not own",
|
||||
seed: "dolt:\n other:\n list:\n - a\n host: keep\n",
|
||||
key: "dolt.host",
|
||||
survivors: map[string]string{"dolt.other.host": "keep"},
|
||||
},
|
||||
{
|
||||
// Blank lines and comments declare no key either, but they are not
|
||||
// content: the key beneath them is still the target.
|
||||
name: "blank line and comment between the section and the key",
|
||||
seed: "sync:\n\n# where bd pushes\n remote: \"file:///origin.git\"\n",
|
||||
key: "sync.remote",
|
||||
},
|
||||
{
|
||||
// yaml.v3 accepts a quoted value continued at column 0. That line
|
||||
// declares no key, and it must not hide the target that follows it.
|
||||
name: "quoted value continued at column 0 before the target",
|
||||
seed: "dolt:\n note: \"line one\nline two\"\n host: kill-me\n",
|
||||
key: "dolt.host",
|
||||
survivors: map[string]string{"dolt.note": "line one line two"},
|
||||
},
|
||||
}
|
||||
for _, tc := range cases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
|
|
@ -448,6 +486,12 @@ func TestUnsetRemovesADottedKeyInEveryShape(t *testing.T) {
|
|||
if wasSet && !strings.Contains(string(body), leaf+":") {
|
||||
t.Errorf("unset removed the key instead of commenting it out:\n%s", body)
|
||||
}
|
||||
// A key that was never set leaves nothing to comment out. Checked on
|
||||
// the text because a key inside a list item has no dotted spelling
|
||||
// the survivors below could read it back by.
|
||||
if !wasSet && string(body) != tc.seed {
|
||||
t.Errorf("unset of a key that was never set edited the file:\n%s", body)
|
||||
}
|
||||
// Anything the unset was not asked about is none of its business.
|
||||
for key, want := range tc.survivors {
|
||||
if got := GetStringFromDir(dir, key); got != want {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue