-
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathMutationTest.php
More file actions
176 lines (132 loc) · 5.1 KB
/
Copy pathMutationTest.php
File metadata and controls
176 lines (132 loc) · 5.1 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
<?php
declare(strict_types=1);
namespace Pest\Mutate;
use ParaTest\Options;
use Pest\Mutate\Event\Facade;
use Pest\Mutate\Plugins\Mutate;
use Pest\Mutate\Repositories\TelemetryRepository;
use Pest\Mutate\Support\Configuration\Configuration;
use Pest\Mutate\Support\MutationTestResult;
use Pest\Support\Container;
use Symfony\Component\Process\Exception\ProcessTimedOutException;
use Symfony\Component\Process\Process;
class MutationTest
{
private MutationTestResult $result = MutationTestResult::None;
private ?float $start = null;
private ?float $finish = null;
private Process $process;
public function __construct(public readonly Mutation $mutation) {}
public function getId(): string
{
return $this->mutation->id;
}
public function result(): MutationTestResult
{
return $this->result;
}
public function updateResult(MutationTestResult $result): void
{
$this->result = $result;
}
/**
* @param array<string, array<int, array<int, string>>> $coveredLines
* @param array<int, string> $originalArguments
*/
public function start(array $coveredLines, Configuration $configuration, array $originalArguments, ?int $processId = null): bool
{
// TODO: we should pass the tests to run in another way, maybe via cache, mutation or env variable
$filters = [];
foreach (range($this->mutation->startLine, $this->mutation->endLine) as $lineNumber) {
$tests = $coveredLines[$this->mutation->file->getRealPath()][$lineNumber] ?? [];
if ($tests === [] && array_key_exists($this->mutation->file->getRealPath(), $coveredLines)) {
$tests = array_unique(array_merge(...array_values($coveredLines[$this->mutation->file->getRealPath()])));
}
foreach ($tests as $test) {
preg_match('/\\\\([a-zA-Z0-9]*)::(__pest_evaluable_)?([^#]*)"?/', $test, $matches);
if ($matches[2] === '__pest_evaluable_') { // @phpstan-ignore-line
$filters[] = $matches[1].'::(.*)'.str_replace(['__', '_'], ['.{1,2}', '.'], $matches[3]); // @phpstan-ignore-line
} else {
$filters[] = $matches[1].'::(.*)'.$matches[3]; // @phpstan-ignore-line
}
}
}
$filters = array_unique($filters);
if ($filters === []) {
$this->updateResult(MutationTestResult::Uncovered);
Facade::instance()->emitter()->mutationUncovered($this);
return false;
}
$envs = [
Mutate::ENV_MUTATION_TESTING => $this->mutation->file->getRealPath(),
Mutate::ENV_MUTATION_FILE => $this->mutation->modifiedSourcePath,
];
if ($processId !== null) {
$envs['PARATEST'] = '1';
$envs[Options::ENV_KEY_TOKEN] = $processId;
$envs[Options::ENV_KEY_UNIQUE_TOKEN] = uniqid($processId.'_');
$envs['LARAVEL_PARALLEL_TESTING'] = 1;
}
// TODO: filter arguments to remove unnecessary stuff (Teamcity, Coverage, etc.)
$process = new Process(
command: [
...$originalArguments,
'--bail',
'--filter="'.implode('|', $filters).'"',
],
env: $envs,
timeout: $this->calculateTimeout(),
);
$this->start = microtime(true);
$process->start();
$this->process = $process;
return true;
}
private function calculateTimeout(): int
{
$initialTestSuiteDuration = Container::getInstance()->get(TelemetryRepository::class) // @phpstan-ignore-line
->getInitialTestSuiteDuration();
return (int) ($initialTestSuiteDuration + max(5, $initialTestSuiteDuration * 0.2));
}
public function hasFinished(): bool
{
try {
if ($this->process->isRunning()) {
$this->process->checkTimeout();
return false;
}
} catch (ProcessTimedOutException) {
$this->updateResult(MutationTestResult::Timeout);
Facade::instance()->emitter()->mutationTimedOut($this);
$this->finish = microtime(true);
return true;
}
if ($this->process->isSuccessful()) {
$this->updateResult(MutationTestResult::Untested);
Facade::instance()->emitter()->mutationEscaped($this);
$this->finish = microtime(true);
return true;
}
$this->updateResult(MutationTestResult::Tested);
Facade::instance()->emitter()->mutationTested($this);
$this->finish = microtime(true);
return true;
}
public function duration(): float
{
if ($this->start === null) {
return 0;
}
if ($this->finish === null) {
return 0;
}
return $this->finish - $this->start;
}
/**
* @param array<string, string> $results
*/
public function lastRunResult(array $results): MutationTestResult
{
return MutationTestResult::from($results[$this->getId()] ?? 'none');
}
}