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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
#![cfg(feature = "mutation-testing")]
//! GREEN Phase Tests for PMAT-070-003: cargo-mutants Backend Integration
//!
//! This test suite validates the cargo-mutants backend for `pmat mutate --use-cargo-mutants`.
//! Following Extreme TDD: Tests written in RED phase, implementation in GREEN phase.
//!
//! Note: Sprint 61 implemented PMAT's built-in mutation testing (`pmat mutate`).
//! Sprint 70 adds cargo-mutants wrapper as an alternative backend via `--use-cargo-mutants`.
use pmat::cli::handlers::cargo_mutants_backend::{self, CargoMutantsConfig};
use pmat::services::mutation::cargo_mutants_wrapper::CargoMutantsWrapper;
use pmat::services::mutation::json_parser::CargoMutantsReport;
use std::path::PathBuf;
// ============================================================================
// Test Helpers
// ============================================================================
/// Helper to create a default test config
fn test_config() -> CargoMutantsConfig {
CargoMutantsConfig {
path: PathBuf::from("."),
output: None,
timeout: 300,
jobs: None,
features: None,
all_features: false,
no_default_features: false,
no_shuffle: false,
}
}
// ============================================================================
// Unit Tests
// ============================================================================
#[test]
#[ignore] // RED phase: Will fail until backend implemented
fn test_cargo_mutants_backend_detects_installation() {
// Test: Verify cargo-mutants detection
// Expected: CargoMutantsWrapper::new() succeeds if installed
// Returns error with installation instructions if not found
let wrapper_result = CargoMutantsWrapper::new();
// Should either succeed (if installed) or fail gracefully
match wrapper_result {
Ok(_) => {
// cargo-mutants is installed, proceed
}
Err(e) => {
// Should contain helpful error message
let error_msg = e.to_string();
assert!(
error_msg.contains("cargo-mutants") || error_msg.contains("not found"),
"Error message should mention cargo-mutants: {}",
error_msg
);
}
}
}
#[test]
#[ignore] // RED phase: Will fail until backend implemented
fn test_cargo_mutants_backend_validates_version() {
// Test: Verify version validation (v24.7.0+)
// Expected: wrapper.validate_version() checks minimum version
if let Ok(wrapper) = CargoMutantsWrapper::new() {
let version_result = wrapper.validate_version();
// Should succeed for v24.7.0+, or fail with upgrade message
match version_result {
Ok(_) => {
// Version is sufficient
}
Err(e) => {
let error_msg = e.to_string();
assert!(
error_msg.contains("24.7.0") || error_msg.contains("version"),
"Error should mention version requirement: {}",
error_msg
);
}
}
}
}
#[test]
fn test_cargo_mutants_backend_parses_some_missed_fixture() {
// Test: Parse real cargo-mutants output with some missed mutants
// Expected:
// 1. Parse outcomes.json from fixture
// 2. Extract 5 mutants (4 caught, 1 missed)
// 3. Calculate 80% mutation score
use pmat::services::mutation::json_parser::MutantOutcome;
// Use real cargo-mutants output fixture
let fixture = PathBuf::from("tests/fixtures/cargo-mutants-output/some-missed");
let report = CargoMutantsReport::from_output_dir(&fixture).expect("Should parse fixture");
// Verify mutant counts
assert_eq!(report.mutants.len(), 5, "Should have 5 mutants");
assert_eq!(
report.count_by_outcome(MutantOutcome::Caught),
4,
"Should have 4 caught mutants"
);
assert_eq!(
report.count_by_outcome(MutantOutcome::Missed),
1,
"Should have 1 missed mutant"
);
// Verify mutation score
assert_eq!(report.mutation_score(), 80.0, "Should have 80% score");
}
#[test]
#[ignore] // RED phase: Will fail until backend implemented
fn test_cargo_mutants_backend_passes_timeout() {
// Test: Verify --timeout flag is passed to cargo-mutants
// Expected: Command includes --timeout <value>
let mut config = test_config();
config.timeout = 600; // 10 minutes
let result = cargo_mutants_backend::execute(config);
// Should build: cargo mutants --timeout 600
assert!(result.is_ok(), "Should handle timeout flag");
}
#[test]
fn test_cargo_mutants_backend_handles_missing_file() {
// Test: Verify graceful error when outcomes.json doesn't exist
// Expected: Return error with helpful message
let nonexistent = PathBuf::from("tests/fixtures/cargo-mutants-output/nonexistent");
let parse_result = CargoMutantsReport::from_output_dir(&nonexistent);
assert!(parse_result.is_err(), "Should fail when file missing");
if let Err(e) = parse_result {
let error_msg = e.to_string();
assert!(
error_msg.contains("outcomes.json") || error_msg.contains("No such file"),
"Error should mention missing file: {}",
error_msg
);
}
}
#[test]
#[ignore] // RED phase: Will fail until backend implemented
fn test_cargo_mutants_backend_saves_output() {
// Test: Verify --output flag saves JSON to file
// Expected: JSON written to specified path
let output_path = PathBuf::from("/tmp/pmat-cargo-mutants-test.json");
let mut config = test_config();
config.output = Some(output_path.clone());
let result = cargo_mutants_backend::execute(config);
assert!(result.is_ok(), "Should save output file");
// GREEN phase will verify: std::fs::exists(&output_path)
}
#[test]
#[ignore] // RED phase: Will fail until backend implemented
fn test_cargo_mutants_backend_passes_all_flags() {
// Test: Verify all flags are passed correctly
// Expected: cargo mutants --features feat1,feat2 --jobs 4 --no-shuffle
let mut config = test_config();
config.jobs = Some(4);
config.features = Some(vec!["feat1".to_string(), "feat2".to_string()]);
config.no_shuffle = true;
let result = cargo_mutants_backend::execute(config);
// Should build command with all flags
assert!(result.is_ok(), "Should pass all flags to cargo-mutants");
}
#[test]
fn test_cargo_mutants_backend_calculates_statistics() {
// Test: Verify statistics calculation via utility methods
// Expected: Calculate correct mutation score and counts
use pmat::services::mutation::json_parser::MutantOutcome;
let fixture = PathBuf::from("tests/fixtures/cargo-mutants-output/with-timeout");
let report = CargoMutantsReport::from_output_dir(&fixture).expect("Should parse fixture");
// Test utility methods
assert_eq!(report.mutants.len(), 5, "Should have 5 mutants");
assert_eq!(
report.count_by_outcome(MutantOutcome::Caught),
3,
"Should have 3 caught"
);
assert_eq!(
report.count_by_outcome(MutantOutcome::Missed),
1,
"Should have 1 missed"
);
assert_eq!(
report.count_by_outcome(MutantOutcome::Timeout),
1,
"Should have 1 timeout"
);
assert_eq!(report.mutation_score(), 60.0, "Should be 60% (3/5 caught)");
}
// ============================================================================
// Integration Tests
// ============================================================================
#[test]
#[ignore] // Requires actual cargo-mutants installation
fn integration_test_mutate_command_end_to_end() {
// Test: End-to-end workflow with real cargo-mutants
// Prerequisites: cargo-mutants v24.7.0+ installed
//
// Workflow:
// 1. Detect cargo-mutants
// 2. Validate version
// 3. Execute on small test project
// 4. Parse results
// 5. Display statistics
// 6. Verify output
unimplemented!("GREEN phase: End-to-end integration test");
}
#[test]
#[ignore] // Requires test project setup
fn integration_test_mutate_command_with_real_project() {
// Test: Run against small Rust project with known mutants
// Expected: Should accurately detect and report mutants
//
// This test validates that:
// 1. cargo-mutants executes correctly
// 2. JSON output is valid
// 3. PMAT conversion works
// 4. Statistics are accurate
unimplemented!("GREEN phase: Real project integration test");
}
// ============================================================================
// Edge Case Tests (Phase 4)
// ============================================================================
#[test]
fn test_empty_project_no_mutants() {
// Test: Handle projects with no mutants found
// Expected: 0 mutants, 0% score
let fixture = PathBuf::from("tests/fixtures/cargo-mutants-output/empty");
let report = CargoMutantsReport::from_output_dir(&fixture).expect("Should parse empty fixture");
assert_eq!(report.mutants.len(), 0, "Should have 0 mutants");
assert_eq!(report.mutation_score(), 0.0, "Score should be 0%");
}
#[test]
fn test_all_mutants_caught_perfect_score() {
// Test: Handle perfect mutation score (100%)
// Expected: 5 mutants, all caught, 100% score
use pmat::services::mutation::json_parser::MutantOutcome;
let fixture = PathBuf::from("tests/fixtures/cargo-mutants-output/all-caught");
let report =
CargoMutantsReport::from_output_dir(&fixture).expect("Should parse all-caught fixture");
assert_eq!(report.mutants.len(), 5, "Should have 5 mutants");
assert_eq!(
report.count_by_outcome(MutantOutcome::Caught),
5,
"All mutants should be caught"
);
assert_eq!(
report.count_by_outcome(MutantOutcome::Missed),
0,
"No missed mutants"
);
assert_eq!(report.mutation_score(), 100.0, "Score should be 100%");
}
#[test]
fn test_unviable_mutants_handling() {
// Test: Handle unviable (non-compiling) mutants
// Expected: Count unviable mutants separately
use pmat::services::mutation::json_parser::MutantOutcome;
let fixture = PathBuf::from("tests/fixtures/cargo-mutants-output/unviable");
let report =
CargoMutantsReport::from_output_dir(&fixture).expect("Should parse unviable fixture");
assert!(
report.count_by_outcome(MutantOutcome::Unviable) > 0,
"Should have unviable mutants"
);
assert_eq!(report.mutants.len(), 5, "Should have 5 total mutants");
}
#[test]
fn test_timeout_mutants_handling() {
// Test: Handle timeout mutants
// Expected: Count timeout mutants separately
use pmat::services::mutation::json_parser::MutantOutcome;
let fixture = PathBuf::from("tests/fixtures/cargo-mutants-output/with-timeout");
let report =
CargoMutantsReport::from_output_dir(&fixture).expect("Should parse timeout fixture");
assert!(
report.count_by_outcome(MutantOutcome::Timeout) > 0,
"Should have timeout mutants"
);
// Timeouts don't count toward mutation score (only caught vs missed)
assert_eq!(
report.mutation_score(),
60.0,
"Score based on caught mutants"
);
}
#[test]
fn test_parsing_performance_reasonable() {
// Test: Verify parsing is fast even with multiple mutants
// Expected: Parse 5 mutants in <10ms
use std::time::Instant;
let fixture = PathBuf::from("tests/fixtures/cargo-mutants-output/some-missed");
let start = Instant::now();
let report = CargoMutantsReport::from_output_dir(&fixture).expect("Should parse fixture");
let elapsed = start.elapsed();
assert_eq!(report.mutants.len(), 5);
assert!(
elapsed.as_millis() < 100,
"Parsing should be fast (<100ms), took {:?}",
elapsed
);
}
// ============================================================================
// Property-Based Tests (Placeholders for Phase 4)
// ============================================================================
#[test]
#[ignore] // Property test - Phase 4
fn property_test_mutation_score_always_between_0_and_100() {
// Property: mutation_score() should always return 0.0 <= score <= 100.0
// This will use proptest in Phase 4
unimplemented!("Phase 4: Property-based testing");
}
#[test]
#[ignore] // Property test - Phase 4
fn property_test_outcome_counts_sum_to_total_mutants() {
// Property: caught + missed + timeout + unviable == total mutants
// This will use proptest in Phase 4
unimplemented!("Phase 4: Property-based testing");
}