use std::io;
use std::path::{Path, PathBuf};
use humansize::{DECIMAL, format_size};
use owo_colors::{OwoColorize, Style};
mod failures;
mod footer;
mod table;
#[cfg(test)]
use failures::COLLAPSE_AFTER;
use failures::failure_lines;
use table::{Line, write_block};
use super::{Entry, RunResult};
use crate::classify::Provenance;
use crate::delete::FailureKind;
use crate::delete::Outcome;
const BECAUSE: &str = "— ";
const UNANCHORED: &str = "unanchored";
const MAX_ECOSYSTEMS_NAMED: usize = 6;
#[derive(Debug, Clone, Copy, Default)]
pub struct HumanOptions {
pub verbose: bool,
pub summary_only: bool,
pub forced: bool,
}
pub(super) mod palette {
use owo_colors::Style;
pub fn removed() -> Style {
Style::new().green()
}
pub fn would_remove() -> Style {
Style::new().yellow()
}
pub fn refused() -> Style {
Style::new().cyan()
}
pub fn failed() -> Style {
Style::new().red()
}
pub fn quiet() -> Style {
Style::new().dimmed()
}
pub fn unanchored() -> Style {
Style::new().magenta()
}
pub fn partial() -> Style {
Style::new().yellow()
}
}
fn outcome_label(outcome: &Outcome) -> &'static str {
match outcome {
Outcome::Removed => "removed",
Outcome::Detached => "detached",
Outcome::WouldRemove => "would remove",
Outcome::Refused(_) => "refused",
Outcome::PartiallyRemoved { .. } => "partial",
_ => "failed",
}
}
fn outcome_style(outcome: &Outcome) -> Style {
match outcome {
Outcome::Removed | Outcome::Detached => palette::removed(),
Outcome::WouldRemove => palette::would_remove(),
Outcome::Refused(_) => palette::refused(),
Outcome::PartiallyRemoved { .. } => palette::partial(),
_ => palette::failed(),
}
}
fn base_path(roots: &[PathBuf]) -> Option<&Path> {
let first = roots.first()?;
let base = first
.ancestors()
.find(|candidate| roots.iter().all(|root| root.starts_with(candidate)))?;
(base.is_absolute() && base.parent().is_some()).then_some(base)
}
fn shorten(path: &Path, base: Option<&Path>) -> String {
let Some(base) = base else {
return path.display().to_string();
};
match path.strip_prefix(base) {
Ok(relative) if relative.as_os_str().is_empty() => ".".to_owned(),
Ok(relative) => relative.display().to_string(),
Err(_) => path.display().to_string(),
}
}
fn with_hint(detail: &str, kind: FailureKind, forced: bool) -> String {
match kind.hint().filter(|_| !forced) {
Some(hint) => format!("{detail}; {hint}"),
None => detail.to_owned(),
}
}
pub(super) fn remaining_words(remaining: u64) -> String {
if remaining == 0 {
return "the directory itself is still there".to_owned();
}
format!("{} left", format_size(remaining, DECIMAL))
}
fn artifact_line(entry: &Entry, base: Option<&Path>, forced: bool) -> Line {
let detail = match &entry.outcome {
Outcome::Refused(refusal) => Some(refusal.to_string()),
Outcome::PartiallyRemoved { remaining, failure, .. } => Some(with_hint(
&format!("{}; {failure}", remaining_words(*remaining)),
failure.kind(),
forced,
)),
Outcome::Failed(failure) => Some(with_hint(&failure.to_string(), failure.kind(), forced)),
Outcome::Removed | Outcome::Detached | Outcome::WouldRemove => match &entry.finding.provenance {
Provenance::Included { pattern } => {
let shown = shorten(Path::new(pattern), base);
(shown != shorten(&entry.path, base))
.then(|| format!("included by `{shown}`"))
.or_else(|| Some("included by name".to_owned()))
}
Provenance::Anchored { .. } | Provenance::Cache { .. } | Provenance::Tagged => None,
},
};
let ecosystem = entry.ecosystem();
Line {
label: outcome_label(&entry.outcome),
label_style: outcome_style(&entry.outcome),
size: Some(if entry.outcome == Outcome::Detached && entry.bytes == 0 {
"—".to_owned()
} else {
format_size(entry.reclaimed_bytes(), DECIMAL)
}),
tag: Some(ecosystem.unwrap_or(UNANCHORED)),
tag_style: if ecosystem.is_some() {
palette::quiet()
} else {
palette::unanchored()
},
path: shorten(&entry.path, base),
detail,
}
}
fn skip_lines(result: &RunResult, base: Option<&Path>) -> Vec<Line> {
result
.skip_reasons()
.map(|(path, reason)| Line {
label: "skipped",
label_style: palette::quiet(),
size: None,
tag: None,
tag_style: palette::quiet(),
path: shorten(path, base),
detail: Some(reason.to_string()),
})
.collect()
}
pub fn render(result: &RunResult, options: HumanOptions, out: &mut impl io::Write) -> io::Result<()> {
let mut wrote_body = false;
if !options.summary_only {
let base = base_path(&result.roots);
let artifacts: Vec<Line> = result
.entries
.iter()
.map(|entry| artifact_line(entry, base, options.forced))
.collect();
let skips = if options.verbose {
skip_lines(result, base)
} else {
Vec::new()
};
let failures = failure_lines(result, options.verbose, base);
if let Some(base) = base.filter(|_| !(artifacts.is_empty() && skips.is_empty() && failures.is_empty())) {
writeln!(out, "{}", base.display().bold())?;
wrote_body = true;
}
let mut wrote_block = false;
for block in [&artifacts, &skips, &failures] {
if block.is_empty() {
continue;
}
if wrote_block {
writeln!(out)?;
}
write_block(block, out)?;
(wrote_block, wrote_body) = (true, true);
}
if let Some(git) = result
.git
.as_ref()
.filter(|git| !git.noteworthy(options.verbose).is_empty())
{
if wrote_block {
writeln!(out)?;
}
crate::git::write_sweep_block(git, base, options.verbose, out)?;
wrote_body = true;
}
}
footer::write_footer(result, options, wrote_body, out)
}
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::footer::{ecosystem_summary, reclaimed_by_ecosystem};
use super::table::{MAX_PATH_COLUMN, Widths};
use super::*;
use crate::classify::SkipReason;
use crate::delete::Refusal;
use crate::report::fixtures;
use crate::report::fixtures::{spell, unspell};
use crate::scan::Skipped;
fn entry(path: &str, spec: &str, bytes: u64, outcome: Outcome) -> Entry {
fixtures::entry(&spell(path), spec, bytes, outcome)
}
fn included(path: &str, pattern: &str, bytes: u64, outcome: Outcome) -> Entry {
fixtures::included(&spell(path), &spell(pattern), bytes, outcome)
}
fn result(entries: Vec<Entry>, dry_run: bool) -> RunResult {
RunResult {
roots: vec![PathBuf::from(spell("/projects"))],
..fixtures::result(entries, dry_run)
}
}
fn path(literal: &str) -> PathBuf {
PathBuf::from(spell(literal))
}
fn render_to_string(result: &RunResult, options: HumanOptions) -> String {
let mut buffer = Vec::new();
render(result, options, &mut buffer).expect("writing to a Vec cannot fail");
let text = String::from_utf8(buffer).expect("the renderer emits UTF-8");
unspell(&anstream::adapter::strip_str(&text).to_string())
}
fn verbose() -> HumanOptions {
HumanOptions {
verbose: true,
summary_only: false,
forced: false,
}
}
fn interrupted() -> RunResult {
let mut result = result(Vec::new(), true);
result.failures = vec![
crate::scan::WalkFailure {
path: Some(path("/projects/flaky")),
message: "Input/output error (os error 5)".to_owned(),
transient: false,
inaccessible: false,
},
crate::scan::WalkFailure {
path: Some(path("/projects/containers")),
message: "Interrupted system call (os error 4)".to_owned(),
transient: true,
inaccessible: false,
},
];
result
}
#[test]
fn should_not_list_a_transient_interruption_by_default() {
let text = render_to_string(&interrupted(), HumanOptions::default());
assert!(text.contains("Input/output error"), "{text}");
assert!(!text.contains("Interrupted system call"), "{text}");
}
#[test]
fn should_collapse_many_identical_walk_errors_into_a_count() {
let mut result = result(Vec::new(), true);
result.failures = (0..40)
.map(|index| crate::scan::WalkFailure {
path: Some(path(&format!("/home/Library/protected{index}"))),
message: "Operation not permitted (os error 1)".to_owned(),
transient: false,
inaccessible: false,
})
.collect();
let text = render_to_string(&result, HumanOptions::default());
assert_eq!(
text.matches("Operation not permitted").count(),
COLLAPSE_AFTER + 1,
"{text}"
);
assert!(text.contains("and 35 more"), "{text}");
let verbose_text = render_to_string(&result, verbose());
assert_eq!(
verbose_text.matches("Operation not permitted").count(),
40,
"{verbose_text}"
);
}
fn protected(count: usize) -> RunResult {
let mut result = result(Vec::new(), true);
result.failures = (0..count)
.map(|index| crate::scan::WalkFailure {
path: Some(path(&format!("/home/Library/protected{index}"))),
message: "Operation not permitted (os error 1)".to_owned(),
transient: false,
inaccessible: true,
})
.collect();
result
}
#[test]
fn should_summarise_inaccessible_directories_in_one_footer_line() {
let text = render_to_string(&protected(108), HumanOptions::default());
assert_eq!(text.matches("Operation not permitted").count(), 0, "{text}");
assert!(text.contains("108 directories inaccessible"), "{text}");
assert!(text.contains("--verbose"), "{text}");
assert!(!text.contains("walk error"), "it is not presented as an error: {text}");
}
#[test]
fn should_say_directory_in_the_singular_for_one_inaccessible_directory() {
let text = render_to_string(&protected(1), HumanOptions::default());
assert!(text.contains("1 directory inaccessible"), "{text}");
}
#[test]
fn should_list_every_inaccessible_directory_when_asked_for_detail() {
let text = render_to_string(&protected(12), verbose());
assert_eq!(text.matches("Operation not permitted").count(), 12, "{text}");
assert!(text.contains("inaccessible"), "{text}");
assert!(text.contains("protected11"), "{text}");
}
#[test]
fn should_list_a_transient_interruption_when_asked_for_detail() {
let text = render_to_string(&interrupted(), verbose());
assert!(text.contains("Interrupted system call"), "{text}");
assert!(
text.contains("interrupted"),
"it is labelled as an interruption, not as a walk error: {text}"
);
}
#[test]
fn should_label_an_unanchored_removal_as_unanchored() {
let result = result(
vec![included(
"/scratch/build-junk",
"/scratch/build-junk",
1024,
Outcome::Removed,
)],
false,
);
let text = render_to_string(&result, HumanOptions::default());
assert!(text.contains("unanchored"), "{text}");
assert!(text.contains("included by"), "{text}");
}
fn mixed() -> RunResult {
result(
vec![
entry("/projects/api/target", "rust.target", 4_200_000_000, Outcome::Removed),
entry("/projects/web/dist", "node.dist", 18_500_000, Outcome::Removed),
entry(
"/projects/link/target",
"rust.target",
0,
Outcome::Refused(Refusal::Symlink),
),
entry(
"/projects/locked/target",
"rust.target",
900,
Outcome::Failed(crate::report::fixtures::failure(std::io::ErrorKind::PermissionDenied)),
),
entry(
"/projects/busy/target",
"rust.target",
130_500_000_000,
Outcome::PartiallyRemoved {
freed: 130_000_000_000,
remaining: 500_000_000,
failure: crate::report::fixtures::failure(std::io::ErrorKind::DirectoryNotEmpty),
},
),
],
false,
)
}
#[test]
fn should_render_a_run_with_every_outcome() {
insta::assert_snapshot!(render_to_string(&mixed(), HumanOptions::default()));
}
#[test]
fn should_render_a_dry_run_as_reclaimable_and_say_nothing_was_removed() {
let result = result(
vec![entry(
"/projects/api/target",
"rust.target",
4_200_000_000,
Outcome::WouldRemove,
)],
true,
);
insta::assert_snapshot!(render_to_string(&result, HumanOptions::default()));
}
fn skipped() -> RunResult {
let mut result = result(
vec![entry("/projects/api/target", "rust.target", 100, Outcome::Removed)],
false,
);
result.skipped_count = 2;
result.skips = vec![
Skipped {
path: path("/projects/data/target"),
reason: SkipReason::NoMarker {
ecosystem: "rust",
markers: &["Cargo.toml"],
},
},
Skipped {
path: path("/projects/tool/bin"),
reason: SkipReason::NotEnabled {
spec: "go.bin".to_owned(),
note: Some("`bin/` beside a go.mod is as often hand-written scripts as output."),
},
},
];
result.sort();
result
}
#[test]
fn should_show_a_skip_count_by_default_and_reasons_when_verbose() {
insta::assert_snapshot!(
"default_hides_skip_detail",
render_to_string(&skipped(), HumanOptions::default())
);
insta::assert_snapshot!("verbose_explains_every_skip", render_to_string(&skipped(), verbose()));
}
#[test]
fn should_not_leave_empty_columns_on_a_skip_line() {
let text = render_to_string(&skipped(), verbose());
let skip = text.lines().find(|line| line.contains("skipped")).expect("a skip line");
assert!(
!skip.contains(" "),
"no run of blanks where a size and an ecosystem would be: {skip:?}"
);
}
#[test]
fn should_introduce_a_reason_without_nesting_parentheses() {
let text = render_to_string(&skipped(), verbose());
assert!(text.contains("— no rust marker (Cargo.toml) proves it"), "{text}");
assert!(!text.contains("(no rust marker"), "{text}");
}
#[test]
fn should_render_only_the_footer_when_asked_for_a_summary() {
let options = HumanOptions {
verbose: false,
summary_only: true,
forced: false,
};
insta::assert_snapshot!(render_to_string(&mixed(), options));
}
#[test]
fn should_render_a_clean_tree_without_a_leading_blank_line() {
insta::assert_snapshot!(render_to_string(&result(Vec::new(), false), HumanOptions::default()));
}
#[test]
fn should_print_paths_relative_to_the_scan_root_named_in_the_header() {
let text = render_to_string(&mixed(), HumanOptions::default());
assert!(text.starts_with("/projects\n"), "the root is stated once: {text}");
assert!(text.contains(" api/target\n"), "{text}");
assert!(
!text.contains(" /projects/api/target"),
"no line repeats the root: {text}"
);
}
#[test]
fn should_print_a_path_outside_the_roots_in_full() {
let result = result(
vec![included("/scratch/build-junk", "/scratch/*", 1024, Outcome::Removed)],
false,
);
let text = render_to_string(&result, HumanOptions::default());
assert!(text.contains("/scratch/build-junk"), "{text}");
}
#[test]
fn should_use_the_common_parent_as_the_base_for_several_roots() {
let roots = vec![path("/projects/api"), path("/projects/web")];
assert_eq!(base_path(&roots), Some(path("/projects").as_path()));
}
#[test]
fn should_not_shorten_against_a_useless_base() {
assert_eq!(base_path(&[path("/a"), path("/b")]), None);
assert_eq!(base_path(&[PathBuf::from(".")]), None);
assert_eq!(base_path(&[]), None);
}
#[test]
fn should_not_pad_a_reason_out_to_the_longest_path_in_the_block() {
let long = format!("/projects/{}/target", "deep/".repeat(20));
let result = result(
vec![
entry(&long, "rust.target", 1_000, Outcome::Removed),
entry(
"/projects/link/target",
"rust.target",
0,
Outcome::Refused(Refusal::Symlink),
),
],
false,
);
let text = render_to_string(&result, HumanOptions::default());
assert!(text.contains("link/target — is a symlink"), "{text}");
}
#[test]
fn should_bound_the_path_column() {
let long = format!("/projects/{}/target", "deep/".repeat(20));
let lines = vec![
artifact_line(
&entry(&long, "rust.target", 0, Outcome::Refused(Refusal::Symlink)),
None,
false,
),
artifact_line(
&entry(
"/projects/a/target",
"rust.target",
0,
Outcome::Refused(Refusal::Symlink),
),
None,
false,
),
];
assert_eq!(Widths::measure(&lines).path, MAX_PATH_COLUMN);
}
#[test]
fn should_break_the_reclaimed_total_down_by_ecosystem() {
let text = render_to_string(&mixed(), HumanOptions::default());
assert!(text.contains("rust 134.20 GB (1), node 18.50 MB (1)"), "{text}");
}
#[test]
fn the_ecosystem_breakdown_sums_to_the_footer_total() {
for result in [mixed(), skipped(), interrupted()] {
let breakdown: u64 = reclaimed_by_ecosystem(&result).iter().map(|row| row.2).sum();
assert_eq!(
breakdown,
result.totals().bytes,
"the per-ecosystem rows must add up to the headline"
);
}
}
#[test]
fn should_omit_the_breakdown_when_there_is_only_one_ecosystem() {
assert_eq!(ecosystem_summary(&skipped()), None);
}
#[test]
fn should_say_how_many_ecosystems_the_breakdown_left_out() {
let specs = [
"rust.target",
"node.dist",
"go.bin",
"python.build",
"maven.target",
"dotnet.obj",
"zig.zig-out",
];
let entries = specs
.iter()
.enumerate()
.map(|(index, spec)| {
let bytes = (specs.len() - index) as u64 * 1_000_000;
entry(&format!("/projects/p{index}/artifact"), spec, bytes, Outcome::Removed)
})
.collect();
let summary = ecosystem_summary(&result(entries, false)).expect("several ecosystems");
assert_eq!(summary.matches('(').count(), MAX_ECOSYSTEMS_NAMED);
assert!(summary.ends_with(", +1 more"), "{summary}");
}
#[test]
fn should_never_write_trailing_whitespace() {
for options in [HumanOptions::default(), verbose()] {
for result in [&mixed(), &skipped(), &interrupted()] {
for line in render_to_string(result, options).lines() {
assert_eq!(line, line.trim_end(), "trailing blanks on {line:?}");
}
}
}
}
#[test]
fn should_write_column_fill_outside_the_styled_span() {
let mut buffer = Vec::new();
render(&mixed(), HumanOptions::default(), &mut buffer).expect("writing to a Vec cannot fail");
let coloured = String::from_utf8(buffer).expect("the renderer emits UTF-8");
assert!(coloured.contains('\u{1b}'), "the renderer emits colour");
for opening in ["\u{1b}[1m", "\u{1b}[2m", "\u{1b}[31m", "\u{1b}[32m", "\u{1b}[33m"] {
assert!(
!coloured.contains(&format!("{opening} ")),
"a styled span opens on padding: {coloured:?}"
);
}
for closing in ["\u{1b}[0m", "\u{1b}[39m"] {
assert!(
!coloured.contains(&format!(" {closing}")),
"a styled span closes on padding: {coloured:?}"
);
}
assert_eq!(
unspell(&anstream::adapter::strip_str(&coloured).to_string()),
render_to_string(&mixed(), HumanOptions::default()),
"stripping the colour is exactly what the snapshots pin"
);
}
#[test]
fn should_be_byte_identical_across_two_renders() {
let result = mixed();
assert_eq!(
render_to_string(&result, HumanOptions::default()),
render_to_string(&result, HumanOptions::default())
);
}
#[test]
fn should_name_a_dependency_directory_it_removed() {
let result = fixtures::result(
vec![
fixtures::entry(&spell("/projects/api/target"), "rust.target", 4_200, Outcome::Removed),
fixtures::entry(
&spell("/projects/web/node_modules"),
"node.node_modules",
9_100,
Outcome::Removed,
),
],
false,
);
let rendered = render_to_string(&result, HumanOptions::default());
assert!(
rendered.contains("1 of them is a dependency directory — a re-download, not build output"),
"the footer must name it:\n{rendered}"
);
assert_eq!(result.totals().dependencies, 1);
assert_eq!(
result.totals().reclaimed,
2,
"and it still counts as an artifact removed"
);
}
#[test]
fn should_say_nothing_about_dependencies_when_none_were_removed() {
let result = fixtures::result(
vec![fixtures::entry(
&spell("/projects/api/target"),
"rust.target",
4_200,
Outcome::Removed,
)],
false,
);
let rendered = render_to_string(&result, HumanOptions::default());
assert!(!rendered.contains("dependency"), "no dependency line:\n{rendered}");
}
#[test]
fn should_not_report_an_emptied_directory_as_nothing_left() {
let entry = fixtures::entry(
&spell("/projects/api/target"),
"rust.target",
4_200,
Outcome::PartiallyRemoved {
freed: 4_200,
remaining: 0,
failure: fixtures::failure(std::io::ErrorKind::PermissionDenied),
},
);
let rendered = render_to_string(&fixtures::result(vec![entry], false), HumanOptions::default());
assert!(
rendered.contains("the directory itself is still there"),
"an emptied directory is still an artifact on disk:\n{rendered}"
);
assert!(
!rendered.contains("0 B left"),
"and never reads as nothing left:\n{rendered}"
);
}
#[test]
fn should_not_suggest_force_to_a_run_that_already_forced() {
let entry = fixtures::entry(
&spell("/projects/api/target"),
"rust.target",
4_200,
Outcome::Failed(fixtures::failure(std::io::ErrorKind::PermissionDenied)),
);
let result = fixtures::result(vec![entry], false);
let plain = render_to_string(&result, HumanOptions::default());
let forced = render_to_string(
&result,
HumanOptions {
forced: true,
..HumanOptions::default()
},
);
assert!(plain.contains("--force"), "an unforced run is told about it:\n{plain}");
assert!(!forced.contains("--force"), "a forced run is not:\n{forced}");
}
}