Skip to content

Commit 0556893

Browse files
committed
add unit test
1 parent 5428aca commit 0556893

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

cli/dotfiles_test.go

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,52 @@ func TestDotfiles(t *testing.T) {
8080
require.NoError(t, err)
8181
require.Equal(t, string(b), "wow\n")
8282
})
83+
t.Run("InstallScriptChangeBranch", func(t *testing.T) {
84+
t.Parallel()
85+
if runtime.GOOS == "windows" {
86+
t.Skip("install scripts on windows require sh and aren't very practical")
87+
}
88+
_, root := clitest.New(t)
89+
testRepo := testGitRepo(t, root)
90+
91+
// We need an initial commit to start the `main` branch
92+
c := exec.Command("git", "commit", "--allow-empty", "-m", `"initial commit"`)
93+
c.Dir = testRepo
94+
err := c.Run()
95+
require.NoError(t, err)
96+
97+
// nolint:gosec
98+
err = os.WriteFile(filepath.Join(testRepo, "install.sh"), []byte("#!/bin/bash\necho wow > "+filepath.Join(string(root), ".bashrc")), 0o750)
99+
require.NoError(t, err)
100+
101+
c = exec.Command("git", "checkout", "-b", "other_branch")
102+
c.Dir = testRepo
103+
err = c.Run()
104+
require.NoError(t, err)
105+
106+
c = exec.Command("git", "add", "install.sh")
107+
c.Dir = testRepo
108+
err = c.Run()
109+
require.NoError(t, err)
110+
111+
c = exec.Command("git", "commit", "-m", `"add install.sh"`)
112+
c.Dir = testRepo
113+
err = c.Run()
114+
require.NoError(t, err)
115+
116+
c = exec.Command("git", "checkout", "main")
117+
c.Dir = testRepo
118+
err = c.Run()
119+
require.NoError(t, err)
120+
121+
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "-y", testRepo, "-b", "other_branch")
122+
err = inv.Run()
123+
require.NoError(t, err)
124+
125+
b, err := os.ReadFile(filepath.Join(string(root), ".bashrc"))
126+
require.NoError(t, err)
127+
require.Equal(t, string(b), "wow\n")
128+
})
83129
t.Run("SymlinkBackup", func(t *testing.T) {
84130
t.Parallel()
85131
_, root := clitest.New(t)
@@ -152,5 +198,10 @@ func testGitRepo(t *testing.T, root config.Root) string {
152198
err = c.Run()
153199
require.NoError(t, err)
154200

201+
c = exec.Command("git", "checkout", "-b", "main")
202+
c.Dir = dir
203+
err = c.Run()
204+
require.NoError(t, err)
205+
155206
return dir
156207
}

0 commit comments

Comments
 (0)