Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use functional options to construct Configs
  • Loading branch information
twpayne committed Feb 10, 2020
1 parent 552151d commit c475c5e
Show file tree
Hide file tree
Showing 14 changed files with 297 additions and 277 deletions.
45 changes: 16 additions & 29 deletions cmd/add_test.go
@@ -1,7 +1,6 @@
package cmd

import (
"os"
"path/filepath"
"testing"

Expand All @@ -19,13 +18,10 @@ func TestAddAfterModification(t *testing.T) {
})
require.NoError(t, err)
defer cleanup()
c := &Config{
fs: fs,
mutator: chezmoi.NewVerboseMutator(os.Stdout, chezmoi.NewFSMutator(fs), false, 0),
SourceDir: "/home/user/.local/share/chezmoi",
DestDir: "/home/user",
Umask: 022,
}
c := newConfig(
withTestFS(fs),
withTestUser("user"),
)
args := []string{"/home/user/.bashrc"}
assert.NoError(t, c.runAddCmd(nil, args))
vfst.RunTests(t, fs, "",
Expand Down Expand Up @@ -455,22 +451,16 @@ func TestAddCommand(t *testing.T) {
fs, cleanup, err := vfst.NewTestFS(tc.root)
require.NoError(t, err)
defer cleanup()
c := &Config{
fs: fs,
mutator: chezmoi.NewVerboseMutator(os.Stdout, chezmoi.NewFSMutator(fs), false, 0),
SourceDir: "/home/user/.local/share/chezmoi",
DestDir: "/home/user",
Follow: tc.follow,
Umask: 022,
SourceVCS: sourceVCSConfig{
Command: "git",
},
Data: map[string]interface{}{
c := newConfig(
withTestFS(fs),
withTestUser("user"),
withFollow(tc.follow),
withData(map[string]interface{}{
"name": "John Smith",
"email": "john.smith@company.com",
},
add: tc.add,
}
}),
withAddCmdConfig(tc.add),
)
assert.NoError(t, c.runAddCmd(nil, tc.args))
vfst.RunTests(t, fs, "", tc.tests)
})
Expand All @@ -495,13 +485,10 @@ func TestIssue192(t *testing.T) {
fs, cleanup, err := vfst.NewTestFS(root)
require.NoError(t, err)
defer cleanup()
c := &Config{
fs: fs,
mutator: chezmoi.NewVerboseMutator(os.Stdout, chezmoi.NewFSMutator(fs), false, 0),
SourceDir: "/home/offbyone/.local/share/chezmoi",
DestDir: "/home/offbyone",
Umask: 022,
}
c := newConfig(
withTestFS(fs),
withTestUser("offbyone"),
)
assert.NoError(t, c.runAddCmd(nil, []string{"/home/offbyone/snoop/.list"}))
vfst.RunTests(t, fs, "",
vfst.TestPath("/local/home/offbyone/.local/share/chezmoi/snoop/dot_list",
Expand Down
89 changes: 32 additions & 57 deletions cmd/apply_test.go
Expand Up @@ -8,7 +8,6 @@ import (

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/twpayne/chezmoi/internal/chezmoi"
vfs "github.com/twpayne/go-vfs"
"github.com/twpayne/go-vfs/vfst"
)
Expand Down Expand Up @@ -140,14 +139,10 @@ func TestApplyCommand(t *testing.T) {
fs, cleanup, err := vfst.NewTestFS(tc.root)
require.NoError(t, err)
defer cleanup()
c := &Config{
fs: fs,
mutator: chezmoi.NewVerboseMutator(os.Stdout, chezmoi.NewFSMutator(fs), false, 0),
SourceDir: "/home/user/.local/share/chezmoi",
DestDir: "/home/user",
Umask: 022,
bds: newTestBaseDirectorySpecification("/home/user"),
}
c := newConfig(
withTestFS(fs),
withTestUser("user"),
)
assert.NoError(t, c.runApplyCmd(nil, nil))
vfst.RunTests(t, fs, "",
vfst.TestPath("/home/user/dir",
Expand Down Expand Up @@ -230,15 +225,11 @@ func TestApplyFollow(t *testing.T) {
fs, cleanup, err := vfst.NewTestFS(tc.root)
require.NoError(t, err)
defer cleanup()
c := &Config{
fs: fs,
mutator: chezmoi.NewVerboseMutator(os.Stdout, chezmoi.NewFSMutator(fs), false, 0),
SourceDir: "/home/user/.local/share/chezmoi",
DestDir: "/home/user",
Follow: tc.follow,
Umask: 022,
bds: newTestBaseDirectorySpecification("/home/user"),
}
c := newConfig(
withTestFS(fs),
withTestUser("user"),
withFollow(tc.follow),
)
assert.NoError(t, c.runApplyCmd(nil, nil))
vfst.RunTests(t, fs, "", tc.tests)
})
Expand Down Expand Up @@ -350,16 +341,12 @@ func TestApplyRemove(t *testing.T) {
fs, cleanup, err := vfst.NewTestFS(tc.root)
require.NoError(t, err)
defer cleanup()
c := &Config{
fs: fs,
mutator: chezmoi.NewVerboseMutator(os.Stdout, chezmoi.NewFSMutator(fs), false, 0),
SourceDir: "/home/user/.local/share/chezmoi",
DestDir: "/home/user",
Data: tc.data,
Remove: !tc.noRemove,
Umask: 022,
bds: newTestBaseDirectorySpecification("/home/user"),
}
c := newConfig(
withTestFS(fs),
withTestUser("user"),
withData(tc.data),
withRemove(!tc.noRemove),
)
assert.NoError(t, c.runApplyCmd(nil, nil))
vfst.RunTests(t, fs, "", tc.tests)
})
Expand All @@ -381,16 +368,12 @@ func TestApplyScript(t *testing.T) {
require.NoError(t, os.Mkdir(tempDir, 0700))
}()
apply := func() {
c := &Config{
fs: fs,
mutator: chezmoi.NewVerboseMutator(os.Stdout, chezmoi.NewFSMutator(fs), false, 0),
SourceDir: "/home/user/.local/share/chezmoi",
DestDir: "/",
Umask: 022,
Data: tc.data,
bds: newTestBaseDirectorySpecification("/home/user"),
scriptStateBucket: []byte("script"),
}
c := newConfig(
withTestFS(fs),
withTestUser("user"),
withDestDir("/"),
withData(tc.data),
)
require.NoError(t, c.runApplyCmd(nil, nil))
}
// Run apply three times. As chezmoi should be idempotent, the
Expand Down Expand Up @@ -419,18 +402,14 @@ func TestApplyRunOnce(t *testing.T) {
require.NoError(t, err)
defer cleanup()

c := &Config{
fs: fs,
mutator: chezmoi.NewVerboseMutator(os.Stdout, chezmoi.NewFSMutator(fs), false, 0),
SourceDir: "/home/user/.local/share/chezmoi",
DestDir: "/",
Umask: 022,
Data: map[string]interface{}{
c := newConfig(
withTestFS(fs),
withTestUser("user"),
withDestDir("/"),
withData(map[string]interface{}{
"TempFile": tempFile,
},
bds: newTestBaseDirectorySpecification("/home/user"),
scriptStateBucket: []byte("script"),
}
}),
)

require.NoError(t, c.runApplyCmd(nil, nil))
vfst.RunTests(t, fs, "",
Expand Down Expand Up @@ -489,14 +468,10 @@ func TestApplyRemoveEmptySymlink(t *testing.T) {
fs, cleanup, err := vfst.NewTestFS(tc.root)
require.NoError(t, err)
defer cleanup()
c := &Config{
fs: fs,
mutator: chezmoi.NewVerboseMutator(os.Stdout, chezmoi.NewFSMutator(fs), false, 0),
SourceDir: "/home/user/.local/share/chezmoi",
DestDir: "/home/user",
Umask: 022,
bds: newTestBaseDirectorySpecification("/home/user"),
}
c := newConfig(
withTestFS(fs),
withTestUser("user"),
)
assert.NoError(t, c.runApplyCmd(nil, nil))
vfst.RunTests(t, fs, "", tc.tests)
})
Expand Down
14 changes: 5 additions & 9 deletions cmd/archive_test.go
Expand Up @@ -5,13 +5,11 @@ import (
"bytes"
"io"
"io/ioutil"
"os"
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/twpayne/chezmoi/internal/chezmoi"
"github.com/twpayne/go-vfs/vfst"
)

Expand All @@ -23,13 +21,11 @@ func TestArchiveCmd(t *testing.T) {
require.NoError(t, err)
defer cleanup()
stdout := &bytes.Buffer{}
c := &Config{
fs: fs,
mutator: chezmoi.NewVerboseMutator(os.Stdout, chezmoi.NewFSMutator(fs), false, 0),
SourceDir: "/home/user/.local/share/chezmoi",
Umask: 022,
stdout: stdout,
}
c := newConfig(
withTestFS(fs),
withTestUser("user"),
withStdout(stdout),
)
assert.NoError(t, c.runArchiveCmd(nil, nil))
r := tar.NewReader(stdout)

Expand Down

0 comments on commit c475c5e

Please sign in to comment.