fix(handlers): de-dup cfg.ConfigFiles init across inject sites (tiny) #2856
Reference in New Issue
Block a user
Delete Branch "fix/dedup-cfg-config-files-init"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
TINY orthogonal cleanup, preserved from the closed SCAFFOLD #2855 per the PM reconcile (1bcb2e75 keystone-structure call). #2845 (
acbc0da9) was MERGED with the full template-asset channel + tests, but the cfg.ConfigFiles init repetition at the two inject sites inworkspace_provision.gowas NOT touched by #2845.The repetition (pre-fix, both sites identical 4-line ceremony):
The de-dup (post-fix, extracted to
ensureConfigFileshelper):Behavior-preserving: the helper allocates on demand (same
if nil { make }ceremony), returns the SAME map (writes through the returned pointer are visible viacfg.ConfigFiles). ThemapsSamePointersentinel-write trick in the test pins this contract — a future regression that returns a copy would fail the test.New tests (2):
TestEnsureConfigFiles_AllocatesWhenNil: nil ConfigFiles → helper allocates + returns the SAME map. Sentinel-write verifies the same-map identity.TestEnsureConfigFiles_ReusesWhenNonNil: non-nil ConfigFiles → helper returns the SAME map (no copy). Pre-populated entries preserved.The existing
TestIssueAndInjectToken_NilConfigFilesAllocatedalready pins the end-to-end nil-alloc behavior via the public API (this PR doesn't change that test, just adds the more focused unit tests on the helper itself).Local validation:
go test ./internal/handlers/— clean (25.8s, all existing pass + 2 new helper tests pass)go test ./internal/provisioner/— clean (0.09s, no regressions)go vet + go build— cleanAuth pattern honored: all Gitea API calls used
${GIT_HTTP_PASSWORD}/${GITEA_ISSUE_TOKEN}env vars in-Hheaders (nocurl -u).Refs #24 (RFC #2843). Diff stat: 2 files, +96 / -6.
APPROVED: I reviewed molecule-core #2856 at head
842f07f879.This is behavior-preserving. The two injection sites still write the same keys (
.auth_tokenand.platform_inbound_secret) intocfg.ConfigFiles; the repeated nil-check/make ceremony is just extracted intoensureConfigFiles(cfg). The helper preserves the old semantics: allocate only when nil, return the same map stored on the config, and reuse an existing map without copying or clobbering caller entries.The new tests are load-bearing for the extraction:
TestEnsureConfigFiles_AllocatesWhenNilproves nil allocation and write visibility throughcfg.ConfigFiles;TestEnsureConfigFiles_ReusesWhenNonNilproves the already-populated map is reused. ThemapsSamePointersentinel is a reasonable way to assert shared map state for this contract.5-axis: correctness/robustness are covered by unchanged call-site behavior and focused tests; security surface is unchanged (same token/secret writes as before); performance is unchanged except avoiding duplicate ceremony; readability improves by removing duplicated map initialization.
Exact-head CI is green on
842f07f8: all-required, Platform Go, E2E API Smoke, Handlers Postgres, Peer Visibility, Shellcheck, and relevant canvas/chat no-op lanes all succeeded.APPROVE on
842f07f879.Reviewed the tiny ensureConfigFiles extraction: both production callers still write the same .auth_token and .platform_inbound_secret entries through the same ConfigFiles map, with nil allocation centralized and no clobber when the map is already present. The new tests are load-bearing for nil allocation and non-nil reuse, including write-through/same-map behavior.
Exact-head required/code CI is green; the only red I see is the known Local Provision real-image advisory (#2851), which is non-blocking and unrelated to this helper-only change.