Skip to content

Commit

Permalink
Fix panic when using diff --exclude
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Apr 18, 2021
1 parent 07127cf commit a7a8ae2
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 1 deletion.
1 change: 1 addition & 0 deletions cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ func newConfig(options ...configOption) (*Config, error) {
Umask: chezmoi.Umask,
Color: "auto",
Diff: diffCmdConfig{
Exclude: chezmoi.NewEntryTypeSet(chezmoi.EntryTypesNone),
Pager: os.Getenv("PAGER"),
include: chezmoi.NewEntryTypeSet(chezmoi.EntryTypesAll),
},
Expand Down
104 changes: 104 additions & 0 deletions cmd/diffcmd_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package cmd

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/twpayne/go-vfs/v2"
"github.com/twpayne/go-vfs/v2/vfst"

"github.com/twpayne/chezmoi/v2/internal/chezmoitest"
)

func TestDiffCmd(t *testing.T) {
if chezmoitest.Umask != 0o22 {
t.Skip("umask not 0o22")
}
for _, tc := range []struct {
name string
extraRoot interface{}
args []string
stdoutStr string
}{
{
name: "empty",
},
{
name: "file",
extraRoot: map[string]interface{}{
"/home/user/.local/share/chezmoi": map[string]interface{}{
"dot_file": "# contents of .file\n",
},
},
stdoutStr: chezmoitest.JoinLines(
`diff --git a/.file b/.file`,
`new file mode 100644`,
`index 0000000000000000000000000000000000000000..8a52cb9ce9551221716a53786ad74104c5902362`,
`--- /dev/null`,
`+++ b/.file`,
`@@ -0,0 +1 @@`,
`+# contents of .file`,
),
},
{
name: "simple_exclude_files",
extraRoot: map[string]interface{}{
"/home/user/.local/share/chezmoi": map[string]interface{}{
"dot_file": "# contents of .file\n",
"symlink_dot_symlink": ".file\n",
},
},
args: []string{
"--exclude", "files",
},
stdoutStr: chezmoitest.JoinLines(
`diff --git a/.symlink b/.symlink`,
`new file mode 120000`,
`index 0000000000000000000000000000000000000000..3e6844d17780d623d817c3e22bcd1128d64422ae`,
`--- /dev/null`,
`+++ b/.symlink`,
`@@ -0,0 +1 @@`,
`+.file`,
),
},
{
name: "simple_exclude_files_with_config",
extraRoot: map[string]interface{}{
"/home/user": map[string]interface{}{
".config/chezmoi/chezmoi.toml": chezmoitest.JoinLines(
`[diff]`,
` exclude = ["files"]`,
),
".local/share/chezmoi": map[string]interface{}{
"dot_file": "# contents of .file\n",
"symlink_dot_symlink": ".file\n",
},
},
},
stdoutStr: chezmoitest.JoinLines(
`diff --git a/.symlink b/.symlink`,
`new file mode 120000`,
`index 0000000000000000000000000000000000000000..3e6844d17780d623d817c3e22bcd1128d64422ae`,
`--- /dev/null`,
`+++ b/.symlink`,
`@@ -0,0 +1 @@`,
`+.file`,
),
},
} {
t.Run(tc.name, func(t *testing.T) {
chezmoitest.WithTestFS(t, map[string]interface{}{
"/home/user": &vfst.Dir{Perm: 0o777 &^ chezmoitest.Umask},
}, func(fs vfs.FS) {
if tc.extraRoot != nil {
require.NoError(t, vfst.NewBuilder().Build(fs, tc.extraRoot))
}
var stdout strings.Builder
require.NoError(t, newTestConfig(t, fs, withStdout(&stdout)).execute(append([]string{"diff"}, tc.args...)))
assert.Equal(t, tc.stdoutStr, stdout.String())
})
})
}
}
2 changes: 1 addition & 1 deletion internal/chezmoi/entrytypeset.go
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ func (s *EntryTypeSet) Type() string {
// from a []string.
func StringSliceToEntryTypeSetHookFunc() mapstructure.DecodeHookFunc {
return func(from, to reflect.Type, data interface{}) (interface{}, error) {
if to != reflect.TypeOf(&EntryTypeSet{}) {
if to != reflect.TypeOf(EntryTypeSet{}) {
return data, nil
}
sl, ok := data.([]interface{})
Expand Down
5 changes: 5 additions & 0 deletions testdata/scripts/exclude.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
[!umask:022] skip

# test chezmoi diff --exclude
chezmoi diff --exclude=scripts
[!windows] cmp stdout golden/diff-no-scripts
[windows] cmp stdout golden/diff-no-scripts-windows

# test that chezmoi diff respects the diff.exclude configuration variable
chezmoi diff
[!windows] cmp stdout golden/diff
Expand Down

0 comments on commit a7a8ae2

Please sign in to comment.