mitm2openapi 0.5.2

Convert mitmproxy flow dumps and HAR files to OpenAPI 3.0 specs — fast Rust rewrite of mitmproxy2swagger
Documentation
use std::path::Path;

#[test]
fn successful_write_creates_file() {
    let dir = tempfile::TempDir::new().unwrap();
    let output = dir.path().join("spec.yaml");
    mitm2openapi::output::write_yaml("hello: world\n", &output).unwrap();
    assert_eq!(std::fs::read_to_string(&output).unwrap(), "hello: world\n");
}

#[test]
fn overwrite_existing_file() {
    let dir = tempfile::TempDir::new().unwrap();
    let output = dir.path().join("spec.yaml");
    mitm2openapi::output::write_yaml("v1: yes\n", &output).unwrap();
    mitm2openapi::output::write_yaml("v2: yes\n", &output).unwrap();
    assert_eq!(std::fs::read_to_string(&output).unwrap(), "v2: yes\n");
}

#[test]
fn creates_parent_directories() {
    let dir = tempfile::TempDir::new().unwrap();
    let output = dir.path().join("nested").join("dir").join("spec.yaml");
    mitm2openapi::output::write_yaml("nested: yes\n", &output).unwrap();
    assert_eq!(std::fs::read_to_string(&output).unwrap(), "nested: yes\n");
}

#[cfg(target_os = "linux")]
#[test]
fn partial_write_preserves_target() {
    use std::os::unix::fs::PermissionsExt;

    let dir = tempfile::TempDir::new().unwrap();
    let output = dir.path().join("spec.yaml");

    mitm2openapi::output::write_yaml("original: yes\n", &output).unwrap();
    let original = std::fs::read_to_string(&output).unwrap();
    assert_eq!(original, "original: yes\n");

    let mut perms = std::fs::metadata(dir.path()).unwrap().permissions();
    perms.set_mode(0o500);
    std::fs::set_permissions(dir.path(), perms).unwrap();

    let result = mitm2openapi::output::write_yaml("new: content\n", &output);
    assert!(result.is_err(), "write to read-only dir should fail");

    let mut perms = std::fs::metadata(dir.path()).unwrap().permissions();
    perms.set_mode(0o755);
    std::fs::set_permissions(dir.path(), perms).unwrap();

    let after = std::fs::read_to_string(&output).unwrap();
    assert_eq!(
        after, original,
        "original file must remain untouched on write failure"
    );
}

#[cfg(unix)]
#[test]
fn write_to_nonexistent_parent_fails_gracefully() {
    // /nonexistent at filesystem root requires privilege on Unix; on Windows a
    // leading slash without a drive letter is interpreted relative to the CWD,
    // which would succeed and invert the assertion. Gate to Unix.
    let result =
        mitm2openapi::output::write_yaml("test\n", Path::new("/nonexistent/dir/spec.yaml"));
    assert!(result.is_err());
}