python_script_runner 1.7.10

Execute Python scripts from Rust with path traversal prevention and environment isolation
Documentation
// FILE:    main.rs
// PURPOSE: Binary entry point for PYTHON_SCRIPT_RUNNER testing
// GOAL:    Test Python script execution
// RUNS TO:  Executable entry point via cargo run

// ═══════════════════════════════════════════════════════════════════════════════════════
// KEY FEATURES & DESIGN DECISIONS
// ═══════════════════════════════════════════════════════════════════════════════════════

// 1. Script Execution Tests
// - Execute script via path and inline temp file
// - Test error handling for missing files
// - WHY: Verifies Python executor correctness

// 2. Async Main
// - Uses tokio::main for async runtime
// - WHY: Required for async script execution

// ── IMPORTS ──────────────────────────────────────────────────────────────────
// Three groups: stdlib · third-party · internal

use std::io;
use std::io::Write;

// ── internal ─────────────────────────────────────────────────────────────────
use python_script_runner::python_script_runner::{run_python_script, PythonExecutor};

// ── MAIN ──────────────────────────────────────────────────────────────────────
// Goal: Test Python script execution

#[tokio::main]
async fn main() -> io::Result<()> {
    dotenvy::dotenv().ok();
    println!("Testing python_script_runner...");

    let executor = PythonExecutor::new();

    println!("Executing script via PythonExecutor (project-relative path)...");
    match executor.execute_script("scripts/hello.py").await {
        Ok(()) => println!("Script completed successfully"),
        Err(e) => println!("Script failed (expected if file absent): {}", e),
    }

    let mut tmp = tempfile::NamedTempFile::new().map_err(io::Error::other)?;
    writeln!(tmp, "print('Hello from run_python_script!')").map_err(io::Error::other)?;
    let tmp_path = tmp
        .path()
        .to_str()
        .ok_or_else(|| io::Error::other("non-UTF-8 temp path"))?
        .to_string();

    println!("Executing inline temp script via run_python_script...");
    match run_python_script(&tmp_path).await {
        Ok(()) => println!("Temp script completed successfully"),
        Err(e) => println!("Temp script failed: {}", e),
    }

    println!("Executing non-existent script (expect NotFound)...");
    let result = run_python_script("/tmp/does_not_exist_abc123.py").await;
    match &result {
        Err(e) if e.kind() == io::ErrorKind::NotFound => {
            println!("Correctly returned NotFound: {}", e);
        }
        Err(other) => println!("Unexpected error: {:?}", other),
        Ok(()) => println!("Unexpected success"),
    }

    println!("Done — all run_python_script operations completed successfully");
    Ok(())
}