Skip to content

Commit

Permalink
fix: Remove broken symbolic links in exact_ directories
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Aug 22, 2022
1 parent 8371e08 commit 7583387
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .golangci.yml
Expand Up @@ -24,7 +24,6 @@ linters:
- gofmt
- gofumpt
- goimports
- gomoddirectives
- gomodguard
- goprintffuncname
- gosec
Expand Down Expand Up @@ -71,6 +70,7 @@ linters:
- godox
- goheader
- gomnd
- gomoddirectives
- maintidx
- maligned
- nakedret
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Expand Up @@ -122,3 +122,6 @@ require (
)

exclude github.com/sergi/go-diff v1.2.0 // Produces incorrect diffs

// FIXME remove when https://github.com/bmatcuk/doublestar/pull/69 is merged and released
replace github.com/bmatcuk/doublestar/v4 v4.2.0 => github.com/twpayne/doublestar/v4 v4.0.0-20220822143754-0ea4707e8a88
2 changes: 2 additions & 0 deletions go.sum
Expand Up @@ -450,6 +450,8 @@ github.com/tklauser/go-sysconf v0.3.10 h1:IJ1AZGZRWbY8T5Vfk04D9WOA5WSejdflXxP03O
github.com/tklauser/go-sysconf v0.3.10/go.mod h1:C8XykCvCb+Gn0oNCWPIlcb0RuglQTYaQ2hGm7jmxEFk=
github.com/tklauser/numcpus v0.4.0 h1:E53Dm1HjH1/R2/aoCtXtPgzmElmn51aOkhCFSuZq//o=
github.com/tklauser/numcpus v0.4.0/go.mod h1:1+UI3pD8NW14VMwdgJNJ1ESk2UnwhAnz5hMwiKKqXCQ=
github.com/twpayne/doublestar/v4 v4.0.0-20220822143754-0ea4707e8a88 h1:rc9w60CQOt0rQz+SqjNU6Bk2QqMPuK7f9iop1UbIW60=
github.com/twpayne/doublestar/v4 v4.0.0-20220822143754-0ea4707e8a88/go.mod h1:xBQ8jztBU6kakFMg+8WGxn0c6z1fTSPVIjEY1Wr7jzc=
github.com/twpayne/go-pinentry v0.2.0 h1:hS5NEJiilop9xP9pBX/1NYduzDlGGMdg1KamTBTrOWw=
github.com/twpayne/go-pinentry v0.2.0/go.mod h1:r6buhMwARxnnL0VRBqfd1tE6Fadk1kfP00GRMutEspY=
github.com/twpayne/go-shell v0.3.1 h1:JIC6cyDpG/p8mRnFUleH07roi90q0J9QC8PnvtmYLRo=
Expand Down
20 changes: 19 additions & 1 deletion pkg/cmd/main_test.go
Expand Up @@ -59,6 +59,7 @@ func TestScript(t *testing.T) {
"expandenv": cmdExpandEnv,
"httpd": cmdHTTPD,
"issymlink": cmdIsSymlink,
"lexists": cmdLExists,
"mkfile": cmdMkFile,
"mkageconfig": cmdMkAgeConfig,
"mkgitconfig": cmdMkGitConfig,
Expand Down Expand Up @@ -222,7 +223,7 @@ func cmdHTTPD(ts *testscript.TestScript, neg bool, args []string) {
ts.Setenv("HTTPD_URL", server.URL)
}

// cmdIsSymlink returns true if all of its arguments are symlinks.
// cmdIsSymlink succeeds if all of its arguments are symlinks.
func cmdIsSymlink(ts *testscript.TestScript, neg bool, args []string) {
for _, arg := range args {
filename := ts.MkAbs(arg)
Expand All @@ -239,6 +240,23 @@ func cmdIsSymlink(ts *testscript.TestScript, neg bool, args []string) {
}
}

// cmdLExists succeeds if all if its arguments exist, without following symlinks.
func cmdLExists(ts *testscript.TestScript, neg bool, args []string) {
if len(args) == 0 {
ts.Fatalf("usage: exists file...")
}

for _, arg := range args {
filename := ts.MkAbs(arg)
switch _, err := os.Lstat(filename); {
case err == nil && neg:
ts.Fatalf("%s unexpectedly exists", filename)
case errors.Is(err, fs.ErrNotExist) && !neg:
ts.Fatalf("%s does not exist", filename)
}
}
}

// cmdMkFile creates empty files.
func cmdMkFile(ts *testscript.TestScript, neg bool, args []string) {
if neg {
Expand Down
8 changes: 8 additions & 0 deletions pkg/cmd/testdata/scripts/issue2016.txtar
@@ -0,0 +1,8 @@
symlink $HOME/.symlink -> .file

# test that chezmoi apply removes broken symlinks
exec chezmoi apply
! lexists $HOME/.symlink

-- home/user/.local/share/chezmoi/.chezmoiremove --
.symlink

0 comments on commit 7583387

Please sign in to comment.