diff --git a/retryer_test.go b/retryer_test.go index e30833d..faa4ba3 100644 --- a/retryer_test.go +++ b/retryer_test.go @@ -27,8 +27,8 @@ func Test(t *testing.T) { t.Run(tc.name, testFromTestCase(tc)) tc.name += "JSON" - tc.cfg.testOutputTypeJSON = true - tc.cfg.testArgs += " -json" + tc.retryerCfg.testOutputTypeJSON = true + tc.retryerCfg.testArgs += " -json" for i, command := range tc.expectedCommands { tc.expectedCommands[i] = command + " -json" } @@ -42,28 +42,31 @@ func testFromTestCase(tc testCase) func(*testing.T) { expectedStdoutStr, expectedStderrStr := getTestCaseExpectedOutputStr(t, tc) - if tc.cfg.testOutputTypeJSON { + if tc.retryerCfg.testOutputTypeJSON { debugLogf(t, "Expected output:\n%v\n%v\n", expectedStdoutStr, expectedStderrStr) } - if tc.testConfig != "" { - testConfigPath := createTestConfigFile(t, tc.testConfig) + if tc.testCfg != "" { + testConfigPath := createTestConfigFile(t, tc.testCfg) defer os.Remove(testConfigPath) - tc.cfg.testArgs += " -config-path=" + testConfigPath + tc.retryerCfg.testArgs += " -config-path=" + testConfigPath } stdout := new(bytes.Buffer) stderr := new(bytes.Buffer) output := new(buffer) - retryerArgs := fmt.Sprintf(`-json=%v -total-retries=%v -retries-per-test=%v -test-command-name="%v" -verbose=%v`, - tc.cfg.testOutputTypeJSON, tc.cfg.maxTotalRetries, tc.cfg.maxRetriesPerTest, tc.cfg.testCommandName, tc.cfg.verbose) - command := fmt.Sprintf(`go run ./cmd/go-test-retryer/main.go %v -test-args="%v"`, retryerArgs, escapeQuotes(tc.cfg.testArgs)) + retryerArgs := fmt.Sprintf( + `-json=%v -total-retries=%v -retries-per-test=%v -test-command-name="%v" -verbose=%v -shell=%v`, + tc.retryerCfg.testOutputTypeJSON, tc.retryerCfg.maxTotalRetries, tc.retryerCfg.maxRetriesPerTest, + tc.retryerCfg.testCommandName, tc.retryerCfg.verbose, tc.retryerCfg.shellPath) + command := fmt.Sprintf(`go run ./cmd/go-test-retryer/main.go %v -test-args="%v"`, retryerArgs, escapeQuotes(tc.retryerCfg.testArgs)) debugLogf(t, "Command:\n%v\n", command) exitCode, err := runCommand( - fmt.Sprintf(`go run ./cmd/go-test-retryer/main.go %v -test-args="%v"`, retryerArgs, escapeQuotes(tc.cfg.testArgs)), + tc.retryerCfg.shellPath, + fmt.Sprintf(`go run ./cmd/go-test-retryer/main.go %v -test-args="%v"`, retryerArgs, escapeQuotes(tc.retryerCfg.testArgs)), io.MultiWriter(stdout, output), io.MultiWriter(stderr, output)) - if tc.cfg.testOutputTypeJSON { + if tc.retryerCfg.testOutputTypeJSON { debugLogf(t, "Actual output:\n%v\n", output.String()) } if err != nil { @@ -72,26 +75,26 @@ func testFromTestCase(tc testCase) func(*testing.T) { } assert.Equal(t, tc.expectedExitCode, exitCode) - checkStdout(t, strings.NewReader(expectedStdoutStr), stdout, tc.cfg.testOutputTypeJSON) + checkStdout(t, strings.NewReader(expectedStdoutStr), stdout, tc.retryerCfg.testOutputTypeJSON) checkStderr(t, strings.NewReader(expectedStderrStr), stderr) } } func getTestCaseExpectedOutputStr(t *testing.T, tc testCase) (string, string) { testConfigPath := "" - if tc.testConfig != "" { - testConfigPath = createTestConfigFile(t, tc.testConfig) + if tc.testCfg != "" { + testConfigPath = createTestConfigFile(t, tc.testCfg) defer os.Remove(testConfigPath) } expectedStdout := new(bytes.Buffer) expectedStderr := new(bytes.Buffer) for _, command := range tc.expectedCommands { - if tc.testConfig != "" { + if tc.testCfg != "" { command += " -config-path=" + testConfigPath } - _, err := runCommand(command, expectedStdout, expectedStderr) + _, err := runCommand(tc.retryerCfg.shellPath, command, expectedStdout, expectedStderr) if err != nil { _, ok := err.(*exec.ExitError) require.True(t, ok, fmt.Sprintf("unexpected exec error: %v", err)) @@ -101,8 +104,8 @@ func getTestCaseExpectedOutputStr(t *testing.T, tc testCase) (string, string) { return readerToString(t, expectedStdout), readerToString(t, expectedStderr) } -func runCommand(command string, stdout, stderr io.Writer) (int, error) { - cmd := exec.Command("/bin/bash", "-c", command) +func runCommand(shellPath, command string, stdout, stderr io.Writer) (int, error) { + cmd := exec.Command(shellPath, "-c", command) cmd.Stdout = stdout cmd.Stderr = stderr diff --git a/retryer_test_cases.go b/retryer_test_cases.go index 8e57dce..aa92b92 100644 --- a/retryer_test_cases.go +++ b/retryer_test_cases.go @@ -3,8 +3,8 @@ package retryer //nolint:unused type testCase struct { name string - cfg Config - testConfig string + retryerCfg Config + testCfg string expectedExitCode int expectedCommands []string } @@ -16,13 +16,14 @@ type testCase struct { var testCases = []testCase{ { name: "SuccessfulTestRetriesNotAllowed", - cfg: Config{ + retryerCfg: Config{ testOutputTypeJSON: false, maxRetriesPerTest: 0, maxTotalRetries: 0, testCommandName: "go test", testArgs: "-v -run=^TestSuccess$ -count=1 github.com/zcapitalz/go-test-retryer/test", verbose: false, + shellPath: "/bin/bash", }, expectedExitCode: 0, expectedCommands: []string{ @@ -31,13 +32,14 @@ var testCases = []testCase{ }, { name: "SuccessfulTestRetriesAllowed", - cfg: Config{ + retryerCfg: Config{ testOutputTypeJSON: false, maxRetriesPerTest: 1, maxTotalRetries: 1, testCommandName: "go test", testArgs: "-v -run=^TestSuccess$ -count=1 github.com/zcapitalz/go-test-retryer/test", verbose: false, + shellPath: "/bin/bash", }, expectedExitCode: 0, expectedCommands: []string{ @@ -46,13 +48,14 @@ var testCases = []testCase{ }, { name: "FailedTestRetriesNotAllowed1", - cfg: Config{ + retryerCfg: Config{ testOutputTypeJSON: false, maxRetriesPerTest: 0, maxTotalRetries: 0, testCommandName: "go test", testArgs: "-v -run=^TestFail$ -count=1 github.com/zcapitalz/go-test-retryer/test", verbose: false, + shellPath: "/bin/bash", }, expectedExitCode: 1, expectedCommands: []string{ @@ -61,13 +64,14 @@ var testCases = []testCase{ }, { name: "FailedTestRetriesNotAllowed2", - cfg: Config{ + retryerCfg: Config{ testOutputTypeJSON: false, maxRetriesPerTest: 0, maxTotalRetries: 1, testCommandName: "go test", testArgs: "-v -run=^TestFail$ -count=1 github.com/zcapitalz/go-test-retryer/test", verbose: false, + shellPath: "/bin/bash", }, expectedExitCode: 1, expectedCommands: []string{ @@ -76,13 +80,14 @@ var testCases = []testCase{ }, { name: "FailedTestRetriesAllowed1", - cfg: Config{ + retryerCfg: Config{ testOutputTypeJSON: false, maxRetriesPerTest: 1, maxTotalRetries: 1, testCommandName: "go test", testArgs: "-v -run=^TestFail$ -count=1 github.com/zcapitalz/go-test-retryer/test", verbose: false, + shellPath: "/bin/bash", }, expectedExitCode: 1, expectedCommands: []string{ @@ -92,13 +97,14 @@ var testCases = []testCase{ }, { name: "FailedTestRetriesAllowed2", - cfg: Config{ + retryerCfg: Config{ testOutputTypeJSON: false, maxRetriesPerTest: 1, maxTotalRetries: 0, testCommandName: "go test", testArgs: "-v -run=^TestFail$ -count=1 github.com/zcapitalz/go-test-retryer/test", verbose: false, + shellPath: "/bin/bash", }, expectedExitCode: 1, expectedCommands: []string{ @@ -108,13 +114,14 @@ var testCases = []testCase{ }, { name: "NotCompilableRetriesNotAllowed", - cfg: Config{ + retryerCfg: Config{ testOutputTypeJSON: false, maxRetriesPerTest: 0, maxTotalRetries: 0, testCommandName: "go test", testArgs: "-v -count=1 github.com/zcapitalz/go-test-retryer/test/notcompilable", verbose: false, + shellPath: "/bin/bash", }, expectedExitCode: 1, expectedCommands: []string{ @@ -123,13 +130,14 @@ var testCases = []testCase{ }, { name: "NotCompilableRetriesAllowed", - cfg: Config{ + retryerCfg: Config{ testOutputTypeJSON: false, maxRetriesPerTest: 1, maxTotalRetries: 1, testCommandName: "go test", testArgs: "-v -count=1 github.com/zcapitalz/go-test-retryer/test/notcompilable", verbose: false, + shellPath: "/bin/bash", }, expectedExitCode: 1, expectedCommands: []string{ @@ -138,15 +146,16 @@ var testCases = []testCase{ }, { name: "FlakyTestRetriesAllowed", - cfg: Config{ + retryerCfg: Config{ testOutputTypeJSON: false, maxRetriesPerTest: 2, maxTotalRetries: 2, testCommandName: "go test", testArgs: "-v -count=1 -run=^TestFlaky$ github.com/zcapitalz/go-test-retryer/test", verbose: false, + shellPath: "/bin/bash", }, - testConfig: "flaky_test_failures_left: 2", + testCfg: "flaky_test_failures_left: 2", expectedExitCode: 0, expectedCommands: []string{ "go test -v -count=1 -run=^TestFlaky$ github.com/zcapitalz/go-test-retryer/test", @@ -156,15 +165,16 @@ var testCases = []testCase{ }, { name: "FlakyTestNotEnoughRetries", - cfg: Config{ + retryerCfg: Config{ testOutputTypeJSON: false, maxRetriesPerTest: 1, maxTotalRetries: 1, testCommandName: "go test", testArgs: "-v -count=1 -run=^TestFlaky$ github.com/zcapitalz/go-test-retryer/test", verbose: false, + shellPath: "/bin/bash", }, - testConfig: "flaky_test_failures_left: 2", + testCfg: "flaky_test_failures_left: 2", expectedExitCode: 1, expectedCommands: []string{ "go test -v -count=1 -run=^TestFlaky$ github.com/zcapitalz/go-test-retryer/test", @@ -173,15 +183,16 @@ var testCases = []testCase{ }, { name: "FlakyAndFailedTest", - cfg: Config{ + retryerCfg: Config{ testOutputTypeJSON: false, maxRetriesPerTest: 1, maxTotalRetries: 2, testCommandName: "go test", testArgs: `-v -count=1 -run="^(TestFlaky|TestFail)$" github.com/zcapitalz/go-test-retryer/test`, verbose: false, + shellPath: "/bin/bash", }, - testConfig: "flaky_test_failures_left: 1", + testCfg: "flaky_test_failures_left: 1", expectedExitCode: 1, expectedCommands: []string{ `go test -v -count=1 -run="^(TestFlaky|TestFail)" github.com/zcapitalz/go-test-retryer/test`,