Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "codem8"
version = "0.7.1"
version = "0.7.2"
edition = "2021"
rust-version = "1.85"
license = "MIT"
Expand Down
28 changes: 14 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ where
.write_all(cli::help_text().as_bytes())
.map_err(|error| CodeM8Error::new(format!("could not write help output: {error}")))?,
cli::CliCommand::ReportDuplicate(config) => {
let should_report_scanned_files = config.git_branch || config.files.is_some();
let should_report_analyzed_files = config.git_branch || config.files.is_some();
let git_branch_files = if config.git_branch {
Some(discovery::changed_files_against_origin(current_dir)?)
} else {
Expand Down Expand Up @@ -61,12 +61,12 @@ where
report::detect_duplicate_blocks(&duplicate_source_files)
});
let report = report::DuplicateReport {
analyzed_files: source_files.len(),
analyzed_files: duplicate_source_files.len(),
analyzed_extensions: config.file_extensions,
scanned_files: should_report_scanned_files.then(|| {
source_files
analyzed_file_paths: should_report_analyzed_files.then(|| {
duplicate_source_files
.iter()
.map(|source_file| source_file.display_path.clone())
.map(|processed_file| processed_file.source.display_path.clone())
.collect()
}),
timings: match (
Expand Down Expand Up @@ -272,7 +272,7 @@ mod tests {
"Duplicate Code Report\n",
"=====================\n",
"\n",
"Number of files scanned: 2\n",
"Number of files analyzed: 2\n",
"Analyzed extensions: ",
&expected_extensions,
"\n",
Expand Down Expand Up @@ -320,26 +320,26 @@ mod tests {
project.write("src/b.ts", "const value = one;\n");
let output =
run_in(&project, &["--report-duplicate", "-files=src/a.ts"]).expect("report succeeds");
assert!(output.contains("Number of files scanned: 1"));
assert!(output.contains("Number of files analyzed: 1"));
assert!(output.contains("Duplicate blocks found: 0"));
}

#[test]
fn verbose_explicit_files_report_lists_scanned_files() {
fn verbose_explicit_files_report_lists_analyzed_files() {
let project = TempProject::new("verbose-explicit-files");
project.write("src/a.ts", "const value = one;\n");
project.write("src/b.ts", "const value = one;\n");
let quiet_output =
run_in(&project, &["--report-duplicate", "-files=src/a.ts"]).expect("report succeeds");
assert!(!quiet_output.contains("Files scanned:"));
assert!(!quiet_output.contains("Files analyzed:"));
let verbose_output = run_in(
&project,
&["--report-duplicate", "-verbose", "-files=src/a.ts"],
)
.expect("report succeeds");
assert!(verbose_output.contains(
"Number of files scanned: 1\n\
Files scanned:\n\
"Number of files analyzed: 1\n\
Files analyzed:\n\
- src/a.ts\n\
Analyzed extensions:"
));
Expand All @@ -351,11 +351,11 @@ mod tests {
project.write("src/a.js", "const value = one;\n");
project.write("src/b.js", "const value = one;\n");
let default_output = run_in(&project, &["--report-duplicate"]).expect("report succeeds");
assert!(default_output.contains("Number of files scanned: 2"));
assert!(default_output.contains("Number of files analyzed: 2"));
assert!(default_output.contains("Duplicate blocks found: 1"));
let js_output = run_in(&project, &["--report-duplicate", "-file-extension=js"])
.expect("report succeeds");
assert!(js_output.contains("Number of files scanned: 2"));
assert!(js_output.contains("Number of files analyzed: 2"));
assert!(js_output.contains("Duplicate blocks found: 1"));
}

Expand All @@ -374,7 +374,7 @@ mod tests {
project.write("src/a.ts", "const shared = 1;\n");
let output =
run_in(&project, &["--report-duplicate", "-git-branch"]).expect("report succeeds");
assert!(output.contains("Number of files scanned: 2"));
assert!(output.contains("Number of files analyzed: 1"));
assert!(output.contains("Duplicate blocks found: 0"));
}

Expand Down
40 changes: 22 additions & 18 deletions src/report/duplicate_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::paths::format_path;
pub struct DuplicateReport {
pub analyzed_files: usize,
pub analyzed_extensions: Vec<String>,
pub scanned_files: Option<Vec<PathBuf>>,
pub analyzed_file_paths: Option<Vec<PathBuf>>,
Comment thread
coderabbitai[bot] marked this conversation as resolved.
pub timings: Option<DuplicateReportTimings>,
pub duplicate_blocks: Vec<DuplicateBlock>,
}
Expand All @@ -26,15 +26,19 @@ pub fn render_duplicate_report(report: &DuplicateReport, verbose: bool) -> Strin
let mut output = String::new();
output.push_str("Duplicate Code Report\n");
output.push_str("=====================\n\n");
let _ = writeln!(output, "Number of files scanned: {}", report.analyzed_files);
let scanned_files = if verbose {
report.scanned_files.as_ref()
let _ = writeln!(
output,
"Number of files analyzed: {}",
report.analyzed_files
);
let analyzed_file_paths = if verbose {
report.analyzed_file_paths.as_ref()
} else {
None
};
if let Some(scanned_files) = scanned_files {
output.push_str("Files scanned:\n");
for file in scanned_files {
if let Some(analyzed_file_paths) = analyzed_file_paths {
output.push_str("Files analyzed:\n");
for file in analyzed_file_paths {
let _ = writeln!(output, "- {}", format_path(file));
}
}
Expand Down Expand Up @@ -124,7 +128,7 @@ mod tests {
let report = DuplicateReport {
analyzed_files: 0,
analyzed_extensions: vec!["ts".to_string()],
scanned_files: None,
analyzed_file_paths: None,
timings: None,
duplicate_blocks: Vec::new(),
};
Expand All @@ -133,7 +137,7 @@ mod tests {
"Duplicate Code Report\n\
=====================\n\
\n\
Number of files scanned: 0\n\
Number of files analyzed: 0\n\
Analyzed extensions: ts\n\
Duplicate blocks found: 0\n"
);
Expand All @@ -144,7 +148,7 @@ mod tests {
let report = DuplicateReport {
analyzed_files: 2,
analyzed_extensions: vec!["ts".to_string(), "js".to_string()],
scanned_files: None,
analyzed_file_paths: None,
timings: None,
duplicate_blocks: vec![DuplicateBlock {
normalized_lines: vec!["return value;".to_string()],
Expand Down Expand Up @@ -181,7 +185,7 @@ mod tests {
let report = DuplicateReport {
analyzed_files: 0,
analyzed_extensions: vec!["ts".to_string(), "js".to_string(), "rs".to_string()],
scanned_files: None,
analyzed_file_paths: None,
timings: None,
duplicate_blocks: Vec::new(),
};
Expand All @@ -194,7 +198,7 @@ mod tests {
let report = DuplicateReport {
analyzed_files: 2,
analyzed_extensions: vec!["ts".to_string()],
scanned_files: None,
analyzed_file_paths: None,
timings: None,
duplicate_blocks: vec![DuplicateBlock {
normalized_lines: vec!["return value;".to_string()],
Expand Down Expand Up @@ -224,23 +228,23 @@ mod tests {
}

#[test]
fn renders_scanned_file_list_only_in_verbose_mode() {
fn renders_analyzed_file_list_only_in_verbose_mode() {
let report = DuplicateReport {
analyzed_files: 2,
analyzed_extensions: vec!["ts".to_string()],
scanned_files: Some(vec![
analyzed_file_paths: Some(vec![
PathBuf::from("src/a.ts"),
PathBuf::from("src/nested/b.ts"),
]),
timings: None,
duplicate_blocks: Vec::new(),
};
let quiet_output = render_duplicate_report(&report, false);
assert!(!quiet_output.contains("Files scanned:"));
assert!(!quiet_output.contains("Files analyzed:"));
let verbose_output = render_duplicate_report(&report, true);
assert!(verbose_output.contains(
"Number of files scanned: 2\n\
Files scanned:\n\
"Number of files analyzed: 2\n\
Files analyzed:\n\
- src/a.ts\n\
- src/nested/b.ts\n\
Analyzed extensions: ts"
Expand All @@ -252,7 +256,7 @@ mod tests {
let report = DuplicateReport {
analyzed_files: 1,
analyzed_extensions: vec!["ts".to_string()],
scanned_files: None,
analyzed_file_paths: None,
timings: Some(DuplicateReportTimings {
discovery: Duration::from_micros(1_234),
file_processing: Duration::from_micros(12_345),
Expand Down
Loading