Skip to content

Commit 44c3b86

Browse files
bepmvdan
authored andcommitted
testscript: use unix.CloneFile on MacOs
To fix unexpected errors of type: ``` [signal: killed] FAIL: testscripts/myecho.txt:1: unexpected command failure ``` Fixes #200
1 parent 22b9127 commit 44c3b86

File tree

6 files changed

+35
-4
lines changed

6 files changed

+35
-4
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ go 1.19
44

55
require (
66
golang.org/x/mod v0.9.0
7+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f
78
golang.org/x/tools v0.1.12
89
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
22
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
3+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
4+
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
35
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
46
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=

testscript/clonefile.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build unix && !darwin
2+
// +build unix,!darwin
3+
4+
package testscript
5+
6+
import "os"
7+
8+
// cloneFile creates to as a hard link to the from file.
9+
func cloneFile(from, to string) error {
10+
return os.Link(from, to)
11+
}

testscript/clonefile_darwin.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
package testscript
2+
3+
import "golang.org/x/sys/unix"
4+
5+
// cloneFile clones the file from to the file to.
6+
func cloneFile(from, to string) error {
7+
return unix.Clonefile(from, to, 0)
8+
}

testscript/clonefile_other.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
//go:build !unix
2+
// +build !unix
3+
4+
package testscript
5+
6+
import "fmt"
7+
8+
// We don't want to use hard links on Windows, as that can lead to "access denied" errors when removing.
9+
func cloneFile(from, to string) error {
10+
return fmt.Errorf("unavailable")
11+
}

testscript/exe.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,10 +122,8 @@ func RunMain(m TestingM, commands map[string]func() int) (exitCode int) {
122122
// system's temporary directory, like we do. We don't use hard links on Windows,
123123
// as that can lead to "access denied" errors when removing.
124124
func copyBinary(from, to string) error {
125-
if runtime.GOOS != "windows" {
126-
if err := os.Link(from, to); err == nil {
127-
return nil
128-
}
125+
if err := cloneFile(from, to); err == nil {
126+
return nil
129127
}
130128
writer, err := os.OpenFile(to, os.O_WRONLY|os.O_CREATE, 0o777)
131129
if err != nil {

0 commit comments

Comments
 (0)