Skip to content

Commit 7d9ee8c

Browse files
committed
add a helper to clearly distinguish between filesystem errors and actual test failures
1 parent 63439c6 commit 7d9ee8c

2 files changed

Lines changed: 20 additions & 69 deletions

File tree

cmd/link_test.go

Lines changed: 0 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1206,62 +1206,6 @@ func TestLink_SkipsBaseFix_ForNewlyCreatedPRs(t *testing.T) {
12061206
// Silence "imported and not used" for fmt in case test helpers use it.
12071207
var _ = fmt.Sprintf
12081208

1209-
func TestLink_FetchesBeforePush(t *testing.T) {
1210-
var callOrder []string
1211-
var fetchedBranches []string
1212-
1213-
mock := newLinkGitMock("feat-a", "feat-b")
1214-
mock.FetchBranchesFn = func(remote string, branches []string) error {
1215-
callOrder = append(callOrder, "fetch")
1216-
fetchedBranches = branches
1217-
assert.Equal(t, "origin", remote)
1218-
return nil
1219-
}
1220-
mock.PushFn = func(remote string, branches []string, force, atomic bool) error {
1221-
callOrder = append(callOrder, "push")
1222-
return nil
1223-
}
1224-
1225-
restore := git.SetOps(mock)
1226-
defer restore()
1227-
1228-
prNum := 0
1229-
cfg, _, errR := config.NewTestConfig()
1230-
cfg.GitHubClientOverride = &github.MockClient{
1231-
FindPRForBranchFn: func(branch string) (*github.PullRequest, error) {
1232-
prNum++
1233-
return &github.PullRequest{
1234-
Number: prNum,
1235-
URL: fmt.Sprintf("https://github.com/o/r/pull/%d", prNum),
1236-
BaseRefName: "main",
1237-
HeadRefName: branch,
1238-
State: "OPEN",
1239-
}, nil
1240-
},
1241-
ListStacksFn: func() ([]github.RemoteStack, error) {
1242-
return []github.RemoteStack{}, nil
1243-
},
1244-
CreateStackFn: func(prNumbers []int) (int, error) {
1245-
return 42, nil
1246-
},
1247-
}
1248-
1249-
cmd := LinkCmd(cfg)
1250-
cmd.SetArgs([]string{"feat-a", "feat-b"})
1251-
cmd.SetOut(io.Discard)
1252-
cmd.SetErr(io.Discard)
1253-
err := cmd.Execute()
1254-
1255-
cfg.Err.Close()
1256-
_, _ = io.ReadAll(errR)
1257-
1258-
assert.NoError(t, err)
1259-
assert.Equal(t, []string{"feat-a", "feat-b"}, fetchedBranches, "should fetch pushed branches")
1260-
require.Len(t, callOrder, 2)
1261-
assert.Equal(t, "fetch", callOrder[0], "fetch must happen before push")
1262-
assert.Equal(t, "push", callOrder[1])
1263-
}
1264-
12651209
func TestLink_BranchNames_UsesPRTemplate(t *testing.T) {
12661210
tmpDir := t.TempDir()
12671211
ghDir := filepath.Join(tmpDir, ".github")

internal/pr/template_test.go

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,40 +8,47 @@ import (
88
"github.com/stretchr/testify/assert"
99
)
1010

11+
// writeTemplate is a test helper that creates a file with the given content,
12+
// creating parent directories as needed. It calls t.Fatal on any error so
13+
// that setup failures are clearly distinguished from feature failures.
14+
func writeTemplate(t *testing.T, path string, content []byte) {
15+
t.Helper()
16+
if err := os.MkdirAll(filepath.Dir(path), 0o755); err != nil {
17+
t.Fatalf("test setup: MkdirAll: %v", err)
18+
}
19+
if err := os.WriteFile(path, content, 0o644); err != nil {
20+
t.Fatalf("test setup: WriteFile: %v", err)
21+
}
22+
}
23+
1124
func TestFindTemplate_GitHubDir(t *testing.T) {
1225
root := t.TempDir()
13-
dir := filepath.Join(root, ".github")
14-
os.MkdirAll(dir, 0o755)
15-
os.WriteFile(filepath.Join(dir, "pull_request_template.md"), []byte("## Description\n\nFill in details."), 0o644)
26+
writeTemplate(t, filepath.Join(root, ".github", "pull_request_template.md"), []byte("## Description\n\nFill in details."))
1627

1728
got := FindTemplate(root)
1829
assert.Equal(t, "## Description\n\nFill in details.", got)
1930
}
2031

2132
func TestFindTemplate_RootDir(t *testing.T) {
2233
root := t.TempDir()
23-
os.WriteFile(filepath.Join(root, "pull_request_template.md"), []byte("Root template"), 0o644)
34+
writeTemplate(t, filepath.Join(root, "pull_request_template.md"), []byte("Root template"))
2435

2536
got := FindTemplate(root)
2637
assert.Equal(t, "Root template", got)
2738
}
2839

2940
func TestFindTemplate_DocsDir(t *testing.T) {
3041
root := t.TempDir()
31-
dir := filepath.Join(root, "docs")
32-
os.MkdirAll(dir, 0o755)
33-
os.WriteFile(filepath.Join(dir, "PULL_REQUEST_TEMPLATE.md"), []byte("Docs template"), 0o644)
42+
writeTemplate(t, filepath.Join(root, "docs", "PULL_REQUEST_TEMPLATE.md"), []byte("Docs template"))
3443

3544
got := FindTemplate(root)
3645
assert.Equal(t, "Docs template", got)
3746
}
3847

3948
func TestFindTemplate_PriorityOrder(t *testing.T) {
4049
root := t.TempDir()
41-
ghDir := filepath.Join(root, ".github")
42-
os.MkdirAll(ghDir, 0o755)
43-
os.WriteFile(filepath.Join(ghDir, "pull_request_template.md"), []byte("github template"), 0o644)
44-
os.WriteFile(filepath.Join(root, "pull_request_template.md"), []byte("root template"), 0o644)
50+
writeTemplate(t, filepath.Join(root, ".github", "pull_request_template.md"), []byte("github template"))
51+
writeTemplate(t, filepath.Join(root, "pull_request_template.md"), []byte("root template"))
4552

4653
got := FindTemplate(root)
4754
assert.Equal(t, "github template", got)
@@ -56,15 +63,15 @@ func TestFindTemplate_NoTemplate(t *testing.T) {
5663

5764
func TestFindTemplate_EmptyFile(t *testing.T) {
5865
root := t.TempDir()
59-
os.WriteFile(filepath.Join(root, "pull_request_template.md"), []byte(" \n "), 0o644)
66+
writeTemplate(t, filepath.Join(root, "pull_request_template.md"), []byte(" \n "))
6067

6168
got := FindTemplate(root)
6269
assert.Equal(t, "", got, "empty/whitespace-only template should be treated as no template")
6370
}
6471

6572
func TestFindTemplate_UpperCase(t *testing.T) {
6673
root := t.TempDir()
67-
os.WriteFile(filepath.Join(root, "PULL_REQUEST_TEMPLATE.md"), []byte("UPPER template"), 0o644)
74+
writeTemplate(t, filepath.Join(root, "PULL_REQUEST_TEMPLATE.md"), []byte("UPPER template"))
6875

6976
got := FindTemplate(root)
7077
assert.Equal(t, "UPPER template", got)

0 commit comments

Comments
 (0)