use super::*;
#[test]
fn test_build_command() {
let temp_dir = TempDir::new().unwrap();
let input_path = temp_dir.path().join("test.rs");
let output_path = temp_dir.path().join("test.sh");
fs::write(&input_path, "fn main() { let x = 42; }").unwrap();
let config = Config {
target: ShellDialect::Posix,
verify: VerificationLevel::Basic,
emit_proof: false,
optimize: true,
strict_mode: false,
validation_level: None,
};
let result = build_command(&input_path, &output_path, config);
let _ = result; assert!(output_path.exists());
let output = fs::read_to_string(&output_path).unwrap();
assert!(output.contains("#!/bin/sh"));
assert!(output.contains("x='42'"));
}
#[test]
fn test_check_command() {
let temp_dir = TempDir::new().unwrap();
let input_path = temp_dir.path().join("test.rs");
fs::write(&input_path, "fn main() { let x = 42; }").unwrap();
let result = check_command(&input_path);
let _ = result;
fs::write(&input_path, "fn main() { unsafe { } }").unwrap();
let result = check_command(&input_path);
assert!(result.is_err());
}
#[test]
fn test_issue_84_check_detects_shell_script_by_extension() {
let temp_dir = TempDir::new().unwrap();
let input_path = temp_dir.path().join("script.sh");
fs::write(&input_path, "#!/bin/bash\necho 'Hello, World!'").unwrap();
let result = check_command(&input_path);
assert!(result.is_err());
let err_msg = format!("{}", result.unwrap_err());
assert!(err_msg.contains("shell script"));
assert!(err_msg.contains("bashrs lint"));
}
#[test]
fn test_issue_84_check_detects_shell_script_by_shebang() {
let temp_dir = TempDir::new().unwrap();
let input_path = temp_dir.path().join("script");
fs::write(&input_path, "#!/bin/bash\necho 'Hello, World!'").unwrap();
let result = check_command(&input_path);
assert!(result.is_err());
let err_msg = format!("{}", result.unwrap_err());
assert!(err_msg.contains("shell script"));
assert!(err_msg.contains("bashrs lint"));
}
#[test]
fn test_issue_84_check_detects_posix_sh_shebang() {
let temp_dir = TempDir::new().unwrap();
let input_path = temp_dir.path().join("script");
fs::write(&input_path, "#!/bin/sh\necho 'Hello'").unwrap();
let result = check_command(&input_path);
assert!(result.is_err());
let err_msg = format!("{}", result.unwrap_err());
assert!(err_msg.contains("shell script"));
}
#[test]
fn test_issue_84_check_allows_rs_files() {
let temp_dir = TempDir::new().unwrap();
let input_path = temp_dir.path().join("test.rs");
fs::write(&input_path, "fn main() { let x = 42; }").unwrap();
let result = check_command(&input_path);
if let Err(ref e) = result {
let err_msg = format!("{}", e);
assert!(
!err_msg.contains("shell script"),
"Should not detect .rs as shell script"
);
}
}
#[test]
fn test_init_command() {
let temp_dir = TempDir::new().unwrap();
let project_path = temp_dir.path();
let result = init_command(project_path, Some("test_project"));
let _ = result;
assert!(project_path.join("Cargo.toml").exists());
assert!(project_path.join("src").exists());
assert!(project_path.join("src/main.rs").exists());
assert!(project_path.join(".rash.toml").exists());
let cargo_toml = fs::read_to_string(project_path.join("Cargo.toml")).unwrap();
assert!(cargo_toml.contains("name = \"test_project\""));
}
#[test]
fn test_compile_command_self_extracting() {
let temp_dir = TempDir::new().unwrap();
let input_path = temp_dir.path().join("test.rs");
let output_path = temp_dir.path().join("test_self_extract.sh");
fs::write(&input_path, "fn main() { let msg = \"test\"; }").unwrap();
let config = Config {
target: ShellDialect::Posix,
verify: VerificationLevel::Basic,
emit_proof: false,
optimize: true,
validation_level: Some(ValidationLevel::Minimal),
strict_mode: false,
};
let result = handle_compile(
&input_path,
&output_path,
CompileRuntime::Dash,
true, false, ContainerFormatArg::Oci,
&config,
);
let _ = result; assert!(output_path.exists());
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let metadata = fs::metadata(&output_path).unwrap();
assert_eq!(metadata.permissions().mode() & 0o111, 0o111);
}
}
#[test]
fn test_verify_command() {
let temp_dir = TempDir::new().unwrap();
let rust_path = temp_dir.path().join("test.rs");
let shell_path = temp_dir.path().join("test.sh");
fs::write(&rust_path, "fn main() { let x = 42; }").unwrap();
let config = Config {
target: ShellDialect::Posix,
verify: VerificationLevel::Basic,
emit_proof: false,
optimize: true,
strict_mode: false,
validation_level: None,
};
let source = fs::read_to_string(&rust_path).unwrap();
let shell_code = crate::transpile(&source, &config).unwrap();
fs::write(&shell_path, &shell_code).unwrap();
let result = verify_command(
&rust_path,
&shell_path,
ShellDialect::Posix,
VerificationLevel::Basic,
);
let _ = result; }
#[test]
fn test_generate_proof() {
let temp_dir = TempDir::new().unwrap();
let proof_path = temp_dir.path().join("test.proof");
let config = Config {
target: ShellDialect::Bash,
verify: VerificationLevel::Strict,
emit_proof: true,
optimize: false,
strict_mode: false,
validation_level: None,
};
let result = generate_proof("fn main() {}", &proof_path, &config);
let _ = result; assert!(proof_path.exists());
let proof = fs::read_to_string(&proof_path).unwrap();
assert!(proof.contains("\"version\": \"1.0\""));
assert!(proof.contains("\"verification_level\": \"Strict\""));
assert!(proof.contains("\"target\": \"Bash\""));
}
#[test]
fn test_normalize_shell_script() {
let script = r#"#!/bin/sh
# This is a comment
x=42
# Another comment
y=43
"#;
let normalized = normalize_shell_script(script);
assert_eq!(normalized, "x=42\ny=43");
}
#[test]
fn test_execute_command_integration() {
use crate::cli::args::{Cli, Commands};
let temp_dir = TempDir::new().unwrap();
let input_path = temp_dir.path().join("test.rs");
let output_path = temp_dir.path().join("test.sh");
fs::write(&input_path, "fn main() { let x = 42; }").unwrap();
let cli = Cli {
command: Commands::Build {
input: input_path.clone(),
output: output_path.clone(),
emit_proof: false,
no_optimize: false,
},
verify: VerificationLevel::Basic,
target: ShellDialect::Posix,
validation: crate::validation::ValidationLevel::Minimal,
strict: false,
verbose: false,
};
let result = execute_command(cli);
if result.is_ok() {
assert!(output_path.exists());
}
}
#[test]
fn test_error_handling() {
let result = check_command(&PathBuf::from("/nonexistent/file.rs"));
assert!(result.is_err());
let temp_dir = TempDir::new().unwrap();
let input_path = temp_dir.path().join("test.rs");
fs::write(&input_path, "fn main() {}").unwrap();
let config = Config::default();
let result = build_command(
&input_path,
&PathBuf::from("/nonexistent/dir/output.sh"),
config,
);
assert!(result.is_err());
}
#[test]
fn test_init_command_existing_directory_with_files() {
let temp_dir = TempDir::new().unwrap();
let project_path = temp_dir.path();
fs::write(project_path.join("existing.txt"), "existing content").unwrap();
let result = init_command(project_path, Some("test_project"));
let _ = result;
assert!(project_path.join("existing.txt").exists());
assert!(project_path.join("Cargo.toml").exists());
}
#[test]
fn test_init_command_no_name() {
let temp_dir = TempDir::new().unwrap();
let result = init_command(temp_dir.path(), None);
let _ = result;
let cargo_toml = fs::read_to_string(temp_dir.path().join("Cargo.toml")).unwrap();
assert!(cargo_toml.contains("name ="));
}
#[test]
fn test_init_command_nested_path() {
let temp_dir = TempDir::new().unwrap();
let nested = temp_dir.path().join("nested/deep/path");
fs::create_dir_all(&nested).unwrap();
let result = init_command(&nested, Some("nested_project"));
let _ = result;
assert!(nested.join("Cargo.toml").exists());
assert!(nested.join(".rash.toml").exists());
}
#[test]
fn test_init_command_creates_rash_config() {
let temp_dir = TempDir::new().unwrap();
init_command(temp_dir.path(), Some("test")).unwrap();
let rash_config = temp_dir.path().join(".rash.toml");
assert!(rash_config.exists());
let config_content = fs::read_to_string(&rash_config).unwrap();
assert!(config_content.contains("[transpiler]"));
}
#[test]
include!("command_tests_build_tests_build_comman_2.rs");