#![allow(deprecated)]
use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use tempfile::TempDir;
mod common;
fn create_test_project(temp_dir: &TempDir) -> std::path::PathBuf {
let project_root = temp_dir.path().to_path_buf();
let cargo_toml = r#"[package]
name = "test-cli-project"
version = "0.1.0"
edition = "2021"
[dependencies]
"#;
fs::write(project_root.join("Cargo.toml"), cargo_toml).expect("Failed to write Cargo.toml");
fs::create_dir(project_root.join("src")).expect("Failed to create src directory");
let lib_rs = "pub fn hello() -> String { \"Hello\".to_string() }";
fs::write(project_root.join("src/lib.rs"), lib_rs).expect("Failed to write lib.rs");
project_root
}
#[test]
fn test_cli_help_flag() {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("wasm-slim"))
.stdout(predicate::str::contains("Usage:"));
}
#[test]
fn test_cli_version_flag() {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("--version")
.assert()
.success()
.stdout(predicate::str::contains("wasm-slim"));
}
#[test]
fn test_cli_help_for_subcommands() {
let subcommands = vec!["analyze", "build", "init"];
for subcmd in subcommands {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg(subcmd)
.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains(subcmd));
}
}
#[test]
fn test_cli_init_command() {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
let project_root = create_test_project(&temp_dir);
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("init")
.current_dir(&project_root)
.assert()
.success();
assert!(
project_root.join(".wasm-slim.toml").exists(),
"Config file should be created by init command"
);
}
#[test]
fn test_cli_init_with_template() {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
let project_root = create_test_project(&temp_dir);
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("init")
.arg("--template")
.arg("balanced")
.current_dir(&project_root)
.assert()
.success();
assert!(project_root.join(".wasm-slim.toml").exists());
}
#[test]
fn test_cli_analyze_without_project() {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("analyze")
.current_dir(temp_dir.path())
.assert()
.failure()
.stderr(predicate::str::contains("Cargo.toml").or(predicate::str::contains("project")));
}
#[test]
fn test_cli_init_command_standalone() {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
let project_root = create_test_project(&temp_dir);
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("init")
.current_dir(&project_root)
.assert()
.success();
}
#[test]
fn test_cli_json_output_flag() {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
let _project_root = create_test_project(&temp_dir);
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
let result = cmd.arg("--version").output();
assert!(result.is_ok());
}
#[test]
fn test_cli_init_with_multiple_options() {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
let project_root = create_test_project(&temp_dir);
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("init")
.arg("--template")
.arg("balanced")
.current_dir(&project_root)
.assert()
.success();
}
#[test]
fn test_cli_init_overwrites_with_force() {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
let project_root = create_test_project(&temp_dir);
let mut cmd1 = Command::cargo_bin("wasm-slim").unwrap();
cmd1.arg("init")
.current_dir(&project_root)
.assert()
.success();
let mut cmd2 = Command::cargo_bin("wasm-slim").unwrap();
let result = cmd2
.arg("init")
.arg("--force")
.current_dir(&project_root)
.output();
assert!(result.is_ok());
}
#[test]
fn test_cli_invalid_subcommand() {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("nonexistent-command")
.assert()
.failure()
.stderr(predicate::str::contains("error").or(predicate::str::contains("invalid")));
}
#[test]
fn test_cli_invalid_flag() {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("--nonexistent-flag").assert().failure();
}
#[test]
fn test_cli_error_messages_are_helpful() {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("analyze")
.current_dir(temp_dir.path())
.assert()
.failure()
.stderr(predicate::str::is_empty().not());
}
#[test]
fn test_cli_completions_command() {
let shells = vec!["bash", "zsh", "fish"];
for shell in shells {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
let result = cmd.arg("completions").arg(shell).output();
assert!(
result.is_ok(),
"Completions command should run for {}",
shell
);
}
}
#[test]
fn test_cli_output_format_consistency() {
let mut cmd1 = Command::cargo_bin("wasm-slim").unwrap();
let output1 = cmd1.arg("--help").output().expect("Failed to run command");
let mut cmd2 = Command::cargo_bin("wasm-slim").unwrap();
let output2 = cmd2
.arg("--version")
.output()
.expect("Failed to run command");
assert!(output1.status.success());
assert!(output2.status.success());
assert!(!output1.stdout.is_empty() || !output1.stderr.is_empty());
assert!(!output2.stdout.is_empty() || !output2.stderr.is_empty());
}
#[test]
fn test_cli_exit_codes() {
let mut cmd_success = Command::cargo_bin("wasm-slim").unwrap();
cmd_success.arg("--version").assert().code(0);
let mut cmd_failure = Command::cargo_bin("wasm-slim").unwrap();
let result = cmd_failure.arg("--nonexistent-flag").assert().failure();
assert!(result.get_output().status.code().unwrap_or(0) != 0);
}
#[test]
fn test_cli_handles_ctrl_c_gracefully() {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
let result = cmd.arg("--help").output();
assert!(result.is_ok());
}
#[test]
fn test_cli_respects_current_directory() {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
let project_root = create_test_project(&temp_dir);
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("init")
.current_dir(&project_root)
.assert()
.success();
assert!(project_root.join(".wasm-slim.toml").exists());
}
#[test]
fn test_cli_help_shows_all_subcommands() {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("--help")
.assert()
.success()
.stdout(predicate::str::contains("init"))
.stdout(predicate::str::contains("analyze"))
.stdout(predicate::str::contains("build"));
}
#[test]
fn test_cli_version_format() {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("--version")
.assert()
.success()
.stdout(predicate::str::contains("wasm-slim"))
.stdout(predicate::str::is_match(r"\d+\.\d+\.\d+").unwrap());
}
#[test]
fn test_cli_stdin_not_required() {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("--help").assert().success();
}
#[test]
fn test_cli_parallel_execution() {
use std::thread;
let handles: Vec<_> = (0..3)
.map(|_| {
thread::spawn(|| {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
cmd.arg("--version").assert().success();
})
})
.collect();
for handle in handles {
handle.join().expect("Thread panicked");
}
}
#[test]
fn test_cli_unicode_in_arguments() {
let temp_dir = tempfile::tempdir().expect("Failed to create temp dir");
let project_root = create_test_project(&temp_dir);
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
let result = cmd
.arg("init")
.arg("--template")
.arg("日本語")
.current_dir(&project_root)
.output();
assert!(result.is_ok());
}
#[test]
fn test_cli_very_long_arguments() {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
let long_string = "a".repeat(1000);
let result = cmd.arg("--template").arg(long_string).output();
assert!(result.is_ok());
}
#[test]
fn test_cli_empty_arguments() {
let mut cmd = Command::cargo_bin("wasm-slim").unwrap();
let result = cmd.arg("").output();
assert!(result.is_ok());
}