Files
coder/scripts/release-action/cmdexec_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

127 lines
3.3 KiB
Go

package main
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRealExecutor_RunOutput(t *testing.T) {
t.Parallel()
exec := realExecutor{}
out, err := exec.RunOutput("echo", "hello")
require.NoError(t, err)
assert.Equal(t, "hello", out)
}
func TestRealExecutor_Run(t *testing.T) {
t.Parallel()
exec := realExecutor{}
err := exec.Run("true")
require.NoError(t, err)
err = exec.Run("false")
require.Error(t, err)
}
func TestRealExecutor_RunMutation(t *testing.T) {
t.Parallel()
exec := realExecutor{}
err := exec.RunMutation("true")
require.NoError(t, err)
err = exec.RunMutation("false")
require.Error(t, err)
}
func TestRealExecutor_RunMutationStdout(t *testing.T) {
t.Parallel()
exec := realExecutor{}
var stdout, stderr bytes.Buffer
err := exec.RunMutationStdout(&stdout, &stderr, "echo", "output")
require.NoError(t, err)
assert.Equal(t, "output\n", stdout.String())
}
func TestDryRunExecutor_RunOutputDelegates(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
exec := newDryRunExecutor(&buf)
// RunOutput should still execute (read-only commands).
out, err := exec.RunOutput("echo", "real-output")
require.NoError(t, err)
assert.Equal(t, "real-output", out)
assert.Empty(t, buf.String(), "RunOutput should not produce dry-run output")
}
func TestDryRunExecutor_RunDelegates(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
exec := newDryRunExecutor(&buf)
err := exec.Run("true")
require.NoError(t, err)
assert.Empty(t, buf.String(), "Run should not produce dry-run output")
}
func TestDryRunExecutor_RunMutationPrints(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
exec := newDryRunExecutor(&buf)
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")
}
func TestDryRunExecutor_RunMutationStdoutPrints(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
exec := newDryRunExecutor(&buf)
var stdout, stderr bytes.Buffer
err := exec.RunMutationStdout(&stdout, &stderr, "gh", "release", "create", "--repo", "coder/coder", "--title", "v2.21.0")
require.NoError(t, err)
assert.Empty(t, stdout.String(), "RunMutationStdout should not produce real output in dry-run")
assert.Contains(t, buf.String(), "[dry-run] would run: gh release create --repo coder/coder --title v2.21.0")
}
func TestDryRunExecutor_RunMutationStdoutQuotesArgs(t *testing.T) {
t.Parallel()
var buf bytes.Buffer
exec := newDryRunExecutor(&buf)
var stdout, stderr bytes.Buffer
err := exec.RunMutationStdout(&stdout, &stderr, "gh", "release", "create", "--title", "has space")
require.NoError(t, err)
assert.Contains(t, buf.String(), "'has space'")
}
func TestShelljoin(t *testing.T) {
t.Parallel()
tests := []struct {
name string
args []string
want string
}{
{"simple", []string{"a", "b"}, "a b"},
{"space", []string{"a", "has space"}, "a 'has space'"},
// shelljoin uses POSIX single-quote escaping, so an embedded
// single quote becomes '"'"'.
{"quote", []string{"it's"}, `'it'"'"'s'`},
{"empty", []string{}, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := shelljoin(tt.args)
assert.Equal(t, tt.want, got)
})
}
}