-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathcypress.go
More file actions
98 lines (79 loc) · 2.23 KB
/
cypress.go
File metadata and controls
98 lines (79 loc) · 2.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package runner
import (
"fmt"
"os/exec"
"slices"
"strings"
"github.com/buildkite/test-engine-client/internal/debug"
"github.com/buildkite/test-engine-client/internal/plan"
"github.com/kballard/go-shellquote"
)
type Cypress struct {
RunnerConfig
}
func (c Cypress) Name() string {
return "Cypress"
}
func NewCypress(c RunnerConfig) Cypress {
if c.TestCommand == "" {
c.TestCommand = "npx cypress run --spec {{testExamples}}"
}
if c.TestFilePattern == "" {
c.TestFilePattern = "**/*.cy.{js,jsx,ts,tsx}"
}
return Cypress{
RunnerConfig: c,
}
}
func (c Cypress) SupportedFeatures() SupportedFeatures {
return SupportedFeatures{
SplitByFile: true,
SplitByExample: false,
FilterTestFiles: true,
AutoRetry: false,
Mute: false,
Skip: false,
}
}
func (c Cypress) Run(result *RunResult, testCases []plan.TestCase, retry bool) error {
testPaths := make([]string, len(testCases))
for i, tc := range testCases {
testPaths[i] = tc.Path
}
cmdName, cmdArgs, err := c.commandNameAndArgs(c.TestCommand, testPaths)
if err != nil {
return fmt.Errorf("failed to build command: %w", err)
}
cmd := exec.Command(cmdName, cmdArgs...)
err = runAndForwardSignal(cmd)
return err
}
func (c Cypress) GetFiles() ([]string, error) {
debug.Println("Discovering test files with include pattern:", c.TestFilePattern, "exclude pattern:", c.TestFileExcludePattern)
files, err := discoverTestFiles(c.TestFilePattern, c.TestFileExcludePattern)
debug.Println("Discovered", len(files), "files")
if err != nil {
return nil, err
}
if len(files) == 0 {
return nil, fmt.Errorf("no files found with pattern %q and exclude pattern %q", c.TestFilePattern, c.TestFileExcludePattern)
}
return files, nil
}
func (c Cypress) GetExamples(files []string) ([]plan.TestCase, error) {
return nil, fmt.Errorf("not supported in Cypress")
}
func (c Cypress) commandNameAndArgs(cmd string, testCases []string) (string, []string, error) {
words, err := shellquote.Split(cmd)
if err != nil {
return "", []string{}, err
}
idx := slices.Index(words, "{{testExamples}}")
specs := strings.Join(testCases, ",")
if idx < 0 {
words = append(words, "--spec", specs)
} else {
words[idx] = specs
}
return words[0], words[1:], nil
}