use std::env;
pub fn get_app_env() -> String {
env::var("APP_ENV").unwrap_or_else(|_| {
#[cfg(debug_assertions)]
{
"development".to_string()
}
#[cfg(not(debug_assertions))]
{
"production".to_string()
}
})
}
#[inline]
pub fn app_env() -> String {
get_app_env()
}
pub fn is_development() -> bool {
app_env() == "development"
}
pub fn is_production() -> bool {
app_env() == "production"
}
#[cfg(test)]
mod tests {
use super::*;
use std::env;
#[test]
fn test_get_app_env_function() {
let env_val = get_app_env();
assert!(env_val == "development" || env_val == "production");
assert!(!env_val.is_empty());
}
#[test]
fn test_app_env_function() {
let env_val = app_env();
assert!(env_val == "development" || env_val == "production");
assert!(!env_val.is_empty());
}
#[test]
fn test_app_env_with_explicit_env_var() {
unsafe {
env::set_var("APP_ENV", "testing");
}
assert_eq!(app_env(), "testing");
unsafe {
env::remove_var("APP_ENV");
}
}
#[test]
fn test_app_env_debug_mode() {
unsafe {
env::remove_var("APP_ENV");
}
#[cfg(debug_assertions)]
{
assert_eq!(app_env(), "development");
}
}
#[test]
fn test_app_env_release_mode() {
unsafe {
env::remove_var("APP_ENV");
}
#[cfg(not(debug_assertions))]
{
assert_eq!(app_env(), "production");
}
}
#[test]
fn test_is_development() {
unsafe {
env::set_var("APP_ENV", "development");
}
assert!(is_development());
assert!(!is_production());
unsafe {
env::remove_var("APP_ENV");
}
}
#[test]
fn test_is_production() {
unsafe {
env::set_var("APP_ENV", "production");
}
assert!(is_production());
assert!(!is_development());
unsafe {
env::remove_var("APP_ENV");
}
}
#[test]
fn test_environment_mutually_exclusive() {
assert!(is_development() != is_production());
}
}