|
| 1 | +package runner |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "os" |
| 7 | + "os/exec" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/buildkite/test-engine-client/internal/plan" |
| 11 | + "github.com/kballard/go-shellquote" |
| 12 | +) |
| 13 | + |
| 14 | +type PytestPants struct { |
| 15 | + RunnerConfig |
| 16 | +} |
| 17 | + |
| 18 | +func (p PytestPants) Name() string { |
| 19 | + return "pytest-pants" |
| 20 | +} |
| 21 | + |
| 22 | +func NewPytestPants(c RunnerConfig) PytestPants { |
| 23 | + fmt.Fprintln(os.Stderr, "Info: Python package 'buildkite-test-collector' is required and will not be verified by bktec. Please ensure it is added to the pants resolve used by pytest. See https://github.com/buildkite/test-engine-client/blob/main/docs/pytest-pants.md for more information.") |
| 24 | + |
| 25 | + if c.TestCommand == "" { |
| 26 | + fmt.Fprintln(os.Stderr, "Error: The test command must be set via BUILDKITE_TEST_ENGINE_TEST_CMD.") |
| 27 | + os.Exit(1) |
| 28 | + } |
| 29 | + |
| 30 | + if c.TestFilePattern != "" || c.TestFileExcludePattern != "" { |
| 31 | + fmt.Fprintln(os.Stderr, "Warning: Pants test runner variant does not support discovering test files. Please ensure the test command is set correctly via BUILDKITE_TEST_ENGINE_TEST_CMD and do *not* set either:") |
| 32 | + fmt.Fprintf(os.Stderr, " BUILDKITE_TEST_ENGINE_TEST_FILE_PATTERN=%q\n", c.TestFilePattern) |
| 33 | + fmt.Fprintf(os.Stderr, " BUILDKITE_TEST_ENGINE_TEST_FILE_EXCLUDE_PATTERN=%q\n", c.TestFileExcludePattern) |
| 34 | + } |
| 35 | + |
| 36 | + if c.TestFilePattern == "" { |
| 37 | + c.TestFilePattern = "**/{*_test,test_*}.py" |
| 38 | + } |
| 39 | + |
| 40 | + if c.RetryTestCommand == "" { |
| 41 | + c.RetryTestCommand = c.TestCommand |
| 42 | + } |
| 43 | + |
| 44 | + if c.ResultPath == "" { |
| 45 | + c.ResultPath = getRandomTempFilename() |
| 46 | + } |
| 47 | + |
| 48 | + return PytestPants{ |
| 49 | + RunnerConfig: c, |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func (p PytestPants) Run(result *RunResult, testCases []plan.TestCase, retry bool) error { |
| 54 | + testPaths := make([]string, len(testCases)) |
| 55 | + for i, tc := range testCases { |
| 56 | + testPaths[i] = tc.Path |
| 57 | + } |
| 58 | + |
| 59 | + command := p.TestCommand |
| 60 | + |
| 61 | + if retry { |
| 62 | + command = p.RetryTestCommand |
| 63 | + } |
| 64 | + |
| 65 | + cmdName, cmdArgs, err := p.commandNameAndArgs(command, testPaths) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("failed to build command: %w", err) |
| 68 | + } |
| 69 | + |
| 70 | + cmd := exec.Command(cmdName, cmdArgs...) |
| 71 | + |
| 72 | + err = runAndForwardSignal(cmd) |
| 73 | + |
| 74 | + // Only rescue exit code 1 because it indicates a test failures. |
| 75 | + // Ref: https://docs.pytest.org/en/7.1.x/reference/exit-codes.html |
| 76 | + if exitError := new(exec.ExitError); errors.As(err, &exitError) && exitError.ExitCode() != 1 { |
| 77 | + return err |
| 78 | + } |
| 79 | + |
| 80 | + tests, parseErr := ParsePytestCollectorResult(p.ResultPath) |
| 81 | + |
| 82 | + if parseErr != nil { |
| 83 | + fmt.Println("Buildkite Test Engine Client: Failed to read json output, failed tests will not be retried.") |
| 84 | + return err |
| 85 | + } |
| 86 | + |
| 87 | + for _, test := range tests { |
| 88 | + |
| 89 | + result.RecordTestResult(plan.TestCase{ |
| 90 | + Identifier: test.Id, |
| 91 | + Format: plan.TestCaseFormatExample, |
| 92 | + Scope: test.Scope, |
| 93 | + Name: test.Name, |
| 94 | + // pytest can execute individual test using node id, which is a filename, classname (if any), and function, separated by `::`. |
| 95 | + // Ref: https://docs.pytest.org/en/6.2.x/usage.html#nodeids |
| 96 | + Path: fmt.Sprintf("%s::%s", test.Scope, test.Name), |
| 97 | + }, test.Result) |
| 98 | + } |
| 99 | + |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func (p PytestPants) GetFiles() ([]string, error) { |
| 104 | + return []string{}, nil |
| 105 | +} |
| 106 | + |
| 107 | +func (p PytestPants) GetExamples(files []string) ([]plan.TestCase, error) { |
| 108 | + return nil, fmt.Errorf("not supported in pytest pants") |
| 109 | +} |
| 110 | + |
| 111 | +func (p PytestPants) commandNameAndArgs(cmd string, testCases []string) (string, []string, error) { |
| 112 | + if strings.Contains(cmd, "{{testExamples}}") { |
| 113 | + return "", []string{}, fmt.Errorf("currently, bktec does not support dynamically injecting {{testExamples}}. Please ensure the test command in BUILDKITE_TEST_ENGINE_TEST_CMD does *not* include {{testExamples}}") |
| 114 | + } |
| 115 | + |
| 116 | + // Split command into parts before and after the first -- |
| 117 | + parts := strings.SplitN(cmd, "--", 2) |
| 118 | + if len(parts) != 2 { |
| 119 | + return "", []string{}, fmt.Errorf("please ensure the test command in BUILDKITE_TEST_ENGINE_TEST_CMD includes a -- separator") |
| 120 | + } |
| 121 | + |
| 122 | + // Check that both required flags are after the -- |
| 123 | + afterDash := parts[1] |
| 124 | + if !strings.Contains(afterDash, "--json={{resultPath}}") { |
| 125 | + return "", []string{}, fmt.Errorf("please ensure the test command in BUILDKITE_TEST_ENGINE_TEST_CMD includes --json={{resultPath}} after the -- separator") |
| 126 | + } |
| 127 | + |
| 128 | + if !strings.Contains(afterDash, "--merge-json") { |
| 129 | + return "", []string{}, fmt.Errorf("please ensure the test command in BUILDKITE_TEST_ENGINE_TEST_CMD includes --merge-json after the -- separator") |
| 130 | + } |
| 131 | + |
| 132 | + cmd = strings.Replace(cmd, "{{resultPath}}", p.ResultPath, 1) |
| 133 | + |
| 134 | + args, err := shellquote.Split(cmd) |
| 135 | + |
| 136 | + if err != nil { |
| 137 | + return "", []string{}, err |
| 138 | + } |
| 139 | + |
| 140 | + return args[0], args[1:], nil |
| 141 | +} |
0 commit comments