|
| 1 | +package errors |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "path/filepath" |
| 6 | + "regexp" |
| 7 | + "runtime" |
| 8 | + "strings" |
| 9 | + "testing" |
| 10 | +) |
| 11 | + |
| 12 | +// codeLiteral matches a CLI-minted error-code string literal: either a `Code:` |
| 13 | +// struct field or a `code =` assignment (the two forms the CLI uses to |
| 14 | +// synthesize a code). API codes relayed from a response come from a variable |
| 15 | +// (apiErr.Code), not a literal, so they do not match. |
| 16 | +var codeLiteral = regexp.MustCompile(`(?:\bCode:\s*|\bcode\s*=\s*)"([a-z][a-z0-9_]*)"`) |
| 17 | + |
| 18 | +func registrySet(t *testing.T) map[string]string { |
| 19 | + t.Helper() |
| 20 | + m := map[string]string{} |
| 21 | + add := func(codes []string, bucket string) { |
| 22 | + for _, c := range codes { |
| 23 | + if prev, dup := m[c]; dup { |
| 24 | + t.Fatalf("code %q registered in both %q and %q", c, prev, bucket) |
| 25 | + } |
| 26 | + m[c] = bucket |
| 27 | + } |
| 28 | + } |
| 29 | + add(cliPrefixedCodes, "cliPrefixed") |
| 30 | + add(grandfatheredBareCodes, "grandfatheredBare") |
| 31 | + add(bareAPISemanticCodes, "bareAPISemantic") |
| 32 | + return m |
| 33 | +} |
| 34 | + |
| 35 | +// TestCLICodePartition enforces the namespace invariants on the registry. |
| 36 | +func TestCLICodePartition(t *testing.T) { |
| 37 | + for _, c := range cliPrefixedCodes { |
| 38 | + if !strings.HasPrefix(c, CLICodePrefix) { |
| 39 | + t.Errorf("cliPrefixedCodes: %q must start with %q", c, CLICodePrefix) |
| 40 | + } |
| 41 | + } |
| 42 | + for _, c := range append(append([]string{}, grandfatheredBareCodes...), bareAPISemanticCodes...) { |
| 43 | + if strings.HasPrefix(c, CLICodePrefix) { |
| 44 | + t.Errorf("bare code %q must not start with %q (only cliPrefixedCodes may)", c, CLICodePrefix) |
| 45 | + } |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +// TestBareAllowlistsAreFrozen pins the exact contents of the two bare sets. |
| 50 | +// The reserved-prefix guarantee relies on these being CLOSED: without this, |
| 51 | +// a new bare CLI code could bypass the cli_ rule by simply being appended to |
| 52 | +// grandfatheredBareCodes. Changing either set now requires editing this |
| 53 | +// expected list too, which surfaces the change in review. |
| 54 | +func TestBareAllowlistsAreFrozen(t *testing.T) { |
| 55 | + wantGrandfathered := []string{ |
| 56 | + "auth_error", "batch_not_supported", "canceled", "confirmation_required", |
| 57 | + "error", "file_exists", "network_error", "timeout", "usage_error", |
| 58 | + "video_failed", "video_not_ready", "wrong_install_method", |
| 59 | + } |
| 60 | + wantAPISemantic := []string{ |
| 61 | + "asset_not_available", "conflict", "forbidden", "insufficient_credit", |
| 62 | + "not_found", "payload_too_large", "rate_limit_exceeded", "unauthorized", |
| 63 | + "unclassified_client_error", "unclassified_server_error", "validation_error", |
| 64 | + } |
| 65 | + assertSetEqual(t, "grandfatheredBareCodes", grandfatheredBareCodes, wantGrandfathered) |
| 66 | + assertSetEqual(t, "bareAPISemanticCodes", bareAPISemanticCodes, wantAPISemantic) |
| 67 | +} |
| 68 | + |
| 69 | +func assertSetEqual(t *testing.T, name string, got, want []string) { |
| 70 | + t.Helper() |
| 71 | + gm := map[string]bool{} |
| 72 | + for _, c := range got { |
| 73 | + gm[c] = true |
| 74 | + } |
| 75 | + wm := map[string]bool{} |
| 76 | + for _, c := range want { |
| 77 | + wm[c] = true |
| 78 | + } |
| 79 | + for c := range gm { |
| 80 | + if !wm[c] { |
| 81 | + t.Errorf("%s: unexpected code %q — if this is a new bare CLI code, it likely must be cli_-prefixed instead; only add to the frozen set with explicit justification", name, c) |
| 82 | + } |
| 83 | + } |
| 84 | + for c := range wm { |
| 85 | + if !gm[c] { |
| 86 | + t.Errorf("%s: missing expected code %q", name, c) |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +// TestNoUnregisteredCLICodes scans production source for CLI-minted code |
| 92 | +// literals and fails on any that is not registered. A new CLI code therefore |
| 93 | +// must be added to the registry, and (unless grandfathered/API-semantic) must |
| 94 | +// carry the cli_ prefix, which is what keeps future codes collision-proof. |
| 95 | +func TestNoUnregisteredCLICodes(t *testing.T) { |
| 96 | + reg := registrySet(t) |
| 97 | + root := repoRoot(t) |
| 98 | + |
| 99 | + // Codes the CLI never originates: relayed from tests as API-response bodies, |
| 100 | + // or test-only fixtures. These are not CLI-minted, so exclude them. |
| 101 | + ignore := map[string]bool{} |
| 102 | + |
| 103 | + for _, dir := range []string{"cmd", "internal"} { |
| 104 | + err := filepath.WalkDir(filepath.Join(root, dir), func(path string, d os.DirEntry, err error) error { |
| 105 | + if err != nil { |
| 106 | + return err |
| 107 | + } |
| 108 | + if d.IsDir() || !strings.HasSuffix(path, ".go") || strings.HasSuffix(path, "_test.go") { |
| 109 | + return nil |
| 110 | + } |
| 111 | + src, err := os.ReadFile(path) |
| 112 | + if err != nil { |
| 113 | + return err |
| 114 | + } |
| 115 | + for _, m := range codeLiteral.FindAllStringSubmatch(string(src), -1) { |
| 116 | + code := m[1] |
| 117 | + if ignore[code] { |
| 118 | + continue |
| 119 | + } |
| 120 | + if _, ok := reg[code]; !ok { |
| 121 | + rel, _ := filepath.Rel(root, path) |
| 122 | + t.Errorf("unregistered CLI-minted code %q in %s: prefix it with %q and add it to cliPrefixedCodes in codes.go (or, if it mirrors an API code / is grandfathered, add it to the matching bare set)", code, rel, CLICodePrefix) |
| 123 | + } |
| 124 | + } |
| 125 | + return nil |
| 126 | + }) |
| 127 | + if err != nil { |
| 128 | + t.Fatalf("walk %s: %v", dir, err) |
| 129 | + } |
| 130 | + } |
| 131 | +} |
| 132 | + |
| 133 | +func repoRoot(t *testing.T) string { |
| 134 | + t.Helper() |
| 135 | + _, file, _, ok := runtime.Caller(0) |
| 136 | + if !ok { |
| 137 | + t.Fatal("runtime.Caller failed") |
| 138 | + } |
| 139 | + // this file lives at <root>/internal/errors/codes_test.go |
| 140 | + return filepath.Clean(filepath.Join(filepath.Dir(file), "..", "..")) |
| 141 | +} |
0 commit comments