diff --git a/clonefile.go b/clonefile.go new file mode 100644 index 00000000..dd2467d4 --- /dev/null +++ b/clonefile.go @@ -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) +} diff --git a/clonefile_darwin.go b/clonefile_darwin.go new file mode 100644 index 00000000..c0e7a9e6 --- /dev/null +++ b/clonefile_darwin.go @@ -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) +} diff --git a/clonefile_other.go b/clonefile_other.go new file mode 100644 index 00000000..5682a927 --- /dev/null +++ b/clonefile_other.go @@ -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") +} diff --git a/exe.go b/exe.go index ed6bd98d..475ab70f 100644 --- a/exe.go +++ b/exe.go @@ -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 { diff --git a/go.mod b/go.mod index 848838e4..0bd85f6b 100644 --- a/go.mod +++ b/go.mod @@ -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 +) diff --git a/go.sum b/go.sum index 223c7faa..b13d5c28 100644 --- a/go.sum +++ b/go.sum @@ -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=