Repository navigation
refactor(test): one workflow parser for internal/config (#504) - #540
Merged
Merged
Conversation
Closes #504. `.github/workflows` was being read seven different ways in this package. #504 was filed when there were two; the fourth arrived with #531's timeout gate, and counting the text walks properly there were seven: - two struct parsers — `release_gate_test.go`'s `workflowFile`/`workflowJobDef`, and `release_signing_test.go`'s `signingWorkflow` for one job's two keys - two `map[any]any` walks — `job_timeouts_test.go` and `pull_request_triggers_test.go` - three text walks over the directory — `action_pins_test.go`, `workflow_expressions_test.go` and `release_checksums_test.go`, each with its own extension filter and its own copy of the floor Everything now goes through `internal/config/workflow_test.go`: `workflowFile`/`workflowJobDef`, `needsOf`, `triggerSection`, `readWorkflow`, `workflowSource`, `readWorkflowTexts`, `readWorkflows`, `cutStep`, `jobStep`, `jobNamed`, and one `workflowFileFloor`. The two facts that cost a bug each are comments on the shared code, because each copy had to rediscover them: - `On` is a **struct field tagged `yaml:"on"`**. yaml.v2 reads YAML 1.1, where `on` is a boolean literal, so a bare `on:` key unmarshals as `true` and a `map[string]any` lookup finds no triggers at all — every file then passes having inspected nothing. `triggerSection` keeps the `[]any{true, "on"}` spelling documented for the map form. - a step-scoped assertion has to **cut the step out first**. `cosign verify-blob` appears twice in release.yml and only one of them runs; an assertion over the whole file was satisfied by the release notes' `echo` of the command, and a mutation deleting the executed verification passed. Four behaviour changes fell out of converging, all in the strict direction: - `triggerSection` returns `(cfg, present)`. `pull_request:` with no body is the wanted shape and unmarshals to nil, so a single nil result cannot tell "present, unfiltered" from "absent" — and the trigger counter that is #512's non-vacuity guard depends on exactly that distinction. - `cutStep` is a line scan. The `strings.Index` version matched a step name as a *prefix*, so a step named `Sign` would have returned `Sign and verify`'s body, and it ended the step at a hardcoded four-space `- name:`, so re-indenting a workflow would silently extend a step to end of file. - `readWorkflowTexts` accepts `.yaml` as well as `.yml`. `workflow_expressions_test.go` filtered on `filepath.Ext(name) != ".yml"`, so a `.yaml` workflow was exempt from the expression gate while the other three walks checked it — and skipping looks like passing. - `release_packages_test.go` reads `publish`'s dependencies through `needsOf` instead of substring-matching the raw `[artifacts, docker-build-push, security-scan]` text, which a job named `artifacts-summary` would have satisfied with the real dependency gone. Also removes the `//nolint:gocognit` on `TestEveryActionIsPinnedToASha`: gocognit is not in .golangci.yml's enabled set and nothing else in the tree runs it, and the nesting its explanation defended left with the walk. Net −304 lines. Counts unchanged and logged by the tests themselves: 63 action references, 3 pull-request triggers, 23 timed jobs plus 1 exempt caller, 5 workflow files.
…tal (#504) Mutation-verifying #504's shared parser found the one gate the convergence did not already cover. Collapsing `triggerSection`'s two return values into `return cfg, cfg != nil` — the exact simplification the doc comment warns against — took TestNoPullRequestTriggerFiltersItsBaseBranch from 3 triggers to 1, and it passed. The 2 it lost were ci.yml's and security.yml's, which are the two files that had #512. An empty `pull_request:` is the *wanted* shape and unmarshals to a nil mapping, so a presence check that means "I got a mapping back" exempts every correct file and keeps only the ones already carrying a filter — and a `triggers == 0` floor cannot see that, because the filtered ones are still counted. So count the unfiltered ones separately and floor them too. Re-run of the same mutation: CAUGHT.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #504.
.github/workflowswas being read seven different ways ininternal/config. #504 was filed when therewere two; the fourth arrived with #531's timeout gate, and counting the text walks properly there were
seven:
workflowFile/workflowJobDefstructrelease_gate_test.gosigningWorkflowstruct, for one job's two keysrelease_signing_test.gomap[any]anywalkjob_timeouts_test.gomap[any]anywalkpull_request_triggers_test.goaction_pins_test.goworkflow_expressions_test.gorelease_checksums_test.goEverything now goes through the new
internal/config/workflow_test.go:workflowFile/workflowJobDef,needsOf,triggerSection,readWorkflow,workflowSource,readWorkflowTexts,readWorkflows,cutStep,jobStep,jobNamed, and oneworkflowFileFloorin place of four copiesof the number 4.
The two facts that cost a bug each
Both are comments on the shared code now, because every copy had to rediscover them:
Onis a struct field taggedyaml:"on". yaml.v2 implements YAML 1.1, whereonis aboolean literal — so a bare
on:key unmarshals astrue, amap[string]anylookup finds notriggers at all, and a test that walks triggers passes on every file having inspected nothing.
triggerSectionkeeps the[]any{true, "on"}spelling documented for the map form.cosign verify-blobappears twice inrelease.yml and only one of them runs; an assertion over the whole file was satisfied by the release
notes'
echoof the command, and a mutation deleting the executed verification passed.Four behaviour changes, all in the strict direction
triggerSectionreturns(cfg, present).pull_request:with no body is the wanted shape andunmarshals to nil, so one nil result cannot tell "present, unfiltered" from "absent".
cutStepis a line scan. Thestrings.Indexversion matched a step name as a prefix — a stepnamed
Signwould have returnedSign and verify's body — and ended the step at a hardcodedfour-space
- name:, so re-indenting a workflow would silently extend a step to end of file.readWorkflowTextsaccepts.yamlas well as.yml.workflow_expressions_test.gofiltered onfilepath.Ext(name) != ".yml", so a.yamlworkflow was exempt from the expression gate while theother three walks checked it. Skipping looks like passing.
release_packages_test.goreadspublish's dependencies throughneedsOfinstead ofsubstring-matching the raw
[artifacts, docker-build-push, security-scan], which a job namedartifacts-summarywould have satisfied with the real dependency gone.Also drops the
//nolint:gocognitonTestEveryActionIsPinnedToASha: gocognit is not in.golangci.yml's enabled set and nothing else in the tree runs it, and the nesting its explanationdefended left with the walk.
Mutation-verified, and it found something
Eight mutations of the shared parser, each confirmed to still compile:
yaml:"on"tagTestCIWorkflowIsReusable,TestNoPullRequestTriggerFiltersItsBaseBranchtriggerSectiontoreturn cfg, cfg != nilcutStepnever end a stepTestTheSigningPathIsKeylessneedsOf's bare-string caseTestReleaseIsGatedByCIjobNamedmatch on the job idTestReleaseAttachesTheLinuxPackagesjobs:never parseThe survivor is the interesting one, and it was a real gap rather than a harness artifact. Collapsing
triggerSection's two return values tookTestNoPullRequestTriggerFiltersItsBaseBranchfrom 3 triggersto 1, and it passed — because the 2 it lost were ci.yml's and security.yml's, the two files that
had #512. An empty
pull_request:is a nil mapping, so "present" meaning "I got a mapping back" exemptsevery correct file and keeps only the ones already carrying a filter, and a
triggers == 0floorcannot see that.
Second commit counts the unfiltered triggers separately and floors them too. Same mutation re-run:
CAUGHT, naming the cause.
Verification
go build ./...,go vet ./...cleango test -count=1 -race ./internal/config/passes(2 unfiltered), 23 timed jobs plus 1 exempt reusable-workflow caller, 5 workflow files
golangci-lintwas not run locally: the installed binary is built against go1.25 and refuses ago1.26.6 target. CI's pinned v2.12.2 is the measurement that counts.