artifactize-foundation 0.27.1

Native file, path and executable primitives for Artifactize.
Documentation
use super::*;
use tokio::io::{AsyncWriteExt, BufReader, duplex};

#[test]
fn decodes_unicode_program_output() {
    assert_eq!(
        output_text("agy 1.5.1 — ready\n".as_bytes()),
        "agy 1.5.1 — ready\n"
    );
}

// Invalid UTF-8 in Unix output has no portable code page fallback.
#[cfg(unix)]
#[test]
fn unix_keeps_line_endings_and_replaces_invalid_bytes() {
    assert_eq!(output_text(b"x\xff\r\n"), "x\u{fffd}\r\n");
}

// A portable reader can receive wide and code-page output from Windows tools.
#[cfg(windows)]
#[test]
fn windows_decodes_utf16_and_console_text() {
    for bytes in [
        &b"\xff\xfea\0g\0y\0\r\0\n\0"[..],
        &b"\xfe\xff\0a\0g\0y\0\r\0\n"[..],
        &b"a\0g\0y\0\r\0\n\0"[..],
        &b"\0a\0g\0y\0\r\0\n"[..],
        &b"\xef\xbb\xbfagy\r\n"[..],
    ] {
        assert_eq!(output_text(bytes), "agy\n");
    }
    // Use a fixed OEM page so this test does not depend on the caller's console settings.
    const US_OEM: u32 = 437;
    assert_eq!(
        windows::code_page_text(b"caf\x82", US_OEM).as_deref(),
        Some("café")
    );
}

#[tokio::test]
async fn reads_bounded_lines_without_losing_a_cancelled_partial_read() {
    let (mut child, pipe) = duplex(64);
    let mut lines = ProgramLines::new(BufReader::new(pipe), 8);
    child.write_all(b"ag").await.unwrap();
    tokio::select! {
        biased;
        _ = lines.next_line() => panic!("returned an unfinished line"),
        () = std::future::ready(()) => {}
    }
    child.write_all(b"y\nready\nlast").await.unwrap();
    drop(child);
    assert_eq!(lines.next_line().await.unwrap().as_deref(), Some("agy\n"));
    assert_eq!(lines.next_line().await.unwrap().as_deref(), Some("ready\n"));
    assert_eq!(lines.next_line().await.unwrap().as_deref(), Some("last"));
    assert_eq!(lines.next_line().await.unwrap(), None);

    let mut too_long = ProgramLines::new(&b"too long\n"[..], 8);
    assert!(matches!(
        too_long.next_line().await,
        Err(ProgramLineError::TooLong)
    ));
}

// Only Windows subprocess output uses UTF-16 line framing.
#[cfg(windows)]
#[tokio::test]
async fn wide_lines_consume_whole_code_units_and_keep_the_stream_encoding() {
    let bytes = b"\xff\xfe\x0a\x01\r\0\n\0\xe9\0\n\0a\0";
    let mut lines = ProgramLines::new(BufReader::with_capacity(1, &bytes[..]), 16);
    assert_eq!(lines.next_line().await.unwrap().as_deref(), Some("Ċ\n"));
    assert_eq!(lines.next_line().await.unwrap().as_deref(), Some("é\n"));
    assert_eq!(lines.next_line().await.unwrap().as_deref(), Some("a"));
    assert_eq!(lines.next_line().await.unwrap(), None);
}