(root fs.FS, docs []string)
| 110 | } |
| 111 | |
| 112 | func readDelegateExamples(root fs.FS, docs []string) ([]string, error) { |
| 113 | if len(docs) == 0 { |
| 114 | return nil, fmt.Errorf("no delegate documents to check") |
| 115 | } |
| 116 | exampleRE := regexp.MustCompile("git [^`\r\n]*(?: -- \"?<path>\"?|\"?<ref>:<path>\"?)|cat \"?<path>\"?") |
| 117 | var canonical []string |
| 118 | for i, doc := range docs { |
| 119 | content, err := fs.ReadFile(root, doc) |
| 120 | if err != nil { |
| 121 | return nil, fmt.Errorf("%s: %w", doc, err) |
| 122 | } |
| 123 | set := map[string]bool{} |
| 124 | for _, example := range exampleRE.FindAllString(string(content), -1) { |
| 125 | set[example] = true |
| 126 | } |
| 127 | var examples []string |
| 128 | for example := range set { |
| 129 | examples = append(examples, example) |
| 130 | } |
| 131 | slices.Sort(examples) |
| 132 | if len(examples) == 0 { |
| 133 | return nil, fmt.Errorf("%s contains no review command examples", doc) |
| 134 | } |
| 135 | if !slices.ContainsFunc(examples, func(example string) bool { return strings.HasPrefix(example, "git ") }) { |
| 136 | return nil, fmt.Errorf("%s contains no Git command examples", doc) |
| 137 | } |
| 138 | if i == 0 { |
| 139 | canonical = examples |
| 140 | } else if !slices.Equal(examples, canonical) { |
| 141 | return nil, fmt.Errorf("%s examples differ from %s:\n%q\nwant %q", doc, docs[0], examples, canonical) |
| 142 | } |
| 143 | } |
| 144 | return canonical, nil |
| 145 | } |
| 146 | |
| 147 | func TestReadDelegateExamples(t *testing.T) { |
| 148 | const diff = "git --no-pager diff --no-ext-diff --no-textconv --no-color HEAD -- \"<path>\"" |
no outgoing calls
no test coverage detected