1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
|
package main
import (
"fmt"
"go/ast"
"go/build/constraint"
"go/parser"
"go/token"
"io/fs"
"os"
"path/filepath"
"regexp"
"strings"
)
var (
testFileRx = regexp.MustCompile("_test.go$")
integrationTestFileRx = regexp.MustCompile("integration(_[a-z0-9_]+)?_test.go$")
helpersTestFileRx = regexp.MustCompile("helpers(_[a-z0-9_]+)?_test.go$")
workingDirectory string
errors []pathError
integrationTag = buildTag{name: "integration", value: true}
nonIntegrationTag = buildTag{name: "non-integration", value: false}
tagOverrides = tagOverridesFilesMap{
"executors/custom/terminal_test.go": tagOverridesMap{
"windows": false,
},
"helpers/archives/zip_create_unix_test.go": tagOverridesMap{
"windows": false,
},
}
)
type tagOverridesFilesMap map[string]tagOverridesMap
type tagOverridesMap map[string]bool
type pathError struct {
path string
err error
}
type buildTag struct {
name string
value bool
}
func init() {
path, err := os.Getwd()
if err != nil {
panic(fmt.Sprintf("checking working directory: %v", err))
}
if len(os.Args) > 1 {
path = os.Args[1]
}
workingDirectory = filepath.Clean(path)
}
func main() {
fmt.Printf("Analyse build directives in test files at %q\n", workingDirectory)
walkNonIntegrationTestFiles(workingDirectory, integrationBuildConstraintDoesntExist)
walkIntegrationTestFiles(workingDirectory, integrationBuildConstraintExists)
checkErrors()
}
func walkNonIntegrationTestFiles(rootPath string, fn func(path string) error) {
walkTestFiles("non-integration", rootPath, func(walkPath string, info fs.FileInfo, _ error) error {
name := info.Name()
if !integrationTestFileRx.MatchString(name) && !helpersTestFileRx.MatchString(name) {
return fn(walkPath)
}
return nil
})
}
func walkIntegrationTestFiles(rootPath string, fn func(path string) error) {
walkTestFiles("integration", rootPath, func(walkPath string, info fs.FileInfo, _ error) error {
if integrationTestFileRx.MatchString(info.Name()) {
return fn(walkPath)
}
return nil
})
}
func walkTestFiles(testType string, rootPath string, walkFunc filepath.WalkFunc) {
fmt.Printf("\nChecking %s test files...\n", testType)
err := filepath.Walk(rootPath, func(path string, info fs.FileInfo, err error) error {
name := info.Name()
if info.IsDir() {
if name == ".git" {
return filepath.SkipDir
}
}
if !testFileRx.MatchString(name) {
return nil
}
recordError(path, walkFunc(path, info, err))
return nil
})
if err != nil {
panic(fmt.Sprintf("walking files: %v", err))
}
}
func recordError(path string, err error) {
if err == nil {
return
}
pe := pathError{path: path, err: err}
errors = append(errors, pe)
}
func integrationBuildConstraintDoesntExist(path string) error {
return checkBuildConstraints(path, nonIntegrationTag)
}
func integrationBuildConstraintExists(path string) error {
return checkBuildConstraints(path, integrationTag)
}
func checkBuildConstraints(path string, integrationTag buildTag) error {
fmt.Printf(" -> %s...\n", trimWDFromPath(path))
comments, err := parseFile(path)
if err != nil {
return err
}
expressions, err := scanAndParseBuildConstraints(comments)
if err != nil {
return err
}
for _, expr := range expressions {
if !expr.Eval(integrationEvalFn(path, integrationTag)) {
return fmt.Errorf(
"invalid integration build constraint %q evaluation for %s test file",
expr.String(),
integrationTag.name,
)
}
}
return nil
}
func parseFile(path string) ([]*ast.CommentGroup, error) {
fileSet := token.NewFileSet()
f, err := parser.ParseFile(fileSet, path, nil, parser.PackageClauseOnly+parser.ParseComments)
if err != nil {
return nil, fmt.Errorf("parsing file: %w", err)
}
if len(f.Comments) < 1 {
return nil, fmt.Errorf("missing top-level comments")
}
return f.Comments, nil
}
func scanAndParseBuildConstraints(comments []*ast.CommentGroup) ([]constraint.Expr, error) {
var expressions []constraint.Expr
for _, group := range comments {
for _, line := range group.List {
text := line.Text
if constraint.IsGoBuild(text) || constraint.IsPlusBuild(text) {
expr, err := constraint.Parse(text)
if err != nil {
return nil, fmt.Errorf("parsing constraint %q: %w", text, err)
}
expressions = append(expressions, expr)
}
}
}
return expressions, nil
}
func integrationEvalFn(path string, integrationTag buildTag) func(tag string) bool {
return func(tag string) bool {
if tag == "integration" {
return integrationTag.value
}
m, ok := tagOverrides[trimWDFromPath(path)]
if ok {
v, ok := m[tag]
if ok {
return v
}
}
return true
}
}
func checkErrors() {
fmt.Println()
if len(errors) < 1 {
fmt.Println("✔ All directives match expectations")
return
}
fmt.Println("✖ Failed directives expectations:")
for _, e := range errors {
fmt.Printf("%80s: %v\n", trimWDFromPath(e.path), e.err)
}
os.Exit(1)
}
func trimWDFromPath(path string) string {
return strings.TrimPrefix(path, workingDirectory+"/")
}
|