2021-10-20 21:56:27 -03:00
|
|
|
package testlib
|
|
|
|
|
|
|
|
import (
|
2024-11-16 10:57:48 -03:00
|
|
|
"fmt"
|
2021-10-20 21:56:27 -03:00
|
|
|
"os"
|
|
|
|
"os/exec"
|
2024-11-16 10:30:39 -03:00
|
|
|
"runtime"
|
2021-10-20 21:56:27 -03:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2024-09-03 20:39:33 -03:00
|
|
|
// CheckPath skips the test if the binary is not in the PATH, or if CI is true.
|
2021-10-20 21:56:27 -03:00
|
|
|
func CheckPath(tb testing.TB, cmd string) {
|
|
|
|
tb.Helper()
|
2024-09-03 20:39:33 -03:00
|
|
|
if !InPath(cmd) {
|
2021-10-20 21:56:27 -03:00
|
|
|
tb.Skipf("%s not in PATH", cmd)
|
|
|
|
}
|
|
|
|
}
|
2024-09-03 20:39:33 -03:00
|
|
|
|
|
|
|
// InPath returns true if the given cmd is in the PATH, or if CI is true.
|
|
|
|
func InPath(cmd string) bool {
|
|
|
|
if os.Getenv("CI") == "true" {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
_, err := exec.LookPath(cmd)
|
|
|
|
return err == nil
|
|
|
|
}
|
2024-11-16 10:30:39 -03:00
|
|
|
|
|
|
|
// IsWindows returns true if current OS is Windows.
|
|
|
|
func IsWindows() bool { return runtime.GOOS == "windows" }
|
|
|
|
|
|
|
|
// SkipIfWindows skips the test if runtime OS is windows.
|
2024-11-16 10:57:48 -03:00
|
|
|
func SkipIfWindows(tb testing.TB, args ...any) {
|
2024-11-16 10:30:39 -03:00
|
|
|
tb.Helper()
|
|
|
|
if IsWindows() {
|
2024-11-16 10:57:48 -03:00
|
|
|
tb.Skip(args...)
|
2024-11-16 10:30:39 -03:00
|
|
|
}
|
|
|
|
}
|
2024-11-16 10:57:48 -03:00
|
|
|
|
|
|
|
// Echo returns a `echo s` command, handling it on windows.
|
|
|
|
func Echo(s string) string {
|
|
|
|
if IsWindows() {
|
|
|
|
return "cmd.exe /c echo " + s
|
|
|
|
}
|
|
|
|
return "echo " + s
|
|
|
|
}
|
|
|
|
|
|
|
|
// Touch returns a `touch name` command, handling it on windows.
|
|
|
|
func Touch(name string) string {
|
|
|
|
if IsWindows() {
|
|
|
|
return "cmd.exe /c copy nul " + name
|
|
|
|
}
|
|
|
|
return "touch " + name
|
|
|
|
}
|
|
|
|
|
|
|
|
// ShC returns the command line for the given cmd wrapped into a `sh -c` in
|
|
|
|
// linux/mac, and the cmd.exe command in windows.
|
|
|
|
func ShC(cmd string) string {
|
|
|
|
if IsWindows() {
|
|
|
|
return fmt.Sprintf("cmd.exe /c '%s'", cmd)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("sh -c '%s'", cmd)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exit returns a command that exits the given status, handling windows.
|
|
|
|
func Exit(status int) string {
|
|
|
|
if IsWindows() {
|
|
|
|
return fmt.Sprintf("cmd.exe /c exit /b %d", status)
|
|
|
|
}
|
|
|
|
return fmt.Sprintf("exit %d", status)
|
|
|
|
}
|