package database import ( "regexp" "testing" ) // TestParseTableNamesIgnoresComments guards against phantom tables parsed out // of SQL comments (e.g. "-- ... declared in CREATE TABLE above" once // registered a table named "above" and crashed startup verification). func TestParseTableNamesIgnoresComments(t *testing.T) { tables, err := parseTableNames() if err != nil { t.Fatalf("parseTableNames() error: %v", err) } if len(tables) == 0 { t.Fatal("parseTableNames() returned no tables") } for _, name := range tables { // Every parsed table must correspond to a real CREATE TABLE statement // at the start of a (non-comment) line. stmt := regexp.MustCompile(`(?m)^CREATE TABLE (?:IF NOT EXISTS )?(?:\w+\.)?` + regexp.QuoteMeta(name) + `\s`) if !stmt.MatchString(SchemaFile) { t.Errorf("parseTableNames() returned phantom table %q with no matching CREATE TABLE statement", name) } } }