verifyBoilerplate verifies if a string contains the boilerplate
(contents string)
| 83 | |
| 84 | // verifyBoilerplate verifies if a string contains the boilerplate |
| 85 | func verifyBoilerplate(contents string) error { |
| 86 | idx := 0 |
| 87 | foundBoilerplateStart := false |
| 88 | lines := strings.SplitSeq(contents, "\n") |
| 89 | for line := range lines { |
| 90 | // handle leading comments |
| 91 | line = trimLeadingComment(line, "//") |
| 92 | line = trimLeadingComment(line, "#") |
| 93 | |
| 94 | // find the start of the boilerplate |
| 95 | bpLine := boilerPlate[idx] |
| 96 | if strings.Contains(line, boilerPlateStart) { |
| 97 | foundBoilerplateStart = true |
| 98 | |
| 99 | // validate the year of the copyright |
| 100 | yearWords := strings.Split(line, " ") |
| 101 | if yearRegexp.MatchString(yearWords[1]) { |
| 102 | bpLine = strings.ReplaceAll(bpLine, yearPlaceholder, yearWords[1]) |
| 103 | } else { |
| 104 | bpLine = strings.ReplaceAll(bpLine, yearPlaceholder+" ", "") |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // match line by line |
| 109 | if foundBoilerplateStart { |
| 110 | if line != bpLine { |
| 111 | return fmt.Errorf("boilerplate line %d does not match\nexpected: %q\ngot: %q", idx+1, bpLine, line) |
| 112 | } |
| 113 | idx++ |
| 114 | // exit after the last line is found |
| 115 | if strings.Index(line, boilerPlateEnd) == 0 { |
| 116 | break |
| 117 | } |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | if !foundBoilerplateStart { |
| 122 | return errors.New("the file is missing a boilerplate") |
| 123 | } |
| 124 | if idx < len(boilerPlate) { |
| 125 | return errors.New("boilerplate has missing lines") |
| 126 | } |
| 127 | return nil |
| 128 | } |
| 129 | |
| 130 | // verifyFile verifies if a file contains the boilerplate |
| 131 | func verifyFile(filePath string) error { |
no test coverage detected