package tests import ( "bufio" "fmt" "io" "os/exec" "slices" "strings" "testing" ) func runner(commands []string, t *testing.T) []string { executablePath := "../tmp/godb" t.Logf("starting godb from %v\n", executablePath) executable := exec.Command(executablePath) results := make([]string, 0) stdin, err := executable.StdinPipe() if err != nil { t.Errorf("Failed to create stdin %q\n", err) } stdout, err := executable.StdoutPipe() if err != nil { t.Errorf("Failed to create stdout %q\n", err) } if err := executable.Start(); err != nil { t.Errorf("Failed to start executable %q\n", err) } go func() { defer stdin.Close() for _, c := range commands { _, _ = io.WriteString(stdin, c+"\n") } }() reader := bufio.NewReader(stdout) for { line, err := reader.ReadString('\n') if line != "" { formattedline := fmt.Sprintf("%v", strings.Trim(strings.ReplaceAll(line, "\x00", ""), "\n")) results = append(results, formattedline) } if err != nil { t.Logf("stoped reading due to %q\n", err) break } } if err := executable.Wait(); err != nil { t.Logf("Error occured while waiting for cleanup %q\n", err) } return results } func TestMain(t *testing.T) { commands := []string{ "insert 1 user1 person1@example.com", "select", ".exit", } expected := []string{ "godb > Executed.", "godb > {1 user1 person1@example.com}", "Executed.", "godb > bye!", } results := runner(commands, t) for i := range results { results[i] = strings.ReplaceAll(results[i], "\n", "") } if !slices.Equal(expected, results) { t.Errorf("Output Mismatch\nExpected: %v\nGot: %v\n", expected, results) } }