Skip to content

Commit d0d6ecc

Browse files
authored
Merge pull request #1225 from cloudbees-oss/mvn-plugin-support-exclude-option
Add --exclude option to the maven profile
2 parents f5d1e92 + 5489f1b commit d0d6ecc

7 files changed

Lines changed: 73 additions & 6 deletions

File tree

launchable/test_runners/maven.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import glob
22
import os
3-
from typing import Dict, List, Optional
3+
import re
4+
from typing import Dict, List, Optional, Tuple
45

56
import click
67

@@ -60,9 +61,24 @@ def is_file(f: str) -> bool:
6061
is_flag=True,
6162
help="Scan testCompile/default-testCompile/createdFiles.lst for *.lst files generated by `mvn compile` and use them as test inputs.", # noqa: E501
6263
)
64+
@click.option(
65+
'--exclude',
66+
'exclude_rules',
67+
required=False,
68+
multiple=True,
69+
help="Exclude tests matching the given Python regular expression pattern. Can be specified multiple times.",
70+
)
6371
@click.argument('source_roots', required=False, nargs=-1)
6472
@launchable.subset
65-
def subset(client, source_roots, test_compile_created_file, is_scan_test_compile_lst):
73+
def subset(client, source_roots, test_compile_created_file, is_scan_test_compile_lst, exclude_rules: Tuple[str, ...]):
74+
75+
# Compile exclude rules
76+
compiled_exclude_rules = []
77+
for rule in exclude_rules:
78+
try:
79+
compiled_exclude_rules.append(re.compile(rule))
80+
except re.error as e:
81+
raise click.BadParameter("Invalid regular expression '{}': {}".format(rule, e))
6682

6783
def file2class_test_path(f: str) -> List[Dict[str, str]]:
6884
# remove extension
@@ -74,7 +90,13 @@ def file2class_test_path(f: str) -> List[Dict[str, str]]:
7490

7591
def file2test(f: str) -> Optional[List]:
7692
if is_file(f):
77-
return file2class_test_path(f)
93+
test_path = file2class_test_path(f)
94+
95+
for pattern in compiled_exclude_rules:
96+
if pattern.search(test_path[0]["name"]):
97+
return None
98+
99+
return test_path
78100
else:
79101
return None
80102

tests/data/maven/java/test/src/java/com/launchableinc/rocket_car_maven/e2e/E2ETest.java

Whitespace-only changes.

tests/data/maven/subset_by_absolute_time_result.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
{"type": "class", "name": "com.launchableinc.rocket_car_maven.App2Test"}
55
],
66
[
7-
{ "type": "class", "name": "com.launchableinc.rocket_car_maven.AppTest"}
7+
{"type": "class", "name": "com.launchableinc.rocket_car_maven.AppTest"}
8+
],
9+
[
10+
{"type": "class", "name": "com.launchableinc.rocket_car_maven.e2e.E2ETest"}
811
]],
912
"testRunner": "maven",
1013
"session": {

tests/data/maven/subset_by_confidence_result.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
{"type": "class", "name": "com.launchableinc.rocket_car_maven.App2Test"}
55
],
66
[
7-
{ "type": "class", "name": "com.launchableinc.rocket_car_maven.AppTest"}
7+
{"type": "class", "name": "com.launchableinc.rocket_car_maven.AppTest"}
8+
],
9+
[
10+
{"type": "class", "name": "com.launchableinc.rocket_car_maven.e2e.E2ETest"}
811
]],
912
"testRunner": "maven",
1013
"session": {

tests/data/maven/subset_result.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,10 @@
44
{"type": "class", "name": "com.launchableinc.rocket_car_maven.App2Test"}
55
],
66
[
7-
{ "type": "class", "name": "com.launchableinc.rocket_car_maven.AppTest"}
7+
{"type": "class", "name": "com.launchableinc.rocket_car_maven.AppTest"}
8+
],
9+
[
10+
{"type": "class", "name": "com.launchableinc.rocket_car_maven.e2e.E2ETest"}
811
]],
912
"testRunner": "maven",
1013
"goal": {"type": "subset-by-percentage", "percentage": 0.1},
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"testPaths": [
3+
[
4+
{"type": "class", "name": "com.launchableinc.rocket_car_maven.App2Test"}
5+
],
6+
[
7+
{"type": "class", "name": "com.launchableinc.rocket_car_maven.AppTest"}
8+
]],
9+
"testRunner": "maven",
10+
"goal": {"type": "subset-by-percentage", "percentage": 0.1},
11+
"ignoreNewTests": false,
12+
"session": {
13+
"id": "16"
14+
},
15+
"getTestsFromGuess": false,
16+
"getTestsFromPreviousSessions": false
17+
}

tests/test_runners/test_maven.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,25 @@ def default_path_builder(case, suite, report_file):
208208
str(self.test_files_dir) + "/maven/reports/TEST-nested.xml")
209209
self.assert_success(result)
210210

211+
@responses.activate
212+
@mock.patch.dict(os.environ, {"LAUNCHABLE_TOKEN": CliTestCase.launchable_token})
213+
def test_subset_with_exclude(self):
214+
# Invalid regexp case
215+
result = self.cli('subset', '--target', '10%', '--session',
216+
self.session, 'maven',
217+
'--exclude', r'[invalid',
218+
str(self.test_files_dir.joinpath('java/test/src/java/').resolve()))
219+
self.assertNotEqual(result.exit_code, 0)
220+
self.assertIn("Invalid regular expression", result.output)
221+
222+
# Success case
223+
result = self.cli('subset', '--target', '10%', '--session',
224+
self.session, 'maven',
225+
'--exclude', r'\.e2e\.',
226+
str(self.test_files_dir.joinpath('java/test/src/java/').resolve()))
227+
self.assert_success(result)
228+
self.assert_subset_payload('subset_with_exclude_rules_result.json')
229+
211230
def test_glob(self):
212231
for x in [
213232
'foo/BarTest.java',

0 commit comments

Comments
 (0)