Skip to content

Commit

Permalink
testscript: 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 authored and ldemailly committed May 7, 2023
1 parent b2b184e commit 6dc92e9
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 7 deletions.
11 changes: 11 additions & 0 deletions clonefile.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
//go:build unix && !darwin
// +build unix,!darwin

package testscript

import "os"

// cloneFile creates to as a hard link to the from file.
func cloneFile(from, to string) error {
return os.Link(from, to)
}
8 changes: 8 additions & 0 deletions 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 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 "fmt"

// 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 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
5 changes: 4 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,7 @@ module fortio.org/testscript

go 1.20

require golang.org/x/tools v0.7.0
require (
golang.org/x/sys v0.8.0
golang.org/x/tools v0.8.0
)
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
golang.org/x/tools v0.7.0 h1:W4OVu8VVOaIO0yzWMNdepAulS7YfoS3Zabrm8DOXXU4=
golang.org/x/tools v0.7.0/go.mod h1:4pg6aUX35JBAogB10C9AtvVL+qowtN4pT3CGSQex14s=
golang.org/x/sys v0.8.0 h1:EBmGv8NaZBZTWvrbjNoL6HVt+IVy3QDQpJs7VRIw3tU=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/tools v0.8.0 h1:vSDcovVPld282ceKgDimkRSC8kpaH1dgyc9UMzlt84Y=
golang.org/x/tools v0.8.0/go.mod h1:JxBZ99ISMI5ViVkT1tr6tdNmXeTrcpVSD3vZ1RsRdN4=

0 comments on commit 6dc92e9

Please sign in to comment.