Skip to content

Commit e43c2a8

Browse files
committed
chore: Add config marshal/unmarshal roundtrip tests
1 parent 8374a57 commit e43c2a8

File tree

4 files changed

+245
-0
lines changed

4 files changed

+245
-0
lines changed

internal/chezmoi/ageencryption_test.go

+126
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,132 @@ func TestAgeEncryption(t *testing.T) {
3232
})
3333
}
3434

35+
func TestAgeEncryptionMarshalUnmarshal(t *testing.T) {
36+
for _, format := range []Format{
37+
FormatJSON,
38+
FormatYAML,
39+
} {
40+
t.Run(format.Name(), func(t *testing.T) {
41+
expected := AgeEncryption{
42+
UseBuiltin: true,
43+
Command: "command",
44+
Args: []string{
45+
"arg1",
46+
"arg2",
47+
},
48+
Identity: NewAbsPath("/identity"),
49+
Identities: []AbsPath{
50+
NewAbsPath("/identity1"),
51+
NewAbsPath("/identity2"),
52+
},
53+
Passphrase: true,
54+
Recipient: "recipient",
55+
RecipientsFile: NewAbsPath("/recipients-file"),
56+
RecipientsFiles: []AbsPath{
57+
NewAbsPath("/recipients-file1"),
58+
NewAbsPath("/recipients-file2"),
59+
},
60+
Suffix: "suffix",
61+
Symmetric: true,
62+
}
63+
data, err := format.Marshal(expected)
64+
assert.NoError(t, err)
65+
var actual AgeEncryption
66+
assert.NoError(t, format.Unmarshal(data, &actual))
67+
assert.Equal(t, expected, actual)
68+
})
69+
}
70+
}
71+
72+
func TestAgeEncryptionMarshalUnmarshalField(t *testing.T) {
73+
type ConfigFile struct {
74+
Age AgeEncryption `json:"age" yaml:"age"`
75+
}
76+
for _, format := range []Format{
77+
FormatJSON,
78+
FormatYAML,
79+
} {
80+
t.Run(format.Name(), func(t *testing.T) {
81+
expected := ConfigFile{
82+
Age: AgeEncryption{
83+
UseBuiltin: true,
84+
Command: "command",
85+
Args: []string{
86+
"arg1",
87+
"arg2",
88+
},
89+
Identity: NewAbsPath("/identity"),
90+
Identities: []AbsPath{
91+
NewAbsPath("/identity1"),
92+
NewAbsPath("/identity2"),
93+
},
94+
Passphrase: true,
95+
Recipient: "recipient",
96+
RecipientsFile: NewAbsPath("/recipients-file"),
97+
RecipientsFiles: []AbsPath{
98+
NewAbsPath("/recipients-file1"),
99+
NewAbsPath("/recipients-file2"),
100+
},
101+
Suffix: "suffix",
102+
Symmetric: true,
103+
},
104+
}
105+
data, err := format.Marshal(expected)
106+
assert.NoError(t, err)
107+
var actual ConfigFile
108+
assert.NoError(t, format.Unmarshal(data, &actual))
109+
assert.Equal(t, expected, actual)
110+
})
111+
}
112+
}
113+
114+
func TestAgeEncryptionMarshalUnmarshalFieldEmbedded(t *testing.T) {
115+
type ConfigFile struct {
116+
Age AgeEncryption `json:"age" yaml:"age"`
117+
}
118+
type Config struct {
119+
ConfigFile
120+
}
121+
for _, format := range []Format{
122+
FormatJSON,
123+
FormatYAML,
124+
} {
125+
t.Run(format.Name(), func(t *testing.T) {
126+
expected := Config{
127+
ConfigFile: ConfigFile{
128+
Age: AgeEncryption{
129+
UseBuiltin: true,
130+
Command: "command",
131+
Args: []string{
132+
"arg1",
133+
"arg2",
134+
},
135+
Identity: NewAbsPath("/identity"),
136+
Identities: []AbsPath{
137+
NewAbsPath("/identity1"),
138+
NewAbsPath("/identity2"),
139+
},
140+
Passphrase: true,
141+
Recipient: "recipient",
142+
RecipientsFile: NewAbsPath("/recipients-file"),
143+
RecipientsFiles: []AbsPath{
144+
NewAbsPath("/recipients-file1"),
145+
NewAbsPath("/recipients-file2"),
146+
},
147+
Suffix: "suffix",
148+
Symmetric: true,
149+
},
150+
},
151+
}
152+
data, err := format.Marshal(expected)
153+
assert.NoError(t, err)
154+
var actual Config
155+
assert.NoError(t, format.Unmarshal(data, &actual))
156+
assert.Equal(t, expected, actual)
157+
})
158+
}
159+
}
160+
35161
func TestAgeMultipleIdentitiesAndMultipleRecipients(t *testing.T) {
36162
forEachAgeCommand(t, func(t *testing.T, command string) {
37163
t.Helper()

internal/cmd/config_test.go

+84
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,90 @@ func TestAddTemplateFuncPanic(t *testing.T) {
2828
})
2929
}
3030

31+
func TestConfigFileFormatRoundTrip(t *testing.T) {
32+
for _, format := range []chezmoi.Format{
33+
chezmoi.FormatJSON,
34+
chezmoi.FormatYAML,
35+
} {
36+
t.Run(format.Name(), func(t *testing.T) {
37+
configFile := ConfigFile{
38+
Color: autoBool{auto: true},
39+
Data: map[string]any{},
40+
Env: map[string]string{},
41+
Hooks: map[string]hookConfig{},
42+
Interpreters: map[string]*chezmoi.Interpreter{},
43+
Mode: chezmoi.ModeFile,
44+
PINEntry: pinEntryConfig{
45+
Args: []string{},
46+
Options: []string{},
47+
},
48+
ScriptEnv: map[string]string{},
49+
Template: templateConfig{
50+
Options: []string{},
51+
},
52+
TextConv: []*textConvElement{},
53+
UseBuiltinAge: autoBool{value: false},
54+
UseBuiltinGit: autoBool{value: true},
55+
Dashlane: dashlaneConfig{
56+
Args: []string{},
57+
},
58+
Doppler: dopplerConfig{
59+
Args: []string{},
60+
},
61+
HCPVaultSecrets: hcpVaultSecretConfig{
62+
Args: []string{},
63+
},
64+
Keepassxc: keepassxcConfig{
65+
Args: []string{},
66+
},
67+
Keeper: keeperConfig{
68+
Args: []string{},
69+
},
70+
Passhole: passholeConfig{
71+
Args: []string{},
72+
},
73+
Secret: secretConfig{
74+
Args: []string{},
75+
},
76+
Age: chezmoi.AgeEncryption{
77+
Args: []string{},
78+
Identity: chezmoi.NewAbsPath("/identity.txt"),
79+
Identities: []chezmoi.AbsPath{},
80+
Recipients: []string{},
81+
RecipientsFiles: []chezmoi.AbsPath{},
82+
},
83+
GPG: chezmoi.GPGEncryption{
84+
Args: []string{},
85+
Recipients: []string{},
86+
},
87+
Add: addCmdConfig{
88+
Secrets: severityError,
89+
},
90+
CD: cdCmdConfig{
91+
Args: []string{},
92+
},
93+
Diff: diffCmdConfig{
94+
Args: []string{},
95+
},
96+
Edit: editCmdConfig{
97+
Args: []string{},
98+
},
99+
Merge: mergeCmdConfig{
100+
Args: []string{},
101+
},
102+
Update: updateCmdConfig{
103+
Args: []string{},
104+
},
105+
}
106+
data, err := format.Marshal(configFile)
107+
assert.NoError(t, err)
108+
var actualConfigFile ConfigFile
109+
assert.NoError(t, format.Unmarshal(data, &actualConfigFile))
110+
assert.Equal(t, configFile, actualConfigFile)
111+
})
112+
}
113+
}
114+
31115
func TestParseCommand(t *testing.T) {
32116
for i, tc := range []struct {
33117
command string

internal/cmd/datacmd_test.go

+22
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ func TestDataCmd(t *testing.T) {
2121
root: map[string]any{
2222
"/home/user/.config/chezmoi/chezmoi.json": chezmoitest.JoinLines(
2323
`{`,
24+
` "mode": "symlink",`,
2425
` "sourceDir": "/tmp/source",`,
26+
` "age": {`,
27+
` "args": [`,
28+
` "arg"`,
29+
` ],`,
30+
` "identity": "/my-age-identity"`,
31+
` },`,
2532
` "data": {`,
2633
` "test": true`,
2734
` }`,
@@ -33,7 +40,12 @@ func TestDataCmd(t *testing.T) {
3340
format: chezmoi.FormatYAML,
3441
root: map[string]any{
3542
"/home/user/.config/chezmoi/chezmoi.yaml": chezmoitest.JoinLines(
43+
`mode: symlink`,
3644
`sourceDir: /tmp/source`,
45+
`age:`,
46+
` args:`,
47+
` - arg`,
48+
` identity: /my-age-identity`,
3749
`data:`,
3850
` test: true`,
3951
),
@@ -52,11 +64,21 @@ func TestDataCmd(t *testing.T) {
5264

5365
var data struct {
5466
Chezmoi struct {
67+
Config struct {
68+
Age struct {
69+
Args []string `json:"args" yaml:"args"`
70+
Identity string `json:"identity" yaml:"identity"`
71+
} `json:"age" yaml:"age"`
72+
Mode string `json:"mode" yaml:"mode"`
73+
} `json:"config" yaml:"config"`
5574
SourceDir string `json:"sourceDir" yaml:"sourceDir"`
5675
} `json:"chezmoi" yaml:"chezmoi"`
5776
Test bool `json:"test" yaml:"test"`
5877
}
5978
assert.NoError(t, tc.format.Unmarshal([]byte(stdout.String()), &data))
79+
assert.Equal(t, []string{"arg"}, data.Chezmoi.Config.Age.Args)
80+
assert.Equal(t, "/my-age-identity", data.Chezmoi.Config.Age.Identity)
81+
assert.Equal(t, "symlink", data.Chezmoi.Config.Mode)
6082
normalizedSourceDir, err := chezmoi.NormalizePath("/tmp/source")
6183
assert.NoError(t, err)
6284
assert.Equal(t, normalizedSourceDir.String(), data.Chezmoi.SourceDir)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# test that chezmoi data shows data read from TOML config files
2+
exec chezmoi data --format=json
3+
stdout '"mode": "file"'
4+
stdout '"pager": "my-pager"'
5+
stdout '"pager": "my-diff-pager"'
6+
stdout '"identity": ".*/my-age-identity"'
7+
8+
-- home/user/.config/chezmoi/chezmoi.toml --
9+
pager = "my-pager"
10+
[diff]
11+
pager = "my-diff-pager"
12+
[age]
13+
identity = "my-age-identity"

0 commit comments

Comments
 (0)