Skip to content

Commit

Permalink
Use unix.CloneFile on MacOs
Browse files Browse the repository at this point in the history
To fix unexpected errors of type:

```
[signal: killed]
FAIL: testscripts/myecho.txt:1: unexpected command failure
```

Fixes rogpeppe#200
  • Loading branch information
bep committed May 5, 2023
1 parent 22b9127 commit 13809f4
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ go 1.19

require (
golang.org/x/mod v0.9.0
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f
golang.org/x/tools v0.1.12
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
golang.org/x/mod v0.9.0 h1:KENHtAZL2y3NLMYZeHY9DW8HW8V+kQyJsY/V9JlKvCs=
golang.org/x/mod v0.9.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.1.12 h1:VveCTK38A2rkS8ZqFY25HIDFscX5X9OoEhJd3quQmXU=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
12 changes: 12 additions & 0 deletions testscript/clonefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//go:build unix && !darwin
// +build unix,!darwin

package testscript

import "golang.org/x/sys/unix"

// cloneFile creates to as a hard link to the from file.
// If there is an error, it will be of type *LinkError.
func cloneFile(from, to string) error {
return os.Link(from, to)
}
8 changes: 8 additions & 0 deletions testscript/clonefile_darwin.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package testscript

import "golang.org/x/sys/unix"

// cloneFile clones the file from to the file to.
func cloneFile(from, to string) error {
return unix.Clonefile(from, to, 0)
}
11 changes: 11 additions & 0 deletions testscript/clonefile_other.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build !unix
// +build !unix

package testscript

import "os"

// We don't want to use hard links on Windows, as that can lead to "access denied" errors when removing.
func cloneFile(from, to string) error {
return fmt.Errorf("unavailable")
}
6 changes: 2 additions & 4 deletions testscript/exe.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,8 @@ func RunMain(m TestingM, commands map[string]func() int) (exitCode int) {
// system's temporary directory, like we do. We don't use hard links on Windows,
// as that can lead to "access denied" errors when removing.
func copyBinary(from, to string) error {
if runtime.GOOS != "windows" {
if err := os.Link(from, to); err == nil {
return nil
}
if err := cloneFile(from, to); err == nil {
return nil
}
writer, err := os.OpenFile(to, os.O_WRONLY|os.O_CREATE, 0o777)
if err != nil {
Expand Down

0 comments on commit 13809f4

Please sign in to comment.