parity: table parser, row rules and page renderer
internal/parity reads parity/parity.tsv and parity/packages.tsv, checks the spec's C1 to C4 rules (unique ids, member shapes, per-status requirements, qualified shared members, known gap packages) and renders docs/parity.md. cmd/parity-doc writes the page and refuses while Check reports problems; make parity-doc runs it. Tests use fixtures only; the real table and its tests come in Task 3.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+1,185
−1
Makefile+6 −1internal/parity/cmd/parity-doc/main.go+42 −0internal/parity/page.go+145 −0internal/parity/page_test.go+140 −0internal/parity/rules.go+213 −0internal/parity/rules_test.go+182 −0internal/parity/table.go+284 −0internal/parity/table_test.go+173 −0
CI failed — run details
Intent
Attributed to this commit: 58.9 g CO₂e (5,210,968 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on design-parity-guard, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/Makefile b/Makefile| index 554ff8b03..6b353802d 100644 |
| --- a/Makefile |
| +++ b/Makefile |
| @@ -7,7 +7,7 @@ LDFLAGS := -s -w -X github.com/carlosframework/platform/internal/version.Build=$ |
| GOOS ?= |
| GOARCH ?= |
| -.PHONY: test build build-console fmt census-card census-propose console-mockup-kit |
| +.PHONY: test build build-console fmt census-card census-propose console-mockup-kit parity-doc |
| # The build gate: vet, format, tests. `.amadan/ci.d/10-gate` runs this same |
| # target, so what CI checks and what you check are one definition. |
| @@ -38,6 +38,11 @@ fmt: |
| console-mockup-kit: |
| CONSOLE_MOCKUP_KIT_DIR="$(CURDIR)/docs/console-mockup-kit" go test ./internal/console/ -run '^TestConsoleMockupKit$$' -count=1 |
| +# Regenerates docs/parity.md from parity/parity.tsv and parity/packages.tsv. |
| +# It writes nothing while the table breaks a rule (internal/parity). |
| +parity-doc: |
| + go run ./internal/parity/cmd/parity-doc |
| + |
| # The root-read census as an approval (#369, AGENTS.md "The census"): |
| # census-card prints what changed and writes nothing; census-propose writes |
| # the overlay, its chain manifest line and the approval card. CLAIMS has |
diff --git a/internal/parity/cmd/parity-doc/main.go b/internal/parity/cmd/parity-doc/main.go| new file mode 100644 |
| index 000000000..a98014022 |
| --- /dev/null |
| +++ b/internal/parity/cmd/parity-doc/main.go |
| @@ -0,0 +1,42 @@ |
| +// Command parity-doc writes docs/parity.md from parity/parity.tsv and |
| +// parity/packages.tsv. It refuses to write while the table breaks a rule, |
| +// so the page never shows a table the tests would reject. |
| +// |
| +// Usage: parity-doc [-root dir] (make parity-doc) |
| +package main |
| + |
| +import ( |
| + "flag" |
| + "fmt" |
| + "os" |
| + "path/filepath" |
| + |
| + "github.com/carlosframework/platform/internal/parity" |
| +) |
| + |
| +func main() { |
| + root := flag.String("root", ".", "repository root holding parity/ and docs/") |
| + flag.Parse() |
| + if err := run(*root); err != nil { |
| + fmt.Fprintln(os.Stderr, "parity-doc:", err) |
| + os.Exit(1) |
| + } |
| +} |
| + |
| +func run(root string) error { |
| + rows, err := parity.Load(filepath.Join(root, "parity", "parity.tsv")) |
| + if err != nil { |
| + return err |
| + } |
| + pkgs, err := parity.LoadPackages(filepath.Join(root, "parity", "packages.tsv")) |
| + if err != nil { |
| + return err |
| + } |
| + if probs := parity.Check(rows, pkgs); len(probs) > 0 { |
| + for _, p := range probs { |
| + fmt.Fprintln(os.Stderr, p) |
| + } |
| + return fmt.Errorf("%d problem(s) in the table; docs/parity.md not written", len(probs)) |
| + } |
| + return os.WriteFile(filepath.Join(root, "docs", "parity.md"), parity.Render(rows, pkgs), 0o644) |
| +} |
diff --git a/internal/parity/page.go b/internal/parity/page.go| new file mode 100644 |
| index 000000000..3f2c692fd |
| --- /dev/null |
| +++ b/internal/parity/page.go |
| @@ -0,0 +1,145 @@ |
| +package parity |
| + |
| +import ( |
| + "bytes" |
| + "fmt" |
| + "sort" |
| + "strings" |
| +) |
| + |
| +// Render returns docs/parity.md for rows and pkgs. The output depends only |
| +// on its inputs: areas sorted, rows in file order, packages in file order. |
| +func Render(rows []Row, pkgs []Package) []byte { |
| + var b bytes.Buffer |
| + p := func(format string, args ...any) { fmt.Fprintf(&b, format, args...) } |
| + |
| + p("# CLI and console parity\n\n") |
| + p("This page is generated from `parity/parity.tsv` and `parity/packages.tsv` by `make parity-doc`. Edit the table, then regenerate the page.\n\n") |
| + |
| + // Counts, customer rows only. |
| + counts := map[Status]int{} |
| + total := 0 |
| + for _, r := range rows { |
| + if r.Status.Customer() { |
| + counts[r.Status]++ |
| + total++ |
| + } |
| + } |
| + p("## Counts\n\n") |
| + p("Customer rows only. Rows outside customer parity are listed at the end.\n\n") |
| + p("Commands below the top level are checked for shape only until slice 4's registry.\n\n") |
| + p("| Status | Rows |\n|---|---:|\n") |
| + for _, s := range customerStatuses { |
| + p("| %s | %d |\n", s, counts[s]) |
| + } |
| + p("| Total | %d |\n", total) |
| + |
| + // Customer rows by area. |
| + byArea := map[string][]Row{} |
| + var areas []string |
| + for _, r := range rows { |
| + if !r.Status.Customer() { |
| + continue |
| + } |
| + if _, ok := byArea[r.Area]; !ok { |
| + areas = append(areas, r.Area) |
| + } |
| + byArea[r.Area] = append(byArea[r.Area], r) |
| + } |
| + sort.Strings(areas) |
| + for _, a := range areas { |
| + p("\n## %s\n\n", a) |
| + p("| Row | Status | CLI | Console | Package | Note |\n|---|---|---|---|---|---|\n") |
| + for _, r := range byArea[a] { |
| + pkg := "" |
| + if r.Package != "" { |
| + pkg = fmt.Sprintf("[%s](#%s)", cell(r.Package), r.Package) |
| + } |
| + p("| %s | %s | %s | %s | %s | %s |\n", cell(r.ID), cell(string(r.Status)), members(r.CLI), members(r.Console), pkg, cell(r.Note)) |
| + } |
| + } |
| + |
| + // Work packages and the gap rows each closes. |
| + p("\n## Work packages\n") |
| + for _, pk := range pkgs { |
| + p("\n<a id=\"%s\"></a>\n\n### %s: %s\n\n", pk.ID, pk.ID, oneLine(pk.Title)) |
| + if pk.Discussion > 0 { |
| + p("Discussion #%d.\n\n", pk.Discussion) |
| + } else { |
| + p("No discussion filed yet.\n\n") |
| + } |
| + var gaps []Row |
| + for _, r := range rows { |
| + if r.Status == StatusGap && r.Package == pk.ID { |
| + gaps = append(gaps, r) |
| + } |
| + } |
| + if len(gaps) == 0 { |
| + p("No gap rows name this package.\n") |
| + continue |
| + } |
| + for _, r := range gaps { |
| + side, ms := "CLI", r.CLI |
| + if len(r.CLI) == 0 { |
| + side, ms = "console", r.Console |
| + } |
| + p("- `%s` (%s), %s has %s\n", r.ID, r.Area, side, plainMembers(ms)) |
| + } |
| + } |
| + |
| + // Rows outside customer parity, grouped by status. |
| + byStatus := map[Status][]Row{} |
| + var extra []Status |
| + for _, r := range rows { |
| + if r.Status.Customer() { |
| + continue |
| + } |
| + if !r.Status.known() { |
| + if _, ok := byStatus[r.Status]; !ok { |
| + extra = append(extra, r.Status) |
| + } |
| + } |
| + byStatus[r.Status] = append(byStatus[r.Status], r) |
| + } |
| + sort.Slice(extra, func(i, j int) bool { return extra[i] < extra[j] }) |
| + p("\n## Outside customer parity\n") |
| + for _, s := range append(append([]Status{}, outsideStatuses...), extra...) { |
| + rs := byStatus[s] |
| + if len(rs) == 0 { |
| + continue |
| + } |
| + p("\n### %s\n\n", cell(string(s))) |
| + p("| Row | Area | CLI | Console | Note |\n|---|---|---|---|---|\n") |
| + for _, r := range rs { |
| + p("| %s | %s | %s | %s | %s |\n", cell(r.ID), cell(r.Area), members(r.CLI), members(r.Console), cell(r.Note)) |
| + } |
| + } |
| + return b.Bytes() |
| +} |
| + |
| +// cell escapes text for a Markdown table cell. |
| +func cell(s string) string { |
| + return strings.ReplaceAll(oneLine(s), "|", `\|`) |
| +} |
| + |
| +func oneLine(s string) string { |
| + return strings.Join(strings.Fields(s), " ") |
| +} |
| + |
| +// members renders members as code spans for a table cell, one per line. |
| +func members(ms []Member) string { |
| + var parts []string |
| + for _, m := range ms { |
| + parts = append(parts, "`"+cell(m.String())+"`") |
| + } |
| + return strings.Join(parts, "<br>") |
| +} |
| + |
| +// plainMembers renders members as code spans for running text. |
| +func plainMembers(ms []Member) string { |
| + var parts []string |
| + for _, m := range ms { |
| + parts = append(parts, "`"+m.String()+"`") |
| + } |
| + return strings.Join(parts, ", ") |
| +} |
diff --git a/internal/parity/page_test.go b/internal/parity/page_test.go| new file mode 100644 |
| index 000000000..9fb418069 |
| --- /dev/null |
| +++ b/internal/parity/page_test.go |
| @@ -0,0 +1,140 @@ |
| +package parity |
| + |
| +import ( |
| + "bytes" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +func pageFixture(t *testing.T) ([]Row, []Package) { |
| + t.Helper() |
| + rows := mustParse(t, tsv( |
| + row("logs", "zeta", "both", "logs", "GET /a/{sqid}/apps/{app}/logs", "", "", "tail | follow"), |
| + row("apps-list", "apps", "both", "apps ; apps show", "GET /a/{sqid}/apps", "", "", ""), |
| + row("moves", "apps", "gap", "instances move [--region]", "", "wp-instances", "", ""), |
| + row("login", "auth", "auth", "login", "GET /login", "", "", "sign-in plumbing"), |
| + row("snap", "demo", "internal", "__demo-snapshot", "", "", "", "runs before the switch"), |
| + )) |
| + pkgs := []Package{ |
| + {ID: "wp-instances", Title: "Instances, including region moves", Discussion: 812, Line: 2}, |
| + {ID: "wp-settings", Title: "Small settings forms", Line: 3}, |
| + } |
| + return rows, pkgs |
| +} |
| + |
| +func TestRenderHeaderAndNotice(t *testing.T) { |
| + rows, pkgs := pageFixture(t) |
| + out := string(Render(rows, pkgs)) |
| + if !strings.HasPrefix(out, "# CLI and console parity\n\n") { |
| + t.Fatalf("page does not start with the title:\n%s", out) |
| + } |
| + notice := strings.SplitN(out, "\n", 4)[2] |
| + if !strings.Contains(notice, "parity/parity.tsv") || !strings.Contains(notice, "make parity-doc") { |
| + t.Fatalf("second paragraph %q is not the generated-file notice", notice) |
| + } |
| +} |
| + |
| +func TestRenderCountsCustomerOnly(t *testing.T) { |
| + rows, pkgs := pageFixture(t) |
| + out := string(Render(rows, pkgs)) |
| + counts := section(t, out, "## Counts") |
| + for _, want := range []string{"| both | 2 |", "| gap | 1 |", "| partial | 0 |", "| Total | 3 |"} { |
| + if !strings.Contains(counts, want) { |
| + t.Errorf("counts lack %q:\n%s", want, counts) |
| + } |
| + } |
| + if strings.Contains(counts, "auth") || strings.Contains(counts, "internal") { |
| + t.Errorf("counts include a non-customer status:\n%s", counts) |
| + } |
| + line := "Commands below the top level are checked for shape only until slice 4's registry." |
| + i, j := strings.Index(counts, line), strings.Index(counts, "| Status |") |
| + if i < 0 || j < 0 || i > j { |
| + t.Errorf("the slice 4 line is not above the counts table:\n%s", counts) |
| + } |
| +} |
| + |
| +func TestRenderAreas(t *testing.T) { |
| + rows, pkgs := pageFixture(t) |
| + out := string(Render(rows, pkgs)) |
| + ia, iz := strings.Index(out, "\n## apps\n"), strings.Index(out, "\n## zeta\n") |
| + if ia < 0 || iz < 0 || ia > iz { |
| + t.Fatalf("areas missing or not alphabetical:\n%s", out) |
| + } |
| + if strings.Contains(out, "\n## auth\n") || strings.Contains(out, "\n## demo\n") { |
| + t.Fatalf("an area with only non-customer rows got a section:\n%s", out) |
| + } |
| + apps := section(t, out, "## apps") |
| + il, im := strings.Index(apps, "| apps-list |"), strings.Index(apps, "| moves |") |
| + if il < 0 || im < 0 || il > im { |
| + t.Fatalf("apps rows missing or not in file order:\n%s", apps) |
| + } |
| + for _, want := range []string{"`apps`<br>`apps show`", "`GET /a/{sqid}/apps`", "`instances move [--region]`", "[wp-instances](#wp-instances)"} { |
| + if !strings.Contains(apps, want) { |
| + t.Errorf("apps section lacks %q:\n%s", want, apps) |
| + } |
| + } |
| + if zeta := section(t, out, "## zeta"); !strings.Contains(zeta, `tail \| follow`) { |
| + t.Errorf("pipe in note not escaped:\n%s", zeta) |
| + } |
| +} |
| + |
| +func TestRenderWorkPackages(t *testing.T) { |
| + rows, pkgs := pageFixture(t) |
| + wp := section(t, string(Render(rows, pkgs)), "## Work packages") |
| + if !strings.Contains(wp, `<a id="wp-instances"></a>`) || !strings.Contains(wp, `<a id="wp-settings"></a>`) { |
| + t.Fatalf("package anchors missing:\n%s", wp) |
| + } |
| + inst := wp[strings.Index(wp, `<a id="wp-instances">`):strings.Index(wp, `<a id="wp-settings">`)] |
| + if !strings.Contains(inst, "Instances, including region moves") || !strings.Contains(inst, "#812") || !strings.Contains(inst, "`moves`") { |
| + t.Errorf("wp-instances lacks title, discussion or its gap row:\n%s", inst) |
| + } |
| + settings := wp[strings.Index(wp, `<a id="wp-settings">`):] |
| + if strings.Contains(settings, "Discussion #") || !strings.Contains(settings, "No gap rows") { |
| + t.Errorf("wp-settings shows a discussion it does not have, or lists rows:\n%s", settings) |
| + } |
| +} |
| + |
| +func TestRenderOutsideCustomerParity(t *testing.T) { |
| + rows, pkgs := pageFixture(t) |
| + outside := section(t, string(Render(rows, pkgs)), "## Outside customer parity") |
| + ia, ii := strings.Index(outside, "### auth"), strings.Index(outside, "### internal") |
| + if ia < 0 || ii < 0 || ia > ii { |
| + t.Fatalf("outside groups missing or out of order:\n%s", outside) |
| + } |
| + if !strings.Contains(outside, "| login |") || !strings.Contains(outside, "`__demo-snapshot`") { |
| + t.Fatalf("outside rows missing:\n%s", outside) |
| + } |
| + if strings.Contains(outside, "### operator") { |
| + t.Fatalf("empty status group rendered:\n%s", outside) |
| + } |
| + if !strings.HasSuffix(string(Render(rows, pkgs)), outside) { |
| + t.Fatalf("outside customer parity is not the final section") |
| + } |
| +} |
| + |
| +func TestRenderDeterministic(t *testing.T) { |
| + rows, pkgs := pageFixture(t) |
| + a, b := Render(rows, pkgs), Render(rows, pkgs) |
| + if !bytes.Equal(a, b) { |
| + t.Fatal("two renders differ") |
| + } |
| + if !bytes.HasSuffix(a, []byte("\n")) || bytes.HasSuffix(a, []byte("\n\n")) { |
| + t.Fatal("page should end with exactly one newline") |
| + } |
| +} |
| + |
| +// section returns the text from heading to the next heading of the same |
| +// level, or the end. |
| +func section(t *testing.T, page, heading string) string { |
| + t.Helper() |
| + i := strings.Index(page, "\n"+heading+"\n") |
| + if i < 0 { |
| + t.Fatalf("no %q section in:\n%s", heading, page) |
| + } |
| + rest := page[i+1:] |
| + level := heading[:strings.Index(heading, " ")+1] |
| + if j := strings.Index(rest[len(heading):], "\n"+level); j >= 0 { |
| + return rest[:len(heading)+j+1] |
| + } |
| + return rest |
| +} |
diff --git a/internal/parity/rules.go b/internal/parity/rules.go| new file mode 100644 |
| index 000000000..3a73c7830 |
| --- /dev/null |
| +++ b/internal/parity/rules.go |
| @@ -0,0 +1,213 @@ |
| +package parity |
| + |
| +import ( |
| + "fmt" |
| + "regexp" |
| + "sort" |
| + "strings" |
| +) |
| + |
| +// Problem is one broken rule, reported against the row to fix. |
| +type Problem struct { |
| + Line int |
| + RowID, Msg string |
| +} |
| + |
| +// String names the file, line and row, then says what to fix. |
| +func (p Problem) String() string { |
| + return fmt.Sprintf("parity/parity.tsv:%d: row %s: %s", p.Line, p.RowID, p.Msg) |
| +} |
| + |
| +var ( |
| + cliWordRE = regexp.MustCompile(`^(__)?[a-z0-9][a-z0-9-]*$`) |
| + consoleMethod = map[string]bool{"GET": true, "HEAD": true, "POST": true, "PUT": true, "PATCH": true, "DELETE": true} |
| +) |
| + |
| +// Check applies the table's structural rules (spec C1 to C4): unique ids, |
| +// no member twice in a row, member shapes, what each status requires, |
| +// shared members qualified, and gap packages that exist in pkgs. It |
| +// returns the problems in line order; none means the table is sound. |
| +func Check(rows []Row, pkgs []Package) []Problem { |
| + var probs []Problem |
| + add := func(r Row, format string, args ...any) { |
| + probs = append(probs, Problem{Line: r.Line, RowID: r.ID, Msg: fmt.Sprintf(format, args...)}) |
| + } |
| + |
| + known := map[string]bool{} |
| + for _, p := range pkgs { |
| + known[p.ID] = true |
| + } |
| + |
| + firstID := map[string]int{} |
| + for _, r := range rows { |
| + if prev, ok := firstID[r.ID]; ok { |
| + add(r, "duplicate row id; line %d already uses it, so give this row its own id", prev) |
| + } else { |
| + firstID[r.ID] = r.Line |
| + } |
| + checkMembers(r, "CLI", r.CLI, checkCLIMember, add) |
| + checkMembers(r, "console", r.Console, checkConsoleMember, add) |
| + checkStatus(r, known, add) |
| + } |
| + |
| + checkShared(rows, "CLI", func(r Row) []Member { return r.CLI }, false, add) |
| + checkShared(rows, "console", func(r Row) []Member { return r.Console }, true, add) |
| + |
| + sort.SliceStable(probs, func(i, j int) bool { return probs[i].Line < probs[j].Line }) |
| + return probs |
| +} |
| + |
| +// checkMembers reports members listed twice in one row and members whose |
| +// text has the wrong shape for its side. |
| +func checkMembers(r Row, side string, ms []Member, shape func(string) string, add func(Row, string, ...any)) { |
| + seen := map[string]bool{} |
| + for _, m := range ms { |
| + s := m.String() |
| + if seen[s] { |
| + add(r, "lists %s member %q twice; remove one", side, s) |
| + } |
| + seen[s] = true |
| + if msg := shape(m.Text); msg != "" { |
| + add(r, "%s member %q: %s", side, s, msg) |
| + } |
| + } |
| +} |
| + |
| +// checkCLIMember returns what is wrong with a CLI member's text, or "". |
| +func checkCLIMember(text string) string { |
| + for _, w := range strings.Split(text, " ") { |
| + if !cliWordRE.MatchString(w) { |
| + return fmt.Sprintf("word %q is not a command word; write the command path as typed, lower-case words and hyphens separated by single spaces, flags in a [qualifier]", w) |
| + } |
| + } |
| + return "" |
| +} |
| + |
| +// checkConsoleMember returns what is wrong with a console member's text, |
| +// or "". The text is a route pattern as registered: "/path" or |
| +// "METHOD /path". |
| +func checkConsoleMember(text string) string { |
| + path := text |
| + if method, rest, ok := strings.Cut(text, " "); ok { |
| + if !consoleMethod[method] { |
| + return fmt.Sprintf("method %q is not one of GET, HEAD, POST, PUT, PATCH, DELETE; write the pattern as registered, \"METHOD /path\" or \"/path\"", method) |
| + } |
| + path = rest |
| + } |
| + if !strings.HasPrefix(path, "/") || strings.ContainsAny(path, " \t") { |
| + return "write the pattern as registered, \"METHOD /path\" or \"/path\"" |
| + } |
| + return "" |
| +} |
| + |
| +// checkStatus applies what the row's status requires of its sides, |
| +// package and note. |
| +func checkStatus(r Row, known map[string]bool, add func(Row, string, ...any)) { |
| + cli, console := len(r.CLI) > 0, len(r.Console) > 0 |
| + if !cli && !console { |
| + add(r, "row has no CLI or console members; add at least one") |
| + } |
| + if !r.Status.known() { |
| + var all []string |
| + for _, s := range append(append([]Status{}, customerStatuses...), outsideStatuses...) { |
| + all = append(all, string(s)) |
| + } |
| + add(r, "unknown status %q; use one of %s", r.Status, strings.Join(all, ", ")) |
| + return |
| + } |
| + if r.Status != StatusGap && r.Package != "" { |
| + add(r, "package %q is set but status is %s; only gap rows name a work package, so clear the package column", r.Package, r.Status) |
| + } |
| + needNote := func(what string) { |
| + if strings.TrimSpace(r.Note) == "" { |
| + add(r, "status %s needs a note saying %s", r.Status, what) |
| + } |
| + } |
| + switch r.Status { |
| + case StatusBoth, StatusPartial: |
| + if !cli { |
| + add(r, "status %s needs at least one CLI member", r.Status) |
| + } |
| + if !console { |
| + add(r, "status %s needs at least one console member", r.Status) |
| + } |
| + if r.Status == StatusPartial { |
| + needNote("what is missing") |
| + } |
| + case StatusGap: |
| + if cli == console { |
| + add(r, "status gap lists members on exactly one side, the side that has the capability") |
| + } |
| + switch { |
| + case r.Package == "": |
| + add(r, "status gap needs the work package that closes it in the package column") |
| + case !known[r.Package]: |
| + add(r, "package %q is not in parity/packages.tsv; use a listed package id or add the package", r.Package) |
| + } |
| + case StatusCLIOnly: |
| + if console { |
| + add(r, "status cli-only cannot list a console member; use both, partial or gap instead") |
| + } |
| + needNote("why the console does not offer it") |
| + case StatusConsoleOnly: |
| + if cli { |
| + add(r, "status console-only cannot list a CLI member; use both, partial or gap instead") |
| + } |
| + needNote("why the CLI does not offer it") |
| + default: |
| + needNote("why it is outside customer parity") |
| + } |
| +} |
| + |
| +// checkShared applies spec C2 to one side: a member text used more than |
| +// once carries a qualifier every time, and its qualifiers are pairwise |
| +// distinct. With unqualifiedOK (console routes), unqualified sharing is |
| +// allowed and only equal qualifiers are reported. |
| +func checkShared(rows []Row, side string, members func(Row) []Member, unqualifiedOK bool, add func(Row, string, ...any)) { |
| + type use struct { |
| + row Row |
| + qual string |
| + } |
| + uses := map[string][]use{} |
| + var order []string |
| + for _, r := range rows { |
| + for _, m := range members(r) { |
| + if _, ok := uses[m.Text]; !ok { |
| + order = append(order, m.Text) |
| + } |
| + uses[m.Text] = append(uses[m.Text], use{r, m.Qualifier}) |
| + } |
| + } |
| + for _, text := range order { |
| + us := uses[text] |
| + if len(us) < 2 { |
| + continue |
| + } |
| + // other returns a use of text in a different row from u, if any. |
| + other := func(u use) (use, bool) { |
| + for _, o := range us { |
| + if o.row.Line != u.row.Line { |
| + return o, true |
| + } |
| + } |
| + return use{}, false |
| + } |
| + firstQual := map[string]int{} |
| + for i, u := range us { |
| + if u.qual == "" { |
| + if o, ok := other(u); ok && !unqualifiedOK { |
| + add(u.row, "%s member %q is also listed in row %s (line %d); a member in more than one row needs a [qualifier] in every row naming the part this row covers", side, text, o.row.ID, o.row.Line) |
| + } |
| + continue |
| + } |
| + if j, ok := firstQual[u.qual]; ok { |
| + if us[j].row.Line == u.row.Line { |
| + continue // reported as a duplicate member in the row |
| + } |
| + add(u.row, "%s member %q repeats the qualifier of row %s (line %d); make the qualifiers of one member distinct", side, Member{text, u.qual}.String(), us[j].row.ID, us[j].row.Line) |
| + continue |
| + } |
| + firstQual[u.qual] = i |
| + } |
| + } |
| +} |
diff --git a/internal/parity/rules_test.go b/internal/parity/rules_test.go| new file mode 100644 |
| index 000000000..040ebcff6 |
| --- /dev/null |
| +++ b/internal/parity/rules_test.go |
| @@ -0,0 +1,182 @@ |
| +package parity |
| + |
| +import ( |
| + "strings" |
| + "testing" |
| +) |
| + |
| +var testPkgs = []Package{{ID: "wp-instances", Title: "Instances", Line: 2}} |
| + |
| +// checkRows parses src and runs Check with testPkgs. |
| +func checkRows(t *testing.T, lines ...string) []Problem { |
| + t.Helper() |
| + return Check(mustParse(t, tsv(lines...)), testPkgs) |
| +} |
| + |
| +// wantProblem asserts one problem names rowID and contains every fragment. |
| +func wantProblem(t *testing.T, probs []Problem, rowID string, fragments ...string) { |
| + t.Helper() |
| + for _, p := range probs { |
| + if p.RowID != rowID { |
| + continue |
| + } |
| + ok := true |
| + for _, f := range fragments { |
| + if !strings.Contains(p.Msg, f) { |
| + ok = false |
| + break |
| + } |
| + } |
| + if ok { |
| + if !strings.Contains(p.String(), "row "+rowID+": ") { |
| + t.Fatalf("String() %q does not name row %s", p.String(), rowID) |
| + } |
| + return |
| + } |
| + } |
| + t.Fatalf("no problem for row %s containing %q; got %v", rowID, fragments, probs) |
| +} |
| + |
| +func wantClean(t *testing.T, probs []Problem) { |
| + t.Helper() |
| + if len(probs) != 0 { |
| + t.Fatalf("want no problems, got %v", probs) |
| + } |
| +} |
| + |
| +func TestCheckCleanFixture(t *testing.T) { |
| + wantClean(t, checkRows(t, |
| + row("apps-list", "apps", "both", "apps", "GET /a/{sqid}/apps", "", "", ""), |
| + row("apps-env", "apps", "partial", "env set", "POST /a/{sqid}/env", "", "", "console cannot set a bundle"), |
| + row("moves", "instances", "gap", "instances move", "", "wp-instances", "", ""), |
| + row("logs", "apps", "cli-only", "logs", "", "", "", "a stream fits a terminal"), |
| + row("graph", "apps", "console-only", "", "/fleet/architecture", "", "", "a picture"), |
| + row("login", "auth", "auth", "login", "GET /login", "", "", "sign-in plumbing"), |
| + row("snap", "demo", "internal", "__demo-snapshot", "", "", "", "dispatched before the switch"), |
| + )) |
| +} |
| + |
| +func TestProblemString(t *testing.T) { |
| + p := Problem{Line: 7, RowID: "apps-list", Msg: "fix it"} |
| + if got, want := p.String(), "parity/parity.tsv:7: row apps-list: fix it"; got != want { |
| + t.Fatalf("String() = %q, want %q", got, want) |
| + } |
| +} |
| + |
| +func TestCheckDuplicateID(t *testing.T) { |
| + probs := checkRows(t, |
| + row("a", "apps", "cli-only", "apps", "", "", "", "why"), |
| + row("a", "apps", "cli-only", "deploy", "", "", "", "why"), |
| + ) |
| + wantProblem(t, probs, "a", "duplicate row id", "line 2") |
| + if probs[0].Line != 3 { |
| + t.Fatalf("duplicate reported on line %d, want 3", probs[0].Line) |
| + } |
| +} |
| + |
| +func TestCheckDuplicateMemberInRow(t *testing.T) { |
| + wantProblem(t, checkRows(t, row("a", "apps", "cli-only", "apps ; apps", "", "", "", "why")), |
| + "a", `CLI member "apps"`, "twice") |
| + wantProblem(t, checkRows(t, row("a", "apps", "console-only", "", "/x ; /x", "", "", "why")), |
| + "a", `console member "/x"`, "twice") |
| +} |
| + |
| +func TestCheckEmptyRow(t *testing.T) { |
| + wantProblem(t, checkRows(t, row("a", "apps", "auth", "", "", "", "", "why")), "a", "no CLI or console members") |
| +} |
| + |
| +func TestCheckBothAndPartialNeedBothSides(t *testing.T) { |
| + wantProblem(t, checkRows(t, row("a", "apps", "both", "apps", "", "", "", "")), "a", "both", "console member") |
| + wantProblem(t, checkRows(t, row("a", "apps", "partial", "", "/x", "", "", "why")), "a", "partial", "CLI member") |
| +} |
| + |
| +func TestCheckPartialNeedsNote(t *testing.T) { |
| + wantProblem(t, checkRows(t, row("a", "apps", "partial", "apps", "/x", "", "", "")), "a", "note", "missing") |
| +} |
| + |
| +func TestCheckGapSides(t *testing.T) { |
| + wantProblem(t, checkRows(t, row("a", "apps", "gap", "apps", "/x", "wp-instances", "", "")), "a", "gap", "one side") |
| + wantProblem(t, checkRows(t, row("a", "apps", "gap", "", "", "wp-instances", "", "")), "a", "gap", "one side") |
| +} |
| + |
| +func TestCheckGapPackage(t *testing.T) { |
| + wantProblem(t, checkRows(t, row("a", "apps", "gap", "apps", "", "", "", "")), "a", "work package") |
| + wantProblem(t, checkRows(t, row("a", "apps", "gap", "apps", "", "wp-nope", "", "")), "a", `"wp-nope"`, "packages.tsv") |
| +} |
| + |
| +func TestCheckNonGapWithPackage(t *testing.T) { |
| + wantProblem(t, checkRows(t, row("a", "apps", "both", "apps", "/x", "wp-instances", "", "")), "a", "only gap rows") |
| +} |
| + |
| +func TestCheckOneSidedStatuses(t *testing.T) { |
| + wantProblem(t, checkRows(t, row("a", "apps", "cli-only", "apps", "/x", "", "", "why")), "a", "cli-only", "console member") |
| + wantProblem(t, checkRows(t, row("a", "apps", "console-only", "apps", "/x", "", "", "why")), "a", "console-only", "CLI member") |
| +} |
| + |
| +func TestCheckReasonRequired(t *testing.T) { |
| + for _, s := range []string{"cli-only", "console-only", "auth", "demo", "asset", "internal", "operator", "box-local"} { |
| + t.Run(s, func(t *testing.T) { |
| + cli, console := "apps", "" |
| + if s == "console-only" { |
| + cli, console = "", "/x" |
| + } |
| + wantProblem(t, checkRows(t, row("a", "apps", s, cli, console, "", "", "")), "a", s, "note") |
| + }) |
| + } |
| +} |
| + |
| +func TestCheckUnknownStatus(t *testing.T) { |
| + wantProblem(t, checkRows(t, row("a", "apps", "maybe", "apps", "", "", "", "why")), "a", `unknown status "maybe"`) |
| +} |
| + |
| +func TestCheckSharedMemberUnqualified(t *testing.T) { |
| + probs := checkRows(t, |
| + row("a", "apps", "both", "apps set [--swap]", "/x", "", "", ""), |
| + row("b", "apps", "cli-only", "apps set", "", "", "", "why"), |
| + ) |
| + wantProblem(t, probs, "b", `CLI member "apps set"`, "row a", "qualifier") |
| +} |
| + |
| +func TestCheckSharedMemberEqualQualifiers(t *testing.T) { |
| + probs := checkRows(t, |
| + row("a", "apps", "both", "apps set [--swap]", "/x", "", "", ""), |
| + row("b", "apps", "cli-only", "apps set [--swap]", "", "", "", "why"), |
| + ) |
| + wantProblem(t, probs, "b", `"apps set [--swap]"`, "row a", "distinct") |
| +} |
| + |
| +func TestCheckSharedMemberDistinctQualifiers(t *testing.T) { |
| + wantClean(t, checkRows(t, |
| + row("a", "apps", "both", "env set [--environment, existing bundle]", "/x", "", "", ""), |
| + row("b", "apps", "gap", "env set [--environment, new bundle]", "", "wp-instances", "", ""), |
| + )) |
| +} |
| + |
| +func TestCheckConsoleMayBeSharedUnqualified(t *testing.T) { |
| + wantClean(t, checkRows(t, |
| + row("a", "settings", "both", "apps rename", "GET /a/{sqid}/settings", "", "", ""), |
| + row("b", "settings", "both", "apps domains", "GET /a/{sqid}/settings", "", "", ""), |
| + )) |
| +} |
| + |
| +func TestCheckCLIWordShape(t *testing.T) { |
| + for _, m := range []string{"Apps", "apps Set", "apps set", "apps --swap", "apps_set", "apps/set"} { |
| + t.Run(m, func(t *testing.T) { |
| + wantProblem(t, checkRows(t, row("a", "apps", "cli-only", m, "", "", "", "why")), "a", "CLI member", "word") |
| + }) |
| + } |
| + wantClean(t, checkRows(t, row("a", "apps", "internal", "__demo-snapshot ; db pull2", "", "", "", "why"))) |
| +} |
| + |
| +func TestCheckConsoleShape(t *testing.T) { |
| + for _, m := range []string{"x", "GETS /x", "get /x", "GET x", "GET /x y", "GET /x"} { |
| + t.Run(m, func(t *testing.T) { |
| + wantProblem(t, checkRows(t, row("a", "apps", "console-only", "", m, "", "", "why")), "a", "console member") |
| + }) |
| + } |
| + for _, m := range []string{"GET /x", "HEAD /x", "POST /x", "PUT /x", "PATCH /x", "DELETE /x", "/x/{a}/b"} { |
| + t.Run("ok "+m, func(t *testing.T) { |
| + wantClean(t, checkRows(t, row("a", "apps", "console-only", "", m, "", "", "why"))) |
| + }) |
| + } |
| +} |
diff --git a/internal/parity/table.go b/internal/parity/table.go| new file mode 100644 |
| index 000000000..abc0b6edf |
| --- /dev/null |
| +++ b/internal/parity/table.go |
| @@ -0,0 +1,284 @@ |
| +// Package parity reads the CLI and console parity table |
| +// (parity/parity.tsv) and its work packages (parity/packages.tsv), checks |
| +// the table's structural rules and renders docs/parity.md from it. |
| +// |
| +// The table is the stock of what a customer can do from the CLI, from the |
| +// console, or from both. Tests in this package and in package console hold |
| +// it against the code; this package knows only the files. |
| +package parity |
| + |
| +import ( |
| + "bufio" |
| + "fmt" |
| + "io" |
| + "os" |
| + "regexp" |
| + "strconv" |
| + "strings" |
| +) |
| + |
| +// Status says how a row's capability is covered. |
| +type Status string |
| + |
| +// Customer statuses count towards parity; the rest are outside it. |
| +const ( |
| + StatusBoth Status = "both" |
| + StatusPartial Status = "partial" |
| + StatusGap Status = "gap" |
| + StatusCLIOnly Status = "cli-only" |
| + StatusConsoleOnly Status = "console-only" |
| + |
| + StatusOperator Status = "operator" |
| + StatusBoxLocal Status = "box-local" |
| + StatusAuth Status = "auth" |
| + StatusDemo Status = "demo" |
| + StatusAsset Status = "asset" |
| + StatusInternal Status = "internal" |
| +) |
| + |
| +// customerStatuses and outsideStatuses fix the order statuses appear in |
| +// on the page and in messages. |
| +var ( |
| + customerStatuses = []Status{StatusBoth, StatusPartial, StatusGap, StatusCLIOnly, StatusConsoleOnly} |
| + outsideStatuses = []Status{StatusOperator, StatusBoxLocal, StatusAuth, StatusDemo, StatusAsset, StatusInternal} |
| +) |
| + |
| +// Customer reports whether rows of this status count towards parity. |
| +func (s Status) Customer() bool { |
| + for _, c := range customerStatuses { |
| + if s == c { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +func (s Status) known() bool { |
| + if s.Customer() { |
| + return true |
| + } |
| + for _, o := range outsideStatuses { |
| + if s == o { |
| + return true |
| + } |
| + } |
| + return false |
| +} |
| + |
| +// Member is one CLI command path or console route in a row, with an |
| +// optional qualifier naming the part of it the row is about. |
| +type Member struct{ Text, Qualifier string } |
| + |
| +// String returns the member as written in the table. |
| +func (m Member) String() string { |
| + if m.Qualifier == "" { |
| + return m.Text |
| + } |
| + return m.Text + " [" + m.Qualifier + "]" |
| +} |
| + |
| +// Row is one line of parity/parity.tsv. |
| +type Row struct { |
| + Line int // 1-based line in the file |
| + ID, Area string |
| + Status Status |
| + CLI, Console []Member |
| + Package, MCP, Note string |
| +} |
| + |
| +// Package is one line of parity/packages.tsv. Discussion is 0 until the |
| +// package's amadan discussion is filed. |
| +type Package struct { |
| + ID, Title string |
| + Discussion int |
| + Line int |
| +} |
| + |
| +const ( |
| + tableHeader = "id\tarea\tstatus\tcli\tconsole\tpackage\tmcp\tnote" |
| + packageHeader = "id\ttitle\tdiscussion" |
| + // memberSep separates members within the cli and console fields. |
| + memberSep = " ; " |
| +) |
| + |
| +var ( |
| + idRE = regexp.MustCompile(`^[a-z0-9][a-z0-9-]*$`) |
| + areaRE = regexp.MustCompile(`^[a-z][a-z-]*$`) |
| +) |
| + |
| +// lines calls fn with every line of r that is not blank or a comment, |
| +// stopping at the first error. The first such line is the header, which |
| +// must equal header exactly. |
| +func lines(r io.Reader, header string, fn func(n int, line string) error) error { |
| + sc := bufio.NewScanner(r) |
| + sc.Buffer(make([]byte, 64*1024), 1024*1024) |
| + n := 0 |
| + seenHeader := false |
| + for sc.Scan() { |
| + n++ |
| + line := strings.TrimSuffix(sc.Text(), "\r") |
| + if strings.TrimSpace(line) == "" || strings.HasPrefix(line, "#") { |
| + continue |
| + } |
| + if !seenHeader { |
| + if line != header { |
| + return fmt.Errorf("line %d: header must be exactly %q", n, header) |
| + } |
| + seenHeader = true |
| + continue |
| + } |
| + if err := fn(n, line); err != nil { |
| + return fmt.Errorf("line %d: %w", n, err) |
| + } |
| + } |
| + if err := sc.Err(); err != nil { |
| + return err |
| + } |
| + if !seenHeader { |
| + return fmt.Errorf("no header line; want %q", header) |
| + } |
| + return nil |
| +} |
| + |
| +// Parse reads a parity table. It checks syntax only (columns, id and area |
| +// shape, member syntax, an empty mcp column); Check applies the rules. |
| +func Parse(r io.Reader) ([]Row, error) { |
| + var rows []Row |
| + err := lines(r, tableHeader, func(n int, line string) error { |
| + f := strings.Split(line, "\t") |
| + if len(f) != 8 { |
| + return fmt.Errorf("want 8 tab-separated columns (%s), got %d", strings.ReplaceAll(tableHeader, "\t", ", "), len(f)) |
| + } |
| + row := Row{Line: n, ID: f[0], Area: f[1], Status: Status(f[2]), Package: f[5], MCP: f[6], Note: f[7]} |
| + if !idRE.MatchString(row.ID) { |
| + return fmt.Errorf("id %q must be lower-case letters, digits and hyphens, starting with a letter or digit", row.ID) |
| + } |
| + if !areaRE.MatchString(row.Area) { |
| + return fmt.Errorf("row %s: area %q must be lower-case letters and hyphens, starting with a letter", row.ID, row.Area) |
| + } |
| + if row.MCP != "" { |
| + return fmt.Errorf("row %s: the mcp column must be empty for now", row.ID) |
| + } |
| + var err error |
| + if row.CLI, err = parseMembers(f[3]); err != nil { |
| + return fmt.Errorf("row %s: cli: %w", row.ID, err) |
| + } |
| + if row.Console, err = parseMembers(f[4]); err != nil { |
| + return fmt.Errorf("row %s: console: %w", row.ID, err) |
| + } |
| + rows = append(rows, row) |
| + return nil |
| + }) |
| + if err != nil { |
| + return nil, err |
| + } |
| + return rows, nil |
| +} |
| + |
| +// parseMembers splits a cli or console field into members. |
| +func parseMembers(field string) ([]Member, error) { |
| + if field == "" { |
| + return nil, nil |
| + } |
| + var out []Member |
| + for _, s := range strings.Split(field, memberSep) { |
| + m, err := parseMember(s) |
| + if err != nil { |
| + return nil, err |
| + } |
| + out = append(out, m) |
| + } |
| + return out, nil |
| +} |
| + |
| +// parseMember reads "text" or "text [qualifier]". |
| +func parseMember(s string) (Member, error) { |
| + if s == "" { |
| + return Member{}, fmt.Errorf("empty member; members are separated by %q", memberSep) |
| + } |
| + if strings.TrimSpace(s) != s { |
| + return Member{}, fmt.Errorf("member %q has leading or trailing spaces", s) |
| + } |
| + text, qual := s, "" |
| + if i := strings.Index(s, " ["); i >= 0 { |
| + if !strings.HasSuffix(s, "]") { |
| + return Member{}, fmt.Errorf("member %q: a qualifier must end the member, as in \"apps set [--swap]\"", s) |
| + } |
| + text, qual = s[:i], s[i+2:len(s)-1] |
| + if qual == "" { |
| + return Member{}, fmt.Errorf("member %q has an empty qualifier", s) |
| + } |
| + if strings.ContainsAny(qual, "[]") { |
| + return Member{}, fmt.Errorf("member %q: a member takes one qualifier, without brackets inside it", s) |
| + } |
| + } |
| + if strings.ContainsAny(text, "[]") { |
| + return Member{}, fmt.Errorf("member %q: write a qualifier after a space, as in \"apps set [--swap]\"", s) |
| + } |
| + return Member{Text: text, Qualifier: qual}, nil |
| +} |
| + |
| +// Load reads the parity table at path. |
| +func Load(path string) ([]Row, error) { |
| + f, err := os.Open(path) |
| + if err != nil { |
| + return nil, err |
| + } |
| + defer f.Close() |
| + rows, err := Parse(f) |
| + if err != nil { |
| + return nil, fmt.Errorf("%s: %w", path, err) |
| + } |
| + return rows, nil |
| +} |
| + |
| +// ParsePackages reads a work-package list. An empty discussion column |
| +// means the package's discussion is not filed yet (Discussion 0). |
| +func ParsePackages(r io.Reader) ([]Package, error) { |
| + var pkgs []Package |
| + seen := map[string]int{} |
| + err := lines(r, packageHeader, func(n int, line string) error { |
| + f := strings.Split(line, "\t") |
| + if len(f) != 3 { |
| + return fmt.Errorf("want 3 tab-separated columns (id, title, discussion), got %d", len(f)) |
| + } |
| + p := Package{ID: f[0], Title: f[1], Line: n} |
| + if !idRE.MatchString(p.ID) { |
| + return fmt.Errorf("package id %q must be lower-case letters, digits and hyphens", p.ID) |
| + } |
| + if prev, ok := seen[p.ID]; ok { |
| + return fmt.Errorf("package %s is already listed on line %d", p.ID, prev) |
| + } |
| + seen[p.ID] = n |
| + if strings.TrimSpace(p.Title) == "" { |
| + return fmt.Errorf("package %s needs a title", p.ID) |
| + } |
| + if f[2] != "" { |
| + d, err := strconv.Atoi(f[2]) |
| + if err != nil || d <= 0 { |
| + return fmt.Errorf("package %s: discussion %q must be a discussion number or empty", p.ID, f[2]) |
| + } |
| + p.Discussion = d |
| + } |
| + pkgs = append(pkgs, p) |
| + return nil |
| + }) |
| + if err != nil { |
| + return nil, err |
| + } |
| + return pkgs, nil |
| +} |
| + |
| +// LoadPackages reads the work-package list at path. |
| +func LoadPackages(path string) ([]Package, error) { |
| + f, err := os.Open(path) |
| + if err != nil { |
| + return nil, err |
| + } |
| + defer f.Close() |
| + pkgs, err := ParsePackages(f) |
| + if err != nil { |
| + return nil, fmt.Errorf("%s: %w", path, err) |
| + } |
| + return pkgs, nil |
| +} |
diff --git a/internal/parity/table_test.go b/internal/parity/table_test.go| new file mode 100644 |
| index 000000000..afee74730 |
| --- /dev/null |
| +++ b/internal/parity/table_test.go |
| @@ -0,0 +1,173 @@ |
| +package parity |
| + |
| +import ( |
| + "reflect" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +const header = "id\tarea\tstatus\tcli\tconsole\tpackage\tmcp\tnote" |
| + |
| +// tsv joins rows of fields with tabs and lines with newlines, after the header. |
| +func tsv(lines ...string) string { |
| + return header + "\n" + strings.Join(lines, "\n") + "\n" |
| +} |
| + |
| +func row(fields ...string) string { return strings.Join(fields, "\t") } |
| + |
| +func mustParse(t *testing.T, src string) []Row { |
| + t.Helper() |
| + rows, err := Parse(strings.NewReader(src)) |
| + if err != nil { |
| + t.Fatalf("Parse: %v", err) |
| + } |
| + return rows |
| +} |
| + |
| +func parseErr(t *testing.T, src, want string) { |
| + t.Helper() |
| + _, err := Parse(strings.NewReader(src)) |
| + if err == nil { |
| + t.Fatalf("Parse succeeded, want error containing %q", want) |
| + } |
| + if !strings.Contains(err.Error(), want) { |
| + t.Fatalf("error %q does not contain %q", err, want) |
| + } |
| +} |
| + |
| +func TestParseHeaderMismatch(t *testing.T) { |
| + parseErr(t, "# comment\n\nid\tarea\tstatus\tcli\tconsole\tpackage\tnote\n", "line 3:") |
| +} |
| + |
| +func TestParseMissingHeader(t *testing.T) { |
| + parseErr(t, "# only comments\n", "no header") |
| +} |
| + |
| +func TestParseColumnCount(t *testing.T) { |
| + parseErr(t, tsv(row("a", "apps", "both", "apps", "/x", "", "")), "line 2:") |
| +} |
| + |
| +func TestParseMembers(t *testing.T) { |
| + rows := mustParse(t, tsv(row("apps-set-swap", "apps", "both", |
| + "apps set [--swap,--swap-concurrency] ; apps show", |
| + "POST /a/{sqid}/apps/{app}/swap ; /fleet/architecture", |
| + "", "", "a note"))) |
| + if len(rows) != 1 { |
| + t.Fatalf("got %d rows, want 1", len(rows)) |
| + } |
| + r := rows[0] |
| + wantCLI := []Member{{"apps set", "--swap,--swap-concurrency"}, {"apps show", ""}} |
| + if !reflect.DeepEqual(r.CLI, wantCLI) { |
| + t.Fatalf("CLI = %#v, want %#v", r.CLI, wantCLI) |
| + } |
| + wantConsole := []Member{{"POST /a/{sqid}/apps/{app}/swap", ""}, {"/fleet/architecture", ""}} |
| + if !reflect.DeepEqual(r.Console, wantConsole) { |
| + t.Fatalf("Console = %#v, want %#v", r.Console, wantConsole) |
| + } |
| + if r.ID != "apps-set-swap" || r.Area != "apps" || r.Status != StatusBoth || r.Note != "a note" || r.Line != 2 { |
| + t.Fatalf("row = %#v", r) |
| + } |
| + if got := r.CLI[0].String(); got != "apps set [--swap,--swap-concurrency]" { |
| + t.Fatalf("String = %q", got) |
| + } |
| + if got := r.CLI[1].String(); got != "apps show" { |
| + t.Fatalf("String = %q", got) |
| + } |
| +} |
| + |
| +func TestParseEmptyMemberFields(t *testing.T) { |
| + rows := mustParse(t, tsv(row("x", "apps", "cli-only", "apps", "", "", "", "why"))) |
| + if len(rows[0].Console) != 0 { |
| + t.Fatalf("Console = %#v, want none", rows[0].Console) |
| + } |
| +} |
| + |
| +func TestParseBadMembers(t *testing.T) { |
| + for _, m := range []string{ |
| + "apps set[--x]", |
| + "apps set []", |
| + "apps set [a] b", |
| + " apps set", |
| + "apps set ", |
| + "apps set [a] [b]", |
| + "apps set ; ", |
| + "apps ]set", |
| + } { |
| + t.Run(m, func(t *testing.T) { |
| + parseErr(t, tsv(row("x", "apps", "cli-only", m, "", "", "", "why")), "line 2:") |
| + }) |
| + } |
| +} |
| + |
| +func TestParseMCPMustBeEmpty(t *testing.T) { |
| + parseErr(t, tsv(row("x", "apps", "cli-only", "apps", "", "", "apps_list", "why")), "mcp") |
| +} |
| + |
| +func TestParseBadIDAndArea(t *testing.T) { |
| + parseErr(t, tsv(row("Apps", "apps", "cli-only", "apps", "", "", "", "why")), "id") |
| + parseErr(t, tsv(row("-a", "apps", "cli-only", "apps", "", "", "", "why")), "id") |
| + parseErr(t, tsv(row("a", "1apps", "cli-only", "apps", "", "", "", "why")), "area") |
| + parseErr(t, tsv(row("a", "Apps", "cli-only", "apps", "", "", "", "why")), "area") |
| +} |
| + |
| +func TestParseCommentsAndBlankLinesKeepLineNumbers(t *testing.T) { |
| + src := "# a comment\n\n" + header + "\n# another\n\n" + |
| + row("a", "apps", "cli-only", "apps", "", "", "", "why") + "\n\n" + |
| + row("b", "apps", "cli-only", "deploy", "", "", "", "why") + "\n" |
| + rows := mustParse(t, src) |
| + if len(rows) != 2 || rows[0].Line != 6 || rows[1].Line != 8 { |
| + t.Fatalf("rows = %#v", rows) |
| + } |
| +} |
| + |
| +func TestParsePackages(t *testing.T) { |
| + src := "# packages\nid\ttitle\tdiscussion\nwp-instances\tInstances\t\nwp-settings\tSmall settings forms\t812\n" |
| + pkgs, err := ParsePackages(strings.NewReader(src)) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + want := []Package{ |
| + {ID: "wp-instances", Title: "Instances", Discussion: 0, Line: 3}, |
| + {ID: "wp-settings", Title: "Small settings forms", Discussion: 812, Line: 4}, |
| + } |
| + if !reflect.DeepEqual(pkgs, want) { |
| + t.Fatalf("got %#v, want %#v", pkgs, want) |
| + } |
| +} |
| + |
| +func TestParsePackagesErrors(t *testing.T) { |
| + for name, src := range map[string]string{ |
| + "header": "id\ttitle\n", |
| + "columns": "id\ttitle\tdiscussion\nwp-a\tA\n", |
| + "discussion": "id\ttitle\tdiscussion\nwp-a\tA\tabc\n", |
| + "zero": "id\ttitle\tdiscussion\nwp-a\tA\t0\n", |
| + "title": "id\ttitle\tdiscussion\nwp-a\t\t\n", |
| + "id": "id\ttitle\tdiscussion\nWP\tA\t\n", |
| + "duplicate": "id\ttitle\tdiscussion\nwp-a\tA\t\nwp-a\tB\t\n", |
| + } { |
| + t.Run(name, func(t *testing.T) { |
| + _, err := ParsePackages(strings.NewReader(src)) |
| + if err == nil || !strings.Contains(err.Error(), "line ") { |
| + t.Fatalf("err = %v, want a line-numbered error", err) |
| + } |
| + }) |
| + } |
| +} |
| + |
| +func TestStatusCustomer(t *testing.T) { |
| + customer := map[Status]bool{ |
| + StatusBoth: true, StatusPartial: true, StatusGap: true, StatusCLIOnly: true, StatusConsoleOnly: true, |
| + StatusOperator: false, StatusBoxLocal: false, StatusAuth: false, StatusDemo: false, StatusAsset: false, StatusInternal: false, |
| + } |
| + if len(customer) != 11 { |
| + t.Fatalf("want eleven statuses, got %d", len(customer)) |
| + } |
| + for s, want := range customer { |
| + if s.Customer() != want { |
| + t.Errorf("%s.Customer() = %v, want %v", s, s.Customer(), want) |
| + } |
| + } |
| + if Status("nope").Customer() { |
| + t.Error("unknown status counted as customer") |
| + } |
| +} |