(t *testing.T)
| 14 | var updateSurface = flag.Bool("update-surface", false, "Update the .surface baseline file") |
| 15 | |
| 16 | func TestSurfaceSnapshot(t *testing.T) { |
| 17 | root := newRootCmd() |
| 18 | |
| 19 | current := collectSurface(root, "") |
| 20 | sort.Strings(current) |
| 21 | snapshot := strings.Join(current, "\n") + "\n" |
| 22 | |
| 23 | baselineFile := "../../.surface" |
| 24 | baseline, err := os.ReadFile(baselineFile) |
| 25 | if os.IsNotExist(err) { |
| 26 | if *updateSurface { |
| 27 | if err := os.WriteFile(baselineFile, []byte(snapshot), 0644); err != nil { |
| 28 | t.Fatalf("writing baseline: %v", err) |
| 29 | } |
| 30 | t.Log("Surface baseline created at .surface") |
| 31 | return |
| 32 | } |
| 33 | t.Fatal("No .surface baseline found. Run with -update-surface to create it.") |
| 34 | } |
| 35 | if err != nil { |
| 36 | t.Fatalf("reading baseline: %v", err) |
| 37 | } |
| 38 | |
| 39 | baselineLines := strings.Split(strings.TrimSpace(string(baseline)), "\n") |
| 40 | sort.Strings(baselineLines) |
| 41 | |
| 42 | currentSet := map[string]bool{} |
| 43 | for _, line := range current { |
| 44 | currentSet[line] = true |
| 45 | } |
| 46 | baselineSet := map[string]bool{} |
| 47 | for _, line := range baselineLines { |
| 48 | baselineSet[line] = true |
| 49 | } |
| 50 | |
| 51 | var removed []string |
| 52 | for _, line := range baselineLines { |
| 53 | if !currentSet[line] { |
| 54 | removed = append(removed, line) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | var added []string |
| 59 | for _, line := range current { |
| 60 | if !baselineSet[line] { |
| 61 | added = append(added, line) |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | if len(removed) > 0 { |
| 66 | t.Errorf("Surface compatibility break — removed commands/flags:\n%s", |
| 67 | strings.Join(removed, "\n")) |
| 68 | } |
| 69 | |
| 70 | if len(added) > 0 { |
| 71 | if *updateSurface { |
| 72 | if err := os.WriteFile(baselineFile, []byte(snapshot), 0644); err != nil { |
| 73 | t.Fatalf("writing updated baseline: %v", err) |
nothing calls this directly
no test coverage detected