Skip to content

Commit

Permalink
fix: Fix error when choice variables are set in flags and config file
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Dec 2, 2024
1 parent 5f5e106 commit 9ce3b0b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
16 changes: 16 additions & 0 deletions internal/cmd/addcmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -292,3 +292,19 @@ func TestAddCmdSecretsError(t *testing.T) {
assert.Error(t, newTestConfig(t, fileSystem).execute([]string{"add", "--secrets=error", "/home/user/.secret"}))
})
}

func TestIssue4107(t *testing.T) {
chezmoitest.WithTestFS(t, map[string]any{
"/home/user": map[string]any{
".secret": "AWS_ACCESS_KEY_ID=AKIA0123456789ABCDEF\n",
".config/chezmoi": map[string]any{
"chezmoi.toml": chezmoitest.JoinLines(
`[add]`,
` secrets = "error"`,
),
},
},
}, func(fileSystem vfs.FS) {
assert.NoError(t, newTestConfig(t, fileSystem).execute([]string{"add", "--secrets=ignore", "/home/user/.secret"}))
})
}
10 changes: 10 additions & 0 deletions internal/cmd/choiceflag.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ func (f *choiceFlag) MarshalText() ([]byte, error) {

// Set implements github.com/spf13/pflag.Value.Set.
func (f *choiceFlag) Set(s string) error {
// If uniqueAbbreviations is nil then all values are allowed. This
// functionality, although counter-intuitive, is required because the unique
// abbreviations are carried in the value, not in the type, so a
// serialization/deserialization round trip discards the unique
// abbreviations. To allow deserialization to succeed, we must allow all
// values.
if f.uniqueAbbreviations == nil {
f.value = s
return nil
}
value, ok := f.uniqueAbbreviations[s]
if !ok {
return errors.New("invalid value")
Expand Down
9 changes: 9 additions & 0 deletions internal/cmd/testdata/scripts/issue4104.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# test that chezmoi add --secrets=ignore succeeds when the add.secrets config variable is set to error
exec chezmoi add --secrets=ignore $HOME${/}.secret
! stderr .

-- home/user/.config/chezmoi/chezmoi.toml --
[add]
secrets = "error"
-- home/user/.secret --
AWS_ACCESS_KEY_ID=AKIA0123456789ABCDEF

0 comments on commit 9ce3b0b

Please sign in to comment.