Skip to content

Instantly share code, notes, and snippets.

@apstndb
Created September 12, 2024 15:42
Show Gist options
  • Save apstndb/db15a21c61ac9a2cfd396a3dcad0102a to your computer and use it in GitHub Desktop.
Save apstndb/db15a21c61ac9a2cfd396a3dcad0102a to your computer and use it in GitHub Desktop.
memefish cloud-spanner-emulator test runner
package main
import (
"context"
"encoding/csv"
"flag"
"fmt"
parser "github.com/cloudspannerecosystem/memefish"
"github.com/cloudspannerecosystem/memefish/token"
"log"
"os"
"strings"
)
const (
testCaseDelimiter = "=="
inputDelimiter = "--"
commentPrefix = "# "
regexFlag = "--regex"
)
func run(ctx context.Context) error {
file := flag.String("file", "", "")
flag.Parse()
b, err := os.ReadFile(*file)
if err != nil {
return err
}
csvWriter := csv.NewWriter(os.Stdout)
csvWriter.Write([]string{"filename", "no", "input", "expected", "err", "result", "comment"})
for i, testcase := range strings.Split(string(b), "\n"+testCaseDelimiter+"\n") {
if strings.TrimSpace(testcase) == "" {
continue
}
before, after, found := strings.Cut(testcase, "\n"+inputDelimiter+"\n")
shouldError := !found ||
strings.HasPrefix(after, "ERROR:") ||
strings.HasPrefix(after, "Cannot") ||
strings.HasPrefix(after, "Unsupported") ||
strings.HasSuffix(strings.TrimSpace(after), ".")
p := &parser.Parser{
Lexer: &parser.Lexer{
File: &token.File{FilePath: *file, Buffer: before},
},
}
_, err := p.ParseDDLs()
// if !shouldError {
// if err != nil {
// log.Printf("fail: %v: %v", err, before)
// continue
// }
// continue
// log.Printf("success: %v", before)
// } else {
// if err != nil {
// continue
// log.Printf("error as expected: %v", before)
// continue
// }
// log.Printf("should %v: %v", after, before)
// }
csvWriter.Write([]string{*file, fmt.Sprint(i), before, after, fmt.Sprint(err), fmt.Sprint(shouldError == (err != nil)), ""})
}
csvWriter.Flush()
return nil
}
func main() {
if err := run(context.Background()); err != nil {
log.Fatalln(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment