Skip to content

Commit 5fc95fc

Browse files
committed
Be better about test setup errors
1 parent 04ffb09 commit 5fc95fc

4 files changed

Lines changed: 69 additions & 54 deletions

File tree

‎cmd/create_test.go‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ func TestRunCreate_WithRevisionName(t *testing.T) {
9090

9191
func TestRunCreate_WithInit(t *testing.T) {
9292
tmp := t.TempDir()
93-
origDir, _ := os.Getwd()
94-
os.Chdir(tmp)
93+
origDir, err := os.Getwd()
94+
require.NoError(t, err)
95+
require.NoError(t, os.Chdir(tmp))
9596
defer os.Chdir(origDir)
9697

9798
client := &mockRESTClient{
@@ -175,8 +176,9 @@ func TestRunCreate_WithNameAndApp(t *testing.T) {
175176

176177
func TestRunCreate_WithNameAndInit(t *testing.T) {
177178
tmp := t.TempDir()
178-
origDir, _ := os.Getwd()
179-
os.Chdir(tmp)
179+
origDir, err := os.Getwd()
180+
require.NoError(t, err)
181+
require.NoError(t, os.Chdir(tmp))
180182
defer os.Chdir(origDir)
181183

182184
var capturedBody []byte
@@ -188,7 +190,7 @@ func TestRunCreate_WithNameAndInit(t *testing.T) {
188190
},
189191
}
190192

191-
_, err := runCreate(client, createCmdFlags{name: "named-app", init: true})
193+
_, err = runCreate(client, createCmdFlags{name: "named-app", init: true})
192194
require.NoError(t, err)
193195

194196
var req createReq

‎cmd/deploy_test.go‎

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,53 @@ import (
1212

1313
func TestRunDeploy_NoDir(t *testing.T) {
1414
tmp := t.TempDir()
15-
origDir, _ := os.Getwd()
16-
os.Chdir(tmp)
15+
origDir, err := os.Getwd()
16+
require.NoError(t, err)
17+
require.NoError(t, os.Chdir(tmp))
1718
defer os.Chdir(origDir)
1819

1920
client := &mockRESTClient{}
20-
err := runDeploy(client, deployCmdFlags{app: "my-app"})
21+
err = runDeploy(client, deployCmdFlags{app: "my-app"})
2122
require.ErrorContains(t, err, "--dir flag is required")
2223
}
2324

2425
func TestRunDeploy_NoAppName(t *testing.T) {
2526
tmp := t.TempDir()
26-
origDir, _ := os.Getwd()
27-
os.Chdir(tmp)
27+
origDir, err := os.Getwd()
28+
require.NoError(t, err)
29+
require.NoError(t, os.Chdir(tmp))
2830
defer os.Chdir(origDir)
2931

3032
deployDir := filepath.Join(tmp, "dist")
31-
os.MkdirAll(deployDir, 0755)
33+
require.NoError(t, os.MkdirAll(deployDir, 0755))
3234

3335
client := &mockRESTClient{}
34-
err := runDeploy(client, deployCmdFlags{dir: deployDir})
36+
err = runDeploy(client, deployCmdFlags{dir: deployDir})
3537
require.ErrorContains(t, err, "--app flag is required")
3638
}
3739

3840
func TestRunDeploy_DirNotExist(t *testing.T) {
3941
tmp := t.TempDir()
40-
origDir, _ := os.Getwd()
41-
os.Chdir(tmp)
42+
origDir, err := os.Getwd()
43+
require.NoError(t, err)
44+
require.NoError(t, os.Chdir(tmp))
4245
defer os.Chdir(origDir)
4346

4447
client := &mockRESTClient{}
45-
err := runDeploy(client, deployCmdFlags{dir: "/nonexistent/path", app: "my-app"})
48+
err = runDeploy(client, deployCmdFlags{dir: "/nonexistent/path", app: "my-app"})
4649
require.ErrorContains(t, err, "does not exist")
4750
}
4851

4952
func TestRunDeploy_Success(t *testing.T) {
5053
tmp := t.TempDir()
51-
origDir, _ := os.Getwd()
52-
os.Chdir(tmp)
54+
origDir, err := os.Getwd()
55+
require.NoError(t, err)
56+
require.NoError(t, os.Chdir(tmp))
5357
defer os.Chdir(origDir)
5458

5559
deployDir := filepath.Join(tmp, "dist")
56-
os.MkdirAll(deployDir, 0755)
57-
os.WriteFile(filepath.Join(deployDir, "index.html"), []byte("<html></html>"), 0644)
60+
require.NoError(t, os.MkdirAll(deployDir, 0755))
61+
require.NoError(t, os.WriteFile(filepath.Join(deployDir, "index.html"), []byte("<html></html>"), 0644))
5862

5963
var capturedPath string
6064
client := &mockRESTClient{
@@ -64,21 +68,22 @@ func TestRunDeploy_Success(t *testing.T) {
6468
},
6569
}
6670

67-
err := runDeploy(client, deployCmdFlags{dir: deployDir, app: "my-app"})
71+
err = runDeploy(client, deployCmdFlags{dir: deployDir, app: "my-app"})
6872
require.NoError(t, err)
6973
assert.Equal(t, "runtime/my-app/deployment/bundle", capturedPath)
7074
require.NoFileExists(t, deployDir+".zip")
7175
}
7276

7377
func TestRunDeploy_WithRevisionAndSha(t *testing.T) {
7478
tmp := t.TempDir()
75-
origDir, _ := os.Getwd()
76-
os.Chdir(tmp)
79+
origDir, err := os.Getwd()
80+
require.NoError(t, err)
81+
require.NoError(t, os.Chdir(tmp))
7782
defer os.Chdir(origDir)
7883

7984
deployDir := filepath.Join(tmp, "dist")
80-
os.MkdirAll(deployDir, 0755)
81-
os.WriteFile(filepath.Join(deployDir, "app.js"), []byte("console.log('hi')"), 0644)
85+
require.NoError(t, os.MkdirAll(deployDir, 0755))
86+
require.NoError(t, os.WriteFile(filepath.Join(deployDir, "app.js"), []byte("console.log('hi')"), 0644))
8287

8388
var capturedPath string
8489
client := &mockRESTClient{
@@ -88,42 +93,44 @@ func TestRunDeploy_WithRevisionAndSha(t *testing.T) {
8893
},
8994
}
9095

91-
err := runDeploy(client, deployCmdFlags{dir: deployDir, app: "my-app", revisionName: "v2", sha: "abc123"})
96+
err = runDeploy(client, deployCmdFlags{dir: deployDir, app: "my-app", revisionName: "v2", sha: "abc123"})
9297
require.NoError(t, err)
9398
assert.Contains(t, capturedPath, "revision_name=v2")
9499
assert.Contains(t, capturedPath, "revision=abc123")
95100
}
96101

97102
func TestRunDeploy_APIError(t *testing.T) {
98103
tmp := t.TempDir()
99-
origDir, _ := os.Getwd()
100-
os.Chdir(tmp)
104+
origDir, err := os.Getwd()
105+
require.NoError(t, err)
106+
require.NoError(t, os.Chdir(tmp))
101107
defer os.Chdir(origDir)
102108

103109
deployDir := filepath.Join(tmp, "dist")
104-
os.MkdirAll(deployDir, 0755)
105-
os.WriteFile(filepath.Join(deployDir, "index.html"), []byte("<html></html>"), 0644)
110+
require.NoError(t, os.MkdirAll(deployDir, 0755))
111+
require.NoError(t, os.WriteFile(filepath.Join(deployDir, "index.html"), []byte("<html></html>"), 0644))
106112

107113
client := &mockRESTClient{
108114
postFunc: mockPostError("upload failed"),
109115
}
110116

111-
err := runDeploy(client, deployCmdFlags{dir: deployDir, app: "my-app"})
117+
err = runDeploy(client, deployCmdFlags{dir: deployDir, app: "my-app"})
112118
require.ErrorContains(t, err, "error deploying app")
113119
}
114120

115121
func TestRunDeploy_WithConfigFile(t *testing.T) {
116122
tmp := t.TempDir()
117-
origDir, _ := os.Getwd()
118-
os.Chdir(tmp)
123+
origDir, err := os.Getwd()
124+
require.NoError(t, err)
125+
require.NoError(t, os.Chdir(tmp))
119126
defer os.Chdir(origDir)
120127

121128
deployDir := filepath.Join(tmp, "dist")
122-
os.MkdirAll(deployDir, 0755)
123-
os.WriteFile(filepath.Join(deployDir, "index.html"), []byte("<html></html>"), 0644)
129+
require.NoError(t, os.MkdirAll(deployDir, 0755))
130+
require.NoError(t, os.WriteFile(filepath.Join(deployDir, "index.html"), []byte("<html></html>"), 0644))
124131

125132
configPath := filepath.Join(tmp, "my-config.json")
126-
os.WriteFile(configPath, []byte(`{"app":"config-deploy-app"}`), 0644)
133+
require.NoError(t, os.WriteFile(configPath, []byte(`{"app":"config-deploy-app"}`), 0644))
127134

128135
var capturedPath string
129136
client := &mockRESTClient{
@@ -133,7 +140,7 @@ func TestRunDeploy_WithConfigFile(t *testing.T) {
133140
},
134141
}
135142

136-
err := runDeploy(client, deployCmdFlags{dir: deployDir, config: configPath})
143+
err = runDeploy(client, deployCmdFlags{dir: deployDir, config: configPath})
137144
require.NoError(t, err)
138145
assert.Contains(t, capturedPath, "config-deploy-app")
139146
}

‎cmd/get_test.go‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ import (
1212

1313
func TestRunGet_NoAppName(t *testing.T) {
1414
tmp := t.TempDir()
15-
origDir, _ := os.Getwd()
16-
os.Chdir(tmp)
15+
origDir, err := os.Getwd()
16+
require.NoError(t, err)
17+
require.NoError(t, os.Chdir(tmp))
1718
defer os.Chdir(origDir)
1819

1920
client := &mockRESTClient{}
20-
_, err := runGet(client, getCmdFlags{})
21+
_, err = runGet(client, getCmdFlags{})
2122
require.ErrorContains(t, err, "--app flag is required")
2223
}
2324

@@ -63,12 +64,13 @@ func TestRunGet_APIError(t *testing.T) {
6364

6465
func TestRunGet_WithConfigFile(t *testing.T) {
6566
tmp := t.TempDir()
66-
origDir, _ := os.Getwd()
67-
os.Chdir(tmp)
67+
origDir, err := os.Getwd()
68+
require.NoError(t, err)
69+
require.NoError(t, os.Chdir(tmp))
6870
defer os.Chdir(origDir)
6971

7072
configPath := filepath.Join(tmp, "my-config.json")
71-
os.WriteFile(configPath, []byte(`{"app":"config-app"}`), 0644)
73+
require.NoError(t, os.WriteFile(configPath, []byte(`{"app":"config-app"}`), 0644))
7274

7375
var capturedPath string
7476
client := &mockRESTClient{
@@ -86,11 +88,12 @@ func TestRunGet_WithConfigFile(t *testing.T) {
8688

8789
func TestRunGet_DefaultConfigFile(t *testing.T) {
8890
tmp := t.TempDir()
89-
origDir, _ := os.Getwd()
90-
os.Chdir(tmp)
91+
origDir, err := os.Getwd()
92+
require.NoError(t, err)
93+
require.NoError(t, os.Chdir(tmp))
9194
defer os.Chdir(origDir)
9295

93-
os.WriteFile(filepath.Join(tmp, "runtime.config.json"), []byte(`{"app":"default-app"}`), 0644)
96+
require.NoError(t, os.WriteFile(filepath.Join(tmp, "runtime.config.json"), []byte(`{"app":"default-app"}`), 0644))
9497

9598
var capturedPath string
9699
client := &mockRESTClient{

‎cmd/init_test.go‎

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ func TestRunInit_AppNotAccessible(t *testing.T) {
2727

2828
func TestRunInit_Success_DefaultPath(t *testing.T) {
2929
tmp := t.TempDir()
30-
origDir, _ := os.Getwd()
31-
os.Chdir(tmp)
30+
origDir, err := os.Getwd()
31+
require.NoError(t, err)
32+
require.NoError(t, os.Chdir(tmp))
3233
defer os.Chdir(origDir)
3334

3435
var capturedPath string
@@ -39,7 +40,7 @@ func TestRunInit_Success_DefaultPath(t *testing.T) {
3940
},
4041
}
4142

42-
err := runInit(client, initCmdFlags{app: "my-app"})
43+
err = runInit(client, initCmdFlags{app: "my-app"})
4344
require.NoError(t, err)
4445
assert.Equal(t, "runtime/my-app/deployment", capturedPath)
4546

@@ -50,16 +51,17 @@ func TestRunInit_Success_DefaultPath(t *testing.T) {
5051

5152
func TestRunInit_Success_CustomOutPath(t *testing.T) {
5253
tmp := t.TempDir()
53-
origDir, _ := os.Getwd()
54-
os.Chdir(tmp)
54+
origDir, err := os.Getwd()
55+
require.NoError(t, err)
56+
require.NoError(t, os.Chdir(tmp))
5557
defer os.Chdir(origDir)
5658

5759
client := &mockRESTClient{
5860
getFunc: mockGetResponse(`{"app_url":"https://my-app.example.com"}`),
5961
}
6062

6163
outPath := filepath.Join(tmp, "subdir", "custom-config.json")
62-
err := runInit(client, initCmdFlags{app: "my-app", out: outPath})
64+
err = runInit(client, initCmdFlags{app: "my-app", out: outPath})
6365
require.NoError(t, err)
6466

6567
data, err := os.ReadFile(outPath)
@@ -69,15 +71,16 @@ func TestRunInit_Success_CustomOutPath(t *testing.T) {
6971

7072
func TestRunInit_VerifiesAPIBeforeWriting(t *testing.T) {
7173
tmp := t.TempDir()
72-
origDir, _ := os.Getwd()
73-
os.Chdir(tmp)
74+
origDir, err := os.Getwd()
75+
require.NoError(t, err)
76+
require.NoError(t, os.Chdir(tmp))
7477
defer os.Chdir(origDir)
7578

7679
client := &mockRESTClient{
7780
getFunc: mockGetError("not found"),
7881
}
7982

80-
err := runInit(client, initCmdFlags{app: "bad-app"})
83+
err = runInit(client, initCmdFlags{app: "bad-app"})
8184
require.Error(t, err)
8285
require.NoFileExists(t, "runtime.config.json")
8386
}

0 commit comments

Comments
 (0)