generate: Fix testGenerateRun so that it actually works
This commit is contained in:
parent
a9d5bb83a2
commit
c70c23d137
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
||||
/generate/test
|
@ -7,10 +7,14 @@ import "testing"
|
||||
import "path/filepath"
|
||||
|
||||
func testGenerateRun(test *testing.T, protocol *Protocol, imports string, testCase string) {
|
||||
// open files
|
||||
dir, err := os.MkdirTemp(os.TempDir(), "hopp-generate-test-*")
|
||||
// reset data directory
|
||||
dir := "test/generate-run"
|
||||
err := os.RemoveAll(dir)
|
||||
if err != nil { test.Fatal(err) }
|
||||
defer os.RemoveAll(dir)
|
||||
err = os.MkdirAll(dir, 0750)
|
||||
if err != nil { test.Fatal(err) }
|
||||
|
||||
// open files
|
||||
sourceFile, err := os.Create(filepath.Join(dir, "protocol.go"))
|
||||
if err != nil { test.Fatal(err) }
|
||||
defer sourceFile.Close()
|
||||
@ -29,15 +33,37 @@ func testGenerateRun(test *testing.T, protocol *Protocol, imports string, testCa
|
||||
// build static source files
|
||||
imports = `
|
||||
import "log"
|
||||
import "bytes"
|
||||
import "slices"
|
||||
import "git.tebibyte.media/sashakoshka/hopp/tape"
|
||||
` + imports
|
||||
setup := `log.Println("*** BEGIN TEST CASE OUTPUT ***")`
|
||||
teardown := `log.Println("--- END TEST CASE OUTPUT ---")`
|
||||
static := `
|
||||
func testEncode(message Message, correct ...byte) {
|
||||
buffer := bytes.Buffer { }
|
||||
encoder := tape.NewEncoder(&buffer)
|
||||
n, err := message.Encode(encoder)
|
||||
if err != nil { log.Fatalf("at %d: %v\n", n, err) }
|
||||
got := buffer.Bytes()
|
||||
if n != len(got) {
|
||||
log.Fatalln("len incorrect: %d != %d", got, correct)
|
||||
}
|
||||
if !slices.Equal(got, correct) {
|
||||
log.Fatalln("not equal:")
|
||||
}
|
||||
}
|
||||
`
|
||||
fmt.Fprintf(
|
||||
mainFile, "package main\n%s\nfunc main() {%s\n%s\n%s\n}\n",
|
||||
imports, setup, testCase, teardown)
|
||||
mainFile, "package main\n%s\nfunc main() {\n%s\n%s\n%s\n}\n%s",
|
||||
imports, setup, testCase, teardown, static)
|
||||
|
||||
// build and run test
|
||||
command := exec.Command("go", "run", filepath.Join(dir, "main.go"))
|
||||
command := exec.Command("go", "run", "./generate/test/generate-run")
|
||||
workingDirAbs, err := filepath.Abs("..")
|
||||
if err != nil { test.Fatal(err) }
|
||||
command.Dir = workingDirAbs
|
||||
command.Env = os.Environ()
|
||||
output, err := command.CombinedOutput()
|
||||
test.Logf("output of %v:\n%s", command, output)
|
||||
if err != nil { test.Fatal(err) }
|
||||
|
Loading…
Reference in New Issue
Block a user