Files
coder/scripts/releaser/v2/prepare_test.go
T
Garrett Delfosse bfbacd64f4 refactor: consolidate release tooling into a single releaser command (#27034)
## What

Consolidates the two separate release programs into a single command at
`scripts/releaser`:

- `scripts/releaser/v1/` — the former interactive releaser (package
`v1`).
- `scripts/releaser/v2/` — the former `scripts/release-action` CI tool
(package `v2`).
- `scripts/releaser/main.go` — new entrypoint. Runs the **v2** tooling
by
  default and the **v1** interactive wizard with `--legacy`.

## CLI shape

Three documented subcommands, each backed by v2 `prepare-release` with
the
release type baked in:

- `releaser rc` — tag a release candidate
- `releaser branch` — cut a new release branch and tag its first RC
- `releaser release` — tag a stable release or patch

The former release-action verbs (`calculate-version`, `prepare-release`,
`generate-notes`, `publish`) are retained as **hidden** top-level
commands with
identical flags and stdout, so `tag-and-release.yaml` migrates with a
path-only
change (`scripts/release-action` -> `scripts/releaser`). `--legacy` runs
the v1
wizard and is mutually exclusive with the subcommands.
`scripts/release.sh` now
launches `releaser --legacy`.

All file moves are rename-detected by git, so the per-file diff is just
the
package declaration.

## Testing

- `go build ./scripts/...`, `go vet ./scripts/releaser/...`, `go test
./scripts/releaser/...`
- `golangci-lint run ./scripts/releaser/...`, `make lint/emdash`,
`shellcheck`, `actionlint`
- Smoke: `releaser --help` shows only rc/branch/release; hidden verbs
still run;
`releaser rc --ref main --dry-run` emits the same JSON contract;
`--legacy rc`
  errors cleanly.

<details>
<summary>Implementation plan</summary>

# Plan: Consolidate release tooling into a single `scripts/releaser`
command

## Goal

Merge the two separate release programs into one binary at
`scripts/releaser`:

- `scripts/releaser/v1/` — the current interactive releaser (package
`v1`).
- `scripts/releaser/v2/` — the current CI `scripts/release-action`
(package `v2`).
- `scripts/releaser/main.go` — new entrypoint (package `main`).
  - Uses v2 by default, v1 with `--legacy`.
- Exposes 3 subcommands: `rc`, `branch` (cut release branch), `release`.

## Design decision (Option A, chosen)

The workflow needs `prepare-release`, `generate-notes`, and `publish`
invokable
separately (a build happens between prepare and publish). The latter two
are
version-driven and type-agnostic, so they do not map cleanly onto
`rc`/`branch`/`release`.

- Visible subcommands `rc`, `branch`, `release` run v2 `prepare-release`
with the
  type baked in and print the same JSON.
- Hidden verbs `calculate-version`, `prepare-release`, `generate-notes`,
  `publish` keep byte-identical flags/stdout, so the workflow change is
  path-only. Lowest risk; honors "3 subcommands" from a UX perspective.

## `--legacy` semantics

- `releaser --legacy` runs the v1 interactive wizard (preserves today's
  behavior; the wizard auto-detects RC vs release from the branch).
- `--legacy` is mutually exclusive with the subcommands (clear error if
  combined), because v1 auto-detects type and cannot cut a branch.

## Work items

1. Create `v1` and `v2` packages via `git mv`, renaming `package main`.
Move the `owner`/`repo` consts into each package. Add `v1.Run(inv,
dryRun)`
   (old wizard `main()` body) and v2 command builders (`CICommands`,
   `TypeCommand`) so internals stay unexported.
2. New `scripts/releaser/main.go`: top-level `releaser` with `--legacy`,
the 3
subcommands, and the hidden compat verbs; delegates to `v1.Run` for
legacy.
3. Update references: `tag-and-release.yaml` (3 command paths + header
comment)
   and `scripts/release.sh` (`--legacy`).
4. Verify: build, vet, test, `go run` smoke tests, fmt, lint.
5. Open a single PR from a feature branch.

## Risks / notes

- stdout contract for rc/branch/release and the hidden verbs must stay
identical
  (workflow parses stdout); logs go to stderr.
- Patch releases from pre-existing `release/X.Y` branches run those
branches'
own (old) workflow + `scripts/release-action`, so they stay
self-consistent.
New releases cut from branches containing this change get the new
workflow +
`scripts/releaser`. No forwarding stub needed since code and workflow
ship
  together.

</details>

---

This PR was created by Coder Agents on behalf of @f0ssel.
2026-07-07 11:13:50 -04:00

188 lines
5.0 KiB
Go

package v2 //nolint:testpackage // Tests unexported release helpers.
import (
"bytes"
"io"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
// mockExecutor records mutating calls and delegates read-only calls
// to configurable functions.
type mockExecutor struct {
// MutationCalls records all calls to RunMutation and
// RunMutationStdout as "name arg1 arg2 ..." strings.
MutationCalls []string
// RunOutputFunc is called for RunOutput. If nil, returns ("", error).
RunOutputFunc func(name string, args ...string) (string, error)
// RunFunc is called for Run. If nil, returns nil.
RunFunc func(name string, args ...string) error
}
func (m *mockExecutor) RunOutput(name string, args ...string) (string, error) {
if m.RunOutputFunc != nil {
return m.RunOutputFunc(name, args...)
}
return "", nil
}
func (m *mockExecutor) Run(name string, args ...string) error {
if m.RunFunc != nil {
return m.RunFunc(name, args...)
}
return nil
}
func (m *mockExecutor) RunMutation(name string, args ...string) error {
call := name
for _, a := range args {
call += " " + a
}
m.MutationCalls = append(m.MutationCalls, call)
return nil
}
func (m *mockExecutor) RunMutationStdout(_, _ io.Writer, name string, args ...string) error {
call := name
for _, a := range args {
call += " " + a
}
m.MutationCalls = append(m.MutationCalls, call)
return nil
}
func TestCreateAndPushTag_New(t *testing.T) {
t.Parallel()
mock := &mockExecutor{
RunOutputFunc: func(name string, args ...string) (string, error) {
// Simulate tag not existing: git rev-parse --verify fails.
if len(args) >= 2 && args[0] == "rev-parse" && args[1] == "--verify" {
return "", assert.AnError
}
return "", nil
},
}
err := createAndPushTag(mock, "v2.21.0", "abc123")
require.NoError(t, err)
require.Len(t, mock.MutationCalls, 2)
assert.Contains(t, mock.MutationCalls[0], "git tag -a v2.21.0 -m Release v2.21.0 abc123")
assert.Contains(t, mock.MutationCalls[1], "git push origin refs/tags/v2.21.0:refs/tags/v2.21.0")
}
func TestCreateAndPushTag_AlreadyExistsMatching(t *testing.T) {
t.Parallel()
mock := &mockExecutor{
RunOutputFunc: func(name string, args ...string) (string, error) {
// Simulate tag existing at the correct commit.
if len(args) >= 2 && args[0] == "rev-parse" && args[1] == "--verify" {
return "abc123", nil
}
return "", nil
},
}
err := createAndPushTag(mock, "v2.21.0", "abc123")
require.NoError(t, err)
// No mutations should happen.
assert.Empty(t, mock.MutationCalls)
}
func TestCreateAndPushTag_AlreadyExistsMismatch(t *testing.T) {
t.Parallel()
mock := &mockExecutor{
RunOutputFunc: func(name string, args ...string) (string, error) {
if len(args) >= 2 && args[0] == "rev-parse" && args[1] == "--verify" {
return "different_sha", nil
}
return "", nil
},
}
err := createAndPushTag(mock, "v2.21.0", "abc123")
require.Error(t, err)
assert.Contains(t, err.Error(), "already exists at different_sha")
assert.Empty(t, mock.MutationCalls)
}
func TestCreateAndPushBranch_New(t *testing.T) {
t.Parallel()
mock := &mockExecutor{
RunOutputFunc: func(name string, args ...string) (string, error) {
// Simulate branch not existing: ls-remote fails.
if len(args) >= 1 && args[0] == "ls-remote" {
return "", assert.AnError
}
return "", nil
},
}
err := createAndPushBranch(mock, "release/2.21", "abc123")
require.NoError(t, err)
require.Len(t, mock.MutationCalls, 1)
assert.Contains(t, mock.MutationCalls[0], "git push origin abc123:refs/heads/release/2.21")
}
func TestCreateAndPushBranch_AlreadyExistsMatching(t *testing.T) {
t.Parallel()
mock := &mockExecutor{
RunOutputFunc: func(name string, args ...string) (string, error) {
if len(args) >= 1 && args[0] == "ls-remote" {
return "abc123\trefs/heads/release/2.21", nil
}
return "", nil
},
}
err := createAndPushBranch(mock, "release/2.21", "abc123")
require.NoError(t, err)
assert.Empty(t, mock.MutationCalls)
}
func TestCreateAndPushBranch_AlreadyExistsMismatch(t *testing.T) {
t.Parallel()
mock := &mockExecutor{
RunOutputFunc: func(name string, args ...string) (string, error) {
if len(args) >= 1 && args[0] == "ls-remote" {
return "other_sha\trefs/heads/release/2.21", nil
}
return "", nil
},
}
err := createAndPushBranch(mock, "release/2.21", "abc123")
require.Error(t, err)
assert.Contains(t, err.Error(), "already exists at other_sha")
assert.Empty(t, mock.MutationCalls)
}
func TestDryRunExecutor_SkipsMutations(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
exec := newDryRunExecutor(&buf)
// RunMutation should print, not execute.
err := exec.RunMutation("git", "tag", "-a", "v2.21.0", "-m", "Release v2.21.0", "abc123")
require.NoError(t, err)
assert.Contains(t, buf.String(), "[dry-run] would run: git tag -a v2.21.0")
buf.Reset()
err = exec.RunMutation("git", "push", "origin", "refs/tags/v2.21.0:refs/tags/v2.21.0")
require.NoError(t, err)
assert.Contains(t, buf.String(), "[dry-run] would run: git push origin refs/tags/v2.21.0:refs/tags/v2.21.0")
}