|
| 1 | +//! Fallback-to-all-tests behavior for `cargo affected run`. |
| 2 | +//! |
| 3 | +//! `run` aims to be a strict superset of `cargo nextest run`: when the |
| 4 | +//! framework can't compute a precise affected-test selection, it falls back |
| 5 | +//! to running every test with a stderr notice rather than bailing or |
| 6 | +//! no-opping. This file pins the behavioral promise for the two cases that |
| 7 | +//! are easy to set up integration-style — no coverage data at all, and a |
| 8 | +//! `collect_sha` that's no longer reachable from HEAD (drift). |
| 9 | +//! |
| 10 | +//! `status` keeps the bail behavior for drift (see `drift.rs`); only `run` |
| 11 | +//! falls back, because `run`'s job is to actually produce CI signal. |
| 12 | +
|
| 13 | +use crate::{ |
| 14 | + cargo_affected, git, git_head, init_git_with_initial_commit, write_two_module_project, |
| 15 | +}; |
| 16 | + |
| 17 | +#[test] |
| 18 | +fn run_falls_back_to_all_when_no_coverage_data() { |
| 19 | + let tmp = tempfile::tempdir().unwrap(); |
| 20 | + let dir = tmp.path(); |
| 21 | + write_two_module_project(dir, "sample_fallback_no_coverage"); |
| 22 | + init_git_with_initial_commit(dir); |
| 23 | + |
| 24 | + // No `cargo affected collect` — DB is empty (or absent). `run` must |
| 25 | + // still succeed by running every test, with a stderr notice explaining |
| 26 | + // why selection wasn't computed. |
| 27 | + let run = cargo_affected(dir, &["affected", "run"]); |
| 28 | + assert!( |
| 29 | + run.status.success(), |
| 30 | + "run with no coverage should succeed by running all: stderr=\n{}\nstdout=\n{}", |
| 31 | + String::from_utf8_lossy(&run.stderr), |
| 32 | + String::from_utf8_lossy(&run.stdout), |
| 33 | + ); |
| 34 | + |
| 35 | + let combined = format!( |
| 36 | + "{}{}", |
| 37 | + String::from_utf8_lossy(&run.stderr), |
| 38 | + String::from_utf8_lossy(&run.stdout) |
| 39 | + ); |
| 40 | + |
| 41 | + // The fallback notice — phrased as "note: ..." per the existing notice |
| 42 | + // style in run.rs. Match on the "no coverage data yet" key phrase. |
| 43 | + assert!( |
| 44 | + combined.contains("no coverage data yet") && combined.contains("running all tests"), |
| 45 | + "expected fallback notice, got:\n{combined}" |
| 46 | + ); |
| 47 | + // nextest actually ran every test in the sample project (PASS lines for |
| 48 | + // each of test_add / test_multiply / test_greet). One PASS would be |
| 49 | + // ambiguous (could be the old narrow-selection path), so check all |
| 50 | + // three to confirm we really ran the full suite. |
| 51 | + for t in ["test_add", "test_multiply", "test_greet"] { |
| 52 | + assert!( |
| 53 | + combined.contains("PASS") && combined.contains(t), |
| 54 | + "expected nextest to PASS {t}, got:\n{combined}" |
| 55 | + ); |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[test] |
| 60 | +fn run_falls_back_to_all_when_collect_sha_diverged() { |
| 61 | + let tmp = tempfile::tempdir().unwrap(); |
| 62 | + let dir = tmp.path(); |
| 63 | + write_two_module_project(dir, "sample_fallback_diverged"); |
| 64 | + init_git_with_initial_commit(dir); |
| 65 | + let init_sha = git_head(dir); |
| 66 | + |
| 67 | + // Add a second commit so we have something to collect against and a |
| 68 | + // commit to reset back from. `collect` runs at this second commit, so |
| 69 | + // its sha becomes the recorded `collect_sha`. |
| 70 | + std::fs::write(dir.join("src/extra.rs"), "pub fn extra() -> i32 { 1 }\n").unwrap(); |
| 71 | + let lib_path = dir.join("src/lib.rs"); |
| 72 | + let lib = std::fs::read_to_string(&lib_path).unwrap(); |
| 73 | + std::fs::write(&lib_path, format!("{lib}pub mod extra;\n")).unwrap(); |
| 74 | + git(dir, &["add", "."]); |
| 75 | + git(dir, &["commit", "-q", "-m", "add extra module"]); |
| 76 | + let collect_commit = git_head(dir); |
| 77 | + assert_ne!(init_sha, collect_commit); |
| 78 | + |
| 79 | + let collect = cargo_affected(dir, &["affected", "collect"]); |
| 80 | + assert!( |
| 81 | + collect.status.success(), |
| 82 | + "collect failed: {}", |
| 83 | + String::from_utf8_lossy(&collect.stderr) |
| 84 | + ); |
| 85 | + |
| 86 | + // Reset HEAD back to the initial commit. The collect_sha is now an |
| 87 | + // orphan — present in the repo but not an ancestor of HEAD — |
| 88 | + // matching `ShaRelation::Diverged`. |
| 89 | + git(dir, &["reset", "--hard", "-q", &init_sha]); |
| 90 | + |
| 91 | + let run = cargo_affected(dir, &["affected", "run"]); |
| 92 | + assert!( |
| 93 | + run.status.success(), |
| 94 | + "run on diverged collect_sha should succeed by running all: stderr=\n{}\nstdout=\n{}", |
| 95 | + String::from_utf8_lossy(&run.stderr), |
| 96 | + String::from_utf8_lossy(&run.stdout), |
| 97 | + ); |
| 98 | + |
| 99 | + let combined = format!( |
| 100 | + "{}{}", |
| 101 | + String::from_utf8_lossy(&run.stderr), |
| 102 | + String::from_utf8_lossy(&run.stdout) |
| 103 | + ); |
| 104 | + |
| 105 | + assert!( |
| 106 | + combined.contains("not reachable from HEAD") && combined.contains("running all tests"), |
| 107 | + "expected diverged-fallback notice, got:\n{combined}" |
| 108 | + ); |
| 109 | + // The whole suite ran — same three-test check as the no-coverage case. |
| 110 | + for t in ["test_add", "test_multiply", "test_greet"] { |
| 111 | + assert!( |
| 112 | + combined.contains("PASS") && combined.contains(t), |
| 113 | + "expected nextest to PASS {t}, got:\n{combined}" |
| 114 | + ); |
| 115 | + } |
| 116 | +} |
0 commit comments