#![allow(deprecated)]
use assert_cmd::cargo::CommandCargoExt;
use assert_cmd::prelude::*;
use predicates::prelude::*;
use std::fs;
use std::path::PathBuf;
use std::process::Command;
use tempfile::TempDir;
fn setup_python_file(filename: &str, content: &str) -> (TempDir, PathBuf) {
let temp_dir = TempDir::new().expect("Failed to create temp dir");
let python_file = temp_dir.path().join(filename);
fs::write(&python_file, content).expect("Failed to write Python file");
(temp_dir, python_file)
}
#[test]
fn test_depyler_0380_compile_command_exists() {
Command::cargo_bin("depyler")
.expect("Failed to find depyler binary")
.args(["compile", "--help"])
.assert()
.success()
.stdout(predicate::str::contains("compile"));
}
#[test]
fn test_depyler_0380_compile_hello_world() {
let python_code = r#"
def main():
print("Hello, World!")
if __name__ == '__main__':
main()
"#;
let (_temp_dir, python_file) = setup_python_file("hello.py", python_code);
Command::cargo_bin("depyler")
.expect("Failed to find depyler binary")
.args(["compile", python_file.to_str().unwrap()])
.assert()
.success();
let binary_name = if cfg!(windows) { "hello.exe" } else { "hello" };
let binary_path = python_file.parent().unwrap().join(binary_name);
assert!(
binary_path.exists(),
"Binary should be created at {}",
binary_path.display()
);
Command::new(&binary_path)
.assert()
.success()
.stdout(predicate::str::contains("Hello, World!"));
}
#[test]
fn test_depyler_0380_compile_with_args() {
let python_code = r#"
import sys
def main():
if len(sys.argv) > 1:
print(f"Hello, {sys.argv[1]}!")
else:
print("Hello, World!")
if __name__ == '__main__':
main()
"#;
let (_temp_dir, python_file) = setup_python_file("greet.py", python_code);
Command::cargo_bin("depyler")
.expect("Failed to find depyler binary")
.args(["compile", python_file.to_str().unwrap()])
.assert()
.success();
let binary_name = if cfg!(windows) { "greet.exe" } else { "greet" };
let binary_path = python_file.parent().unwrap().join(binary_name);
Command::new(&binary_path)
.arg("Alice")
.assert()
.success()
.stdout(predicate::str::contains("Hello, Alice!"));
}
#[test]
fn test_depyler_0380_compile_with_output_flag() {
let python_code = r#"
def main():
print("Custom output!")
if __name__ == '__main__':
main()
"#;
let (_temp_dir, python_file) = setup_python_file("custom.py", python_code);
let output_path = python_file.parent().unwrap().join("my_binary");
Command::cargo_bin("depyler")
.expect("Failed to find depyler binary")
.args([
"compile",
python_file.to_str().unwrap(),
"-o",
output_path.to_str().unwrap(),
])
.assert()
.success();
let binary_path = if cfg!(windows) {
output_path.with_extension("exe")
} else {
output_path
};
assert!(binary_path.exists(), "Binary should exist at custom path");
}
#[test]
fn test_depyler_0380_compile_with_profile_release() {
let python_code = r#"
def fibonacci(n: int) -> int:
if n <= 1:
return n
return fibonacci(n - 1) + fibonacci(n - 2)
def main():
result = fibonacci(10)
print(f"fib(10) = {result}")
if __name__ == '__main__':
main()
"#;
let (_temp_dir, python_file) = setup_python_file("fib.py", python_code);
Command::cargo_bin("depyler")
.expect("Failed to find depyler binary")
.args([
"compile",
python_file.to_str().unwrap(),
"--profile",
"release",
])
.assert()
.success();
let binary_name = if cfg!(windows) { "fib.exe" } else { "fib" };
let binary_path = python_file.parent().unwrap().join(binary_name);
Command::new(&binary_path)
.assert()
.success()
.stdout(predicate::str::contains("fib(10) = 55"));
}
#[test]
fn test_depyler_0380_compile_missing_file_error() {
Command::cargo_bin("depyler")
.expect("Failed to find depyler binary")
.args(["compile", "/nonexistent/file.py"])
.assert()
.failure()
.stderr(predicate::str::contains("not found").or(predicate::str::contains("No such file")));
}
#[test]
fn test_depyler_0380_compile_invalid_python_error() {
let invalid_python = r#"
def main(
print("Missing closing paren")
"#;
let (_temp_dir, python_file) = setup_python_file("invalid.py", invalid_python);
Command::cargo_bin("depyler")
.expect("Failed to find depyler binary")
.args(["compile", python_file.to_str().unwrap()])
.assert()
.failure()
.stderr(predicate::str::contains("parse").or(predicate::str::contains("syntax")));
}