prview 0.6.0

PR Review & Artifact Generator - cross-language PR analysis tool
Documentation
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
//! Integration tests for `prview scope` against real temp repositories.
//!
//! Covers the QC-risk surfaces: include/exclude semantics end-to-end, the
//! exclude-only WIP path, merge-commit skipping, and output-dir safety.

use assert_cmd::prelude::*;
use prview::git::git_cmd;
use std::path::Path;
use std::process::Command;
use tempfile::TempDir;

fn run_git(repo: &Path, args: &[&str]) {
    let status = git_cmd()
        .args(args)
        .current_dir(repo)
        .status()
        .expect("failed to run git");
    assert!(status.success(), "git {:?} failed", args);
}

fn write(repo: &Path, rel: &str, content: &str) {
    let path = repo.join(rel);
    std::fs::create_dir_all(path.parent().unwrap()).unwrap();
    std::fs::write(path, content).unwrap();
}

fn init_repo() -> TempDir {
    let dir = tempfile::tempdir().expect("tempdir");
    let repo = dir.path();
    run_git(repo, &["init", "-b", "main"]);
    run_git(repo, &["config", "user.email", "test@test.com"]);
    run_git(repo, &["config", "user.name", "Test"]);
    dir
}

fn commit_all(repo: &Path, msg: &str) {
    run_git(repo, &["add", "-A"]);
    run_git(repo, &["commit", "-m", msg]);
}

fn prview_scope(repo: &Path, args: &[&str]) -> assert_cmd::assert::Assert {
    Command::new(assert_cmd::cargo::cargo_bin!("prview"))
        .current_dir(repo)
        .arg("scope")
        .args(args)
        .assert()
}

/// `--include 'src/**'` keeps src files and drops docs.
#[test]
fn include_only_keeps_matching_files() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/app.rs", "fn main() {}\n");
    write(repo, "docs/readme.md", "# docs\n");
    commit_all(repo, "init");

    run_git(repo, &["checkout", "-b", "feature"]);
    write(repo, "src/app.rs", "fn main() { println!(\"hi\"); }\n");
    write(repo, "docs/readme.md", "# docs\nmore\n");
    commit_all(repo, "feature change");

    let out = repo.join("pack");
    prview_scope(
        repo,
        &[
            "--include",
            "src/**",
            "--base",
            "main",
            "-o",
            out.to_str().unwrap(),
        ],
    )
    .success();

    let full = std::fs::read_to_string(out.join("full.patch")).expect("full.patch");
    assert!(full.contains("src/app.rs"), "src must be in pack:\n{full}");
    assert!(
        !full.contains("docs/readme.md"),
        "docs must be excluded:\n{full}"
    );
    assert!(out.join("SCOPE.md").exists());
}

/// `--exclude 'docs/**'` drops docs, keeps everything else.
#[test]
fn exclude_only_drops_matching_files() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/app.rs", "a\n");
    write(repo, "docs/readme.md", "d\n");
    commit_all(repo, "init");

    run_git(repo, &["checkout", "-b", "feature"]);
    write(repo, "src/app.rs", "a\nb\n");
    write(repo, "docs/readme.md", "d\ne\n");
    commit_all(repo, "feature change");

    let out = repo.join("pack");
    prview_scope(
        repo,
        &[
            "--exclude",
            "docs/**",
            "--base",
            "main",
            "-o",
            out.to_str().unwrap(),
        ],
    )
    .success();

    let full = std::fs::read_to_string(out.join("full.patch")).expect("full.patch");
    assert!(full.contains("src/app.rs"));
    assert!(!full.contains("docs/readme.md"), "docs excluded:\n{full}");
}

/// Regression: a WIP-only change to a tracked file that is NOT in the committed
/// diff must still be captured under an exclude-only scope. The previous
/// implementation fed the exclude glob to git as a positive pathspec and missed
/// this entirely.
#[test]
fn wip_exclude_only_captures_wip_only_file() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/stable.rs", "stable\n");
    write(repo, "docs/readme.md", "d\n");
    commit_all(repo, "init");

    // Feature commits touch ONLY docs — committed diff vs main has no src files.
    run_git(repo, &["checkout", "-b", "feature"]);
    write(repo, "docs/readme.md", "d\nmore\n");
    commit_all(repo, "docs only commit");

    // WIP: modify a tracked src file that is unchanged across main..feature.
    write(repo, "src/stable.rs", "stable\nWIP EDIT\n");

    let out = repo.join("pack");
    prview_scope(
        repo,
        &[
            "--exclude",
            "docs/**",
            "--wip",
            "--base",
            "main",
            "-o",
            out.to_str().unwrap(),
        ],
    )
    .success();

    let wip = std::fs::read_to_string(out.join("wip.patch"))
        .expect("wip.patch must exist for exclude-only WIP-only change");
    assert!(
        wip.contains("src/stable.rs"),
        "WIP src change must be captured:\n{wip}"
    );
    assert!(wip.contains("WIP EDIT"));
    assert!(
        !wip.contains("docs/readme.md"),
        "docs WIP must be excluded:\n{wip}"
    );
}

/// Regression (#2/#7): `prview scope` must work when launched from a
/// subdirectory of the repo. The repo root was previously taken from the process
/// working directory, so any invocation below the top level failed to open the
/// repository.
#[test]
fn scope_runs_from_subdirectory() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/app.rs", "a\n");
    commit_all(repo, "init");
    run_git(repo, &["checkout", "-b", "feature"]);
    write(repo, "src/app.rs", "a\nb\n");
    commit_all(repo, "change");

    let subdir = repo.join("src");
    let assert = Command::new(assert_cmd::cargo::cargo_bin!("prview"))
        .current_dir(&subdir)
        .arg("scope")
        .args(["--include", "src/**", "--base", "main", "-o", "pack"])
        .assert()
        .success();
    let _ = assert;

    // Relative `-o` resolves against the working dir (the subdirectory).
    let full = std::fs::read_to_string(subdir.join("pack/full.patch")).expect("full.patch");
    assert!(full.contains("src/app.rs"), "src change captured:\n{full}");
}

/// Regression (#5): when the base branch moves on after divergence, the scope
/// pack must diff from the merge-base, not the base tip. A tip-to-tip diff would
/// surface the base-only change as a spurious (reversed) hunk.
#[test]
fn scope_diffs_from_merge_base_not_base_tip() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/app.rs", "orig\n");
    commit_all(repo, "init");

    // Feature diverges and changes app.rs.
    run_git(repo, &["checkout", "-b", "feature"]);
    write(repo, "src/app.rs", "orig\nfeature edit\n");
    commit_all(repo, "feature change");

    // main moves on independently, adding a file feature never saw.
    run_git(repo, &["checkout", "main"]);
    write(
        repo,
        "src/base_only.rs",
        "landed on base after divergence\n",
    );
    commit_all(repo, "base-only change");

    run_git(repo, &["checkout", "feature"]);

    let out = repo.join("pack");
    prview_scope(
        repo,
        &[
            "--include",
            "src/**",
            "--base",
            "main",
            "-o",
            out.to_str().unwrap(),
        ],
    )
    .success();

    let full = std::fs::read_to_string(out.join("full.patch")).expect("full.patch");
    assert!(
        full.contains("src/app.rs"),
        "feature's own change must be present:\n{full}"
    );
    assert!(
        !full.contains("base_only.rs"),
        "base-only change must NOT leak into the scope pack (merge-base diff):\n{full}"
    );
}

/// Regression (#4): an empty committed scope combined with `--wip` must NOT
/// pull in every commit. Previously the empty file list became an empty
/// pathspec that matched all files, so unrelated commits (and their diffs)
/// leaked into the pack.
#[test]
fn empty_committed_scope_with_wip_does_not_leak_all_commits() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/app.rs", "a\n");
    write(repo, "docs/readme.md", "d\n");
    commit_all(repo, "init");

    // Feature commit touches ONLY docs; `--include src/**` yields an empty
    // committed scope.
    run_git(repo, &["checkout", "-b", "feature"]);
    write(repo, "docs/readme.md", "d\nmore\n");
    commit_all(repo, "docs only commit");

    // WIP change to a src file keeps the run alive.
    write(repo, "src/app.rs", "a\nWIP\n");

    let out = repo.join("pack");
    prview_scope(
        repo,
        &[
            "--include",
            "src/**",
            "--wip",
            "--base",
            "main",
            "-o",
            out.to_str().unwrap(),
        ],
    )
    .success();

    // No committed commit is in scope: per-commit dir must be empty and the
    // docs change must not appear anywhere in the committed artifacts.
    let per_commit_count = std::fs::read_dir(out.join("per-commit"))
        .map(|rd| rd.filter_map(|e| e.ok()).count())
        .unwrap_or(0);
    assert_eq!(
        per_commit_count, 0,
        "empty committed scope must yield zero per-commit patches"
    );
    let commits_log = std::fs::read_to_string(out.join("commits.log")).unwrap_or_default();
    assert!(
        !commits_log.contains("docs only commit"),
        "unrelated commit must not leak into commits.log:\n{commits_log}"
    );
    let full = std::fs::read_to_string(out.join("full.patch")).unwrap_or_default();
    assert!(
        !full.contains("docs/readme.md"),
        "docs must not leak into full.patch:\n{full}"
    );

    // The WIP src change is still captured.
    let wip = std::fs::read_to_string(out.join("wip.patch")).expect("wip.patch");
    assert!(
        wip.contains("src/app.rs"),
        "WIP src change captured:\n{wip}"
    );
}

/// Regression (#6): a rename within scope must carry the deletion of the old
/// path. Per-path diffing showed only the added new path and dropped the old
/// one, so a reviewer never saw the file move.
#[test]
fn scoped_full_patch_preserves_rename_old_path() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/old_name.rs", "fn a() {}\nfn b() {}\nfn c() {}\n");
    commit_all(repo, "init");

    run_git(repo, &["checkout", "-b", "feature"]);
    run_git(repo, &["mv", "src/old_name.rs", "src/new_name.rs"]);
    commit_all(repo, "rename module");

    let out = repo.join("pack");
    prview_scope(
        repo,
        &[
            "--include",
            "src/**",
            "--base",
            "main",
            "-o",
            out.to_str().unwrap(),
        ],
    )
    .success();

    let full = std::fs::read_to_string(out.join("full.patch")).expect("full.patch");
    assert!(
        full.contains("new_name.rs"),
        "new path must be present:\n{full}"
    );
    assert!(
        full.contains("old_name.rs"),
        "old (pre-rename) path must remain visible in the scoped patch:\n{full}"
    );
}

/// Regression (#8): a WIP-only file (present in the working tree but not in the
/// committed diff) must be listed in SCOPE.md, marked as WIP, so the pack's
/// manifest reflects everything actually included.
#[test]
fn scope_md_lists_wip_only_files() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/stable.rs", "stable\n");
    write(repo, "docs/readme.md", "d\n");
    commit_all(repo, "init");

    run_git(repo, &["checkout", "-b", "feature"]);
    write(repo, "docs/readme.md", "d\nmore\n");
    commit_all(repo, "docs only commit");

    // WIP-only change to a src file that never appears in the committed diff.
    write(repo, "src/stable.rs", "stable\nWIP EDIT\n");

    let out = repo.join("pack");
    prview_scope(
        repo,
        &[
            "--exclude",
            "docs/**",
            "--wip",
            "--base",
            "main",
            "-o",
            out.to_str().unwrap(),
        ],
    )
    .success();

    let scope_md = std::fs::read_to_string(out.join("SCOPE.md")).expect("SCOPE.md");
    assert!(
        scope_md.contains("src/stable.rs"),
        "WIP-only file must be listed in SCOPE.md:\n{scope_md}"
    );
    assert!(
        scope_md.contains("(WIP)"),
        "WIP-only file must be marked as WIP:\n{scope_md}"
    );
}

/// Regression (#10): `--wip` must capture brand-new untracked files, not just
/// changes to tracked files. The workdir diff previously omitted untracked
/// entries entirely.
#[test]
fn wip_captures_untracked_file() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/app.rs", "a\n");
    commit_all(repo, "init");
    run_git(repo, &["checkout", "-b", "feature"]);

    // A brand-new file that was never `git add`ed — untracked.
    write(repo, "src/brand_new.rs", "fn fresh() {}\n");

    let out = repo.join("pack");
    prview_scope(
        repo,
        &[
            "--include",
            "src/**",
            "--wip",
            "--base",
            "main",
            "-o",
            out.to_str().unwrap(),
        ],
    )
    .success();

    let wip = std::fs::read_to_string(out.join("wip.patch")).expect("wip.patch");
    assert!(
        wip.contains("src/brand_new.rs"),
        "untracked WIP file must be captured:\n{wip}"
    );
    assert!(
        wip.contains("fn fresh()"),
        "content must be present:\n{wip}"
    );
}

/// Merge commits are skipped per spec — their first-parent diff duplicates the
/// individual commits.
#[test]
fn merge_commits_are_skipped() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/base.rs", "base\n");
    commit_all(repo, "init");
    let base_sha = {
        let o = git_cmd()
            .args(["rev-parse", "HEAD"])
            .current_dir(repo)
            .output()
            .unwrap();
        String::from_utf8_lossy(&o.stdout).trim().to_string()
    };

    run_git(repo, &["checkout", "-b", "topic"]);
    write(repo, "src/topic.rs", "topic\n");
    commit_all(repo, "topic commit");

    run_git(repo, &["checkout", "main"]);
    write(repo, "src/main2.rs", "main2\n");
    commit_all(repo, "main commit");

    run_git(repo, &["merge", "--no-ff", "-m", "Merge topic", "topic"]);

    let out = repo.join("pack");
    prview_scope(
        repo,
        &[
            "--include",
            "src/**",
            "--base",
            &base_sha,
            "-o",
            out.to_str().unwrap(),
        ],
    )
    .success();

    // Two real commits touch src/ (topic + main2); the merge must not appear.
    let per_commit = out.join("per-commit");
    let count = std::fs::read_dir(&per_commit)
        .map(|rd| rd.filter_map(|e| e.ok()).count())
        .unwrap_or(0);
    assert_eq!(
        count, 2,
        "merge commit must be skipped, expected 2 per-commit patches"
    );

    let commits_log = std::fs::read_to_string(out.join("commits.log")).unwrap_or_default();
    assert!(
        !commits_log.contains("Merge topic"),
        "merge commit must not appear in commits.log:\n{commits_log}"
    );
}

/// Output-dir guard: refuse to wipe a directory that isn't a prior scope pack.
#[test]
fn output_dir_guard_refuses_non_pack() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/app.rs", "a\n");
    commit_all(repo, "init");
    run_git(repo, &["checkout", "-b", "feature"]);
    write(repo, "src/app.rs", "a\nb\n");
    commit_all(repo, "change");

    // A precious, non-pack directory the operator might accidentally target.
    write(repo, "important/keep.txt", "do not delete me\n");

    prview_scope(
        repo,
        &["--include", "src/**", "--base", "main", "-o", "important"],
    )
    .failure();

    assert!(
        repo.join("important/keep.txt").exists(),
        "guard must not delete a non-pack directory"
    );
}

/// Regression (#16): a directory that merely contains a `SCOPE.md` — but not the
/// schema-tagged pack marker — must be refused. `SCOPE.md` alone is a filename an
/// operator's own directory can legitimately carry; only our marker authorises a
/// wipe.
#[test]
fn output_dir_guard_refuses_bare_scope_md() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/app.rs", "a\n");
    commit_all(repo, "init");
    run_git(repo, &["checkout", "-b", "feature"]);
    write(repo, "src/app.rs", "a\nb\n");
    commit_all(repo, "change");

    // Operator's own directory that happens to hold a SCOPE.md and precious data.
    write(repo, "notes/SCOPE.md", "my project scope, do not delete\n");
    write(repo, "notes/keep.txt", "precious\n");

    prview_scope(
        repo,
        &["--include", "src/**", "--base", "main", "-o", "notes"],
    )
    .failure();

    assert!(
        repo.join("notes/keep.txt").exists(),
        "guard must not delete a dir carrying only a bare SCOPE.md"
    );
    assert!(repo.join("notes/SCOPE.md").exists());
}

/// Regression (#16): re-running scope into a previously generated pack succeeds —
/// the marker written on the first run authorises cleaning on the second.
#[test]
fn output_dir_guard_allows_rerun_of_prior_pack() {
    let dir = init_repo();
    let repo = dir.path();
    write(repo, "src/app.rs", "a\n");
    commit_all(repo, "init");
    run_git(repo, &["checkout", "-b", "feature"]);
    write(repo, "src/app.rs", "a\nb\n");
    commit_all(repo, "change");

    let out = repo.join("pack");
    let args = &[
        "--include",
        "src/**",
        "--base",
        "main",
        "-o",
        out.to_str().unwrap(),
    ];
    prview_scope(repo, args).success();
    assert!(
        out.join(".prview-scope-pack.json").exists(),
        "marker written"
    );
    // Second run must be allowed to clean and regenerate the prior pack.
    prview_scope(repo, args).success();
    assert!(out.join("full.patch").exists());
}