Files
coder/scripts/release-action/prepare_test.go
T
Garrett Delfosse ff7e0bc193 feat: add dry-run flag via CommandExecutor interface (#26422)
## Summary

Adds a `--dry-run` capability to the `release-action` Go tool and
exposes it through a **new** manual workflow, `tag-and-release.yaml`,
without disturbing the existing `release.yaml` pipeline.

PR #25162 had rewritten `release.yaml` in place to be driven by
`scripts/release-action`, which changed its `workflow_dispatch` inputs
from `release_channel`/`release_notes`/`dry_run` to
`release_type`/`commit_sha`. That broke `scripts/releaser`, which
dispatches `release.yaml` with the original inputs. This PR restores
`release.yaml` and moves the Go-driven pipeline to its own workflow.

## Workflow layout after this PR

| Workflow | Trigger | Driven by | Purpose |
|---|---|---|---|
| `release.yaml` | `scripts/releaser` (`gh workflow run`) | legacy
inline shell | Existing pipeline, restored to pre-#25162 state |
| `tag-and-release.yaml` | Manual (Actions UI) |
`scripts/release-action` Go tool | New pipeline with `prepare-release` +
`dry_run` |

`release.yaml` is restored byte-for-byte to its pre-#25162 version, so
its inputs match what `scripts/releaser` sends again.

## `release-action` design

### CommandExecutor interface

Abstracts CLI command execution behind read-only and mutating methods:

| Method | Purpose | Dry-run behavior |
|---|---|---|
| `RunOutput` | Read-only, capture stdout | Executes normally |
| `Run` | Read-only, exit code only | Executes normally |
| `RunMutation` | Changes remote state, no output | **Prints command,
skips execution** |
| `RunMutationStdout` | Changes remote state, streaming I/O | **Prints
command, skips execution** |

Two implementations: `realExecutor` (executes via `os/exec`) and
`dryRunExecutor` (delegates read-only calls, prints mutating calls).

### `prepare-release` subcommand

Composes `calculateNextVersion` with idempotent tag and branch
creation+push, emitting the same JSON as `calculate-version`. Matching
existing refs are skipped; mismatched refs error.

### `tag-and-release.yaml` `dry_run` input

When enabled: `prepare-release` runs with `--dry-run` (version
calculated, plan printed, nothing pushed), notes are generated for
inspection, and the build+publish job is skipped via an `if` guard
(cascading to homebrew/winget/docs).

## Mutating commands covered by `--dry-run`

| Command | Call site |
|---|---|
| `git tag -a <version> ...` | `createAndPushTag` |
| `git push origin refs/tags/...` | `createAndPushTag` |
| `git push origin <sha>:refs/heads/...` | `createAndPushBranch` |
| `gh release create ...` | `publishRelease` |

`git fetch --tags --force origin` is intentionally not a mutation; it
only updates local remote-tracking refs and must run for accurate
version calculation.

## Changes

- **New**: `scripts/release-action/cmdexec.go`, `prepare.go` (+ tests)
- **Refactored**: `git.go`, `github.go`, `calculate.go`, `notes.go`,
`commit.go`, `publish.go` to thread `CommandExecutor`; added `gitMutate`
- **Updated**: `main.go` adds `--dry-run` flag and `prepare-release`
subcommand
- **New**: `.github/workflows/tag-and-release.yaml` (manual, Go-driven,
with `dry_run`)
- **Reverted**: `.github/workflows/release.yaml` to its pre-#25162 state

> [!NOTE]
> Generated by Coder Agents on behalf of @f0ssel
2026-07-01 16:20:00 -04:00

188 lines
5.0 KiB
Go

package main
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")
}