Tests can now compile dependencies first

This commit is contained in:
Sasha Koshka 2024-02-26 23:32:40 -05:00
parent 785a77b21b
commit cc2b1315a0

View File

@ -13,6 +13,52 @@ import "git.tebibyte.media/fspl/fspl/testcommon"
//go:embed all:test-data/*
var testData embed.FS
func defaultCompiler () *Compiler {
// instantiate and configure the compiler
comp := new(Compiler)
comp.Prefix = "compiler"
comp.Resolver = NewResolver (
"/test-data/usr/local/src/fspl",
"/test-data/usr/src/fspl",
"/test-data/usr/local/incude/fspl",
"/test-data/usr/include/fspl")
comp.Resolver.FS = testData
comp.Format = ".o"
comp.Debug = true
return comp
}
func compileDependency (
test *testing.T,
address entity.Address,
) string {
// create temporary directory
temp := test.TempDir()
// instantiate and configure the compiler
compOutputBuilder := new(strings.Builder)
comp := defaultCompiler()
defer func () {
test.Logf (
"compiler log for dependency %s:\n%s",
address, compOutputBuilder)
} ()
comp.Writer = compOutputBuilder
nickname, ok := address.Nickname()
if !ok {
test.Fatal("could not generate nickname for", address)
}
comp.Output = filepath.Join(temp, nickname)
// compile to object file
err := comp.CompileUnit(address)
if err != nil {
test.Fatal("compiler returned error:", errors.Format(err))
}
return comp.Output
}
func testUnit (
test *testing.T,
address entity.Address,
@ -26,27 +72,18 @@ func testUnit (
// instantiate and configure the compiler
compOutputBuilder := new(strings.Builder)
comp := defaultCompiler()
defer func () {
test.Log("compiler log:\n" + compOutputBuilder.String())
} ()
comp := new(Compiler)
comp.Writer = compOutputBuilder
comp.Prefix = "compiler"
comp.Resolver = NewResolver (
"/test-data/usr/local/src/fspl",
"/test-data/usr/src/fspl",
"/test-data/usr/local/incude/fspl",
"/test-data/usr/include/fspl")
comp.Resolver.FS = testData
comp.Output = filepath.Join(temp, "output.o")
comp.Format = ".o"
comp.Debug = true
comp.Writer = compOutputBuilder
comp.Output = filepath.Join(temp, "output.o")
// compile to object file
err := comp.CompileUnit(address)
if err != nil {
test.Error("compiler returned error:", errors.Format(err))
return
test.Fatal("compiler returned error:", errors.Format(err))
}
// link the object file into an executable
@ -61,8 +98,7 @@ func testUnit (
test.Log("running link command: ", linkCommand)
err = linkCommand.Run()
if err != nil {
test.Error("error linking executable:", err)
return
test.Fatal("error linking executable:", err)
}
// run the executable file and check its output
@ -82,8 +118,7 @@ func testUnit (
exit, code)
}
} else if err != nil {
test.Errorf("error running %s: %v", executablePath, err)
return
test.Fatalf("error running %s: %v", executablePath, err)
} else {
if 0 != exit {
test.Errorf("expecting exit code %d, got 0", exit)
@ -94,6 +129,7 @@ func testUnit (
gotStdout := stdoutBuilder.String()
if gotStdout != stdout {
testcommon.CompareHex(test, stdout, gotStdout)
test.Fail()
}
}