Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make dotenv tests serial since they define env vars. #23

Merged
merged 3 commits into from
Jan 16, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -18,7 +18,4 @@ jobs:
override: true

- name: Run tests
# since we're playing with environment variables
# and these are shared between executions, we need
# to run one test at a time
run: cargo test -- --test-threads=1
run: cargo test
282 changes: 232 additions & 50 deletions Cargo.lock
8 changes: 5 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -4,12 +4,14 @@ version = "0.0.0"
edition = "2021"

[dependencies]
anyhow = "1.0.94"
anyhow = "1.0.95"
clap = { version = "4.5.26", features = ["derive"] }
dirs = "6.0.0"
libc = "0.2.169"
tempfile = "3.14.0"
which = "7.0.1"

[dev-dependencies]
serial_test = "3.2.0"
tempfile = "3.15.0"

[profile.release]
opt-level = "z" # Optimize for size.
11 changes: 11 additions & 0 deletions src/env_parser.rs
Original file line number Diff line number Diff line change
@@ -90,8 +90,10 @@ fn strip_quotes(s: &str) -> &str {
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;

#[test]
#[serial]
fn test_parse_env_line() {
assert_eq!(
parse_env_line("KEY=VALUE"),
@@ -107,6 +109,7 @@ mod tests {
}

#[test]
#[serial]
fn test_parse_env_str_basic() -> Result<()> {
let input = r#"
KEY=VALUE
@@ -120,6 +123,7 @@ mod tests {
}

#[test]
#[serial]
fn test_parse_env_str_comments_and_spaces() -> Result<()> {
let input = r#"
# A comment
@@ -138,6 +142,7 @@ mod tests {
}

#[test]
#[serial]
fn test_parse_env_str_shebang_and_complex_comment_lines() -> Result<()> {
let input = r#"
#!/usr/bin/env bash # this line should be ignored if present
@@ -153,6 +158,7 @@ mod tests {
}

#[test]
#[serial]
fn test_parse_env_str_invalid_lines() -> Result<()> {
let input = r#"
KEY=VALUE
@@ -171,6 +177,7 @@ mod tests {
}

#[test]
#[serial]
fn test_parse_env_str_quoted_values() -> Result<()> {
let input = r#"
KEY="some value with spaces"
@@ -192,6 +199,7 @@ mod tests {
}

#[test]
#[serial]
fn test_parse_env_str_values_with_equals() -> Result<()> {
let input = r#"
KEY=VALUE=WITH=EQUALS
@@ -208,6 +216,7 @@ mod tests {
}

#[test]
#[serial]
fn test_parse_env_str_whitespace_around_equals() -> Result<()> {
let input = r#"
KEY = VALUE
@@ -221,6 +230,7 @@ mod tests {
}

#[test]
#[serial]
fn test_parse_env_str_empty_key_or_value() -> Result<()> {
let input = r#"
=VALUE
@@ -238,6 +248,7 @@ mod tests {
}

#[test]
#[serial]
fn test_parse_env_str_complex_comments() -> Result<()> {
let input = r#"
# Initial comment
9 changes: 9 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -183,10 +183,12 @@ fn get_named_env_file(name: &str) -> Result<Option<PathBuf>> {
#[cfg(test)]
mod tests {
use super::*;
use serial_test::serial;
use std::{io::Write, path};
use tempfile::NamedTempFile;

#[test]
#[serial]
fn test_is_truthy() {
assert!(is_truthy("true"));
assert!(is_truthy("True"));
@@ -204,6 +206,7 @@ mod tests {
}

#[test]
#[serial]
fn test_dotenv_strict_sets_strict_mode() -> anyhow::Result<()> {
let mut file = NamedTempFile::new()?;
writeln!(file, "DOTENV_STRICT=true")?;
@@ -234,13 +237,15 @@ mod tests {
}

#[test]
#[serial]
fn test_clear_environment() {
env::set_var("TESTVAR", "VALUE");
clear_environment();
assert!(env::var("TESTVAR").is_err());
}

#[test]
#[serial]
fn test_strict_mode_removes_unlisted_vars() -> anyhow::Result<()> {
// Set some environment variables that should NOT persist in strict mode
env::set_var("UNSAFE_VAR", "123");
@@ -292,6 +297,7 @@ mod tests {
}

#[test]
#[serial]
fn test_non_strict_mode_keeps_existing_vars() -> anyhow::Result<()> {
// Simulate existing environment variable
env::set_var("EXISTING_VAR", "EXISTING_VALUE");
@@ -324,6 +330,7 @@ mod tests {
}

#[test]
#[serial]
fn test_missing_environment_file() -> anyhow::Result<()> {
let non_existent = PathBuf::from("this_file_does_not_exist.env");

@@ -338,6 +345,7 @@ mod tests {
}

#[test]
#[serial]
fn test_env_file_strict_mode_applied() -> anyhow::Result<()> {
let mut file = NamedTempFile::new()?;
writeln!(file, "DOTENV_STRICT=true")?;
@@ -368,6 +376,7 @@ mod tests {
}

#[test]
#[serial]
fn test_env_overrides_system_in_non_strict_mode() -> anyhow::Result<()> {
// Set a system var
env::set_var("FOO", "SYSTEM_VALUE");