Skip to content

Commit

Permalink
chore: Add config marshal/unmarshal roundtrip tests
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Feb 25, 2024
1 parent 3b3541c commit 39bd915
Show file tree
Hide file tree
Showing 4 changed files with 247 additions and 0 deletions.
126 changes: 126 additions & 0 deletions internal/chezmoi/ageencryption_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,132 @@ func TestAgeEncryption(t *testing.T) {
})
}

func TestAgeEncryptionMarshalUnmarshal(t *testing.T) {
for _, format := range []Format{
FormatJSON,
FormatYAML,
} {
t.Run(format.Name(), func(t *testing.T) {
expected := AgeEncryption{
UseBuiltin: true,
Command: "command",
Args: []string{
"arg1",
"arg2",
},
Identity: NewAbsPath("/identity"),
Identities: []AbsPath{
NewAbsPath("/identity1"),
NewAbsPath("/identity2"),
},
Passphrase: true,
Recipient: "recipient",
RecipientsFile: NewAbsPath("/recipients-file"),
RecipientsFiles: []AbsPath{
NewAbsPath("/recipients-file1"),
NewAbsPath("/recipients-file2"),
},
Suffix: "suffix",
Symmetric: true,
}
data, err := format.Marshal(expected)
assert.NoError(t, err)
var actual AgeEncryption
assert.NoError(t, format.Unmarshal(data, &actual))
assert.Equal(t, expected, actual)
})
}
}

func TestAgeEncryptionMarshalUnmarshalField(t *testing.T) {
type ConfigFile struct {
Age AgeEncryption `json:"age" yaml:"age"`
}
for _, format := range []Format{
FormatJSON,
FormatYAML,
} {
t.Run(format.Name(), func(t *testing.T) {
expected := ConfigFile{
Age: AgeEncryption{
UseBuiltin: true,
Command: "command",
Args: []string{
"arg1",
"arg2",
},
Identity: NewAbsPath("/identity"),
Identities: []AbsPath{
NewAbsPath("/identity1"),
NewAbsPath("/identity2"),
},
Passphrase: true,
Recipient: "recipient",
RecipientsFile: NewAbsPath("/recipients-file"),
RecipientsFiles: []AbsPath{
NewAbsPath("/recipients-file1"),
NewAbsPath("/recipients-file2"),
},
Suffix: "suffix",
Symmetric: true,
},
}
data, err := format.Marshal(expected)
assert.NoError(t, err)
var actual ConfigFile
assert.NoError(t, format.Unmarshal(data, &actual))
assert.Equal(t, expected, actual)
})
}
}

func TestAgeEncryptionMarshalUnmarshalFieldEmbedded(t *testing.T) {
type ConfigFile struct {
Age AgeEncryption `json:"age" yaml:"age"`
}
type Config struct {
ConfigFile
}
for _, format := range []Format{
FormatJSON,
FormatYAML,
} {
t.Run(format.Name(), func(t *testing.T) {
expected := Config{
ConfigFile: ConfigFile{
Age: AgeEncryption{
UseBuiltin: true,
Command: "command",
Args: []string{
"arg1",
"arg2",
},
Identity: NewAbsPath("/identity"),
Identities: []AbsPath{
NewAbsPath("/identity1"),
NewAbsPath("/identity2"),
},
Passphrase: true,
Recipient: "recipient",
RecipientsFile: NewAbsPath("/recipients-file"),
RecipientsFiles: []AbsPath{
NewAbsPath("/recipients-file1"),
NewAbsPath("/recipients-file2"),
},
Suffix: "suffix",
Symmetric: true,
},
},
}
data, err := format.Marshal(expected)
assert.NoError(t, err)
var actual Config
assert.NoError(t, format.Unmarshal(data, &actual))
assert.Equal(t, expected, actual)
})
}
}

func TestAgeMultipleIdentitiesAndMultipleRecipients(t *testing.T) {
forEachAgeCommand(t, func(t *testing.T, command string) {
t.Helper()
Expand Down
84 changes: 84 additions & 0 deletions internal/cmd/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,90 @@ func TestAddTemplateFuncPanic(t *testing.T) {
})
}

func TestConfigFileFormatRoundTrip(t *testing.T) {
for _, format := range []chezmoi.Format{
chezmoi.FormatJSON,
chezmoi.FormatYAML,
} {
t.Run(format.Name(), func(t *testing.T) {
configFile := ConfigFile{
Color: autoBool{auto: true},
Data: map[string]any{},
Env: map[string]string{},
Hooks: map[string]hookConfig{},
Interpreters: map[string]*chezmoi.Interpreter{},
Mode: chezmoi.ModeFile,
PINEntry: pinEntryConfig{
Args: []string{},
Options: []string{},
},
ScriptEnv: map[string]string{},
Template: templateConfig{
Options: []string{},
},
TextConv: []*textConvElement{},
UseBuiltinAge: autoBool{value: false},
UseBuiltinGit: autoBool{value: true},
Dashlane: dashlaneConfig{
Args: []string{},
},
Doppler: dopplerConfig{
Args: []string{},
},
HCPVaultSecrets: hcpVaultSecretConfig{
Args: []string{},
},
Keepassxc: keepassxcConfig{
Args: []string{},
},
Keeper: keeperConfig{
Args: []string{},
},
Passhole: passholeConfig{
Args: []string{},
},
Secret: secretConfig{
Args: []string{},
},
Age: chezmoi.AgeEncryption{
Args: []string{},
Identity: chezmoi.NewAbsPath("/identity.txt"),
Identities: []chezmoi.AbsPath{},
Recipients: []string{},
RecipientsFiles: []chezmoi.AbsPath{},
},
GPG: chezmoi.GPGEncryption{
Args: []string{},
Recipients: []string{},
},
Add: addCmdConfig{
Secrets: severityError,
},
CD: cdCmdConfig{
Args: []string{},
},
Diff: diffCmdConfig{
Args: []string{},
},
Edit: editCmdConfig{
Args: []string{},
},
Merge: mergeCmdConfig{
Args: []string{},
},
Update: updateCmdConfig{
Args: []string{},
},
}
data, err := format.Marshal(configFile)
assert.NoError(t, err)
var actualConfigFile ConfigFile
assert.NoError(t, format.Unmarshal(data, &actualConfigFile))
assert.Equal(t, configFile, actualConfigFile)
})
}
}

func TestParseCommand(t *testing.T) {
for i, tc := range []struct {
command string
Expand Down
24 changes: 24 additions & 0 deletions internal/cmd/datacmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,14 @@ func TestDataCmd(t *testing.T) {
root: map[string]any{
"/home/user/.config/chezmoi/chezmoi.json": chezmoitest.JoinLines(
`{`,
` "mode": "symlink",`,
` "sourceDir": "/tmp/source",`,
` "age": {`,
` "args": [`,
` "arg"`,
` ],`,
` "identity": "/my-age-identity"`,
` },`,
` "data": {`,
` "test": true`,
` }`,
Expand All @@ -33,7 +40,12 @@ func TestDataCmd(t *testing.T) {
format: chezmoi.FormatYAML,
root: map[string]any{
"/home/user/.config/chezmoi/chezmoi.yaml": chezmoitest.JoinLines(
`mode: symlink`,
`sourceDir: /tmp/source`,
`age:`,
` args:`,
` - arg`,
` identity: /my-age-identity`,
`data:`,
` test: true`,
),
Expand All @@ -52,11 +64,23 @@ func TestDataCmd(t *testing.T) {

var data struct {
Chezmoi struct {
Config struct {
Age struct {
Args []string `json:"args" yaml:"args"`
Identity string `json:"identity" yaml:"identity"`
} `json:"age" yaml:"age"`
Mode string `json:"mode" yaml:"mode"`
} `json:"config" yaml:"config"`
SourceDir string `json:"sourceDir" yaml:"sourceDir"`
} `json:"chezmoi" yaml:"chezmoi"`
Test bool `json:"test" yaml:"test"`
}
assert.NoError(t, tc.format.Unmarshal([]byte(stdout.String()), &data))
assert.Equal(t, []string{"arg"}, data.Chezmoi.Config.Age.Args)
normalizedAgeIdentity, err := chezmoi.NormalizePath("/my-age-identity")
assert.NoError(t, err)
assert.Equal(t, normalizedAgeIdentity.String(), data.Chezmoi.Config.Age.Identity)
assert.Equal(t, "symlink", data.Chezmoi.Config.Mode)
normalizedSourceDir, err := chezmoi.NormalizePath("/tmp/source")
assert.NoError(t, err)
assert.Equal(t, normalizedSourceDir.String(), data.Chezmoi.SourceDir)
Expand Down
13 changes: 13 additions & 0 deletions internal/cmd/testdata/scripts/issue3582.txtar
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# test that chezmoi data shows data read from TOML config files
exec chezmoi data --format=json
stdout '"mode": "file"'
stdout '"pager": "my-pager"'
stdout '"pager": "my-diff-pager"'
stdout '"identity": ".*/my-age-identity"'

-- home/user/.config/chezmoi/chezmoi.toml --
pager = "my-pager"
[diff]
pager = "my-diff-pager"
[age]
identity = "my-age-identity"

0 comments on commit 39bd915

Please sign in to comment.