diff --git a/system-map/tests/binary.rs b/system-map/tests/binary.rs index 88c6e54e..e79ac335 100644 --- a/system-map/tests/binary.rs +++ b/system-map/tests/binary.rs @@ -4,9 +4,11 @@ use std::process::{Command, Output}; use serde_json::Value; -use crate::common::{FILES, base, project}; +use crate::common::{FILES, base, git_repo, project}; +/// `check` with `args` run by the binary on the folder at `root`, made a real repository first. fn run(root: &str, args: &[&str]) -> Output { + git_repo(root); Command::new(env!("CARGO_BIN_EXE_system-map")) .arg("check") .args(args) @@ -45,7 +47,6 @@ fn the_pass_line_counts_in_the_plural_links_by_kind_and_laws_by_backing() { &["alpha/a.rs", "alpha/b.rs", "beta/b.rs", "docs/d.md"], ); crate::common::write(dir.path(), "beta/b.rs", &format!("// @{}: held\n", "law")); - studio_test_support::git(dir.path(), &["commit", "-qam", "Tag"]); let out = run(&root, &[]); assert!(out.status.success(), "{out:?}"); assert_eq!( @@ -96,8 +97,6 @@ fn json_lists_each_law_with_its_backing_and_its_tags_by_file_and_line() { let (dir, root) = project(&map, &FILES); let tag = format!("// @{}: stay-true\n", "law"); crate::common::write(dir.path(), "alpha/a.rs", &format!("fn a() {{}}\n{tag}")); - studio_test_support::git(dir.path(), &["add", "-A"]); - studio_test_support::git(dir.path(), &["commit", "-q", "-m", "Tag"]); let out = run(&root, &["--json"]); let value: Value = serde_json::from_slice(&out.stdout).unwrap(); assert_eq!(value["problems"].as_array().unwrap().len(), 0); diff --git a/system-map/tests/c.rs b/system-map/tests/c.rs index 6d8119f7..92e3dad2 100644 --- a/system-map/tests/c.rs +++ b/system-map/tests/c.rs @@ -4,7 +4,8 @@ use serde_json::{Value, json}; use crate::common::{ - assert_only_candidate_links_relaxed, extractor_map, pairs, repo, report, run, sites, write, + assert_only_candidate_links_relaxed, commit, extractor_map, pairs, repo, report, run, sites, + write, }; /// A map naming `c-includes` that gives every tracked file but `ignored` ones its own part, so that a file pair is a @@ -377,6 +378,7 @@ fn commit_mode_gives_the_pairs_of_the_working_tree() { ), &files, ); + commit(&root); let tree = run(&root, "check", &["--json"]); let commit = run(&root, "check", &["--json", "--commit", "HEAD"]); assert_eq!( diff --git a/system-map/tests/common.rs b/system-map/tests/common.rs index 6f427172..4e63fbba 100644 --- a/system-map/tests/common.rs +++ b/system-map/tests/common.rs @@ -1,10 +1,104 @@ -//! Temporary git repositories with a map in them. +//! Temporary folders with a map in them. A folder is plain, with no git: the files a test tracks are listed here and +//! handed to `check_files`, and a file written but not tracked is one the list leaves out. A folder becomes a real git +//! repository, tracking the listed files, only where git or the binary reads it: `run`, `imports_json` and `commit`. -use std::{fs, path::Path, process::Command}; +use std::{ + collections::{BTreeMap, BTreeSet}, + fs, + path::{Path, PathBuf}, + process::Command, + sync::Mutex, +}; use serde_json::{Value, json}; use studio_test_support::git; +/// The files each folder tracks, by its real path. +static TRACKED: Mutex>> = Mutex::new(BTreeMap::new()); + +/// The registry's key for the folder at `root`: its real path, however the test spells it. +fn key(root: &Path) -> PathBuf { + root.canonicalize().unwrap() +} + +/// A new plain folder, and its real path. +pub(crate) fn folder() -> (tempfile::TempDir, String) { + let dir = tempfile::tempdir().unwrap(); + let root = key(dir.path()); + TRACKED + .lock() + .unwrap() + .insert(root.clone(), BTreeSet::new()); + (dir, root.display().to_string()) +} + +/// Tracks `files` in the folder at `root`, as `git add` would. +pub(crate) fn track(root: &Path, files: &[&str]) { + TRACKED + .lock() + .unwrap() + .entry(key(root)) + .or_default() + .extend(files.iter().map(ToString::to_string)); + if root.join(".git").exists() && !files.is_empty() { + git(root, &[&["add", "--"], files].concat()); + } +} + +/// Tracks every file on disk in the folder at `root`, as `git add -A` would. +pub(crate) fn track_all(root: &Path) { + fn walk(dir: &Path, prefix: &str, found: &mut BTreeSet) { + for entry in fs::read_dir(dir).unwrap() { + let entry = entry.unwrap(); + let name = entry.file_name().to_string_lossy().into_owned(); + let path = format!("{prefix}{name}"); + if entry.file_type().unwrap().is_dir() { + if name != ".git" { + walk(&entry.path(), &format!("{path}/"), found); + } + } else { + found.insert(path); + } + } + } + let mut found = BTreeSet::new(); + walk(root, "", &mut found); + TRACKED.lock().unwrap().insert(key(root), found); + if root.join(".git").exists() { + git(root, &["add", "-A"]); + } +} + +/// The files the folder at `root` tracks. +fn tracked(root: &Path) -> Vec { + let tracked = TRACKED.lock().unwrap(); + tracked[&key(root)].iter().cloned().collect() +} + +/// Makes the folder at `root` a git repository whose index holds its tracked files, with no commit: `git ls-files` reads +/// the index. +pub(crate) fn git_repo(root: &str) { + let root = Path::new(root); + if root.join(".git").exists() { + return; + } + git(root, &["init", "-q", "-b", "main"]); + let files = tracked(root); + if !files.is_empty() { + let files: Vec<&str> = files.iter().map(String::as_str).collect(); + git(root, &[&["add", "--"], &files[..]].concat()); + } +} + +/// Commits the tracked files of the folder at `root`, making it a repository first: for a case that reads a commit. +pub(crate) fn commit(root: &str) { + git_repo(root); + git( + Path::new(root), + &["commit", "-q", "--allow-empty", "-m", "Files"], + ); +} + /// A valid map: two parts, an actor, an import link (unproven, since the map names no extractor), an ignored folder and /// a law. pub(crate) fn base() -> Value { @@ -43,19 +137,18 @@ pub(crate) fn base_v1() -> Value { /// The files `base` places: one per part and one ignored. pub(crate) const FILES: [&str; 3] = ["alpha/a.rs", "beta/b.rs", "docs/d.md"]; -/// A repository that tracks `files` and `system-map.json` holding `map`, and its root. +/// A folder that tracks `files` and `system-map.json` holding `map`, and its root. pub(crate) fn project(map: &Value, files: &[&str]) -> (tempfile::TempDir, String) { project_text(&map.to_string(), files) } pub(crate) fn project_text(map: &str, files: &[&str]) -> (tempfile::TempDir, String) { - let (dir, root) = studio_test_support::repo(); + let (dir, root) = folder(); write(dir.path(), "system-map.json", map); for file in files { write(dir.path(), file, "x\n"); } - git(dir.path(), &["add", "-A"]); - git(dir.path(), &["commit", "-q", "-m", "Files"]); + track(dir.path(), &[&["system-map.json"], files].concat()); (dir, root) } @@ -65,9 +158,9 @@ pub(crate) fn write(root: &Path, file: &str, text: &str) { fs::write(path, text).unwrap(); } -/// The problems `check` reports for a repository. +/// The problems `check` reports for a folder. pub(crate) fn problems(root: &str) -> Vec { - system_map::check(Path::new(root)).problems + report(root).problems } /// Whether some problem mentions every one of `needles`. @@ -102,20 +195,21 @@ pub(crate) fn extractor_map( }) } -/// A repository holding `map` and `files` with their texts, committed. +/// A folder tracking `map` and `files` with their texts. pub(crate) fn repo(map: &Value, files: &[(&str, &str)]) -> (tempfile::TempDir, String) { let (dir, root) = project_text(&map.to_string(), &[]); for (file, text) in files { write(dir.path(), file, text); } - git(dir.path(), &["add", "-A"]); - git(dir.path(), &["commit", "-q", "-m", "Code"]); + let names: Vec<&str> = files.iter().map(|(file, _)| *file).collect(); + track(dir.path(), &names); (dir, root) } -/// What `check` reports for a repository. +/// What `check` reports for a folder, against its tracked files. pub(crate) fn report(root: &str) -> system_map::Report { - system_map::check(Path::new(root)) + let root = Path::new(root); + system_map::check_files(root, tracked(root)) } /// Every file pair of the computed edges, as `from>to`. @@ -127,8 +221,9 @@ pub(crate) fn pairs(report: &system_map::Report) -> Vec { .collect() } -/// The `system-map` binary run as `command` with `args` on the repository at `root`. +/// The `system-map` binary run as `command` with `args` on the folder at `root`, made a repository first. pub(crate) fn run(root: &str, command: &str, args: &[&str]) -> std::process::Output { + git_repo(root); Command::new(env!("CARGO_BIN_EXE_system-map")) .arg(command) .args(args) @@ -143,10 +238,15 @@ pub(crate) fn imports_json(root: &str) -> Value { serde_json::from_slice(&out.stdout).unwrap() } -/// Each file pair's import sites in `check --json` with `args`, as `from>to file:line`, sorted. +/// Each file pair's import sites in `check --json` with `args`, as `from>to file:line`, sorted: from the report itself +/// when there are no `args`, which is what `check --json` prints. pub(crate) fn sites(root: &str, args: &[&str]) -> Vec { - let out = run(root, "check", &[&["--json"], args].concat()); - let value: Value = serde_json::from_slice(&out.stdout).unwrap(); + let value: Value = if args.is_empty() { + serde_json::to_value(report(root)).unwrap() + } else { + let out = run(root, "check", &[&["--json"], args].concat()); + serde_json::from_slice(&out.stdout).unwrap() + }; let mut found = Vec::new(); for edge in value["edges"].as_array().into_iter().flatten() { for pair in edge["pairs"].as_array().unwrap() { @@ -169,9 +269,9 @@ pub(crate) fn sites(root: &str, args: &[&str]) -> Vec { /// `to` with no dependency behind it: the links to the candidates of an unsure import pass as unproven, any other /// link of the part still fails. pub(crate) fn assert_only_candidate_links_relaxed(root: &str, note: &str, from: &str, to: &str) { - let out = run(root, "check", &[]); - assert_eq!(String::from_utf8_lossy(&out.stderr), note); - let problems = String::from_utf8_lossy(&out.stdout); + let (stdout, stderr) = printed(&report(root)); + assert_eq!(stderr, note); + let problems = stdout; assert_eq!(problems.lines().count(), 1, "{problems}"); let link = format!( "import link \"{from}-{to}\" runs from \"{from}\" to \"{to}\", but the code shows no dependency" @@ -185,11 +285,30 @@ pub(crate) fn assert_unsure_file_listed_and_link_unproven(root: &str, file: &str let value = imports_json(root); assert_eq!(value["unsure"][0]["file"], file); assert_eq!(value["unsure"][0]["reason"], "declared twice"); - let out = run(root, "check", &[]); - let stdout = String::from_utf8_lossy(&out.stdout); - assert!(out.status.success(), "{stdout}"); - assert!( - stdout.contains("1 import (0 proven, 1 unproven)"), - "{stdout}" + let report = report(root); + assert_eq!(report.problems, Vec::::new()); + let links = &report.counts.links; + assert_eq!( + (links.import, links.proven, links.unproven), + (1, 0, 1), + "{links:?}" ); } + +/// What `check` without `--json` prints of `report` on standard output, its problems, and on standard error, its notes. +fn printed(report: &system_map::Report) -> (String, String) { + let stdout = report.problems.iter().map(|p| format!("{p}\n")).collect(); + let stderr = report + .suggested + .iter() + .map(|name| format!("note: {name} could read this repository; add it to extractors\n")) + .chain( + report + .unsure_notes() + .iter() + .chain(&report.notes) + .map(|note| format!("note: {note}\n")), + ) + .collect(); + (stdout, stderr) +} diff --git a/system-map/tests/dart.rs b/system-map/tests/dart.rs index fb3a8e9a..528eb8ed 100644 --- a/system-map/tests/dart.rs +++ b/system-map/tests/dart.rs @@ -4,7 +4,7 @@ use serde_json::{Value, json}; use crate::common::{ - assert_only_candidate_links_relaxed, assert_unsure_file_listed_and_link_unproven, + assert_only_candidate_links_relaxed, assert_unsure_file_listed_and_link_unproven, commit, extractor_map, has, imports_json, pairs, repo, report, run, sites, write, }; @@ -950,6 +950,7 @@ fn commit_mode_gives_the_edges_of_the_working_tree() { ("lib/z.dart", ""), ], ); + commit(&root); let tree = run(&root, "check", &["--json"]); let commit = run(&root, "check", &["--json", "--commit", "HEAD"]); assert_eq!( diff --git a/system-map/tests/dotnet.rs b/system-map/tests/dotnet.rs index 403923ee..7d7ee1da 100644 --- a/system-map/tests/dotnet.rs +++ b/system-map/tests/dotnet.rs @@ -4,7 +4,8 @@ use serde_json::{Value, json}; use crate::common::{ - assert_only_candidate_links_relaxed, extractor_map, has, pairs, repo, report, run, sites, write, + assert_only_candidate_links_relaxed, commit, extractor_map, has, pairs, repo, report, run, + sites, write, }; /// A map naming `dotnet-projects` whose parts are `parts`, each an id with its paths, and whose import links are `links`. @@ -756,6 +757,7 @@ fn commit_mode_gives_the_edges_of_the_working_tree() { ("core/Core.csproj", EMPTY), ], ); + commit(&root); let tree = run(&root, "check", &["--json"]); let commit = run(&root, "check", &["--json", "--commit", "HEAD"]); assert_eq!( diff --git a/system-map/tests/edges.rs b/system-map/tests/edges.rs index 9883e222..cb39bcbd 100644 --- a/system-map/tests/edges.rs +++ b/system-map/tests/edges.rs @@ -1,10 +1,8 @@ //! Computed edges, read by the named extractors, and the link check that holds the map's import links to them. -use std::{path::Path, process::Command}; - use serde_json::{Value, json}; -use crate::common::{base, has, project_text, write}; +use crate::common::{base, commit, has, project_text, report, run, track_all, write}; /// The map `base` makes, naming `extractors` and with `links` instead of its own. fn map(extractors: &[&str], links: Value) -> Value { @@ -47,16 +45,10 @@ fn workspace( write(dir.path(), &format!("{folder}/Cargo.toml"), &text); write(dir.path(), &format!("{folder}/src/lib.rs"), ""); } - let git = |args: &[&str]| studio_test_support::git(dir.path(), args); - git(&["add", "-A"]); - git(&["commit", "-q", "-m", "Workspace"]); + track_all(dir.path()); (dir, root) } -fn report(root: &str) -> system_map::Report { - system_map::check(Path::new(root)) -} - /// The computed edges no import link declares, as `from>to` strings, read from their problems. fn edges(report: &system_map::Report) -> Vec { report @@ -276,8 +268,7 @@ fn a_registry_dependency_sharing_a_workspace_crate_name_is_not_reported() { "alpha/Cargo.toml", &manifest("alpha", "beta = \"0.4\"\n", ""), ); - studio_test_support::git(dir.path(), &["add", "-A"]); - studio_test_support::git(dir.path(), &["commit", "-q", "-m", "Registry beta"]); + track_all(dir.path()); assert_eq!(edges(&report(&root)), Vec::::new()); } @@ -306,8 +297,7 @@ fn workspace_in_one_part(map: &Value) -> (tempfile::TempDir, String) { for f in ["alpha/src/lib.rs", "beta/src/lib.rs"] { write(dir.path(), f, ""); } - studio_test_support::git(dir.path(), &["add", "-A"]); - studio_test_support::git(dir.path(), &["commit", "-q", "-m", "Workspace"]); + track_all(dir.path()); (dir, root) } @@ -362,9 +352,7 @@ fn tree(map: &Value, files: &[(&str, &str)]) -> (tempfile::TempDir, String) { for (file, text) in files { write(dir.path(), file, text); } - let git = |args: &[&str]| studio_test_support::git(dir.path(), args); - git(&["add", "-A"]); - git(&["commit", "-q", "-m", "Tree"]); + track_all(dir.path()); (dir, root) } @@ -518,7 +506,7 @@ fn a_mod_line_naming_a_deleted_or_symlinked_module_file_still_pairs_and_does_not std::fs::remove_file(&a).unwrap(); if symlink { std::os::unix::fs::symlink("target.rs", &a).unwrap(); - studio_test_support::git(dir.path(), &["add", "-A"]); + track_all(dir.path()); } let report = report(&root); assert_eq!(edges(&report), ["alpha>beta"], "symlink {symlink}"); @@ -993,19 +981,10 @@ fn the_new_extractors_do_not_run_when_the_map_does_not_name_them() { assert_eq!(edges(&report(&root)), Vec::::new()); } -fn run_check(root: &str, args: &[&str]) -> std::process::Output { - Command::new(env!("CARGO_BIN_EXE_system-map")) - .arg("check") - .args(args) - .args(["--root", root]) - .output() - .unwrap() -} - #[test] fn check_prints_an_undeclared_edge_as_a_problem_and_json_has_no_unlinked() { let (_dir, root) = workspace(&map(&["cargo"], json!([])), &[("alpha", "beta")], &[]); - let out = run_check(&root, &[]); + let out = run(&root, "check", &[]); assert!(!out.status.success()); let text = String::from_utf8(out.stdout).unwrap(); assert_eq!(text.lines().count(), 1, "{text}"); @@ -1014,7 +993,7 @@ fn check_prints_an_undeclared_edge_as_a_problem_and_json_has_no_unlinked() { "{text}" ); - let out = run_check(&root, &["--json"]); + let out = run(&root, "check", &["--json"]); let value: Value = serde_json::from_slice(&out.stdout).unwrap(); assert_eq!(value.get("unlinked"), None); } @@ -1680,7 +1659,7 @@ fn a_file_whose_name_only_looks_like_a_test_keeps_its_edges() { fn sites(root: &str, args: &[&str]) -> Vec { let mut args = args.to_vec(); args.push("--json"); - let out = run_check(root, &args); + let out = run(root, "check", &args); let value: Value = serde_json::from_slice(&out.stdout) .unwrap_or_else(|_| panic!("{}", String::from_utf8_lossy(&out.stderr))); let mut found = Vec::new(); @@ -1792,8 +1771,7 @@ fn a_cargo_site_is_the_line_of_the_dependency_s_key_when_a_text_search_finds_it( write(dir.path(), "beta/Cargo.toml", &manifest("beta", "", "")); write(dir.path(), "alpha/src/lib.rs", ""); write(dir.path(), "beta/src/lib.rs", ""); - studio_test_support::git(dir.path(), &["add", "-A"]); - studio_test_support::git(dir.path(), &["commit", "-q", "-m", "Workspace"]); + track_all(dir.path()); assert_eq!(sites(&root, &[]), [site], "{text}"); } } @@ -1834,8 +1812,7 @@ fn a_cargo_site_in_a_root_manifest_is_the_package_s_dependency_never_the_workspa write(dir.path(), "src/lib.rs", ""); write(dir.path(), "beta/Cargo.toml", &manifest("beta", "", "")); write(dir.path(), "beta/src/lib.rs", ""); - studio_test_support::git(dir.path(), &["add", "-A"]); - studio_test_support::git(dir.path(), &["commit", "-q", "-m", "Workspace"]); + track_all(dir.path()); assert_eq!(sites(&root, &[]), [site], "{text}"); } } @@ -1850,7 +1827,7 @@ fn every_extractor() -> (tempfile::TempDir, String) { ); map["ignored"] = json!(["system-map.json", "Cargo.toml", "lib/"]); map["laws"][0]["backing"] = json!("check"); - tree( + let (dir, root) = tree( &map, &[ ( @@ -1875,14 +1852,16 @@ fn every_extractor() -> (tempfile::TempDir, String) { ("lib/index.ts", "export { B } from '../beta/b';\n"), ("beta/b.ts", "export const B = 1;\n"), ], - ) + ); + commit(&root); + (dir, root) } #[test] fn commit_mode_gives_what_the_working_tree_gives_on_the_same_tree() { let (_dir, root) = every_extractor(); - let tree = run_check(&root, &["--json"]); - let commit = run_check(&root, &["--json", "--commit", "HEAD"]); + let tree = run(&root, "check", &["--json"]); + let commit = run(&root, "check", &["--json", "--commit", "HEAD"]); assert_eq!( String::from_utf8_lossy(&commit.stderr), String::from_utf8_lossy(&tree.stderr) @@ -1912,11 +1891,11 @@ fn commit_mode_reads_the_commit_not_the_working_tree() { write(dir.path(), "alpha/c.ts", "import { B } from '../beta/b';\n"); assert_eq!(sites(&root, &["--commit", "HEAD"]), before); assert_ne!(sites(&root, &[]), before); - studio_test_support::git(dir.path(), &["add", "-A"]); - studio_test_support::git(dir.path(), &["commit", "-q", "-m", "Edit"]); + track_all(dir.path()); + commit(&root); assert_eq!(sites(&root, &["--commit", "HEAD~1"]), before); - let out = run_check(&root, &["--commit", "no-such-commit"]); + let out = run(&root, "check", &["--commit", "no-such-commit"]); assert!(!out.status.success()); let text = String::from_utf8_lossy(&out.stdout); assert!(text.contains("no-such-commit"), "{text}"); diff --git a/system-map/tests/go.rs b/system-map/tests/go.rs index 8a0127cf..14f39149 100644 --- a/system-map/tests/go.rs +++ b/system-map/tests/go.rs @@ -4,8 +4,8 @@ use serde_json::{Value, json}; use crate::common::{ - assert_only_candidate_links_relaxed, assert_unsure_file_listed_and_link_unproven, - extractor_map, has, imports_json, pairs, repo, report, run, sites, write, + assert_only_candidate_links_relaxed, assert_unsure_file_listed_and_link_unproven, commit, + extractor_map, has, imports_json, pairs, repo, report, run, sites, track_all, write, }; /// A map naming `go-imports` whose parts are `parts`, each an id with its paths, and whose import links are `links`. @@ -396,8 +396,7 @@ fn worked(work: &[(&str, &str)]) -> system_map::Report { for (file, text) in work { write(dir.path(), file, text); } - studio_test_support::git(dir.path(), &["add", "-A"]); - studio_test_support::git(dir.path(), &["commit", "-q", "-m", "Work"]); + track_all(dir.path()); report(&root) } @@ -862,8 +861,8 @@ fn commit_mode_gives_the_edges_and_doubts_of_the_working_tree() { "app/main.go", "package main\n\nimport (\n\t\"example.com/x/sub\"\n\t\"example.com/app/lib\"\n)\n", ); - studio_test_support::git(dir.path(), &["add", "-A"]); - studio_test_support::git(dir.path(), &["commit", "-q", "-m", "Lib"]); + track_all(dir.path()); + commit(&root); let tree = run(&root, "check", &["--json"]); let commit = run(&root, "check", &["--json", "--commit", "HEAD"]); assert_eq!( diff --git a/system-map/tests/imports.rs b/system-map/tests/imports.rs index 334df573..98e9f8ea 100644 --- a/system-map/tests/imports.rs +++ b/system-map/tests/imports.rs @@ -7,7 +7,7 @@ use std::{ use serde_json::{Value, json}; -use crate::common::{base, project_text, write}; +use crate::common::{self, base, git_repo, project_text}; /// The map `base` makes, naming `extractors`, with alpha and beta owning `alpha` and `beta` paths. fn map(extractors: &[&str], alpha: &[&str], beta: &[&str]) -> Value { @@ -18,14 +18,10 @@ fn map(extractors: &[&str], alpha: &[&str], beta: &[&str]) -> Value { map } -/// A repository holding `map` and `files` with their texts. +/// A real repository whose index holds `map` and `files` with their texts: `imports` lists them with `git ls-files`. fn repo(map: &Value, files: &[(&str, &str)]) -> (tempfile::TempDir, String) { - let (dir, root) = project_text(&map.to_string(), &[]); - for (file, text) in files { - write(dir.path(), file, text); - } - studio_test_support::git(dir.path(), &["add", "-A"]); - studio_test_support::git(dir.path(), &["commit", "-q", "-m", "Code"]); + let (dir, root) = common::repo(map, files); + git_repo(&root); (dir, root) } @@ -139,6 +135,7 @@ fn a_crate_roots_mod_lines_are_left_out_but_its_paths_count() { } fn run(root: &str, args: &[&str]) -> Output { + git_repo(root); Command::new(env!("CARGO_BIN_EXE_system-map")) .arg("imports") .args(args) diff --git a/system-map/tests/java.rs b/system-map/tests/java.rs index 52d4864e..8149a89a 100644 --- a/system-map/tests/java.rs +++ b/system-map/tests/java.rs @@ -4,8 +4,8 @@ use serde_json::{Value, json}; use crate::common::{ - assert_only_candidate_links_relaxed, assert_unsure_file_listed_and_link_unproven, - extractor_map, has, imports_json, pairs, repo, report, run, sites, write, + assert_only_candidate_links_relaxed, assert_unsure_file_listed_and_link_unproven, commit, + extractor_map, has, imports_json, pairs, repo, report, run, sites, track_all, write, }; /// A map naming `java-imports` whose parts are `parts`, each an id with its paths, whose import links are `links`, and @@ -1005,8 +1005,8 @@ fn commit_mode_gives_the_edges_and_doubts_of_the_working_tree() { "c/src/main/java/c/Thing.java", "package c;\n\nimport app.Main;\n\npublic class Thing {}\n", ); - studio_test_support::git(dir.path(), &["add", "-A"]); - studio_test_support::git(dir.path(), &["commit", "-q", "-m", "Thing"]); + track_all(dir.path()); + commit(&root); let tree = run(&root, "check", &["--json"]); let commit = run(&root, "check", &["--json", "--commit", "HEAD"]); assert_eq!( diff --git a/system-map/tests/laws.rs b/system-map/tests/laws.rs index 58d2d0b2..ccf74717 100644 --- a/system-map/tests/laws.rs +++ b/system-map/tests/laws.rs @@ -4,8 +4,7 @@ use std::path::Path; use serde_json::{Value, json}; -use crate::common::{FILES, base, has, problems, project, write}; -use studio_test_support::git; +use crate::common::{FILES, base, has, problems, project, report, track_all, write}; /// A tag line for `id`. The tag's marker is spliced in, so that no line of this file is itself a /// tag line: the checker runs on this repository and would count it. @@ -19,10 +18,10 @@ fn with_backing(backing: &str) -> Value { map } -fn commit(root: &str, file: &str, text: &str) { +/// Writes `file` holding `text` into the folder at `root` and tracks it. +fn write_tracked(root: &str, file: &str, text: &str) { write(Path::new(root), file, text); - git(Path::new(root), &["add", "-A"]); - git(Path::new(root), &["commit", "-q", "-m", "More"]); + track_all(Path::new(root)); } #[test] @@ -34,12 +33,12 @@ fn a_law_that_says_check_with_no_tag_fails_naming_the_law() { #[test] fn a_law_that_says_check_with_a_tag_passes_and_lists_the_tag() { let (_dir, root) = project(&with_backing("check"), &FILES); - commit( + write_tracked( &root, "alpha/a.rs", &format!("fn a() {{}}\n{}fn b() {{}}\n", tag("stay-true")), ); - let report = system_map::check(Path::new(&root)); + let report = report(&root); assert_eq!(report.problems, Vec::::new()); let law = report.laws.iter().find(|l| l.id == "stay-true").unwrap(); assert_eq!(law.tags.len(), 1); @@ -50,7 +49,7 @@ fn a_law_that_says_check_with_a_tag_passes_and_lists_the_tag() { #[test] fn a_law_on_trust_with_no_tag_passes() { let (_dir, root) = project(&with_backing("trust"), &FILES); - let report = system_map::check(Path::new(&root)); + let report = report(&root); assert_eq!(report.problems, Vec::::new()); assert!(report.laws[0].tags.is_empty()); } @@ -58,7 +57,7 @@ fn a_law_on_trust_with_no_tag_passes() { #[test] fn a_law_on_trust_with_a_tag_fails_naming_the_law_and_the_tag() { let (_dir, root) = project(&with_backing("trust"), &FILES); - commit(&root, "beta/b.rs", &format!("\n\n{}", tag("stay-true"))); + write_tracked(&root, "beta/b.rs", &format!("\n\n{}", tag("stay-true"))); assert!(has( &problems(&root), &["stay-true", "trust", "beta/b.rs:3"] @@ -68,7 +67,7 @@ fn a_law_on_trust_with_a_tag_fails_naming_the_law_and_the_tag() { #[test] fn a_tag_naming_no_law_fails_naming_the_tag() { let (_dir, root) = project(&with_backing("trust"), &FILES); - commit(&root, "beta/b.rs", &tag("no-such-law")); + write_tracked(&root, "beta/b.rs", &tag("no-such-law")); assert!(has(&problems(&root), &["no-such-law", "beta/b.rs:1"])); } @@ -76,9 +75,9 @@ fn a_tag_naming_no_law_fails_naming_the_tag() { fn tags_are_found_in_ignored_paths_and_one_file_may_carry_several() { let (_dir, root) = project(&with_backing("check"), &FILES); let two = format!("{}{}", tag("stay-true"), tag("stay-true")); - commit(&root, "docs/d.md", &two); - commit(&root, "alpha/a.rs", &tag("stay-true")); - let report = system_map::check(Path::new(&root)); + write_tracked(&root, "docs/d.md", &two); + write_tracked(&root, "alpha/a.rs", &tag("stay-true")); + let report = report(&root); assert_eq!(report.problems, Vec::::new()); let lines: Vec<_> = report.laws[0] .tags @@ -105,7 +104,7 @@ fn only_a_comment_line_of_its_own_is_a_tag() { let text = format!( "let s = \"// {marker} stay-true\";\nprintln!(\"{marker} stay-true\");\nsee {marker} stay-true\n" ); - commit(&root, "alpha/a.rs", &text); + write_tracked(&root, "alpha/a.rs", &text); assert_eq!(problems(&root), Vec::::new()); } @@ -123,7 +122,7 @@ fn comment_leaders_of_other_languages_and_indentation_are_accepted() { .enumerate() { let file = format!("alpha/f{i}.txt"); - commit(&root, &file, &format!("{line}\n")); + write_tracked(&root, &file, &format!("{line}\n")); assert!(has(&problems(&root), &[&file, "stay-true"]), "{line}"); } } @@ -138,9 +137,8 @@ fn a_tag_behind_a_tracked_symlink_to_a_file_outside_the_repository_is_not_read() Path::new(&root).join("alpha/link.rs"), ) .unwrap(); - git(Path::new(&root), &["add", "-A"]); - git(Path::new(&root), &["commit", "-q", "-m", "Link"]); - let report = system_map::check(Path::new(&root)); + track_all(Path::new(&root)); + let report = report(&root); assert_eq!(report.problems, Vec::::new()); assert!(report.laws[0].tags.is_empty()); let alpha = report.parts.iter().find(|p| p.id == "alpha").unwrap(); @@ -153,9 +151,8 @@ fn a_tag_in_a_file_that_is_not_valid_utf8_is_still_found() { let mut bytes = b"\xff\xfe junk\n".to_vec(); bytes.extend(tag("stay-true").as_bytes()); std::fs::write(Path::new(&root).join("alpha/a.rs"), bytes).unwrap(); - git(Path::new(&root), &["add", "-A"]); - git(Path::new(&root), &["commit", "-q", "-m", "Bytes"]); - let report = system_map::check(Path::new(&root)); + track_all(Path::new(&root)); + let report = report(&root); assert_eq!(report.problems, Vec::::new()); assert_eq!(report.laws[0].tags.len(), 1); assert_eq!(report.laws[0].tags[0].line, 2); @@ -165,7 +162,7 @@ mod resolver { use system_map::{LawTest, Tag, law_tests, test_name_after}; use super::tag; - use crate::common::{base, project, write}; + use crate::common::{base, project, report, track_all, write}; fn after(source: &str) -> Option { test_name_after(source, 1) @@ -263,9 +260,9 @@ mod resolver { "alpha/a.rs", &format!("{}#[test]\nfn holds() {{}}\n", tag("stay-true")), ); - studio_test_support::git(dir.path(), &["add", "-A"]); + track_all(dir.path()); + let report = report(&root); let root = std::path::Path::new(&root); - let report = system_map::check(root); assert_eq!( law_tests(root, &report.laws), vec![LawTest { diff --git a/system-map/tests/map_file.rs b/system-map/tests/map_file.rs index 7028b9d7..17b1bbe7 100644 --- a/system-map/tests/map_file.rs +++ b/system-map/tests/map_file.rs @@ -2,7 +2,7 @@ use serde_json::{Value, json}; -use crate::common::{FILES, base, has, problems, project, project_text}; +use crate::common::{FILES, base, has, problems, project, project_text, report}; fn failing(map: Value) -> Vec { let (_dir, root) = project(&map, &FILES); @@ -156,7 +156,7 @@ fn check_text_reports_what_check_reports_for_the_same_map_on_disk() { .chain(&["system-map.json"]) .map(ToString::to_string) .collect(); - let on_disk = system_map::check(std::path::Path::new(&root)); + let on_disk = report(&root); let from_text = system_map::check_text(std::path::Path::new(&root), files, &map.to_string()); assert_eq!( diff --git a/system-map/tests/ownership.rs b/system-map/tests/ownership.rs index e7808c66..60981f9d 100644 --- a/system-map/tests/ownership.rs +++ b/system-map/tests/ownership.rs @@ -4,11 +4,11 @@ use std::path::Path; use serde_json::{Value, json}; -use crate::common::{base, has, problems, project, write}; +use crate::common::{base, folder, git_repo, has, problems, project, report, track, write}; fn owned(map: &Value, files: &[&str]) -> Vec<(String, Vec)> { let (_dir, root) = project(map, files); - let report = system_map::check(Path::new(&root)); + let report = report(&root); assert_eq!(report.problems, Vec::::new()); report.parts.into_iter().map(|p| (p.id, p.files)).collect() } @@ -100,7 +100,9 @@ fn an_ignored_match_wins_over_every_part() { #[test] fn an_untracked_file_is_never_counted() { + // A real repository: what git leaves out of `ls-files` is what `check` leaves out. let (dir, root) = project(&base(), &["alpha/a.rs", "beta/b", "docs/d"]); + git_repo(&root); write(dir.path(), "stray/new.rs", "x"); write(dir.path(), "alpha/untracked.rs", "x"); let report = system_map::check(Path::new(&root)); @@ -147,16 +149,20 @@ fn without_map_entry() -> Value { /// A project whose map file is on disk but no longer tracked. fn untracked_map(map: &Value) -> (tempfile::TempDir, String) { - let (dir, root) = project(map, &["alpha/a.rs", "beta/b", "docs/d"]); - studio_test_support::git(dir.path(), &["rm", "-q", "--cached", "system-map.json"]); - studio_test_support::git(dir.path(), &["commit", "-q", "-m", "Untrack the map"]); + let (dir, root) = folder(); + write(dir.path(), "system-map.json", &map.to_string()); + let files = ["alpha/a.rs", "beta/b", "docs/d"]; + for file in files { + write(dir.path(), file, "x\n"); + } + track(dir.path(), &files); (dir, root) } #[test] fn a_tracked_map_file_needs_no_owner_and_no_ignored_entry() { let (_dir, root) = project(&without_map_entry(), &["alpha/a.rs", "beta/b", "docs/d"]); - let report = system_map::check(Path::new(&root)); + let report = report(&root); assert_eq!(report.problems, Vec::::new()); assert_eq!(report.counts.ignored, 2); } diff --git a/system-map/tests/php.rs b/system-map/tests/php.rs index 3370533d..fd78c544 100644 --- a/system-map/tests/php.rs +++ b/system-map/tests/php.rs @@ -4,7 +4,7 @@ use serde_json::{Value, json}; -use crate::common::{extractor_map, pairs, repo, report, run, sites, write}; +use crate::common::{commit, extractor_map, pairs, repo, report, run, sites, write}; /// A map naming `php-imports` whose parts are `parts`, each an id with its paths, whose import links are `links`, and /// which ignores `ignored` besides its own file. @@ -735,6 +735,7 @@ fn a_commit_resolves_a_class_use_through_its_own_composer_json() { let out = run(&root, "check", &args); serde_json::from_slice::(&out.stdout).unwrap()["edges"].clone() }; + commit(&root); let at_commit = edges(&["--commit", "HEAD"]); assert_eq!(at_commit[0]["pairs"][0]["to"], "src/X.php", "{at_commit}"); assert_eq!(edges(&[]), at_commit); @@ -1283,6 +1284,7 @@ fn commit_mode_gives_the_edges_of_the_working_tree() { (X, "to`, sorted. @@ -958,8 +958,8 @@ fn commit_mode_gives_the_edges_of_the_working_tree() { ); write(dir.path(), "proj/src/pkg/__init__.py", "import other\n"); write(dir.path(), "proj/other.py", ""); - studio_test_support::git(dir.path(), &["add", "-A"]); - studio_test_support::git(dir.path(), &["commit", "-q", "-m", "More"]); + track_all(dir.path()); + commit(&root); let tree = run(&root, "check", &["--json"]); let commit = run(&root, "check", &["--json", "--commit", "HEAD"]); assert_eq!( diff --git a/system-map/tests/scan.rs b/system-map/tests/scan.rs index 0512089a..00b02392 100644 --- a/system-map/tests/scan.rs +++ b/system-map/tests/scan.rs @@ -3,18 +3,18 @@ use std::path::Path; use serde_json::Value; -use studio_test_support::{git, repo}; +use studio_test_support::git; -use crate::common::write; +use crate::common::{folder, write}; -/// A repository tracking `files`, each with its text. +/// A repository tracking `files`, each with its text, in its index: the scan lists them with `git ls-files`. fn tracked(files: &[(&str, &str)]) -> (tempfile::TempDir, String) { - let (dir, root) = repo(); + let (dir, root) = folder(); + git(dir.path(), &["init", "-q", "-b", "main"]); for (file, text) in files { write(dir.path(), file, text); } git(dir.path(), &["add", "-A"]); - git(dir.path(), &["commit", "-q", "-m", "Files"]); (dir, root) } diff --git a/system-map/tests/touched.rs b/system-map/tests/touched.rs index 99d80c38..1ecf4e8a 100644 --- a/system-map/tests/touched.rs +++ b/system-map/tests/touched.rs @@ -4,7 +4,7 @@ use std::path::Path; use serde_json::{Value, json}; -use crate::common::{base, project, project_text}; +use crate::common::{base, project, project_text, report}; fn touched(map: &Value, paths: &[&str]) -> system_map::Touched { let (_dir, root) = project(map, &["alpha/a.rs"]); @@ -179,7 +179,7 @@ fn a_map_that_fails_the_shape_check_makes_touched_fail_with_every_problem() { let (_dir, root) = project(&map, &["alpha/a.rs"]); let problems = system_map::touched(Path::new(&root), &["alpha/a.rs".to_string()]) .expect_err("a map with problems must not answer"); - let check = system_map::check(Path::new(&root)).problems; + let check = report(&root).problems; for needle in ["Bad Id", "more than once", "nowhere", "nothing"] { assert!( problems.iter().any(|p| p.contains(needle)),