Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions internal/utils/prompts.go
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,15 @@ var updateResourceTagsPromptFn = func(existingTags map[string]string, noColor bo
return nil, err
}

// Empty value means remove the tag
if value == "" && tags[key] != "" {
delete(tags, key)
fmt.Fprintf(os.Stderr, " Removed tag: %s\n", key)
} else if value != "" {
// Empty value means remove the tag, if it exists.
if value == "" {
if _, exists := tags[key]; exists {
delete(tags, key)
fmt.Fprintf(os.Stderr, " Removed tag: %s\n", key)
} else {
fmt.Fprintf(os.Stderr, " Tag '%s' does not exist, nothing to remove\n", key)
}
} else {
tags[key] = value
fmt.Fprintf(os.Stderr, " Updated tag: %s: %s\n", key, value)
}
Expand Down
28 changes: 28 additions & 0 deletions internal/utils/prompts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,34 @@ func TestUpdateResourceTagsPrompt_DefaultModifyExistingRemovesTag(t *testing.T)
assert.Empty(t, stdout)
}

// TestUpdateResourceTagsPrompt_DefaultModifyRemovesEmptyValueTag exercises
// removing an existing tag whose current value is already empty.
func TestUpdateResourceTagsPrompt_DefaultModifyRemovesEmptyValueTag(t *testing.T) {
mockPromptSequence(t, []bool{true, true}, []string{"2", "foo", "", ""})
stdout, stderr := withMockedIO("", func() {
tags, err := UpdateResourceTagsPrompt(map[string]string{"foo": ""}, true)
assert.NoError(t, err)
assert.Empty(t, tags)
})
assert.Contains(t, stderr, "Removed tag: foo")
assert.Empty(t, stdout)
}

// TestUpdateResourceTagsPrompt_DefaultModifyEmptyValueForNewKey exercises
// entering an empty value for a key that does not exist yet: it must not be
// added to the tag set, and the user gets a clear "nothing to remove" notice
// instead of the key silently vanishing.
func TestUpdateResourceTagsPrompt_DefaultModifyEmptyValueForNewKey(t *testing.T) {
mockPromptSequence(t, []bool{true, true}, []string{"2", "baz", "", ""})
stdout, stderr := withMockedIO("", func() {
tags, err := UpdateResourceTagsPrompt(map[string]string{"foo": "bar"}, true)
assert.NoError(t, err)
assert.Equal(t, map[string]string{"foo": "bar"}, tags)
})
assert.Contains(t, stderr, "Tag 'baz' does not exist, nothing to remove")
assert.Empty(t, stdout)
}

// TestDesignConfirmPrompt_DefaultRendering verifies the default
// designConfirmPromptFn renders design-stage wording instead of
// BuyConfirmPrompt's purchase wording.
Expand Down
14 changes: 9 additions & 5 deletions internal/utils/prompts_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -270,11 +270,15 @@ func wasmUpdateResourceTagsPrompt(existingTags map[string]string, noColor bool)
return nil, err
}

// Empty value means remove the tag
if value == "" && tags[key] != "" {
delete(tags, key)
buf.add(fmt.Sprintf(" Removed tag: %s", key))
} else if value != "" {
// Empty value means remove the tag, if it exists.
if value == "" {
if _, exists := tags[key]; exists {
delete(tags, key)
buf.add(fmt.Sprintf(" Removed tag: %s", key))
} else {
buf.add(fmt.Sprintf(" Tag '%s' does not exist, nothing to remove", key))
}
} else {
tags[key] = value
buf.add(fmt.Sprintf(" Updated tag: %s: %s", key, value))
}
Expand Down
18 changes: 18 additions & 0 deletions internal/utils/prompts_wasm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,24 @@ func TestWasmUpdateResourceTagsPrompt(t *testing.T) {
},
expectError: false,
},
{
name: "empty value for key that does not exist is a no-op",
existingTags: map[string]string{
"foo": "bar",
},
mockResponses: []string{
"y", // Continue
"2", // Start with existing
"baz", // Key that does not exist
"", // Empty value = attempt remove
"", // Finish
"y", // Apply changes
},
expectedTags: map[string]string{
"foo": "bar",
},
expectError: false,
},
Comment thread
Phil-Browne marked this conversation as resolved.
{
name: "no existing tags - add new",
existingTags: map[string]string{},
Expand Down
Loading