1
1
mirror of https://github.com/wader/fq.git synced 2024-12-29 00:22:38 +03:00
fq/pkg/fqtest/fqtest.go
Mattias Wadman ca68e6a1e4 interp: Add Platform() method to OS interface
Felt bad that main should have to know about it
2022-01-29 11:31:36 +01:00

57 lines
1.2 KiB
Go

package fqtest
import (
"context"
"io/ioutil"
"os"
"strconv"
"testing"
"github.com/wader/fq/internal/difftest"
"github.com/wader/fq/internal/script"
"github.com/wader/fq/pkg/interp"
"github.com/wader/fq/pkg/registry"
)
func TestPath(t *testing.T, registry *registry.Registry) {
difftest.TestWithOptions(t, difftest.Options{
Path: ".",
Pattern: "*.fqtest",
ColorDiff: os.Getenv("DIFF_COLOR") != "",
WriteOutput: os.Getenv("WRITE_ACTUAL") != "",
Fn: func(t *testing.T, path, input string) (string, string, error) {
b, err := ioutil.ReadFile(path)
if err != nil {
t.Fatal(err)
}
c := script.ParseCases(string(b))
c.Path = path
for _, p := range c.Parts {
cr, ok := p.(*script.CaseRun)
if !ok {
continue
}
t.Run(strconv.Itoa(cr.LineNr)+"/"+cr.Command, func(t *testing.T) {
cr.WasRun = true
i, err := interp.New(cr, registry)
if err != nil {
t.Fatal(err)
}
err = i.Main(context.Background(), cr.Stdout(), "testversion")
if err != nil {
if ex, ok := err.(interp.Exiter); ok { //nolint:errorlint
cr.ActualExitCode = ex.ExitCode()
}
}
})
}
return path, c.ToActual(), nil
},
})
}