|
| 1 | +package githubplugin_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestReleaseArchiveIncludesGitHubRunnerProvider(t *testing.T) { |
| 10 | + data, err := os.ReadFile(".goreleaser.yaml") |
| 11 | + if err != nil { |
| 12 | + t.Fatalf("read .goreleaser.yaml: %v", err) |
| 13 | + } |
| 14 | + text := string(data) |
| 15 | + |
| 16 | + if !goreleaserBuildIncludes(text, "github-runner-provider", []string{ |
| 17 | + "main: ./cmd/github-runner-provider", |
| 18 | + "binary: github-runner-provider", |
| 19 | + }) { |
| 20 | + t.Fatal("release config must build the versioned github-runner-provider binary") |
| 21 | + } |
| 22 | + |
| 23 | + if !goreleaserArchiveIncludesBuild(text, "workflow-plugin-github", "github-runner-provider") { |
| 24 | + t.Fatal("release archive must include github-runner-provider so wfctl plugin install/fetch can provide it") |
| 25 | + } |
| 26 | + |
| 27 | + if _, err := os.Stat("cmd/github-runner-provider/main.go"); err != nil { |
| 28 | + t.Fatalf("github-runner-provider command must exist: %v", err) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestReleaseArchiveCheckRejectsProviderBuildOutsideArchive(t *testing.T) { |
| 33 | + config := ` |
| 34 | +builds: |
| 35 | + - id: workflow-plugin-github |
| 36 | + main: ./cmd/workflow-plugin-github |
| 37 | + - id: github-runner-provider |
| 38 | + main: ./cmd/github-runner-provider |
| 39 | + binary: github-runner-provider |
| 40 | +
|
| 41 | +archives: |
| 42 | + - id: workflow-plugin-github |
| 43 | + ids: |
| 44 | + - workflow-plugin-github |
| 45 | +` |
| 46 | + |
| 47 | + if goreleaserArchiveIncludesBuild(config, "workflow-plugin-github", "github-runner-provider") { |
| 48 | + t.Fatal("archive check must reject configs that build github-runner-provider without packaging it") |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +func goreleaserBuildIncludes(config, id string, required []string) bool { |
| 53 | + builds := topLevelSection(config, "builds:") |
| 54 | + build := listItemWithID(builds, id) |
| 55 | + if build == "" { |
| 56 | + return false |
| 57 | + } |
| 58 | + for _, want := range required { |
| 59 | + if !strings.Contains(build, want) { |
| 60 | + return false |
| 61 | + } |
| 62 | + } |
| 63 | + return true |
| 64 | +} |
| 65 | + |
| 66 | +func goreleaserArchiveIncludesBuild(config, archiveID, buildID string) bool { |
| 67 | + archives := topLevelSection(config, "archives:") |
| 68 | + archive := listItemWithID(archives, archiveID) |
| 69 | + return strings.Contains(archive, "ids:") && strings.Contains(archive, "- "+buildID) |
| 70 | +} |
| 71 | + |
| 72 | +func topLevelSection(config, header string) string { |
| 73 | + lines := strings.Split(config, "\n") |
| 74 | + for i, line := range lines { |
| 75 | + if line != header { |
| 76 | + continue |
| 77 | + } |
| 78 | + |
| 79 | + var section []string |
| 80 | + for _, next := range lines[i+1:] { |
| 81 | + if next != "" && !strings.HasPrefix(next, " ") && !strings.HasPrefix(next, "\t") { |
| 82 | + break |
| 83 | + } |
| 84 | + section = append(section, next) |
| 85 | + } |
| 86 | + return strings.Join(section, "\n") |
| 87 | + } |
| 88 | + return "" |
| 89 | +} |
| 90 | + |
| 91 | +func listItemWithID(section, id string) string { |
| 92 | + lines := strings.Split(section, "\n") |
| 93 | + marker := "- id: " + id |
| 94 | + for i, line := range lines { |
| 95 | + if strings.TrimSpace(line) != marker { |
| 96 | + continue |
| 97 | + } |
| 98 | + |
| 99 | + item := []string{line} |
| 100 | + for _, next := range lines[i+1:] { |
| 101 | + if strings.HasPrefix(next, " - id: ") { |
| 102 | + break |
| 103 | + } |
| 104 | + item = append(item, next) |
| 105 | + } |
| 106 | + return strings.Join(item, "\n") |
| 107 | + } |
| 108 | + return "" |
| 109 | +} |
0 commit comments