scripts/check-deps.sh previously ran `go list -m all`, which fails with "module lookup disabled by GOPROXY=off" whenever the module cache does not hold the full module graph, even though the build only needs part of it. Read the pins with `go mod edit -json` instead: the online branch asks the module proxy about only the pinned modules, and the offline branch compares those pins with the newest versions in the module cache. Neither path loads the whole graph, and an empty cache is reported instead of failing. Run the dependency check from the single check job that already gates pull requests, so one action covers both the code/tests and dependency freshness. Add internal/ci tests that parse .gitea/workflows/ci.yml, fail if the workflow stops running on pull requests or stops invoking the check scripts, and fail if those scripts are not executable. Document the two-step job and the new mise tasks.
114 lines
2.8 KiB
Go
114 lines
2.8 KiB
Go
// Package ci tests the repository's Gitea Actions configuration.
|
|
package ci
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"testing"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// workflowPath is the workflow that gates every pull request.
|
|
const workflowPath = "../../.gitea/workflows/ci.yml"
|
|
|
|
func TestPullRequestWorkflow(t *testing.T) {
|
|
data, err := os.ReadFile(filepath.FromSlash(workflowPath))
|
|
if err != nil {
|
|
t.Fatalf("read %s: %v", workflowPath, err)
|
|
}
|
|
|
|
var doc yaml.Node
|
|
if err := yaml.Unmarshal(data, &doc); err != nil {
|
|
t.Fatalf("parse %s: %v", workflowPath, err)
|
|
}
|
|
if len(doc.Content) != 1 || doc.Content[0].Kind != yaml.MappingNode {
|
|
t.Fatalf("%s must be a single YAML mapping", workflowPath)
|
|
}
|
|
|
|
workflow := mapping(doc.Content[0])
|
|
on := workflow["on"]
|
|
if on == nil {
|
|
t.Fatal("workflow has no on: trigger")
|
|
}
|
|
if mapping(on)["pull_request"] == nil {
|
|
t.Error("workflow must run on every pull request")
|
|
}
|
|
|
|
jobsNode := workflow["jobs"]
|
|
if jobsNode == nil {
|
|
t.Fatal("workflow has no jobs")
|
|
}
|
|
jobs := mapping(jobsNode)
|
|
if len(jobs) != 1 {
|
|
t.Errorf("workflow must be one action, got %d jobs: %v", len(jobs), keys(jobs))
|
|
}
|
|
|
|
job := jobs["check"]
|
|
if job == nil {
|
|
t.Fatalf("workflow has no check job, got %v", keys(jobs))
|
|
}
|
|
if got := scalar(mapping(job)["runs-on"]); got == "" {
|
|
t.Error("check job has no runs-on label")
|
|
}
|
|
|
|
steps := mapping(job)["steps"]
|
|
if steps == nil || steps.Kind != yaml.SequenceNode {
|
|
t.Fatal("check job has no steps")
|
|
}
|
|
|
|
runs := map[string]bool{}
|
|
for _, step := range steps.Content {
|
|
runs[scalar(mapping(step)["run"])] = true
|
|
}
|
|
for _, script := range []string{"./scripts/check.sh", "./scripts/check-deps.sh"} {
|
|
if !runs[script] {
|
|
t.Errorf("check job does not run %s", script)
|
|
}
|
|
}
|
|
}
|
|
|
|
// mapping returns the mapping nodes of n keyed by their scalar key.
|
|
func mapping(n *yaml.Node) map[string]*yaml.Node {
|
|
out := map[string]*yaml.Node{}
|
|
if n == nil || n.Kind != yaml.MappingNode {
|
|
return out
|
|
}
|
|
for i := 0; i < len(n.Content)-1; i += 2 {
|
|
out[n.Content[i].Value] = n.Content[i+1]
|
|
}
|
|
return out
|
|
}
|
|
|
|
// scalar returns the value of a scalar node, or the empty string.
|
|
func scalar(n *yaml.Node) string {
|
|
if n == nil || n.Kind != yaml.ScalarNode {
|
|
return ""
|
|
}
|
|
return n.Value
|
|
}
|
|
|
|
// keys returns the sorted keys of m for error messages.
|
|
func keys(m map[string]*yaml.Node) []string {
|
|
out := make([]string, 0, len(m))
|
|
for k := range m {
|
|
out = append(out, k)
|
|
}
|
|
sort.Strings(out)
|
|
return out
|
|
}
|
|
|
|
func TestCheckScriptsAreExecutable(t *testing.T) {
|
|
for _, script := range []string{"../../scripts/check.sh", "../../scripts/check-deps.sh"} {
|
|
info, err := os.Stat(filepath.FromSlash(script))
|
|
if err != nil {
|
|
t.Errorf("stat %s: %v", script, err)
|
|
continue
|
|
}
|
|
if info.Mode()&0o111 == 0 {
|
|
t.Errorf("%s is not executable; the ci workflow runs it directly", script)
|
|
}
|
|
}
|
|
}
|