|
| 1 | +package readiness |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "strings" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +const projectName = "mark2note" |
| 10 | + |
| 11 | +var releaseSuffixes = []string{"darwin-amd64", "darwin-arm64", "linux-amd64", "linux-arm64"} |
| 12 | + |
| 13 | +func TestGitHubCIWorkflowReadiness(t *testing.T) { |
| 14 | + content := readTextFile(t, ".github/workflows/ci.yml") |
| 15 | + for _, want := range []string{ |
| 16 | + "name: CI", |
| 17 | + "push:", |
| 18 | + "pull_request:", |
| 19 | + "actions/checkout@v6", |
| 20 | + "fetch-depth: 0", |
| 21 | + "actions/setup-go@v6", |
| 22 | + "go-version-file: go.mod", |
| 23 | + "scripts/secret-scan.sh", |
| 24 | + "gofmt -l", |
| 25 | + "go vet ./...", |
| 26 | + "go test ./...", |
| 27 | + } { |
| 28 | + assertContains(t, content, want) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +func TestGitHubReleaseWorkflowReadiness(t *testing.T) { |
| 33 | + content := readTextFile(t, ".github/workflows/release.yml") |
| 34 | + for _, want := range []string{ |
| 35 | + "name: Release", |
| 36 | + "tags:", |
| 37 | + "v*", |
| 38 | + "contents: write", |
| 39 | + "preflight:", |
| 40 | + "build:", |
| 41 | + "release:", |
| 42 | + "needs: preflight", |
| 43 | + "needs: build", |
| 44 | + "actions/checkout@v6", |
| 45 | + "fetch-depth: 0", |
| 46 | + "actions/setup-go@v6", |
| 47 | + "go-version-file: go.mod", |
| 48 | + "scripts/ci-local.sh clean", |
| 49 | + "package=\"" + projectName + "-${SUFFIX}\"", |
| 50 | + "actions/upload-artifact@v7", |
| 51 | + "actions/download-artifact@v8", |
| 52 | + "sha256sum", |
| 53 | + "checksums.txt", |
| 54 | + "gh release view", |
| 55 | + "gh release upload", |
| 56 | + "gh release create", |
| 57 | + "GH_TOKEN", |
| 58 | + } { |
| 59 | + assertContains(t, content, want) |
| 60 | + } |
| 61 | + for _, suffix := range releaseSuffixes { |
| 62 | + assertContains(t, content, suffix) |
| 63 | + } |
| 64 | + for _, forbidden := range []string{ |
| 65 | + "secrets.", |
| 66 | + "softprops/action-gh-release", |
| 67 | + "GoReleaser", |
| 68 | + "goreleaser", |
| 69 | + "git push", |
| 70 | + "git tag", |
| 71 | + } { |
| 72 | + assertNotContains(t, content, forbidden) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +func TestGitHubCodeQLWorkflowReadiness(t *testing.T) { |
| 77 | + content := readTextFile(t, ".github/workflows/codeql.yml") |
| 78 | + for _, want := range []string{ |
| 79 | + "name: CodeQL", |
| 80 | + "push:", |
| 81 | + "pull_request:", |
| 82 | + "schedule:", |
| 83 | + "security-events: write", |
| 84 | + "actions/checkout@v6", |
| 85 | + "github/codeql-action/init@v4", |
| 86 | + "languages: go", |
| 87 | + "github/codeql-action/autobuild@v4", |
| 88 | + "github/codeql-action/analyze@v4", |
| 89 | + } { |
| 90 | + assertContains(t, content, want) |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func TestGitHubReadinessScriptUsesDedicatedEndpoints(t *testing.T) { |
| 95 | + path := "scripts/github-readiness.sh" |
| 96 | + content := readTextFile(t, path) |
| 97 | + info, err := os.Stat(path) |
| 98 | + if err != nil { |
| 99 | + t.Fatalf("expected %s to exist: %v", path, err) |
| 100 | + } |
| 101 | + if info.Mode().Perm()&0o111 == 0 { |
| 102 | + t.Fatalf("expected %s to be executable", path) |
| 103 | + } |
| 104 | + for _, want := range []string{ |
| 105 | + "gh api \"repos/${repo}\"", |
| 106 | + "security_and_analysis.secret_scanning.status", |
| 107 | + "security_and_analysis.secret_scanning_push_protection.status", |
| 108 | + "gh api \"repos/${repo}/private-vulnerability-reporting\"", |
| 109 | + "private-vulnerability-reporting", |
| 110 | + "branches/${default_branch}/protection", |
| 111 | + "code-scanning/analyses", |
| 112 | + } { |
| 113 | + assertContains(t, content, want) |
| 114 | + } |
| 115 | + assertNotContains(t, content, "security_and_analysis.private") |
| 116 | +} |
| 117 | + |
| 118 | +func readTextFile(t *testing.T, path string) string { |
| 119 | + t.Helper() |
| 120 | + content, err := os.ReadFile(path) |
| 121 | + if err != nil { |
| 122 | + t.Fatalf("expected %s to exist: %v", path, err) |
| 123 | + } |
| 124 | + return string(content) |
| 125 | +} |
| 126 | + |
| 127 | +func assertContains(t *testing.T, content, want string) { |
| 128 | + t.Helper() |
| 129 | + if !strings.Contains(content, want) { |
| 130 | + t.Fatalf("expected content to contain %q", want) |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +func assertNotContains(t *testing.T, content, forbidden string) { |
| 135 | + t.Helper() |
| 136 | + if strings.Contains(content, forbidden) { |
| 137 | + t.Fatalf("expected content not to contain %q", forbidden) |
| 138 | + } |
| 139 | +} |
0 commit comments