| 1 | // Package thirdparty executes integration tests based on third-party projects that use avo. |
| 2 | package thirdparty |
| 3 | |
| 4 | import ( |
| 5 | "encoding/json" |
| 6 | "errors" |
| 7 | "fmt" |
| 8 | "io" |
| 9 | "os" |
| 10 | "path" |
| 11 | "path/filepath" |
| 12 | "sort" |
| 13 | "strings" |
| 14 | "time" |
| 15 | ) |
| 16 | |
| 17 | // GithubRepository specifies a repository on github. |
| 18 | type GithubRepository struct { |
| 19 | Owner string `json:"owner"` |
| 20 | Name string `json:"name"` |
| 21 | } |
| 22 | |
| 23 | func (r GithubRepository) String() string { |
| 24 | return path.Join(r.Owner, r.Name) |
| 25 | } |
| 26 | |
| 27 | // URL returns the Github repository URL. |
| 28 | func (r GithubRepository) URL() string { |
| 29 | return fmt.Sprintf("https://github.com/%s", r) |
| 30 | } |
| 31 | |
| 32 | // CloneURL returns the git clone URL. |
| 33 | func (r GithubRepository) CloneURL() string { |
| 34 | return fmt.Sprintf("https://github.com/%s.git", r) |
| 35 | } |
| 36 | |
| 37 | // Metadata about the repository. |
| 38 | type Metadata struct { |
| 39 | // Repository description. |
| 40 | Description string `json:"description,omitempty"` |
| 41 | |
| 42 | // Homepage URL. Not the same as the Github page. |
| 43 | Homepage string `json:"homepage,omitempty"` |
| 44 | |
| 45 | // Number of Github stars. |
| 46 | Stars int `json:"stars,omitempty"` |
| 47 | } |
| 48 | |
| 49 | // Project defines an integration test based on a third-party project using avo. |
| 50 | type Project struct { |
| 51 | // Repository for the project. At the moment, all projects are available on |
| 52 | // github. |
| 53 | Repository GithubRepository `json:"repository"` |
| 54 | |
| 55 | // Repository metadata. |
| 56 | Metadata Metadata `json:"metadata"` |
| 57 | |
| 58 | // Default git branch. This is used when testing against the latest version. |
| 59 | DefaultBranch string `json:"default_branch,omitempty"` |
| 60 | |
| 61 | // Version as a git sha, tag or branch. |
| 62 | Version string `json:"version"` |
| 63 | |
| 64 | // If the project test has a known problem, record it by setting this to a |
| 65 | // non-zero avo issue number. If set, the project will be skipped in |
| 66 | // testing. |
| 67 | KnownIssue int `json:"known_issue,omitempty"` |
| 68 | |
| 69 | // Packages within the project to test. |
| 70 | Packages []*Package `json:"packages"` |
| 71 | } |
| 72 | |
| 73 | func (p *Project) defaults(set bool) { |
| 74 | for _, pkg := range p.Packages { |
| 75 | pkg.defaults(set) |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // Validate project definition. |
| 80 | func (p *Project) Validate() error { |
| 81 | if p.DefaultBranch == "" { |
| 82 | return errors.New("missing default branch") |
| 83 | } |
| 84 | if p.Version == "" { |
| 85 | return errors.New("missing version") |
| 86 | } |
| 87 | if len(p.Packages) == 0 { |
| 88 | return errors.New("missing packages") |
| 89 | } |
| 90 | for _, pkg := range p.Packages { |
| 91 | if err := pkg.Validate(); err != nil { |
| 92 | return fmt.Errorf("package %s: %w", pkg.Name(), err) |
| 93 | } |
| 94 | } |
| 95 | return nil |
| 96 | } |
| 97 | |
| 98 | // ID returns an identifier for the project. |
| 99 | func (p *Project) ID() string { |
| 100 | return strings.ReplaceAll(p.Repository.String(), "/", "-") |
| 101 | } |
| 102 | |
| 103 | // Skip reports whether the project test should be skipped. If skipped, a known |
| 104 | // issue will be set. |
| 105 | func (p *Project) Skip() bool { |
| 106 | return p.KnownIssue != 0 |
| 107 | } |
| 108 | |
| 109 | // Reason returns the reason why the test is skipped. |
| 110 | func (p *Project) Reason() string { |
| 111 | return fmt.Sprintf("https://github.com/mmcloughlin/avo/issues/%d", p.KnownIssue) |
| 112 | } |
| 113 | |
| 114 | // Step represents a set of commands to run as part of the testing plan for a |
| 115 | // third-party package. |
| 116 | type Step struct { |
| 117 | Name string `json:"name,omitempty"` |
| 118 | WorkingDirectory string `json:"dir,omitempty"` |
| 119 | Commands []string `json:"commands"` |
| 120 | } |
| 121 | |
| 122 | // Validate step parameters. |
| 123 | func (s *Step) Validate() error { |
| 124 | if s.Name == "" { |
| 125 | return errors.New("missing name") |
| 126 | } |
| 127 | if len(s.Commands) == 0 { |
| 128 | return errors.New("missing commands") |
| 129 | } |
| 130 | return nil |
| 131 | } |
| 132 | |
| 133 | // Package defines an integration test for a package within a project. |
| 134 | type Package struct { |
| 135 | // Sub-package within the project under test. Used as the root directory for |
| 136 | // the test unless explicitly overridden. |
| 137 | SubPackage string `json:"pkg,omitempty"` |
| 138 | |
| 139 | // Path to the module file for the avo generator package. This is necessary |
| 140 | // so the integration test can insert replace directives to point at the avo |
| 141 | // version under test. |
| 142 | Module string `json:"module"` |
| 143 | |
| 144 | // Root directory for all steps in the test. All file path references will |
| 145 | // be relative to this directory. If empty, the sub-package directory is |
| 146 | // used, or otherwise the root of the repository. |
| 147 | Root string `json:"root,omitempty"` |
| 148 | |
| 149 | // Setup steps. These run prior to the insertion of avo replace directives, |
| 150 | // therefore should be used if it's necessary to initialize new go modules |
| 151 | // within the repository. |
| 152 | Setup []*Step `json:"setup,omitempty"` |
| 153 | |
| 154 | // Steps to run the avo code generator. |
| 155 | Generate []*Step `json:"generate"` |
| 156 | |
| 157 | // Test steps. If empty, defaults to "go test ./...". |
| 158 | Test []*Step `json:"test,omitempty"` |
| 159 | } |
| 160 | |
| 161 | // defaults sets or removes default field values. |
| 162 | func (p *Package) defaults(set bool) { |
| 163 | for _, stage := range []struct { |
| 164 | Steps []*Step |
| 165 | DefaultName string |
| 166 | }{ |
| 167 | {p.Setup, "Setup"}, |
| 168 | {p.Generate, "Generate"}, |
| 169 | {p.Test, "Test"}, |
| 170 | } { |
| 171 | if len(stage.Steps) == 1 { |
| 172 | stage.Steps[0].Name = applydefault(set, stage.Steps[0].Name, stage.DefaultName) |
| 173 | } |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | func applydefault(set bool, s, def string) string { |
| 178 | switch { |
| 179 | case set && s == "": |
| 180 | return def |
| 181 | case !set && s == def: |
| 182 | return "" |
| 183 | default: |
| 184 | return s |
| 185 | } |
| 186 | } |
| 187 | |
| 188 | // Validate package definition. |
| 189 | func (p *Package) Validate() error { |
| 190 | if p.Module == "" { |
| 191 | return errors.New("missing module") |
| 192 | } |
| 193 | if len(p.Generate) == 0 { |
| 194 | return errors.New("no generate commands") |
| 195 | } |
| 196 | |
| 197 | stages := map[string][]*Step{ |
| 198 | "setup": p.Setup, |
| 199 | "generate": p.Generate, |
| 200 | "test": p.Test, |
| 201 | } |
| 202 | for name, steps := range stages { |
| 203 | for _, s := range steps { |
| 204 | if err := s.Validate(); err != nil { |
| 205 | return fmt.Errorf("%s step: %w", name, err) |
| 206 | } |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | return nil |
| 211 | } |
| 212 | |
| 213 | // Name of the package. |
| 214 | func (p *Package) Name() string { |
| 215 | if p.IsRoot() { |
| 216 | return "root" |
| 217 | } |
| 218 | return p.SubPackage |
| 219 | } |
| 220 | |
| 221 | // IsRoot reports whether the package is the root of the containing project. |
| 222 | func (p *Package) IsRoot() bool { |
| 223 | return p.SubPackage == "" |
| 224 | } |
| 225 | |
| 226 | // WorkingDirectory returns the base directory for all steps in the test. |
| 227 | func (p *Package) WorkingDirectory() string { |
| 228 | if p.Root != "" { |
| 229 | return p.Root |
| 230 | } |
| 231 | if p.SubPackage != "" { |
| 232 | return p.SubPackage |
| 233 | } |
| 234 | return "" |
| 235 | } |
| 236 | |
| 237 | // Context specifies execution environment parameters for a third-party test. |
| 238 | type Context struct { |
| 239 | // Path to the avo version under test. |
| 240 | AvoDirectory string |
| 241 | |
| 242 | // Path to the checked out third-party repository. |
| 243 | RepositoryDirectory string |
| 244 | } |
| 245 | |
| 246 | // Steps generates the list of steps required to execute the integration test |
| 247 | // for this package. Context specifies execution environment parameters. |
| 248 | func (p *Package) Steps(c *Context) []*Step { |
| 249 | var steps []*Step |
| 250 | |
| 251 | // Optional setup. |
| 252 | steps = append(steps, p.Setup...) |
| 253 | |
| 254 | // Replace avo dependency. |
| 255 | const invalid = "v0.0.0-00010101000000-000000000000" |
| 256 | moddir := filepath.Dir(p.Module) |
| 257 | modfile := filepath.Base(p.Module) |
| 258 | steps = append(steps, &Step{ |
| 259 | Name: "Avo Module Replacement", |
| 260 | WorkingDirectory: moddir, |
| 261 | Commands: []string{ |
| 262 | "go mod edit -modfile=" + modfile + " -require=github.com/mmcloughlin/avo@" + invalid, |
| 263 | "go mod edit -modfile=" + modfile + " -replace=github.com/mmcloughlin/avo=" + c.AvoDirectory, |
| 264 | "go mod tidy -modfile=" + modfile, |
| 265 | }, |
| 266 | }) |
| 267 | |
| 268 | // Run generation. |
| 269 | steps = append(steps, p.Generate...) |
| 270 | |
| 271 | // Display changes. |
| 272 | steps = append(steps, &Step{ |
| 273 | Name: "Diff", |
| 274 | Commands: []string{"git diff"}, |
| 275 | }) |
| 276 | |
| 277 | // Tests. |
| 278 | if len(p.Test) > 0 { |
| 279 | steps = append(steps, p.Test...) |
| 280 | } else { |
| 281 | steps = append(steps, &Step{ |
| 282 | Name: "Test", |
| 283 | Commands: []string{ |
| 284 | "go test ./...", |
| 285 | }, |
| 286 | }) |
| 287 | } |
| 288 | |
| 289 | // Prepend sub-directory to every step. |
| 290 | if dir := p.WorkingDirectory(); dir != "" { |
| 291 | for _, s := range steps { |
| 292 | s.WorkingDirectory = filepath.Join(dir, s.WorkingDirectory) |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | return steps |
| 297 | } |
| 298 | |
| 299 | // Test case for a given package within a project. |
| 300 | type Test struct { |
| 301 | Project *Project |
| 302 | Package *Package |
| 303 | } |
| 304 | |
| 305 | // ID returns an identifier for the test case. |
| 306 | func (t *Test) ID() string { |
| 307 | pkgpath := path.Join(t.Project.Repository.String(), t.Package.SubPackage) |
| 308 | return strings.ReplaceAll(pkgpath, "/", "-") |
| 309 | } |
| 310 | |
| 311 | // Projects is a collection of third-party integration tests. |
| 312 | type Projects []*Project |
| 313 | |
| 314 | func (p Projects) defaults(set bool) { |
| 315 | for _, prj := range p { |
| 316 | prj.defaults(set) |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | // Validate the project collection. |
| 321 | func (p Projects) Validate() error { |
| 322 | // Projects are valid. |
| 323 | for _, prj := range p { |
| 324 | if err := prj.Validate(); err != nil { |
| 325 | return fmt.Errorf("project %s: %w", prj.ID(), err) |
| 326 | } |
| 327 | } |
| 328 | |
| 329 | // No duplicate project IDs. |
| 330 | pid := map[string]bool{} |
| 331 | for _, prj := range p { |
| 332 | id := prj.ID() |
| 333 | if pid[id] { |
| 334 | return fmt.Errorf("duplicate project %q", id) |
| 335 | } |
| 336 | pid[id] = true |
| 337 | } |
| 338 | |
| 339 | // No duplicate test IDs. |
| 340 | tid := map[string]bool{} |
| 341 | for _, t := range p.Tests() { |
| 342 | id := t.ID() |
| 343 | if tid[id] { |
| 344 | return fmt.Errorf("duplicate test %q", id) |
| 345 | } |
| 346 | tid[id] = true |
| 347 | } |
| 348 | |
| 349 | return nil |
| 350 | } |
| 351 | |
| 352 | // Tests returns all test cases for the projects collection. |
| 353 | func (p Projects) Tests() []*Test { |
| 354 | var ts []*Test |
| 355 | for _, prj := range p { |
| 356 | for _, pkg := range prj.Packages { |
| 357 | ts = append(ts, &Test{ |
| 358 | Project: prj, |
| 359 | Package: pkg, |
| 360 | }) |
| 361 | } |
| 362 | } |
| 363 | return ts |
| 364 | } |
| 365 | |
| 366 | // Ranked returns a copy of the projects list ranked in desending order of |
| 367 | // popularity. |
| 368 | func (p Projects) Ranked() Projects { |
| 369 | ranked := append(Projects(nil), p...) |
| 370 | sort.SliceStable(ranked, func(i, j int) bool { |
| 371 | return ranked[i].Metadata.Stars > ranked[j].Metadata.Stars |
| 372 | }) |
| 373 | return ranked |
| 374 | } |
| 375 | |
| 376 | // Top returns the top n most popular projects. |
| 377 | func (p Projects) Top(n int) Projects { |
| 378 | top := p.Ranked() |
| 379 | if len(top) > n { |
| 380 | top = top[:n] |
| 381 | } |
| 382 | return top |
| 383 | } |
| 384 | |
| 385 | // Suite defines a third-party test suite. |
| 386 | type Suite struct { |
| 387 | // Projects to test. |
| 388 | Projects Projects `json:"projects"` |
| 389 | |
| 390 | // Time of the last update to project metadata. |
| 391 | MetadataLastUpdate time.Time `json:"metadata_last_update"` |
| 392 | } |
| 393 | |
| 394 | func (s *Suite) defaults(set bool) { |
| 395 | s.Projects.defaults(set) |
| 396 | } |
| 397 | |
| 398 | // Validate the test suite. |
| 399 | func (s *Suite) Validate() error { |
| 400 | if s.MetadataLastUpdate.IsZero() { |
| 401 | return errors.New("empty metadata update time") |
| 402 | } |
| 403 | |
| 404 | if s.MetadataLastUpdate.Location() != time.UTC { |
| 405 | return errors.New("metadata update time not in UTC") |
| 406 | } |
| 407 | |
| 408 | if err := s.Projects.Validate(); err != nil { |
| 409 | return err |
| 410 | } |
| 411 | |
| 412 | return nil |
| 413 | } |
| 414 | |
| 415 | // LoadSuite loads a test suite from JSON format. |
| 416 | func LoadSuite(r io.Reader) (*Suite, error) { |
| 417 | var s *Suite |
| 418 | d := json.NewDecoder(r) |
| 419 | d.DisallowUnknownFields() |
| 420 | if err := d.Decode(&s); err != nil { |
| 421 | return nil, err |
| 422 | } |
| 423 | s.defaults(true) |
| 424 | return s, nil |
| 425 | } |
| 426 | |
| 427 | // LoadSuiteFile loads a test suite from a JSON file. |
| 428 | func LoadSuiteFile(filename string) (*Suite, error) { |
| 429 | f, err := os.Open(filename) |
| 430 | if err != nil { |
| 431 | return nil, err |
| 432 | } |
| 433 | defer f.Close() |
| 434 | return LoadSuite(f) |
| 435 | } |
| 436 | |
| 437 | // StoreSuite writes a test suite in JSON format. |
| 438 | func StoreSuite(w io.Writer, s *Suite) error { |
| 439 | e := json.NewEncoder(w) |
| 440 | e.SetIndent("", " ") |
| 441 | s.defaults(false) |
| 442 | err := e.Encode(s) |
| 443 | s.defaults(true) |
| 444 | return err |
| 445 | } |
| 446 | |
| 447 | // StoreSuiteFile writes a test suite to a JSON file. |
| 448 | func StoreSuiteFile(filename string, s *Suite) error { |
| 449 | f, err := os.Create(filename) |
| 450 | if err != nil { |
| 451 | return err |
| 452 | } |
| 453 | defer f.Close() |
| 454 | return StoreSuite(f, s) |
| 455 | } |