Skip to content

Commit a928342

Browse files
committed
wip build test binary
1 parent 897bca3 commit a928342

File tree

1 file changed

+45
-10
lines changed

1 file changed

+45
-10
lines changed

tools/flakeguard/runner/runner.go

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ type Runner struct {
2323
Verbose bool // If true, provides detailed logging.
2424
RunCount int // Number of times to run the tests.
2525
UseRace bool // Enable race detector.
26+
Count int // -test.count flag value.
27+
UseShuffle bool // -test.shuffle flag value.
28+
JsonOutput bool // -test.json flag value.
2629
FailFast bool // Stop on first test failure.
2730
SkipTests []string // Test names to exclude.
2831
SelectedTestPackages []string // Explicitly selected packages to run.
@@ -33,8 +36,13 @@ type Runner struct {
3336
func (r *Runner) RunTests() ([]reports.TestResult, error) {
3437
var jsonFilePaths []string
3538
for _, p := range r.SelectedTestPackages {
39+
binaryPath, err := r.buildTestBinary(p)
40+
if err != nil {
41+
return nil, fmt.Errorf("failed to build test binary for package %s: %w", p, err)
42+
}
43+
defer os.Remove(binaryPath) // Clean up test binary after running tests
3644
for i := 0; i < r.RunCount; i++ {
37-
jsonFilePath, passed, err := r.runTests(p)
45+
jsonFilePath, passed, err := r.runTestBinary(binaryPath)
3846
if err != nil {
3947
return nil, fmt.Errorf("failed to run tests in package %s: %w", p, err)
4048
}
@@ -52,19 +60,48 @@ type exitCoder interface {
5260
ExitCode() int
5361
}
5462

55-
// runTests runs the tests for a given package and returns the path to the output file.
56-
func (r *Runner) runTests(packageName string) (string, bool, error) {
57-
args := []string{"test", packageName, "-json", "-count=1"} // Enable JSON output
63+
// buildTestBinary builds the test binary for a given package and returns the path to the binary.
64+
func (r *Runner) buildTestBinary(packageName string) (string, error) {
65+
binaryPath := fmt.Sprintf("%s/test-binary-%s", os.TempDir(), strings.ReplaceAll(packageName, "/", "-"))
66+
// Compile-time flags for building the binary
67+
args := []string{"test", "-c", packageName, "-o", binaryPath}
5868
if r.UseRace {
5969
args = append(args, "-race")
6070
}
71+
72+
if r.Verbose {
73+
log.Printf("Building test binary with command: go %s\n", strings.Join(args, " "))
74+
}
75+
76+
cmd := exec.Command("go", args...)
77+
cmd.Dir = r.ProjectPath
78+
if err := cmd.Run(); err != nil {
79+
return "", fmt.Errorf("failed to build test binary for %s: %w", packageName, err)
80+
}
81+
82+
return binaryPath, nil
83+
}
84+
85+
// runTestBinary runs the tests for a given package and returns the path to the output file.
86+
func (r *Runner) runTestBinary(binaryPath string) (string, bool, error) {
87+
// Runtime flags for executing the binary
88+
args := []string{binaryPath}
89+
if r.UseShuffle {
90+
args = append(args, "-test.shuffle=on")
91+
}
92+
if r.JsonOutput {
93+
args = append(args, "-test.json")
94+
}
95+
if r.Count > 0 {
96+
args = append(args, fmt.Sprintf("-test.count=%d", r.Count))
97+
}
6198
if len(r.SkipTests) > 0 {
6299
skipPattern := strings.Join(r.SkipTests, "|")
63-
args = append(args, fmt.Sprintf("-skip=%s", skipPattern))
100+
args = append(args, fmt.Sprintf("-test.skip=%s", skipPattern))
64101
}
65102

66103
if r.Verbose {
67-
log.Printf("Running command: go %s\n", strings.Join(args, " "))
104+
log.Printf("Running command: %s\n", strings.Join(args, " "))
68105
}
69106

70107
// Create a temporary file to store the output
@@ -73,9 +110,7 @@ func (r *Runner) runTests(packageName string) (string, bool, error) {
73110
return "", false, fmt.Errorf("failed to create temp file: %w", err)
74111
}
75112
defer tmpFile.Close()
76-
77-
// Run the command with output directed to the file
78-
cmd := exec.Command("go", args...)
113+
cmd := exec.Command(args[0], args[1:]...)
79114
cmd.Dir = r.ProjectPath
80115
cmd.Stdout = tmpFile
81116
cmd.Stderr = tmpFile
@@ -85,7 +120,7 @@ func (r *Runner) runTests(packageName string) (string, bool, error) {
85120
var exErr exitCoder
86121
// Check if the error is due to a non-zero exit code
87122
if errors.As(err, &exErr) && exErr.ExitCode() == 0 {
88-
return "", false, fmt.Errorf("test command failed at %s: %w", packageName, err)
123+
return "", false, fmt.Errorf("test command failed for binary at %s: %w", binaryPath, err)
89124
}
90125
return tmpFile.Name(), false, nil // Test failed
91126
}

0 commit comments

Comments
 (0)