Skip to content

Commit

Permalink
fix: run test cases with provided shell
Browse files Browse the repository at this point in the history
  • Loading branch information
zcapitalz committed Jul 24, 2024
1 parent d027cdb commit 610f8cf
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 34 deletions.
39 changes: 21 additions & 18 deletions retryer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
Expand All @@ -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 {
Expand All @@ -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))
Expand All @@ -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

Expand Down
43 changes: 27 additions & 16 deletions retryer_test_cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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{
Expand All @@ -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",
Expand All @@ -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",
Expand All @@ -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`,
Expand Down

0 comments on commit 610f8cf

Please sign in to comment.