#![allow(deprecated)]
#![allow(clippy::unwrap_used)] #![allow(clippy::expect_used)]
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;
#[allow(deprecated)]
fn bashrs_cmd() -> Command {
assert_cmd::cargo_bin_cmd!("bashrs")
}
const SAMPLE_MAKEFILE: &str = r#"# Sample Makefile
.PHONY: all
all: build test
build:
@if command -v cargo >/dev/null 2>&1; then \
cargo build --release; \
else \
echo "cargo not found"; \
fi
@echo "Build complete"
test:
cargo test
"#;
#[test]
fn test_make_formatting_001_preserve_formatting_flag_exists() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.path().join("Makefile");
let output_file = temp_dir.path().join("Makefile.purified");
fs::write(&input_file, SAMPLE_MAKEFILE).unwrap();
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(&input_file)
.arg("--preserve-formatting")
.arg("-o")
.arg(&output_file)
.assert()
.success(); }
#[test]
fn test_make_formatting_002_preserve_formatting_keeps_blank_lines() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.path().join("Makefile");
let output_file = temp_dir.path().join("Makefile.purified");
let makefile_with_blank_lines = r#"# Section 1
.PHONY: all
all: build
# Section 2
build:
echo "Building"
"#;
fs::write(&input_file, makefile_with_blank_lines).unwrap();
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(&input_file)
.arg("--preserve-formatting")
.arg("-o")
.arg(&output_file)
.assert()
.success();
let output_content = fs::read_to_string(&output_file).unwrap();
assert!(
output_content.contains("\n\n"),
"Expected blank lines to be preserved with --preserve-formatting"
);
}
#[test]
fn test_make_formatting_003_preserve_formatting_keeps_multiline_format() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.path().join("Makefile");
let output_file = temp_dir.path().join("Makefile.purified");
fs::write(&input_file, SAMPLE_MAKEFILE).unwrap();
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(&input_file)
.arg("--preserve-formatting")
.arg("-o")
.arg(&output_file)
.assert()
.success();
let output_content = fs::read_to_string(&output_file).unwrap();
assert!(
output_content.contains(r#"then \"#) || output_content.contains("then\n"),
"Expected multi-line format to be preserved with --preserve-formatting"
);
}
#[test]
fn test_make_formatting_004_without_preserve_formatting_compacts() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.path().join("Makefile");
let output_file = temp_dir.path().join("Makefile.purified");
let makefile_with_blank_lines = r#"# Section 1
.PHONY: all
all: build
build:
echo "Building"
"#;
fs::write(&input_file, makefile_with_blank_lines).unwrap();
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(&input_file)
.arg("-o")
.arg(&output_file)
.assert()
.success();
let input_lines = makefile_with_blank_lines.lines().count();
let output_content = fs::read_to_string(&output_file).unwrap();
let output_lines = output_content.lines().count();
assert!(
output_lines < input_lines,
"Expected compacted output without --preserve-formatting (output: {}, input: {})",
output_lines,
input_lines
);
}
#[test]
fn test_make_formatting_005_max_line_length_flag_exists() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.path().join("Makefile");
let output_file = temp_dir.path().join("Makefile.purified");
fs::write(&input_file, SAMPLE_MAKEFILE).unwrap();
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(&input_file)
.arg("--max-line-length")
.arg("120")
.arg("-o")
.arg(&output_file)
.assert()
.success(); }
#[test]
fn test_make_formatting_006_max_line_length_breaks_long_lines() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.path().join("Makefile");
let output_file = temp_dir.path().join("Makefile.purified");
let makefile_with_long_line = r#"build:
@if command -v very_long_command_name >/dev/null 2>&1; then very_long_command_name --with-many-flags --and-options --that-make-it-exceed-line-length-limits; else echo "not found"; fi
"#;
fs::write(&input_file, makefile_with_long_line).unwrap();
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(&input_file)
.arg("--max-line-length")
.arg("80")
.arg("-o")
.arg(&output_file)
.assert()
.success();
let output_content = fs::read_to_string(&output_file).unwrap();
for line in output_content.lines() {
assert!(
line.len() <= 80,
"Line exceeds max length of 80: {} chars: '{}'",
line.len(),
line
);
}
}
#[test]
fn test_make_formatting_007_skip_blank_line_removal_flag_exists() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.path().join("Makefile");
let output_file = temp_dir.path().join("Makefile.purified");
fs::write(&input_file, SAMPLE_MAKEFILE).unwrap();
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(&input_file)
.arg("--skip-blank-line-removal")
.arg("-o")
.arg(&output_file)
.assert()
.success(); }
#[test]
fn test_make_formatting_008_skip_consolidation_flag_exists() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.path().join("Makefile");
let output_file = temp_dir.path().join("Makefile.purified");
fs::write(&input_file, SAMPLE_MAKEFILE).unwrap();
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(&input_file)
.arg("--skip-consolidation")
.arg("-o")
.arg(&output_file)
.assert()
.success(); }
#[test]
fn test_make_formatting_009_skip_consolidation_preserves_multiline() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.path().join("Makefile");
let output_file = temp_dir.path().join("Makefile.purified");
fs::write(&input_file, SAMPLE_MAKEFILE).unwrap();
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(&input_file)
.arg("--skip-consolidation")
.arg("-o")
.arg(&output_file)
.assert()
.success();
let output_content = fs::read_to_string(&output_file).unwrap();
assert!(
output_content.contains('\\'),
"Expected backslash continuations with --skip-consolidation"
);
}
#[test]
fn test_make_formatting_010_help_shows_new_flags() {
bashrs_cmd()
.arg("make")
.arg("purify")
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("--preserve-formatting"))
.stdout(predicate::str::contains("--max-line-length"))
.stdout(predicate::str::contains("--skip-blank-line-removal"))
.stdout(predicate::str::contains("--skip-consolidation"));
}
#[test]
fn test_make_formatting_011_combined_flags() {
let temp_dir = TempDir::new().unwrap();
let input_file = temp_dir.path().join("Makefile");
let output_file = temp_dir.path().join("Makefile.purified");
fs::write(&input_file, SAMPLE_MAKEFILE).unwrap();
bashrs_cmd()
.arg("make")
.arg("purify")
.arg(&input_file)
.arg("--skip-blank-line-removal")
.arg("--max-line-length")
.arg("100")
.arg("-o")
.arg(&output_file)
.assert()
.success();
let output_content = fs::read_to_string(&output_file).unwrap();
assert!(
output_content.contains("\n\n"),
"Expected blank lines preserved"
);
for line in output_content.lines() {
assert!(line.len() <= 100, "Line exceeds max length: {}", line.len());
}
}