-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpit-run
executable file
·291 lines (224 loc) · 8.81 KB
/
pit-run
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#!/usr/bin/env python3
"""
Script add PIT mutation plugin to maven files and run mutations.
"""
import os
import os.path
import shutil
import subprocess
import sys
from argparse import ArgumentParser, Namespace
from contextlib import contextmanager
from dataclasses import dataclass
from typing import Iterator, Optional, Self, Any
from xml.etree.ElementTree import SubElement, Element, parse, register_namespace
PIT_GROUP_ID = "org.pitest"
PIT_ARTIFACT_ID = "pitest-maven"
PIT_VERSION = "1.16.1"
PIT_PLUGINS = [
(PIT_GROUP_ID, "pitest-junit5-plugin", "1.2.1"),
]
MUTATORS = ["STRONGER"]
BROWSER_COMMAND = "x-www-browser" # or xdg-open
NAMESPACES = {"": "http://maven.apache.org/POM/4.0.0"}
def create_args() -> Namespace:
parser = ArgumentParser(description=__doc__)
parser.add_argument(
"-pl",
"--projects",
help="Comma-delimited list of specified reactor projects to build instead of all projects (maven options)",
)
parser.add_argument(
"-id", "--artifact-id", help="artifactId of project in which PIT should run"
)
parser.add_argument(
"-am",
"--also-make",
action="store_true",
help="If project list is specified, also build projects required by the list (maven options)",
)
parser.add_argument(
"-q",
"--quiet",
action="store_true",
help="Quiet output - only show errors (maven options)",
)
parser.add_argument(
"-w", "--open-browser", action="store_true", help="Open result in browser"
)
parser.add_argument("globs", nargs="+")
return parser.parse_args()
@dataclass(frozen=True)
class XmlElement:
underlying_element: Element
@property
def text(self) -> Optional[str]:
return self.underlying_element.text
def find(self, path: str) -> Optional[Self]:
element = self.underlying_element.find(path, NAMESPACES)
return None if element is None else XmlElement(element)
def get_or_create(self, tag: str) -> Self:
element = self.find(tag)
return (
XmlElement(SubElement(self.underlying_element, tag))
if element is None
else element
)
def add_text_child(self, tag: str, text: Any) -> None:
child = xml_text_element(tag, text)
self.add_child(child)
def add_child(self, child: Self) -> None:
self.underlying_element.append(child.underlying_element)
def xml_element(tag: str) -> XmlElement:
element = Element(tag)
return XmlElement(element)
def xml_text_element(tag: str, text: Any) -> XmlElement:
element = Element(tag)
element.text = str(text)
return XmlElement(element)
def create_skip_configuration() -> XmlElement:
configuration = xml_element("configuration")
configuration.add_child(xml_text_element("skip", "True"))
return configuration
def create_configuration(globs: list[str]) -> XmlElement:
configuration = xml_element("configuration")
configuration.add_child(xml_text_element("skip", "False"))
target_classes = xml_element("targetClasses")
target_tests = xml_element("targetTests")
for glob in globs:
parameter = xml_text_element("param", glob)
target_classes.add_child(parameter)
target_tests.add_child(parameter)
mutators = xml_element("mutators")
for mutator in MUTATORS:
mutators.add_text_child("mutator", mutator)
output_formats = xml_element("outputFormats")
output_formats.add_text_child("outputFormat", "HTML")
output_formats.add_text_child("outputFormat", "XML")
configuration.add_child(target_classes)
configuration.add_child(target_tests)
configuration.add_child(mutators)
configuration.add_child(output_formats)
return configuration
def create_plugin(root: XmlElement, configuration: XmlElement) -> None:
plugin = xml_element("plugin")
plugin.add_text_child("groupId", PIT_GROUP_ID)
plugin.add_text_child("artifactId", PIT_ARTIFACT_ID)
plugin.add_text_child("version", PIT_VERSION)
plugin.add_child(configuration)
dependencies = xml_element("dependencies")
for group_id, artifact_id, version in PIT_PLUGINS:
dependency = xml_element("dependency")
dependency.add_text_child("groupId", group_id)
dependency.add_text_child("artifactId", artifact_id)
dependency.add_text_child("version", version)
dependencies.add_child(dependency)
plugin.add_child(dependencies)
root.get_or_create("build").get_or_create("plugins").add_child(plugin)
class PomFile:
def __init__(self, filename: str) -> None:
self.tree = parse(filename)
self.root = XmlElement(self.tree.getroot())
def is_skip_pit_for(self, expected_artifact_id: Optional[str]) -> bool:
if expected_artifact_id is None:
return False
artifact_element = self.root.find("./artifactId")
return artifact_element is None or artifact_element.text != expected_artifact_id
def configure_pit_plugin(
self, project_artifact_id: Optional[str], globs: list[str]
) -> None:
if self.is_skip_pit_for(project_artifact_id):
configuration = create_skip_configuration()
else:
configuration = create_configuration(globs)
create_plugin(self.root, configuration)
def write(self, filename: str) -> None:
self.tree.write(filename)
def get_pom_files() -> Iterator[str]:
for root, _, files in os.walk("."):
for file in files:
if file == "pom.xml":
yield os.path.join(root, file)
def make_file_backup(filename: str) -> str:
backup_filename = f"{filename}.bak.{os.getpid()}"
shutil.copyfile(filename, backup_filename)
return backup_filename
def create_pit_plugin_configuration(
globs: list[str], skip_pit_in_project: bool
) -> XmlElement:
configuration = xml_element("configuration")
configuration.add_child(xml_text_element("skip", skip_pit_in_project))
if skip_pit_in_project:
return configuration
target_classes = xml_element("targetClasses")
target_tests = xml_element("targetTests")
for glob in globs:
parameter = xml_text_element("param", glob)
target_classes.add_child(parameter)
target_tests.add_child(parameter)
mutators = xml_element("mutators")
for mutator in MUTATORS:
mutators.add_text_child("mutator", mutator)
output_formats = xml_element("outputFormats")
output_formats.add_text_child("outputFormat", "HTML")
output_formats.add_text_child("outputFormat", "XML")
configuration.add_child(target_classes)
configuration.add_child(target_tests)
configuration.add_child(mutators)
configuration.add_child(output_formats)
return configuration
def run_mutation_coverage(args: Namespace) -> None:
command = [
"mvn",
"--batch-mode",
"test-compile",
"org.pitest:pitest-maven:mutationCoverage",
]
if args.also_make:
command.append("--also-make")
if args.projects:
command.append("--projects")
command.append(args.projects)
if args.quiet:
command.append("--quiet")
subprocess.call(" ".join(command), shell=True, stdout=sys.stdout, stderr=sys.stderr)
def print_details(filename: str) -> None:
subprocess.call(
["pandoc", "--to", "plain", filename], stdout=sys.stdout, stderr=sys.stderr
)
def open_in_browser(filename: str) -> None:
subprocess.call([BROWSER_COMMAND, os.path.abspath(filename)])
@contextmanager
def pit_in_poms(project_artifact_id: str | None, globs: list[str]) -> Iterator[None]:
backups: list[tuple[str, str]] = []
try:
for filename in get_pom_files():
backup_filename = f"{filename}.bak.{os.getpid()}"
shutil.copyfile(filename, backup_filename)
backups.append((filename, backup_filename))
pom_file = PomFile(filename)
pom_file.configure_pit_plugin(project_artifact_id, globs)
pom_file.write(filename)
yield None
finally:
for target_filename, backup_filename in backups:
shutil.move(backup_filename, target_filename)
def find_report_filename_for(projects: str) -> Optional[str]:
filename = os.path.join(projects, "target", "pit-reports", "index.html")
if os.path.isfile(filename):
return filename
return None
def main() -> None:
args = create_args()
for prefix, uri in NAMESPACES.items():
register_namespace(prefix, uri)
with pit_in_poms(args.artifact_id or args.projects, args.globs):
run_mutation_coverage(args)
report_filename = find_report_filename_for(args.projects)
if report_filename:
print_details(report_filename)
print("Report:", os.path.abspath(report_filename))
if args.open_browser:
open_in_browser(report_filename)
if __name__ == "__main__":
main()