Skip to content

Commit 460df03

Browse files
denikIsaac
andauthored
selection: list commit message names in the cmd, warn on rejected lines, exact CloudSlow bypass (#7548)
## Changes Follow-up to [#7524](#7524), addressing the review comments that came in after it merged: - **`CloudSlow` bypass needs an exact name.** Only a name that is the test dir itself, or one of its variants, lifts `CloudSlow`. A path above the dir (e.g. `cloud: TestAccept/bundle`) or a bare `TestAccept` still selects the whole subtree, but its `CloudSlow` tests stay skipped under `-short`, which keeps the PR run inside `cloud-select`'s 2h timeout. To support this, `selection.Named` now records per dir whether a name was exact (`Named.Exact`). - **Rejected lines are logged.** A line with a cloud prefix that is not read as test names, because some word does not start with `Test` (``aws: `TestAccept/x` ``, a trailing word, prose), now gets a `WARNING: ignoring "<line>" ...` in the test log, next to the existing warning for unmatched names. `ParseCommitMessage` returns these lines, and `FromCommitMessage` now returns a `CommitSelection` with `Summary()` and `Warnings()`, shared by the test runner and the cmd. - **The selection cmd lists named tests.** `go run ./acceptance/internal/selection/cmd [-cloud aws|azure|gcp]` prints, after the changed tests, the tests the last commit message names for that run (empty `-cloud` means a local run) as `go test -run` patterns, plus the warnings. It does this only in git mode, and only when the message names something or has a warning, so the output for ordinary commits is unchanged. With paths as arguments it does not read the message. - **Test helper reuse.** `TestLastCommitMessage` uses `runSelectionGit`, which now returns git's output. `commit_test.go` moves to `package selection_test` to share it. - `acceptance/README.md` updated. ## Why Review comments from a code owner on [#7524](#7524) that auto-merge landed before they were addressed. ## Tests - Unit tests: rejected lines (backticks, prose) are returned trimmed, and empty `aws:` or unknown prefixes are not reported. `Exact` is set for a dir name and for a variant name, unset for a prefix name and for bare `TestAccept`, and an exact name wins over a prefix name for the same dir. `Warnings()` text. - `acceptance/selftest/selection_exclusions` gets a case that commits a message with names, covering the cmd's local and `-cloud gcp` output and both warnings. - Local, with a commit naming `aws: TestAccept/cmd/fs/cp/file-to-file`, `cloud: TestAccept/bundle/validate/enum/DMS=true TestAccept/nope` and ``gcp: `TestAccept/cmd/fs/cp/dir-to-dir` ``: - `DATABRICKS_TEST_SELECT_CHANGED=50 go test ./acceptance -run '^TestAccept$' -v` ran the two named tests and logged both warnings. - `go run ./acceptance/internal/selection/cmd` printed `^bundle$/^validate$/^enum$/^DMS=true$` and `^cmd$/^fs$/^cp$/^file-to-file$` plus the warnings. With `-cloud gcp` it printed only the `cloud:` test. This pull request and its description were written by Isaac. --------- Co-authored-by: Isaac <no-reply@databricks.com>
1 parent 6a3c8ae commit 460df03

8 files changed

Lines changed: 258 additions & 72 deletions

File tree

‎acceptance/README.md‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,14 @@ cloud: TestAccept/bundle/resources/pipelines TestAccept/cmd/fs/cp/file-to-file
2727
gcp: TestAccept/bundle/validate/enum/DMS=true
2828
```
2929

30-
- `aws:`, `azure:`, `gcp:` run the test on that cloud only; `cloud:` runs it on every cloud. Prefixes are lowercase and must start the line (surrounding whitespace is ignored). Lines with any other prefix are ignored, and so is a line with a word that does not start with `Test` (so `aws: fix flaky TestAccept retries` is prose, not a request to run the suite).
30+
- `aws:`, `azure:`, `gcp:` run the test on that cloud only; `cloud:` runs it on every cloud. Prefixes are lowercase and must start the line (surrounding whitespace is ignored). Lines with any other prefix are ignored. A line with a word that does not start with `Test` is not read either (so `aws: fix flaky TestAccept retries` is prose, not a request to run the suite), and is logged as a warning, as is ``aws: `TestAccept/x` ``.
3131
- Every named test is also selected for the local (testserver) runs, whatever its prefix.
3232
- `TestAccept/<path>` selects every test dir that is `<path>` or under `<path>/`, with all its variants. `TestAccept/<dir>/KEY=value[/KEY=value...]` selects only the matching variants of test dir `<dir>`, as `go test` names them; each `KEY=value` must be in one of the dir's variants (per its `out.test.toml`). `TestAccept` alone selects every test. A name that matches no acceptance test (including non-acceptance test names) is logged as a warning and otherwise ignored.
3333
- The named tests are added to the changed-test selection (`DATABRICKS_TEST_SELECT_CHANGED`, used by the PR cloud run and `./task local-select`) and to the windows/macOS PR subset (`TESTS_SELECT_SUBSET_PCT`), without counting toward their limits. Runs with neither selection (Linux PR cell, nightly) do not read the commit message: they run every test.
34-
- On cloud, a named test still needs `Cloud = true` (and a matching `CloudEnvs`), but it runs even if it is `CloudSlow` (naming any variant of a dir lifts this for the dir).
34+
- On cloud, a named test still needs `Cloud = true` (and a matching `CloudEnvs`), but it runs even if it is `CloudSlow` when it is named by its own dir or variant name (naming any variant of a dir lifts this for the dir). A path above the dir (or a bare `TestAccept`) does not lift `CloudSlow`, so a whole subtree stays within the PR run's `-short` budget.
3535
- On pull requests, such a line makes `tools/testmask` select the tests and the cloud run even if the changed files alone would not. The merge queue ignores it.
3636
- On the merge commit GitHub creates for a PR (`refs/pull/N/merge`), the message of the PR's head commit is used. The parser is in `acceptance/internal/selection/commit.go`.
37+
- To see what a commit message selects without running the suite: `go run ./acceptance/internal/selection/cmd [-cloud aws|azure|gcp]` prints the named tests and the warnings after the changed ones.
3738

3839
## Authoring
3940

‎acceptance/acceptance_test.go‎

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -539,14 +539,14 @@ func testAccept(t *testing.T, inprocessMode bool, selectedTests []string, skipTo
539539
if cloudEnv != "" {
540540
namedCloud = getCloudEnvBase(cloudEnv)
541541
}
542-
named, summary, unmatched, err := selection.FromCommitMessage(".", testDirsSet, namedCloud)
542+
commit, err := selection.FromCommitMessage(".", testDirsSet, namedCloud)
543543
require.NoError(t, err)
544-
namedTests = named
544+
namedTests = commit.Named
545545
if len(namedTests) > 0 {
546-
t.Log(summary)
546+
t.Log(commit.Summary())
547547
}
548-
for _, name := range unmatched {
549-
t.Logf("WARNING: %q in the last commit message matches no acceptance test", name)
548+
for _, warning := range commit.Warnings() {
549+
t.Log("WARNING: " + warning)
550550
}
551551
}
552552
subset.changed = changedTests
@@ -610,7 +610,7 @@ func testAccept(t *testing.T, inprocessMode bool, selectedTests []string, skipTo
610610
t.Skip("Skipping test execution (only regenerating out.test.toml)")
611611
}
612612

613-
skipReason := getSkipReason(&config, configPath, namedTests.Has(dir))
613+
skipReason := getSkipReason(&config, configPath, namedTests.Exact(dir))
614614
if skipReason != "" {
615615
skippedDirs += 1
616616
t.Skip(skipReason)
@@ -745,7 +745,8 @@ func isSelectedVariant(changedTests map[string][]string, namedTests selection.Na
745745
}
746746

747747
// Return a reason to skip the test. Empty string means "don't skip". named is set for a test
748-
// the last commit message names, which runs on cloud even if it is CloudSlow.
748+
// the last commit message names by its own name (not by a path above it), which runs on cloud
749+
// even if it is CloudSlow.
749750
func getSkipReason(config *internal.TestConfig, configPath string, named bool) string {
750751
if Forcerun {
751752
return ""

‎acceptance/internal/selection/cmd/main.go‎

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,14 @@
55
//
66
// go run ./acceptance/internal/selection/cmd
77
// go run ./acceptance/internal/selection/cmd -limit 5
8+
// go run ./acceptance/internal/selection/cmd -cloud aws
89
// go run ./acceptance/internal/selection/cmd acceptance/bundle/invariant/configs/job.yml.tmpl
910
//
10-
// With no arguments the selection comes from git, exactly as the test runner gets it. Each
11-
// argument stands for a changed file instead, and counts as modified unless it carries a
12-
// status ("A:path" for an added file, "R100:old:new" for a rename).
11+
// With no arguments the selection comes from git, exactly as the test runner gets it, and
12+
// also lists the tests the last commit message names (see acceptance/README.md) for the run
13+
// -cloud picks: aws, azure or gcp, or empty for a local run. Each argument stands for a
14+
// changed file instead, and counts as modified unless it carries a status ("A:path" for an
15+
// added file, "R100:old:new" for a rename); the commit message is not read then.
1316
//
1417
// Each line is a `go test -run` pattern, so a selected test can be run as printed:
1518
//
@@ -27,6 +30,7 @@ package main
2730
import (
2831
"flag"
2932
"fmt"
33+
"maps"
3034
"os"
3135
"path/filepath"
3236
"regexp"
@@ -39,11 +43,15 @@ import (
3943
func main() {
4044
limit := flag.Int("limit", 50, "maximum number of changed tests to select")
4145
root := flag.String("root", "acceptance", "path to the acceptance directory")
46+
cloud := flag.String("cloud", "", "cloud to list commit message names for: aws, azure, gcp, or empty for a local run")
4247
flag.Parse()
4348

4449
if *limit <= 0 {
4550
fatalf("-limit must be a positive integer, got %d", *limit)
4651
}
52+
if !slices.Contains([]string{"", "aws", "azure", "gcp"}, *cloud) {
53+
fatalf("-cloud must be aws, azure, gcp or empty, got %q", *cloud)
54+
}
4755

4856
dirs, err := selection.FindTestDirs(*root)
4957
if err != nil {
@@ -55,13 +63,19 @@ func main() {
5563
}
5664

5765
var result selection.Result
58-
if args := flag.Args(); len(args) > 0 {
59-
result = selection.FromDiff(*root, diffFromArgs(args), testDirs, *limit)
66+
var commit selection.CommitSelection
67+
fromGit := len(flag.Args()) == 0
68+
if !fromGit {
69+
result = selection.FromDiff(*root, diffFromArgs(flag.Args()), testDirs, *limit)
6070
} else {
6171
result, err = selection.FromGit(*root, testDirs, *limit)
6272
if err != nil {
6373
fatalf("%s", err)
6474
}
75+
commit, err = selection.FromCommitMessage(*root, testDirs, *cloud)
76+
if err != nil {
77+
fatalf("%s", err)
78+
}
6579
}
6680

6781
fmt.Println(result.Counts())
@@ -73,6 +87,41 @@ func main() {
7387
if len(result.Selected) == 0 {
7488
fmt.Println(" none of the changed files belong to a test dir")
7589
}
90+
91+
if fromGit {
92+
printNamed(commit)
93+
}
94+
}
95+
96+
// printNamed prints the tests the last commit message names, as go test -run patterns, and
97+
// the warnings the test runner would log for it. A commit that names nothing prints nothing,
98+
// like the test runner, which logs the section only when there is something in it.
99+
func printNamed(commit selection.CommitSelection) {
100+
warnings := commit.Warnings()
101+
if len(commit.Named) == 0 && len(warnings) == 0 {
102+
return
103+
}
104+
fmt.Printf("Selected %d test dirs named in the last commit message (%s)\n", len(commit.Named), commit.Where)
105+
for _, dir := range slices.Sorted(maps.Keys(commit.Named)) {
106+
elements := strings.Split(dir, "/")
107+
var names []string
108+
for _, filters := range commit.Named[dir].Filters {
109+
if filters == nil {
110+
// Every variant runs, so the variant names below add nothing.
111+
names = []string{runPattern(elements)}
112+
break
113+
}
114+
if name := runPattern(append(slices.Clone(elements), filters...)); !slices.Contains(names, name) {
115+
names = append(names, name)
116+
}
117+
}
118+
for _, name := range names {
119+
fmt.Printf(" %s\n", name)
120+
}
121+
}
122+
for _, warning := range warnings {
123+
fmt.Printf("WARNING: %s\n", warning)
124+
}
76125
}
77126

78127
// variantNames returns the go test names of the selected test. go test reads a name as a

‎acceptance/internal/selection/commit.go‎

Lines changed: 93 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -35,25 +35,30 @@ type CommitName struct {
3535
}
3636

3737
// ParseCommitMessage returns the test names on the "<cloud>: <names>" lines of msg. A line may
38-
// list several whitespace-separated names. Lines with any other prefix are ignored, and so are
39-
// lines with a word that is not a go test name: "aws: fix flaky TestAccept retries" is prose,
40-
// and taking its bare TestAccept as a name would run the whole suite.
41-
func ParseCommitMessage(msg string) []CommitName {
42-
var names []CommitName
38+
// list several whitespace-separated names. Lines with any other prefix are ignored. A line with
39+
// a cloud prefix and a word that is not a go test name is returned as rejected instead: "aws:
40+
// fix flaky TestAccept retries" is prose, and taking its bare TestAccept as a name would run
41+
// the whole suite, but "aws: `TestAccept/x`" is a request that must not be dropped silently.
42+
func ParseCommitMessage(msg string) (names []CommitName, rejected []string) {
4343
for line := range strings.SplitSeq(msg, "\n") {
44-
m := commitLineRe.FindStringSubmatch(strings.TrimSpace(line))
44+
line = strings.TrimSpace(line)
45+
m := commitLineRe.FindStringSubmatch(line)
4546
if m == nil {
4647
continue
4748
}
4849
words := strings.Fields(m[2])
49-
if len(words) == 0 || slices.ContainsFunc(words, func(word string) bool { return !strings.HasPrefix(word, "Test") }) {
50+
if len(words) == 0 {
51+
continue
52+
}
53+
if slices.ContainsFunc(words, func(word string) bool { return !strings.HasPrefix(word, "Test") }) {
54+
rejected = append(rejected, line)
5055
continue
5156
}
5257
for _, name := range words {
5358
names = append(names, CommitName{Cloud: m[1], Name: name})
5459
}
5560
}
56-
return names
61+
return names, rejected
5762
}
5863

5964
// NamesForCloud returns the names that apply to cloud (an aws/azure/gcp base cloud name), or
@@ -71,30 +76,59 @@ func NamesForCloud(names []CommitName, cloud string) []string {
7176
return out
7277
}
7378

74-
// Named maps a test dir to the variant filters of each name that selected it. A nil entry
75-
// means a name selected every variant of the dir. Names are kept apart because each one's
76-
// filters must all hold together, while the names themselves are alternatives.
77-
type Named map[string][][]string
79+
// Named maps a test dir to how the names in a commit message selected it.
80+
type Named map[string]*NamedDir
81+
82+
// NamedDir records the names that selected one test dir.
83+
type NamedDir struct {
84+
// Filters holds the variant filters of each name that selected the dir. A nil entry means
85+
// a name selected every variant of the dir. Names are kept apart because each one's
86+
// filters must all hold together, while the names themselves are alternatives.
87+
Filters [][]string
88+
89+
// Exact is set when a name is the dir itself or one of its variants, as opposed to a
90+
// path above it (or a bare TestAccept) that selects a whole subtree.
91+
Exact bool
92+
}
7893

7994
// Has reports whether any name selected dir.
8095
func (n Named) Has(dir string) bool {
8196
_, ok := n[dir]
8297
return ok
8398
}
8499

100+
// Exact reports whether a name is dir itself or one of its variants. Only such names lift
101+
// CloudSlow: a name like "cloud: TestAccept/bundle" asks for a subtree, not for its slowest
102+
// tests, which -short keeps out of the PR run's time budget.
103+
func (n Named) Exact(dir string) bool {
104+
d, ok := n[dir]
105+
return ok && d.Exact
106+
}
107+
85108
// Matches reports whether a name selected the variant envset of dir.
86109
func (n Named) Matches(dir string, envset []string) bool {
87-
for _, filters := range n[dir] {
110+
d, ok := n[dir]
111+
if !ok {
112+
return false
113+
}
114+
for _, filters := range d.Filters {
88115
if filters == nil || MatchesFilters(envset, filters) {
89116
return true
90117
}
91118
}
92119
return false
93120
}
94121

95-
// add records that a name selected dir with filters, nil for every variant.
96-
func (n Named) add(dir string, filters []string) {
97-
n[dir] = append(n[dir], filters)
122+
// add records that a name selected dir with filters, nil for every variant. exact is set
123+
// when the name is dir or one of its variants.
124+
func (n Named) add(dir string, filters []string, exact bool) {
125+
d, ok := n[dir]
126+
if !ok {
127+
d = &NamedDir{}
128+
n[dir] = d
129+
}
130+
d.Filters = append(d.Filters, filters)
131+
d.Exact = d.Exact || exact
98132
}
99133

100134
// ResolveNames maps go test names to the test dirs they select, and returns the names that
@@ -126,7 +160,7 @@ func resolveName(root, name string, testDirs map[string]bool, named Named) bool
126160
found := false
127161
for dir := range testDirs {
128162
if rest == "" || dir == rest || strings.HasPrefix(dir, rest+"/") {
129-
named.add(dir, nil)
163+
named.add(dir, nil, rest != "" && dir == rest)
130164
found = true
131165
}
132166
}
@@ -144,7 +178,7 @@ func resolveName(root, name string, testDirs map[string]bool, named Named) bool
144178
if !slices.ContainsFunc(Variants(root, dir), func(envset []string) bool { return hasAll(envset, filters) }) {
145179
return false
146180
}
147-
named.add(dir, filters)
181+
named.add(dir, filters, true)
148182
return true
149183
}
150184
return false
@@ -186,22 +220,55 @@ func commitMessage(rev string) (string, error) {
186220
return string(out), nil
187221
}
188222

223+
// CommitSelection is what the last commit message selects for one run.
224+
type CommitSelection struct {
225+
// Named holds the selected test dirs.
226+
Named Named
227+
228+
// Matched lists the names that selected a test, Unmatched the ones that selected none.
229+
Matched []string
230+
Unmatched []string
231+
232+
// Rejected lists the lines with a cloud prefix that were not read as test names (see
233+
// ParseCommitMessage).
234+
Rejected []string
235+
236+
// Where is the cloud the selection is for, or "local".
237+
Where string
238+
}
239+
240+
// Summary is the selection on one line, for the test log.
241+
func (c CommitSelection) Summary() string {
242+
return fmt.Sprintf("Selected %d test dirs named in the last commit message (%s): %s", len(c.Named), c.Where, strings.Join(c.Matched, " "))
243+
}
244+
245+
// Warnings returns one line per name that selected nothing and per rejected line, so a
246+
// request that is not honored is visible in the test log.
247+
func (c CommitSelection) Warnings() []string {
248+
var warnings []string
249+
for _, name := range c.Unmatched {
250+
warnings = append(warnings, fmt.Sprintf("%q in the last commit message matches no acceptance test", name))
251+
}
252+
for _, line := range c.Rejected {
253+
warnings = append(warnings, fmt.Sprintf("ignoring %q in the last commit message: every word after the cloud prefix must be a test name such as TestAccept/<path>", line))
254+
}
255+
return warnings
256+
}
257+
189258
// FromCommitMessage selects among testDirs the tests the last commit message names for cloud
190-
// (empty for a local run, see NamesForCloud). root is the acceptance directory. It returns the
191-
// selection, a one-line summary of the names that matched for the test log, and the names that
192-
// matched no test.
193-
func FromCommitMessage(root string, testDirs map[string]bool, cloud string) (Named, string, []string, error) {
259+
// (empty for a local run, see NamesForCloud). root is the acceptance directory.
260+
func FromCommitMessage(root string, testDirs map[string]bool, cloud string) (CommitSelection, error) {
194261
msg, err := LastCommitMessage()
195262
if err != nil {
196-
return nil, "", nil, err
263+
return CommitSelection{}, err
197264
}
198-
names := NamesForCloud(ParseCommitMessage(msg), cloud)
265+
parsed, rejected := ParseCommitMessage(msg)
266+
names := NamesForCloud(parsed, cloud)
199267
named, unmatched := ResolveNames(root, names, testDirs)
200268
matched := slices.DeleteFunc(names, func(name string) bool { return slices.Contains(unmatched, name) })
201269
where := "local"
202270
if cloud != "" {
203271
where = cloud
204272
}
205-
summary := fmt.Sprintf("Selected %d test dirs named in the last commit message (%s): %s", len(named), where, strings.Join(matched, " "))
206-
return named, summary, unmatched, nil
273+
return CommitSelection{Named: named, Matched: matched, Unmatched: unmatched, Rejected: rejected, Where: where}, nil
207274
}

0 commit comments

Comments
 (0)