Skip to content

Commit 4a48447

Browse files
committed
fix: detect nested dotfiles scripts
1 parent 47f2c7d commit 4a48447

File tree

2 files changed

+39
-8
lines changed

2 files changed

+39
-8
lines changed

cli/dotfiles.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bytes"
55
"errors"
66
"fmt"
7-
"io/fs"
87
"os"
98
"os/exec"
109
"path/filepath"
@@ -184,7 +183,7 @@ func (r *RootCmd) dotfiles() *serpent.Command {
184183
}
185184
}
186185

187-
script := findScript(installScriptSet, files)
186+
script := findScript(installScriptSet, dotfilesDir)
188187
if script != "" {
189188
_, err = cliui.Prompt(inv, cliui.PromptOptions{
190189
Text: fmt.Sprintf("Running install script %s.\n\n Continue?", script),
@@ -361,15 +360,12 @@ func dirExists(name string) (bool, error) {
361360
}
362361

363362
// findScript will find the first file that matches the script set.
364-
func findScript(scriptSet []string, files []fs.DirEntry) string {
363+
func findScript(scriptSet []string, directory string) string {
365364
for _, i := range scriptSet {
366-
for _, f := range files {
367-
if f.Name() == i {
368-
return f.Name()
369-
}
365+
if _, err := os.Stat(filepath.Join(directory, i)); err == nil {
366+
return i
370367
}
371368
}
372-
373369
return ""
374370
}
375371

cli/dotfiles_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,41 @@ func TestDotfiles(t *testing.T) {
142142
require.NoError(t, err)
143143
require.Equal(t, string(b), "wow\n")
144144
})
145+
146+
t.Run("NestedInstallScript", func(t *testing.T) {
147+
t.Parallel()
148+
if runtime.GOOS == "windows" {
149+
t.Skip("install scripts on windows require sh and aren't very practical")
150+
}
151+
_, root := clitest.New(t)
152+
testRepo := testGitRepo(t, root)
153+
154+
scriptPath := filepath.Join("script", "setup")
155+
err := os.MkdirAll(filepath.Join(testRepo, "script"), 0o750)
156+
require.NoError(t, err)
157+
// nolint:gosec
158+
err = os.WriteFile(filepath.Join(testRepo, scriptPath), []byte("#!/bin/bash\necho wow > "+filepath.Join(string(root), ".bashrc")), 0o750)
159+
require.NoError(t, err)
160+
161+
c := exec.Command("git", "add", scriptPath)
162+
c.Dir = testRepo
163+
err = c.Run()
164+
require.NoError(t, err)
165+
166+
c = exec.Command("git", "commit", "-m", `"add script"`)
167+
c.Dir = testRepo
168+
err = c.Run()
169+
require.NoError(t, err)
170+
171+
inv, _ := clitest.New(t, "dotfiles", "--global-config", string(root), "--symlink-dir", string(root), "-y", testRepo)
172+
err = inv.Run()
173+
require.NoError(t, err)
174+
175+
b, err := os.ReadFile(filepath.Join(string(root), ".bashrc"))
176+
require.NoError(t, err)
177+
require.Equal(t, string(b), "wow\n")
178+
})
179+
145180
t.Run("InstallScriptChangeBranch", func(t *testing.T) {
146181
t.Parallel()
147182
if runtime.GOOS == "windows" {

0 commit comments

Comments
 (0)