Files
fspl/compiler/common_test.go
2024-02-27 02:36:35 -05:00

145 lines
3.5 KiB
Go

package compiler
import "io/fs"
import "embed"
import "strings"
import "testing"
import "os/exec"
import "path/filepath"
import "git.tebibyte.media/fspl/fspl/entity"
import "git.tebibyte.media/fspl/fspl/errors"
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 (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,
clangArgs []string,
stdin, stdout string,
exit int,
args ...string,
) {
// create temporary directory
temp := test.TempDir()
// instantiate and configure the compiler
compOutputBuilder := new(strings.Builder)
comp := defaultCompiler()
defer func () {
test.Log("COMPILER LOG (main unit):\n" + compOutputBuilder.String())
} ()
comp.Writer = compOutputBuilder
comp.Output = filepath.Join(temp, "output.o")
// compile to object file
err := comp.CompileUnit(address)
if err != nil {
test.Fatal("compiler returned error:", errors.Format(err))
}
// link the object file into an executable
executablePath := filepath.Join(temp, "output")
linkCommand := exec.Command("clang", append (
clangArgs,
comp.Output,
"-o",
executablePath)...)
linkCommand.Stdout = compOutputBuilder
linkCommand.Stderr = compOutputBuilder
test.Log("running link command: ", linkCommand)
err = linkCommand.Run()
if err != nil {
test.Fatal("error linking executable:", err)
}
// run the executable file and check its output
executableCommand := exec.Command(executablePath, args...)
stdoutBuilder := new(strings.Builder)
executableCommand.Stdin = strings.NewReader(stdin)
executableCommand.Stdout = stdoutBuilder
test.Log("running executable command: ", executableCommand)
err = executableCommand.Run()
// check error, compare exit code
if exitErr, ok := err.(*exec.ExitError); ok {
code := exitErr.ExitCode()
if code != exit {
test.Errorf (
"expecting exit code %d, got %d",
exit, code)
}
} else if err != nil {
test.Fatalf("error running %s: %v", executablePath, err)
} else {
if 0 != exit {
test.Errorf("expecting exit code %d, got 0", exit)
}
}
// compare stdout
gotStdout := stdoutBuilder.String()
if gotStdout != stdout {
testcommon.CompareHex(test, stdout, gotStdout)
test.Fail()
}
}
func TestEmbedOk (test *testing.T) {
err := fs.WalkDir(testData, ".", func (path string, d fs.DirEntry, err error) error {
test.Log("file:", path)
return err
})
if err != nil {
test.Error("walk dir failed: ", err)
}
}