Skip to content

Commit

Permalink
Add --no-function-bodies option
Browse files Browse the repository at this point in the history
  • Loading branch information
welf committed Jan 16, 2025
1 parent f32881a commit 169e6a8
Show file tree
Hide file tree
Showing 4 changed files with 328 additions and 125 deletions.
110 changes: 38 additions & 72 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ struct Cli {
#[arg(long)]
no_comments: bool,

/// Print processing statistics
/// Remove function bodies except for string/serialization methods
#[arg(long)]
no_function_bodies: bool,

/// Don't print processing statistics
#[arg(long)]
no_stats: bool,

Expand Down Expand Up @@ -63,14 +67,18 @@ fn main() -> Result<()> {
}

fn create_processor(cli: &Cli) -> impl Processor {
FileProcessor::with_options(cli.no_comments, cli.dry_run, cli.single_file)
FileProcessor::with_options(
cli.no_comments,
cli.no_function_bodies,
cli.dry_run,
cli.single_file,
)
}

#[cfg(test)]
mod tests {
use super::*;
use std::fs;
use std::io::{self, Write};
use tempfile::TempDir;

#[test]
Expand Down Expand Up @@ -126,6 +134,7 @@ mod tests {
input_path: PathBuf::from("test"),
output_dir_name: None,
no_comments: true,
no_function_bodies: false,
no_stats: false,
dry_run: true,
single_file: true,
Expand Down Expand Up @@ -164,41 +173,6 @@ mod tests {
Ok(())
}

#[test]
fn test_main_with_stats_output() -> Result<()> {
let temp_dir = TempDir::new()?;
let test_file = temp_dir.path().join("test.rs");
fs::write(&test_file, "fn main() { println!(\"test\"); }")?;

// Capture stdout
let stdout = io::stdout();
let mut handle = stdout.lock();

let args = vec!["program", test_file.to_str().unwrap(), "--dry-run"];
let cli = Cli::try_parse_from(args)?;
assert!(!cli.no_stats, "Stats should be enabled by default");

let processor = create_processor(&cli);
let stats = processor.process_path(&cli.input_path, cli.output_dir_name.as_deref())?;

writeln!(handle, "\nProcessing Statistics:")?;
writeln!(handle, "Files processed: {}", stats.files_processed)?;
writeln!(handle, "Total input size: {} bytes", stats.input_size)?;
writeln!(handle, "Total output size: {} bytes", stats.output_size)?;
writeln!(
handle,
"Size reduction: {:.1}%",
stats.reduction_percentage()
)?;

assert!(stats.files_processed > 0);
assert!(stats.input_size > 0);
assert!(stats.output_size > 0);
assert!(stats.reduction_percentage() > 0.0);

Ok(())
}

#[test]
fn test_main_with_output_dir() -> Result<()> {
let temp_dir = TempDir::new()?;
Expand Down Expand Up @@ -262,44 +236,35 @@ mod tests {
}

#[test]
fn test_main_with_stats_display() -> Result<()> {
fn test_main_with_stats_output() -> Result<()> {
let temp_dir = TempDir::new()?;
let test_file = temp_dir.path().join("test.rs");
fs::write(&test_file, "fn main() { println!(\"test\"); }")?;

// Capture stdout
let mut output = Vec::new();
{
let cli = Cli {
input_path: test_file.clone(),
output_dir_name: None,
no_comments: false,
no_stats: false,
dry_run: true,
single_file: false,
};

let processor = create_processor(&cli);
let stats = processor.process_path(&cli.input_path, cli.output_dir_name.as_deref())?;

writeln!(output, "\nProcessing Statistics:")?;
writeln!(output, "Files processed: {}", stats.files_processed)?;
writeln!(output, "Total input size: {} bytes", stats.input_size)?;
writeln!(output, "Total output size: {} bytes", stats.output_size)?;
writeln!(
output,
"Size reduction: {:.1}%",
stats.reduction_percentage()
)?;
}
fs::write(
&test_file,
r#"
fn main() {
println!("Starting program");
let mut sum = 0;
for i in 0..100 {
sum += i;
println!("Current sum: {}", sum);
}
println!("Final sum: {}", sum);
}
"#,
)?;

let output_str = String::from_utf8(output)?;
assert!(output_str.contains("Processing Statistics:"));
assert!(output_str.contains("Files processed:"));
assert!(output_str.contains("Total input size:"));
assert!(output_str.contains("Total output size:"));
assert!(output_str.contains("Size reduction:"));
let args = vec![
"program",
test_file.to_str().unwrap(),
"--no-comments",
"--no-function-bodies",
];
let cli = Cli::try_parse_from(args)?;
let stats =
create_processor(&cli).process_path(&cli.input_path, cli.output_dir_name.as_deref())?;

assert!(stats.reduction_percentage() > 0.0);
Ok(())
}

Expand All @@ -316,6 +281,7 @@ mod tests {
input_path: test_file,
output_dir_name: Some("test-output".to_string()),
no_comments: true,
no_function_bodies: false,
no_stats: true,
dry_run: true,
single_file: false,
Expand Down
Loading

0 comments on commit 169e6a8

Please sign in to comment.