Repository navigation
Expand file tree
/
Copy pathgotest_stories.go
More file actions
70 lines (64 loc) · 1.62 KB
/
Copy pathgotest_stories.go
File metadata and controls
70 lines (64 loc) · 1.62 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
package devflow
import (
"bytes"
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
)
const (
storiesPassLabel = "stories"
storiesMinTimeout = 180
)
// hasStoriesTests reports whether rootDir has a tests/ package containing at least one
// //go:build stories test file.
func hasStoriesTests(rootDir string) bool {
testsDir := filepath.Join(rootDir, "tests")
entries, err := os.ReadDir(testsDir)
if err != nil {
return false
}
for _, e := range entries {
if e.IsDir() || !strings.HasSuffix(e.Name(), "_test.go") {
continue
}
data, err := os.ReadFile(filepath.Join(testsDir, e.Name()))
if err != nil {
continue
}
if hasStoriesBuildTag(string(data)) {
return true
}
}
return false
}
func hasStoriesBuildTag(src string) bool {
for _, line := range strings.Split(src, "\n") {
trimmed := strings.TrimSpace(line)
if strings.HasPrefix(trimmed, "package ") {
return false
}
if strings.HasPrefix(trimmed, "//go:build ") && strings.Contains(trimmed, "stories") {
return true
}
}
return false
}
// runStoriesTests runs E2E user stories (`go test -tags stories ./tests/`).
func (g *Go) runStoriesTests(timeoutSec int) (string, bool) {
tSec := timeoutSec * 4
if tSec < storiesMinTimeout {
tSec = storiesMinTimeout
}
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(tSec+30)*time.Second)
defer cancel()
args := []string{"test", "-v", "-tags=stories", "-count=1", fmt.Sprintf("-timeout=%ds", tSec), "./tests/"}
cmd := GoTestCmdFn(ctx, g.rootDir, "go", args...)
var buf bytes.Buffer
cmd.Stdout = &buf
cmd.Stderr = &buf
err := cmd.Run()
return buf.String(), err != nil
}