dotenv-verbatim 0.1.0

A .env loader that takes the value verbatim: no expansion, no escapes, no inline comments; one malformed line is skipped, not the rest of the file
Documentation
//! The only test that touches the process environment. One test in one file on purpose:
//! `set_var` must not run concurrently with another test reading the environment.

use std::env;
use std::fs;
use std::path::Path;
use std::path::PathBuf;

const PRESET_KEY: &str = "DOTENV_VERBATIM_TEST_PRESET";
const FROM_FILE_KEY: &str = "DOTENV_VERBATIM_TEST_FROM_FILE";
const REMOVED_FILE_KEY: &str = "DOTENV_VERBATIM_TEST_REMOVED_FILE";

#[test]
fn load_keeps_preset_vars_fills_absent_ones_and_ignores_a_missing_file() {
    let env_file = temp_path("present");
    fs::write(
        &env_file,
        format!("{PRESET_KEY}=from-file\nnosep\n{FROM_FILE_KEY}=from-file\n"),
    )
    .unwrap();
    env::set_var(PRESET_KEY, "from-process");

    dotenv_verbatim::load(&env_file);

    assert_eq!(env::var(PRESET_KEY).unwrap(), "from-process");
    assert_eq!(env::var(FROM_FILE_KEY).unwrap(), "from-file");

    // Same content, but the file is gone by the time load runs: nothing happens, no panic.
    let removed_file = temp_path("removed");
    fs::write(&removed_file, format!("{REMOVED_FILE_KEY}=from-file\n")).unwrap();
    fs::remove_file(&removed_file).unwrap();

    dotenv_verbatim::load(&removed_file);

    assert!(env::var_os(REMOVED_FILE_KEY).is_none());
    assert!(!Path::new(&removed_file).exists());

    fs::remove_file(&env_file).unwrap();
}

fn temp_path(tag: &str) -> PathBuf {
    env::temp_dir().join(format!("dotenv-verbatim-{}-{tag}.env", std::process::id()))
}