|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +package executor |
| 4 | + |
| 5 | +import ( |
| 6 | + "os" |
| 7 | + "path/filepath" |
| 8 | + "regexp" |
| 9 | + "sort" |
| 10 | + "strings" |
| 11 | + "testing" |
| 12 | +) |
| 13 | + |
| 14 | +// MDL-WIDGET rule numbers are handed out by hand, from no registry, by grepping |
| 15 | +// for the highest one in use. That has now failed twice: |
| 16 | +// |
| 17 | +// - once at authoring time, picking 27 for a rule that already existed; |
| 18 | +// - once at MERGE time, where two branches independently took 29 — one for a |
| 19 | +// retired-spelling rule, one for an unroutable child — and each was correct |
| 20 | +// in isolation. Nothing compared them until a rebase put both in the tree. |
| 21 | +// |
| 22 | +// The second is the one that matters, because no amount of care at authoring |
| 23 | +// time prevents it. This test does the comparison, so the collision fails on the |
| 24 | +// merge that creates it rather than shipping as two rules answering to one id. |
| 25 | +// |
| 26 | +// The invariant is NOT "one file per id": a rule legitimately raised from two |
| 27 | +// places stays one rule. So known pairs are listed, and anything new fails — |
| 28 | +// which is exactly the moment to check whether it is one rule or two. |
| 29 | +var widgetRuleIDsRaisedFromSeveralFiles = map[string][]string{ |
| 30 | + // One rule about a property that is hidden under the current configuration, |
| 31 | + // raised from the content-params path and the editability path. |
| 32 | + "MDL-WIDGET21": {"validate_widget_contentparams.go", "validate_widget_editability.go"}, |
| 33 | +} |
| 34 | + |
| 35 | +// Matched as a QUOTED literal rather than after `RuleID:`, because an id is |
| 36 | +// also declared as a named constant (imageSourceRule = "MDL-WIDGET22") — the |
| 37 | +// first version of this guard missed that one and reported a gap that was not |
| 38 | +// there. Quoting is what separates a declaration from a doc comment mentioning |
| 39 | +// a neighbouring rule. |
| 40 | +var widgetRuleIDRe = regexp.MustCompile(`"(MDL-WIDGET\d+)"`) |
| 41 | + |
| 42 | +func TestWidgetRuleIDsAreNotReused(t *testing.T) { |
| 43 | + files, err := filepath.Glob("*.go") |
| 44 | + if err != nil { |
| 45 | + t.Fatal(err) |
| 46 | + } |
| 47 | + byID := map[string]map[string]bool{} |
| 48 | + for _, f := range files { |
| 49 | + if strings.HasSuffix(f, "_test.go") { |
| 50 | + continue |
| 51 | + } |
| 52 | + src, err := os.ReadFile(f) |
| 53 | + if err != nil { |
| 54 | + t.Fatal(err) |
| 55 | + } |
| 56 | + for _, m := range widgetRuleIDRe.FindAllStringSubmatch(string(src), -1) { |
| 57 | + if byID[m[1]] == nil { |
| 58 | + byID[m[1]] = map[string]bool{} |
| 59 | + } |
| 60 | + byID[m[1]][f] = true |
| 61 | + } |
| 62 | + } |
| 63 | + if len(byID) == 0 { |
| 64 | + t.Fatal("no MDL-WIDGET rule ids found — this guard would pass vacuously") |
| 65 | + } |
| 66 | + |
| 67 | + for id, files := range byID { |
| 68 | + if len(files) < 2 { |
| 69 | + continue |
| 70 | + } |
| 71 | + var got []string |
| 72 | + for f := range files { |
| 73 | + got = append(got, f) |
| 74 | + } |
| 75 | + sort.Strings(got) |
| 76 | + want := append([]string(nil), widgetRuleIDsRaisedFromSeveralFiles[id]...) |
| 77 | + sort.Strings(want) |
| 78 | + if strings.Join(got, ",") != strings.Join(want, ",") { |
| 79 | + t.Errorf("%s is raised from %v.\n"+ |
| 80 | + "If those are TWO different rules, one of them needs a new number — take the next "+ |
| 81 | + "free one after the highest in use, and check again after rebasing, because another "+ |
| 82 | + "branch may have taken it meanwhile.\n"+ |
| 83 | + "If it is ONE rule raised from two places, add it to "+ |
| 84 | + "widgetRuleIDsRaisedFromSeveralFiles.", id, got) |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +// The numbers are also expected to be dense, so "the next free one" is a |
| 90 | +// question with an answer. A gap means a rule was removed without its number |
| 91 | +// being reused, which is fine but worth stating deliberately rather than |
| 92 | +// leaving the next author to guess. |
| 93 | +func TestWidgetRuleIDsHaveNoGaps(t *testing.T) { |
| 94 | + files, _ := filepath.Glob("*.go") |
| 95 | + seen := map[int]bool{} |
| 96 | + max := 0 |
| 97 | + numRe := regexp.MustCompile(`MDL-WIDGET(\d+)`) |
| 98 | + for _, f := range files { |
| 99 | + if strings.HasSuffix(f, "_test.go") { |
| 100 | + continue |
| 101 | + } |
| 102 | + src, err := os.ReadFile(f) |
| 103 | + if err != nil { |
| 104 | + t.Fatal(err) |
| 105 | + } |
| 106 | + for _, m := range widgetRuleIDRe.FindAllStringSubmatch(string(src), -1) { |
| 107 | + n := 0 |
| 108 | + for _, d := range numRe.FindStringSubmatch(m[1])[1] { |
| 109 | + n = n*10 + int(d-'0') |
| 110 | + } |
| 111 | + seen[n] = true |
| 112 | + if n > max { |
| 113 | + max = n |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + var missing []int |
| 118 | + for i := 1; i < max; i++ { |
| 119 | + if !seen[i] { |
| 120 | + missing = append(missing, i) |
| 121 | + } |
| 122 | + } |
| 123 | + if len(missing) > 0 { |
| 124 | + t.Errorf("MDL-WIDGET numbers are not dense: %v missing below %d. "+ |
| 125 | + "Reuse the lowest free number, or note here why it is retired.", missing, max) |
| 126 | + } |
| 127 | +} |
0 commit comments