Skip to content

Commit 3ef3f47

Browse files
committed
test: improve integration tests for run
1 parent e931b7f commit 3ef3f47

1 file changed

Lines changed: 158 additions & 10 deletions

File tree

command/run_integration_test.go

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

1313
const (
1414
runCommandHelperEnv = "REACTENV_RUN_HELPER"
15+
runCommandHelperMode = "REACTENV_RUN_MODE"
1516
runCommandHelperPath = "REACTENV_RUN_PATH"
1617
)
1718

19+
const (
20+
runHelperModeMissingArgs = "missing-args"
21+
runHelperModeInvalidMatch = "invalid-match"
22+
runHelperModeMissingEnv = "missing-env"
23+
runHelperModeNoFiles = "no-files"
24+
runHelperModePartialWrite = "partial-write"
25+
)
26+
1827
func TestRunCommandRunSuccessDefaultMatcher(t *testing.T) {
1928
tempDir := t.TempDir()
2029

@@ -75,10 +84,7 @@ func TestRunCommandRunSuccessCustomMatcher(t *testing.T) {
7584
}
7685

7786
func TestRunCommandRunMissingEnvExits(t *testing.T) {
78-
if os.Getenv(runCommandHelperEnv) == "1" {
79-
helperPath := os.Getenv(runCommandHelperPath)
80-
cmd := &RunCommand{FileMatchPattern: defaultFileMatchPattern}
81-
cmd.Run([]string{helperPath})
87+
if handleRunCommandHelper(t) {
8288
return
8389
}
8490

@@ -96,20 +102,162 @@ func TestRunCommandRunMissingEnvExits(t *testing.T) {
96102
writeFile(t, rootJS, originalRoot)
97103
writeFile(t, nestedJS, originalNested)
98104

99-
cmd := exec.Command(os.Args[0], "-test.run=TestRunCommandRunMissingEnvExits")
100-
env := append([]string{}, os.Environ()...)
101-
env = filterOutEnv(env, "MISSING=")
102-
env = append(env, runCommandHelperEnv+"=1", runCommandHelperPath+"="+tempDir, "PRESENT=present")
103-
cmd.Env = env
105+
err := runCommandHelperProcess("TestRunCommandRunMissingEnvExits", runHelperModeMissingEnv, tempDir, "PRESENT=present", "MISSING=")
106+
107+
var exitErr *exec.ExitError
108+
require.ErrorAs(t, err, &exitErr)
109+
require.Equal(t, 1, exitErr.ExitCode())
110+
111+
require.Equal(t, originalRoot, readFile(t, rootJS))
112+
require.Equal(t, originalNested, readFile(t, nestedJS))
113+
}
114+
115+
func TestRunCommandRunMissingArgsExits(t *testing.T) {
116+
if handleRunCommandHelper(t) {
117+
return
118+
}
104119

105-
err := cmd.Run()
120+
err := runCommandHelperProcess("TestRunCommandRunMissingArgsExits", runHelperModeMissingArgs, "", "")
121+
122+
var exitErr *exec.ExitError
123+
require.ErrorAs(t, err, &exitErr)
124+
require.Equal(t, 1, exitErr.ExitCode())
125+
}
126+
127+
func TestRunCommandRunInvalidMatchExits(t *testing.T) {
128+
if handleRunCommandHelper(t) {
129+
return
130+
}
131+
132+
tempDir := t.TempDir()
133+
writeFile(t, filepath.Join(tempDir, "root.js"), `const api="__reactenv.API_URL";`)
134+
135+
err := runCommandHelperProcess("TestRunCommandRunInvalidMatchExits", runHelperModeInvalidMatch, tempDir, "API_URL=https://example.com")
136+
137+
var exitErr *exec.ExitError
138+
require.ErrorAs(t, err, &exitErr)
139+
require.Equal(t, 1, exitErr.ExitCode())
140+
}
141+
142+
func TestRunCommandRunNoFilesFoundExits(t *testing.T) {
143+
if handleRunCommandHelper(t) {
144+
return
145+
}
146+
147+
tempDir := t.TempDir()
148+
writeFile(t, filepath.Join(tempDir, "style.css"), "body { color: red; }")
149+
150+
err := runCommandHelperProcess("TestRunCommandRunNoFilesFoundExits", runHelperModeNoFiles, tempDir)
151+
152+
var exitErr *exec.ExitError
153+
require.ErrorAs(t, err, &exitErr)
154+
require.Equal(t, 1, exitErr.ExitCode())
155+
}
156+
157+
func TestRunCommandRunNoOccurrencesReturnsOne(t *testing.T) {
158+
tempDir := t.TempDir()
159+
160+
nestedDir := filepath.Join(tempDir, "nested")
161+
require.NoError(t, os.MkdirAll(nestedDir, 0755), "create nested dir")
162+
163+
rootJS := filepath.Join(tempDir, "root.js")
164+
nestedJS := filepath.Join(nestedDir, "app.js")
165+
166+
writeFile(t, rootJS, "const api='no matches';")
167+
writeFile(t, nestedJS, "const name='still none';")
168+
169+
cmd := &RunCommand{FileMatchPattern: defaultFileMatchPattern}
170+
exitCode := cmd.Run([]string{tempDir})
171+
require.Equal(t, 1, exitCode)
172+
173+
require.Equal(t, "const api='no matches';", readFile(t, rootJS))
174+
require.Equal(t, "const name='still none';", readFile(t, nestedJS))
175+
}
176+
177+
func TestRunCommandRunMissingEnvNoPartialWrites(t *testing.T) {
178+
if handleRunCommandHelper(t) {
179+
return
180+
}
181+
182+
tempDir := t.TempDir()
183+
184+
nestedDir := filepath.Join(tempDir, "nested")
185+
require.NoError(t, os.MkdirAll(nestedDir, 0755), "create nested dir")
186+
187+
rootJS := filepath.Join(tempDir, "root.js")
188+
nestedJS := filepath.Join(nestedDir, "app.js")
189+
otherJS := filepath.Join(nestedDir, "other.js")
190+
191+
originalRoot := `const missing="__reactenv.MISSING";`
192+
originalNested := `const present="__reactenv.PRESENT";`
193+
originalOther := `const another="__reactenv.ANOTHER";`
194+
195+
writeFile(t, rootJS, originalRoot)
196+
writeFile(t, nestedJS, originalNested)
197+
writeFile(t, otherJS, originalOther)
198+
199+
err := runCommandHelperProcess("TestRunCommandRunMissingEnvNoPartialWrites", runHelperModePartialWrite, tempDir, "PRESENT=present", "ANOTHER=another", "MISSING=")
106200

107201
var exitErr *exec.ExitError
108202
require.ErrorAs(t, err, &exitErr)
109203
require.Equal(t, 1, exitErr.ExitCode())
110204

111205
require.Equal(t, originalRoot, readFile(t, rootJS))
112206
require.Equal(t, originalNested, readFile(t, nestedJS))
207+
require.Equal(t, originalOther, readFile(t, otherJS))
208+
}
209+
210+
func handleRunCommandHelper(t *testing.T) bool {
211+
t.Helper()
212+
213+
if os.Getenv(runCommandHelperEnv) != "1" {
214+
return false
215+
}
216+
217+
mode := os.Getenv(runCommandHelperMode)
218+
helperPath := os.Getenv(runCommandHelperPath)
219+
220+
cmd := &RunCommand{FileMatchPattern: defaultFileMatchPattern}
221+
222+
switch mode {
223+
case runHelperModeMissingArgs:
224+
cmd.Run([]string{})
225+
case runHelperModeInvalidMatch:
226+
cmd.FileMatchPattern = "["
227+
cmd.Run([]string{helperPath})
228+
case runHelperModeMissingEnv:
229+
cmd.Run([]string{helperPath})
230+
case runHelperModeNoFiles:
231+
cmd.Run([]string{helperPath})
232+
case runHelperModePartialWrite:
233+
cmd.Run([]string{helperPath})
234+
default:
235+
t.Fatalf("unknown helper mode: %s", mode)
236+
}
237+
238+
return true
239+
}
240+
241+
func runCommandHelperProcess(testName string, mode string, dir string, envOverrides ...string) error {
242+
cmd := exec.Command(os.Args[0], "-test.run="+testName)
243+
env := append([]string{}, os.Environ()...)
244+
env = append(env, runCommandHelperEnv+"=1", runCommandHelperMode+"="+mode)
245+
if dir != "" {
246+
env = append(env, runCommandHelperPath+"="+dir)
247+
}
248+
249+
if len(envOverrides) > 0 {
250+
for _, override := range envOverrides {
251+
if strings.HasSuffix(override, "=") {
252+
env = filterOutEnv(env, override)
253+
continue
254+
}
255+
env = append(env, override)
256+
}
257+
}
258+
259+
cmd.Env = env
260+
return cmd.Run()
113261
}
114262

115263
func writeFile(t *testing.T, path string, content string) {

0 commit comments

Comments
 (0)