q/tests/testRun_test.go
Eduard Urbach 7a294061d6
All checks were successful
/ test (push) Successful in 15s
Added more tests
2025-07-05 12:14:34 +02:00

61 lines
No EOL
1.3 KiB
Go

package tests_test
import (
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"git.urbach.dev/cli/q/src/build"
"git.urbach.dev/cli/q/src/compiler"
"git.urbach.dev/cli/q/src/linker"
"git.urbach.dev/go/assert"
)
type testRun struct {
Name string
Input string
Output string
ExitCode int
}
// Run builds and runs the file to check if the output matches the expected output.
func (test *testRun) Run(t *testing.T, path string) {
t.Run(test.Name, func(t *testing.T) {
b := build.New(path)
env, err := compiler.Compile(b)
assert.Nil(t, err)
tmpDir := filepath.Join(os.TempDir(), "q", "tests")
err = os.MkdirAll(tmpDir, 0o755)
assert.Nil(t, err)
executable := b.Executable()
executable = filepath.Join(tmpDir, filepath.Base(executable))
err = linker.WriteFile(executable, b, env)
assert.Nil(t, err)
stat, err := os.Stat(executable)
assert.Nil(t, err)
assert.True(t, stat.Size() > 0)
cmd := exec.Command(executable)
cmd.Stdin = strings.NewReader(test.Input)
output, err := cmd.Output()
exitCode := 0
if err != nil {
exitError, ok := err.(*exec.ExitError)
if !ok {
t.Fatal(exitError)
}
exitCode = exitError.ExitCode()
}
assert.Equal(t, exitCode, test.ExitCode)
assert.DeepEqual(t, string(output), test.Output)
})
}