69 строки
1,6 КиБ
Go
69 строки
1,6 КиБ
Go
package godog_and_gomega
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"runtime"
|
|
"strings"
|
|
|
|
"git.golang1.ru/softonik/godog"
|
|
. "github.com/onsi/gomega"
|
|
)
|
|
|
|
const CALL_STACK_DEPTH_TO_TEST_FILE = 5
|
|
|
|
var TestResult error
|
|
|
|
func InitializeGomegaForGodog(ctx *godog.ScenarioContext) {
|
|
ctx.StepContext().Before(func(ctx context.Context, st *godog.Step) (context.Context, error) {
|
|
TestResult = nil
|
|
return ctx, nil
|
|
})
|
|
ctx.StepContext().After(func(ctx context.Context, st *godog.Step, status godog.StepResultStatus, err error) (context.Context, error) {
|
|
return ctx, TestResult
|
|
})
|
|
RegisterFailHandler(func(message string, callerSkip ...int) {
|
|
if TestResult != nil {
|
|
return
|
|
}
|
|
|
|
TestResult = errors.Join(TestResult,
|
|
errors.New(message),
|
|
errors.New(""),
|
|
errors.New(" "+getTestAssertPlace()),
|
|
)
|
|
})
|
|
}
|
|
|
|
func getTestAssertPlace() string {
|
|
place := getTestAssertFullPathPlace()
|
|
return getProjectRootRelativeTestAssertPlace(place)
|
|
}
|
|
func getTestAssertFullPathPlace() string {
|
|
_, file, line, _ := runtime.Caller(CALL_STACK_DEPTH_TO_TEST_FILE)
|
|
return fmt.Sprintf("%v:%v", file, line)
|
|
}
|
|
func getProjectRootRelativeTestAssertPlace(place string) string {
|
|
project_dir, err := findGoModDir(place)
|
|
if err != nil {
|
|
return place
|
|
}
|
|
|
|
return strings.ReplaceAll(place, project_dir+"/", "")
|
|
}
|
|
func findGoModDir(startPath string) (string, error) {
|
|
dir := filepath.Dir(startPath)
|
|
|
|
if _, err := os.Stat(filepath.Join(dir, "go.mod")); err == nil {
|
|
return dir, nil
|
|
}
|
|
|
|
if startPath == dir {
|
|
return "", errors.New("go.mod not found")
|
|
}
|
|
|
|
return findGoModDir(dir)
|
|
}
|