-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintegration_test.go
More file actions
188 lines (156 loc) · 5.84 KB
/
integration_test.go
File metadata and controls
188 lines (156 loc) · 5.84 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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package main
import (
"bytes"
"encoding/json"
"os"
"os/exec"
"path/filepath"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
func TestRunWithRealGitHandlesAddedFileAtRevision(t *testing.T) {
t.Parallel()
requireGit(t)
repoRoot := t.TempDir()
runGit(t, repoRoot, "init")
runGit(t, repoRoot, "config", "user.email", "test@example.com")
runGit(t, repoRoot, "config", "user.name", "Test User")
runGit(t, repoRoot, "commit", "--allow-empty", "-m", "base")
baseSHA := strings.TrimSpace(runGit(t, repoRoot, "rev-parse", "HEAD"))
writeTestFile(t, repoRoot, "pkg/new_test.go", `package sample
import "testing"
func TestAdded(t *testing.T) {
t.Log("added")
}
`)
runGit(t, repoRoot, "add", ".")
runGit(t, repoRoot, "commit", "-m", "head")
headSHA := strings.TrimSpace(runGit(t, repoRoot, "rev-parse", "HEAD"))
matrixPath := filepath.Join(repoRoot, "matrix.json")
summaryPath := filepath.Join(repoRoot, "summary.md")
var stdout bytes.Buffer
var stderr bytes.Buffer
err := runCommand(t.Context(), commandConfig{config: config{RepoRoot: repoRoot, BaseSHA: baseSHA, HeadSHA: headSHA, OutMatrix: matrixPath, OutSummary: summaryPath}}, &stdout, &stderr, execGit, nil)
require.NoError(t, err)
var matrix matrixOutput
matrixData, err := os.ReadFile(matrixPath)
require.NoError(t, err)
require.NoError(t, json.Unmarshal(matrixData, &matrix))
require.Len(t, matrix.Include, 1)
require.Equal(t, "./pkg", matrix.Include[0].Package)
require.Equal(t, "^(TestAdded)(/.*)?$", matrix.Include[0].RunRegex)
summary, err := os.ReadFile(summaryPath)
require.NoError(t, err)
require.Contains(t, string(summary), "TestAdded")
}
func TestRunWithRealGitIgnoresDeletedSetupFile(t *testing.T) {
t.Parallel()
requireGit(t)
repoRoot := t.TempDir()
runGit(t, repoRoot, "init")
runGit(t, repoRoot, "config", "user.email", "test@example.com")
runGit(t, repoRoot, "config", "user.name", "Test User")
writeTestFile(t, repoRoot, "pkg/setup_test.go", `package sample
import "testing"
func setup(t *testing.T) {
t.Helper()
}
`)
writeTestFile(t, repoRoot, "pkg/alpha_test.go", `package sample
import "testing"
func TestAlpha(t *testing.T) {
t.Log("alpha")
}
`)
runGit(t, repoRoot, "add", ".")
runGit(t, repoRoot, "commit", "-m", "base")
baseSHA := strings.TrimSpace(runGit(t, repoRoot, "rev-parse", "HEAD"))
runGit(t, repoRoot, "rm", "pkg/setup_test.go")
runGit(t, repoRoot, "commit", "-m", "head")
headSHA := strings.TrimSpace(runGit(t, repoRoot, "rev-parse", "HEAD"))
matrixPath := filepath.Join(repoRoot, "matrix.json")
summaryPath := filepath.Join(repoRoot, "summary.md")
var stdout bytes.Buffer
var stderr bytes.Buffer
err := runCommand(t.Context(), commandConfig{config: config{RepoRoot: repoRoot, BaseSHA: baseSHA, HeadSHA: headSHA, OutMatrix: matrixPath, OutSummary: summaryPath}}, &stdout, &stderr, execGit, nil)
require.NoError(t, err)
var matrix matrixOutput
matrixData, err := os.ReadFile(matrixPath)
require.NoError(t, err)
require.NoError(t, json.Unmarshal(matrixData, &matrix))
require.Empty(t, matrix.Include)
summary, err := os.ReadFile(summaryPath)
require.NoError(t, err)
require.Contains(t, string(summary), "pkg/setup_test.go")
require.Contains(t, string(summary), "no runnable top-level tests were selected")
require.NotContains(t, string(summary), "TestAlpha")
}
func TestEnsureRangeAvailableWithRealGitFetchesMovedBase(t *testing.T) {
t.Parallel()
requireGit(t)
root := t.TempDir()
workRoot := filepath.Join(root, "work")
bareRoot := filepath.Join(root, "upstream.git")
cloneRoot := filepath.Join(root, "clone")
require.NoError(t, os.MkdirAll(workRoot, 0o750))
runGit(t, workRoot, "init")
runGit(t, workRoot, "config", "user.email", "test@example.com")
runGit(t, workRoot, "config", "user.name", "Test User")
writeTestFile(t, workRoot, "pkg/sample_test.go", `package sample
import "testing"
func TestAlpha(t *testing.T) {
t.Log("base")
}
`)
runGit(t, workRoot, "add", ".")
runGit(t, workRoot, "commit", "-m", "base")
runGit(t, workRoot, "branch", "-M", "main")
runGit(t, workRoot, "checkout", "-b", "feature")
writeTestFile(t, workRoot, "pkg/sample_test.go", `package sample
import "testing"
func TestAlpha(t *testing.T) {
t.Log("feature")
}
`)
runGit(t, workRoot, "commit", "-am", "feature")
headSHA := strings.TrimSpace(runGit(t, workRoot, "rev-parse", "HEAD"))
runGit(t, workRoot, "checkout", "main")
writeTestFile(t, workRoot, "README.md", "base branch moved\n")
runGit(t, workRoot, "add", "README.md")
runGit(t, workRoot, "commit", "-m", "move base")
baseSHA := strings.TrimSpace(runGit(t, workRoot, "rev-parse", "HEAD"))
runGit(t, workRoot, "init", "--bare", bareRoot)
runGit(t, workRoot, "remote", "add", "origin", bareRoot)
runGit(t, workRoot, "push", "origin", "main", "feature")
runGit(t, root, "clone", "--single-branch", "--branch", "feature", "file://"+bareRoot, cloneRoot)
_, err := execGit(t.Context(), cloneRoot, "cat-file", "-e", baseSHA+"^{commit}")
require.Error(t, err)
req := runRequest{
RepoRoot: cloneRoot,
Range: diffRange{BaseSHA: baseSHA, HeadSHA: headSHA},
Fetches: []fetchSpec{
{Remote: "origin", Ref: remoteTrackingRefspec(defaultDispatchBaseRef)},
{Remote: "origin", Ref: baseSHA},
},
}
err = ensureRangeAvailable(t.Context(), &req, execGit, execGitFetch)
require.NoError(t, err)
changed := strings.TrimSpace(runGit(t, cloneRoot, "diff", "--name-only", baseSHA+"..."+headSHA))
require.Equal(t, "pkg/sample_test.go", changed)
}
func requireGit(t *testing.T) {
t.Helper()
if _, err := exec.LookPath("git"); err != nil {
t.Skipf("git is not available on PATH: %v", err)
}
}
func runGit(t *testing.T, dir string, args ...string) string {
t.Helper()
cmd := exec.Command("git", args...)
cmd.Dir = dir
cmd.Env = append(os.Environ(), "LC_ALL=C", "LANG=C")
output, err := cmd.CombinedOutput()
require.NoErrorf(t, err, "git %s failed: %s", strings.Join(args, " "), string(output))
return string(output)
}