package main import ( "os" "path/filepath" "strings" "testing" ) func TestRunDiff(t *testing.T) { oldS, oldSP, oldSC, oldT, oldTP, oldTC, oldF, oldO := sourceType, sourcePath, sourceConn, targetType, targetPath, targetConn, outputFormat, outputPath t.Cleanup(func() { sourceType, sourcePath, sourceConn, targetType, targetPath, targetConn, outputFormat, outputPath = oldS, oldSP, oldSC, oldT, oldTP, oldTC, oldF, oldO }) src := filepath.Join(fixturesDir, "dbml/simple.dbml") cmplx := filepath.Join(fixturesDir, "dbml/complex.dbml") for _, format := range []string{"summary", "json", "html"} { t.Run(format, func(t *testing.T) { sourceType, sourcePath, sourceConn = "dbml", src, "" targetType, targetPath, targetConn = "dbml", cmplx, "" outputFormat = format outputPath = filepath.Join(t.TempDir(), "diff.out") if format == "summary" { outputPath = "" } if err := runDiff(nil, nil); err != nil { t.Fatalf("runDiff: %v", err) } if outputPath != "" { if b, err := os.ReadFile(outputPath); err != nil || len(b) == 0 { t.Errorf("empty output: %v", err) } } }) } t.Run("bad source", func(t *testing.T) { sourceType, sourcePath = "dbml", filepath.Join(t.TempDir(), "missing.dbml") targetType, targetPath = "dbml", src outputFormat, outputPath = "summary", "" if err := runDiff(nil, nil); err == nil || !strings.Contains(err.Error(), "failed to read source database") { t.Errorf("got %v", err) } }) t.Run("bad target", func(t *testing.T) { sourceType, sourcePath = "dbml", src targetType, targetPath = "dbml", filepath.Join(t.TempDir(), "missing.dbml") outputFormat, outputPath = "summary", "" if err := runDiff(nil, nil); err == nil || !strings.Contains(err.Error(), "failed to read target database") { t.Errorf("got %v", err) } }) } func TestRunInspect(t *testing.T) { oldT, oldP, oldC, oldR, oldF, oldO, oldS := inspectSourceType, inspectSourcePath, inspectSourceConn, inspectRulesPath, inspectOutputFormat, inspectOutputPath, inspectSchemaFilter t.Cleanup(func() { inspectSourceType, inspectSourcePath, inspectSourceConn, inspectRulesPath, inspectOutputFormat, inspectOutputPath, inspectSchemaFilter = oldT, oldP, oldC, oldR, oldF, oldO, oldS }) inspectSourceType = "dbml" inspectSourcePath = filepath.Join(fixturesDir, "dbml/simple.dbml") inspectSourceConn = "" inspectRulesPath = filepath.Join(t.TempDir(), "no-rules.yaml") // missing: defaults used or error inspectSchemaFilter = "" // Whatever the rules outcome, the run must not panic; formats are exercised. for _, format := range []string{"markdown", "json"} { inspectOutputFormat = format inspectOutputPath = filepath.Join(t.TempDir(), "report."+format) _ = runInspect(nil, nil) } inspectOutputFormat = "bogus" inspectOutputPath = "" if err := runInspect(nil, nil); err == nil { t.Error("bogus output format must fail") } inspectSourcePath = filepath.Join(t.TempDir(), "missing.dbml") if err := runInspect(nil, nil); err == nil || !strings.Contains(err.Error(), "failed to read source") { t.Errorf("missing source: %v", err) } }