pmat 3.17.0

PMAT - Zero-config AI context generation and code quality toolkit (CLI, MCP, HTTP)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
# Cargo-Mutants Integration: Practical Examples

**Feature**: `pmat mutate --use-cargo-mutants`
**Audience**: Rust developers
**Level**: Beginner to Advanced

---

## Table of Contents

1. [Basic Examples]#basic-examples
2. [Timeout Configuration]#timeout-configuration
3. [Parallel Execution]#parallel-execution
4. [Feature Selection]#feature-selection
5. [Output Customization]#output-customization
6. [CI/CD Integration]#cicd-integration
7. [Real-World Workflows]#real-world-workflows

---

## Basic Examples

### Example 1: Simplest Usage

**Scenario**: First time running mutation testing on a Rust project

```bash
# Navigate to project
cd ~/my-rust-project

# Run mutation testing
pmat mutate --target . --use-cargo-mutants
```

**Expected Output**:
```
๐Ÿงช cargo-mutants Backend
โœ… Detected: cargo-mutants 25.3.1
โณ Running mutation testing...
โœ… Mutation testing complete

๐Ÿ“Š Mutation Testing Results:
   Total mutants: 15
   Caught: 12 (80.0%)
   Missed: 3 (20.0%)
   Timeout: 0 (0.0%)
   Unviable: 0 (0.0%)
๐Ÿ“ˆ Mutation Score: 80.0%
```

**Interpretation**: 80% of mutants were caught, indicating good test coverage. 3 mutants survived, suggesting potential test gaps.

---

### Example 2: Test a Specific Module

**Scenario**: Focus mutation testing on a critical module

```bash
# Test only the core module
pmat mutate --target src/core/ --use-cargo-mutants
```

**Why**: Faster iteration when working on specific code areas.

---

### Example 3: Explicit Target Path

**Scenario**: Test from outside the project directory

```bash
# From parent directory
pmat mutate --target ~/projects/my-rust-app --use-cargo-mutants
```

---

## Timeout Configuration

### Example 4: Default Timeout

**Scenario**: Fast test suite (< 5 seconds)

```bash
# Use default timeout (300s = 5 minutes)
pmat mutate --use-cargo-mutants
```

**When to Use**: For small projects with fast tests.

---

### Example 5: Increased Timeout

**Scenario**: Slow test suite (30+ seconds)

```bash
# Increase timeout to 10 minutes
pmat mutate --use-cargo-mutants --timeout 600
```

**Expected Outcome**: Fewer timeout failures.

**Before** (timeout too low):
```
๐Ÿ“Š Mutation Testing Results:
   Total mutants: 20
   Caught: 8 (40.0%)
   Timeout: 10 (50.0%)  โ† Too many timeouts!
```

**After** (timeout increased):
```
๐Ÿ“Š Mutation Testing Results:
   Total mutants: 20
   Caught: 16 (80.0%)
   Timeout: 0 (0.0%)    โ† Much better!
```

---

### Example 6: Very Long Timeout

**Scenario**: Integration tests or end-to-end tests

```bash
# Allow up to 20 minutes per mutant
pmat mutate --use-cargo-mutants --timeout 1200
```

**When to Use**: Projects with slow integration tests.

---

## Parallel Execution

### Example 7: Specify Job Count

**Scenario**: Speed up mutation testing with parallelism

```bash
# Use 4 parallel jobs
pmat mutate --use-cargo-mutants --jobs 4
```

**Performance Comparison**:
- **Sequential** (`--jobs 1`): 50 mutants ร— 10s = 500s (~8 minutes)
- **Parallel** (`--jobs 4`): 50 mutants ร— 10s / 4 = 125s (~2 minutes)

**Performance Gain**: 4x speedup!

---

### Example 8: Auto-Detect CPU Cores

**Scenario**: Let cargo-mutants choose optimal parallelism

```bash
# Omit --jobs flag (auto-detect)
pmat mutate --use-cargo-mutants
```

**Default Behavior**: Uses number of CPU cores.

---

### Example 9: Conservative Parallelism

**Scenario**: Memory-limited environment or CI/CD

```bash
# Use only 2 jobs to conserve resources
pmat mutate --use-cargo-mutants --jobs 2
```

**When to Use**: Shared CI runners, low-memory machines.

---

## Feature Selection

### Example 10: Enable Specific Features

**Scenario**: Test code behind feature flags

```bash
# Test with "serde" and "logging" features enabled
pmat mutate --use-cargo-mutants --features "serde,logging"
```

**Project Context**:
```toml
# Cargo.toml
[features]
serde = ["dep:serde"]
logging = ["dep:log"]
```

---

### Example 11: Test All Features

**Scenario**: Comprehensive testing across all feature combinations

```bash
# Enable all features
pmat mutate --use-cargo-mutants --all-features
```

**When to Use**: Before releases to ensure all features are tested.

---

### Example 12: No Default Features

**Scenario**: Test minimal build (no default features)

```bash
# Disable default features
pmat mutate --use-cargo-mutants --no-default-features
```

**Project Context**:
```toml
# Cargo.toml
[features]
default = ["std", "alloc"]
std = []
alloc = []
```

---

### Example 13: Combine Feature Flags

**Scenario**: Test specific feature combination

```bash
# No defaults, but enable specific features
pmat mutate --use-cargo-mutants \
  --no-default-features \
  --features "minimal,logging"
```

---

## Output Customization

### Example 14: Custom Output Directory

**Scenario**: Save results to specific location

```bash
# Save to custom directory
pmat mutate --use-cargo-mutants --output results/mutation-$(date +%Y%m%d)
```

**Result**:
```
results/mutation-20251029/
โ”œโ”€โ”€ outcomes.json
โ”œโ”€โ”€ mutants.json
โ””โ”€โ”€ lock.json
```

---

### Example 15: Preserve Multiple Runs

**Scenario**: Compare results across runs

```bash
# Run 1: Before changes
pmat mutate --use-cargo-mutants --output baseline-results/

# Make code changes...

# Run 2: After changes
pmat mutate --use-cargo-mutants --output updated-results/

# Compare
diff baseline-results/outcomes.json updated-results/outcomes.json
```

---

## CI/CD Integration

### Example 16: GitHub Actions Workflow

**File**: `.github/workflows/mutation-testing.yml`

```yaml
name: Mutation Testing

on:
  push:
    branches: [main, develop]
  pull_request:
    branches: [main]

jobs:
  mutation-testing:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout code
        uses: actions/checkout@v3

      - name: Install Rust toolchain
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable
          override: true

      - name: Cache cargo registry
        uses: actions/cache@v3
        with:
          path: ~/.cargo/registry
          key: ${{ runner.os }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }}

      - name: Install PMAT and cargo-mutants
        run: |
          cargo install pmat cargo-mutants

      - name: Run mutation testing
        run: |
          pmat mutate \
            --target . \
            --use-cargo-mutants \
            --timeout 600 \
            --jobs 2

      - name: Upload results
        uses: actions/upload-artifact@v3
        if: always()
        with:
          name: mutation-results
          path: mutants.out/
```

**What This Does**:
1. Installs Rust toolchain
2. Caches cargo dependencies (faster builds)
3. Installs PMAT and cargo-mutants
4. Runs mutation testing with 10-minute timeout and 2 parallel jobs
5. Uploads results as artifact (available for download)

---

### Example 17: GitHub Actions with Quality Gate

**File**: `.github/workflows/mutation-gate.yml`

```yaml
name: Mutation Quality Gate

on:
  pull_request:
    branches: [main]

jobs:
  mutation-gate:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Install tools
        run: |
          cargo install pmat cargo-mutants jq

      - name: Run mutation testing
        id: mutants
        run: |
          pmat mutate \
            --use-cargo-mutants \
            --timeout 600 \
            --output results/ > output.txt

          cat output.txt

      - name: Parse mutation score
        id: score
        run: |
          # Extract score from output (example parsing)
          SCORE=$(grep "Mutation Score:" output.txt | awk '{print $NF}' | tr -d '%')
          echo "score=$SCORE" >> $GITHUB_OUTPUT
          echo "Mutation Score: $SCORE%"

      - name: Check quality gate
        run: |
          SCORE=${{ steps.score.outputs.score }}
          MIN_SCORE=70

          if (( $(echo "$SCORE < $MIN_SCORE" | bc -l) )); then
            echo "โŒ Mutation score $SCORE% is below minimum $MIN_SCORE%"
            exit 1
          else
            echo "โœ… Mutation score $SCORE% meets minimum $MIN_SCORE%"
          fi
```

**Quality Gate**: Fails PR if mutation score < 70%

---

### Example 18: GitLab CI Pipeline

**File**: `.gitlab-ci.yml`

```yaml
stages:
  - test
  - quality

mutation-testing:
  stage: quality
  image: rust:latest

  before_script:
    - cargo install pmat cargo-mutants

  script:
    - |
      pmat mutate \
        --use-cargo-mutants \
        --timeout 600 \
        --jobs 2 \
        --output $CI_PROJECT_DIR/mutation-results/

  artifacts:
    paths:
      - mutation-results/
    expire_in: 1 week

  allow_failure: true  # Don't block pipeline on low score

  only:
    - main
    - merge_requests
```

---

### Example 19: Jenkins Pipeline

**File**: `Jenkinsfile`

```groovy
pipeline {
    agent any

    stages {
        stage('Setup') {
            steps {
                sh 'cargo install pmat cargo-mutants'
            }
        }

        stage('Mutation Testing') {
            steps {
                sh '''
                    pmat mutate \
                        --use-cargo-mutants \
                        --timeout 600 \
                        --jobs ${env.BUILD_NUMBER % 4 + 1} \
                        --output results/
                '''
            }
        }

        stage('Archive Results') {
            steps {
                archiveArtifacts artifacts: 'results/**/*', fingerprint: true
            }
        }
    }

    post {
        always {
            cleanWs()
        }
    }
}
```

---

## Real-World Workflows

### Example 20: Pre-Release Checklist

**Scenario**: Comprehensive quality check before release

```bash
#!/bin/bash
# File: scripts/pre-release-quality.sh

set -e

echo "๐Ÿš€ Pre-Release Quality Checks"
echo "=============================="

# Step 1: Run tests
echo "๐Ÿ“‹ Step 1: Running unit tests..."
cargo test --all-features

# Step 2: TDG baseline
echo "๐Ÿ“Š Step 2: Creating TDG baseline..."
pmat tdg baseline create --output .pmat/release-baseline.json

# Step 3: Mutation testing
echo "๐Ÿงช Step 3: Running mutation testing..."
pmat mutate \
  --use-cargo-mutants \
  --all-features \
  --timeout 900 \
  --jobs 4 \
  --output mutation-results/

# Step 4: Quality gates
echo "โœ… Step 4: Checking quality gates..."
pmat tdg check-quality --min-grade B

echo "๐ŸŽ‰ All quality checks passed!"
```

**Usage**:
```bash
chmod +x scripts/pre-release-quality.sh
./scripts/pre-release-quality.sh
```

---

### Example 21: Weekly Quality Report

**Scenario**: Track mutation score over time

```bash
#!/bin/bash
# File: scripts/weekly-mutation-report.sh

DATE=$(date +%Y-%m-%d)
RESULTS_DIR="reports/mutation-$DATE"

echo "๐Ÿ“ˆ Weekly Mutation Testing Report"
echo "=================================="
echo "Date: $DATE"

# Run mutation testing
pmat mutate \
  --use-cargo-mutants \
  --timeout 600 \
  --output "$RESULTS_DIR/"

# Parse results
SCORE=$(cat "$RESULTS_DIR/outcomes.json" | jq -r '.mutation_score // "N/A"')
echo "Mutation Score: $SCORE%"

# Append to history
echo "$DATE,$SCORE" >> reports/mutation-history.csv

# Generate trend chart (requires gnuplot or similar)
# gnuplot -e "set terminal png; set output 'reports/trend.png'; plot 'reports/mutation-history.csv' using 1:2 with lines"

echo "โœ… Report saved to $RESULTS_DIR"
```

**Usage**:
```bash
# Run weekly (via cron)
0 9 * * 1 /path/to/weekly-mutation-report.sh
```

---

### Example 22: PR Comment Bot

**Scenario**: Post mutation results as PR comment

**File**: `.github/workflows/pr-comment.yml`

```yaml
name: PR Mutation Comment

on:
  pull_request:
    branches: [main]

jobs:
  mutation-comment:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Install tools
        run: cargo install pmat cargo-mutants

      - name: Run mutation testing
        id: mutants
        run: |
          pmat mutate --use-cargo-mutants --timeout 600 > results.txt
          cat results.txt

      - name: Post comment
        uses: actions/github-script@v6
        with:
          github-token: ${{ secrets.GITHUB_TOKEN }}
          script: |
            const fs = require('fs');
            const results = fs.readFileSync('results.txt', 'utf8');

            github.rest.issues.createComment({
              issue_number: context.issue.number,
              owner: context.repo.owner,
              repo: context.repo.repo,
              body: '## ๐Ÿงช Mutation Testing Results\n\n```\n' + results + '\n```'
            });
```

---

### Example 23: Incremental Module Testing

**Scenario**: Test modules one at a time for large projects

```bash
#!/bin/bash
# File: scripts/test-all-modules.sh

MODULES=(
  "src/core"
  "src/api"
  "src/utils"
  "src/models"
)

for module in "${MODULES[@]}"; do
  echo "๐Ÿงช Testing module: $module"

  pmat mutate \
    --target "$module" \
    --use-cargo-mutants \
    --timeout 300 \
    --output "results/$module/"

  echo "โœ… $module complete"
  echo ""
done

echo "๐ŸŽ‰ All modules tested!"
```

---

### Example 24: Benchmark Mode (No Shuffle)

**Scenario**: Consistent benchmarking across runs

```bash
#!/bin/bash
# File: scripts/benchmark-mutations.sh

# Run 3 times with same mutant order
for i in {1..3}; do
  echo "๐Ÿ“Š Run $i/3"

  time pmat mutate \
    --use-cargo-mutants \
    --no-shuffle \
    --timeout 300 \
    --output "benchmark-run-$i/"
done

# Compare execution times
echo "Execution times:"
ls -lh benchmark-run-*/
```

---

### Example 25: Combined Quality Workflow

**Scenario**: Full quality analysis (TDG + Mutation + Coverage)

```bash
#!/bin/bash
# File: scripts/full-quality-check.sh

set -e

echo "๐Ÿ” Comprehensive Quality Analysis"
echo "================================="

# Step 1: TDG Analysis
echo "1๏ธโƒฃ TDG Quality Scoring..."
pmat tdg baseline create \
  --output quality-results/tdg-baseline.json \
  --path .

# Step 2: Mutation Testing
echo "2๏ธโƒฃ Mutation Testing..."
pmat mutate \
  --use-cargo-mutants \
  --timeout 600 \
  --jobs 4 \
  --output quality-results/mutation/

# Step 3: Code Coverage (using cargo-llvm-cov)
echo "3๏ธโƒฃ Code Coverage..."
cargo llvm-cov --all-features --workspace --html

# Step 4: Generate report
echo "4๏ธโƒฃ Generating Combined Report..."
cat << EOF > quality-results/REPORT.md
# Quality Analysis Report
**Date**: $(date)

## TDG Scores
$(pmat tdg baseline list | head -20)

## Mutation Testing
$(cat quality-results/mutation/outcomes.json | jq -r '.summary')

## Code Coverage
See: target/llvm-cov/html/index.html

EOF

echo "โœ… Quality report: quality-results/REPORT.md"
```

---

## Summary

These examples demonstrate:

โœ… **Basic Usage**: Simple commands for getting started
โœ… **Performance**: Timeout and parallelism tuning
โœ… **Features**: Testing different feature combinations
โœ… **CI/CD**: GitHub Actions, GitLab CI, Jenkins integration
โœ… **Workflows**: Real-world automation scripts
โœ… **Quality**: Combined analysis with TDG and coverage

---

## Next Steps

1. Try examples 1-3 on your project
2. Tune timeouts and parallelism (examples 4-9)
3. Set up CI/CD integration (examples 16-19)
4. Create quality workflows (examples 20-25)

---

## Additional Resources

- **User Guide**: `docs/user-guides/cargo-mutants-integration.md`
- **cargo-mutants Docs**: https://mutants.rs/
- **PMAT Book**: https://paiml.github.io/pmat-book/

---

**Last Updated**: October 29, 2025