mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-09-01 15:46:23 -07:00
41 lines
921 B
Go
41 lines
921 B
Go
package testutil
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
const (
|
|
numParentDirsBeforeFail = 6
|
|
)
|
|
|
|
// PanicIfNotTest panics if the function was called directly or indirectly by a test function via go test
|
|
func PanicIfNotTest() {
|
|
if !strings.HasSuffix(os.Args[0], ".test") && !strings.HasSuffix(os.Args[0], ".test.exe") && os.Args[1] != "-test.run" {
|
|
panic("the testutil package should only be used in tests")
|
|
}
|
|
}
|
|
|
|
// GoToGochanRoot gets the
|
|
func GoToGochanRoot(t *testing.T) (string, error) {
|
|
t.Helper()
|
|
|
|
var dir string
|
|
var err error
|
|
for d := 0; d < numParentDirsBeforeFail; d++ {
|
|
dir, err = os.Getwd()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if filepath.Base(dir) == "gochan" {
|
|
return dir, nil
|
|
}
|
|
if err = os.Chdir(".."); err != nil {
|
|
return dir, err
|
|
}
|
|
}
|
|
return dir, errors.New("test running from unexpected dir, should be in gochan root or the current testing dir")
|
|
}
|