2019-12-22 13:13:29 +03:00
|
|
|
use std::env;
|
2020-01-07 16:14:45 +03:00
|
|
|
use std::env::VarError;
|
2019-12-25 10:04:20 +03:00
|
|
|
|
2019-12-22 13:13:29 +03:00
|
|
|
#[macro_use]
|
2019-12-22 23:16:32 +03:00
|
|
|
extern crate itconfig;
|
2019-12-22 13:13:29 +03:00
|
|
|
|
2019-12-25 23:05:09 +03:00
|
|
|
|
|
|
|
#[test]
|
2020-01-16 22:05:35 +03:00
|
|
|
#[should_panic(expected = "Environment variable \"MISS_VARIABLE\" is missing")]
|
2019-12-25 23:05:09 +03:00
|
|
|
fn should_panic_if_miss_env_variable() {
|
|
|
|
config! {
|
|
|
|
MISS_VARIABLE: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg::init();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2019-12-22 13:13:29 +03:00
|
|
|
#[test]
|
|
|
|
fn one_variable() {
|
2019-12-25 23:05:09 +03:00
|
|
|
env::set_var("DEBUG", "t");
|
|
|
|
|
|
|
|
config! {
|
|
|
|
DEBUG: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg::init();
|
|
|
|
assert_eq!(cfg::DEBUG(), true);
|
|
|
|
env::remove_var("DEBUG");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn one_variable_with_default_value() {
|
2019-12-22 13:13:29 +03:00
|
|
|
config! {
|
|
|
|
DEBUG: bool => true,
|
|
|
|
}
|
|
|
|
|
2019-12-25 23:05:09 +03:00
|
|
|
cfg::init();
|
2019-12-22 13:13:29 +03:00
|
|
|
assert_eq!(cfg::DEBUG(), true);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-25 23:05:09 +03:00
|
|
|
fn few_variables_with_default_value() {
|
2019-12-22 13:13:29 +03:00
|
|
|
config! {
|
|
|
|
FOO: bool => true,
|
|
|
|
BAR: bool => false,
|
|
|
|
}
|
|
|
|
|
2019-12-25 23:05:09 +03:00
|
|
|
cfg::init();
|
2019-12-22 13:13:29 +03:00
|
|
|
assert_eq!(cfg::FOO(), true);
|
|
|
|
assert_eq!(cfg::BAR(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-25 23:05:09 +03:00
|
|
|
fn different_types_with_default_value() {
|
2019-12-22 13:13:29 +03:00
|
|
|
config! {
|
|
|
|
NUMBER: i32 => 30,
|
|
|
|
BOOL: bool => true,
|
2020-01-07 16:14:45 +03:00
|
|
|
STR: String => "str",
|
2019-12-22 13:13:29 +03:00
|
|
|
STRING: String => "string".to_string(),
|
|
|
|
}
|
|
|
|
|
2019-12-25 23:05:09 +03:00
|
|
|
cfg::init();
|
2019-12-22 13:13:29 +03:00
|
|
|
assert_eq!(cfg::NUMBER(), 30);
|
|
|
|
assert_eq!(cfg::BOOL(), true);
|
2020-01-07 16:14:45 +03:00
|
|
|
assert_eq!(cfg::STR(), "str".to_string());
|
|
|
|
assert_eq!(cfg::STRING(), "string".to_string());
|
2019-12-22 13:13:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-25 23:05:09 +03:00
|
|
|
fn convert_bool_type_value_from_env() {
|
2019-12-22 13:13:29 +03:00
|
|
|
env::set_var("T_BOOL", "t");
|
|
|
|
env::set_var("TRUE_BOOL", "true");
|
|
|
|
env::set_var("NUM_BOOL", "1");
|
|
|
|
env::set_var("ON_BOOL", "on");
|
|
|
|
env::set_var("CAMEL_CASE", "True");
|
|
|
|
env::set_var("FALSE_BOOL", "false");
|
|
|
|
|
|
|
|
config! {
|
|
|
|
T_BOOL: bool,
|
|
|
|
TRUE_BOOL: bool,
|
|
|
|
NUM_BOOL: bool,
|
|
|
|
ON_BOOL: bool,
|
|
|
|
CAMEL_CASE: bool,
|
|
|
|
FALSE_BOOL: bool,
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-12-25 23:05:09 +03:00
|
|
|
cfg::init();
|
2019-12-22 13:13:29 +03:00
|
|
|
assert_eq!(cfg::T_BOOL(), true);
|
|
|
|
assert_eq!(cfg::TRUE_BOOL(), true);
|
|
|
|
assert_eq!(cfg::NUM_BOOL(), true);
|
|
|
|
assert_eq!(cfg::ON_BOOL(), true);
|
|
|
|
assert_eq!(cfg::CAMEL_CASE(), true);
|
|
|
|
assert_eq!(cfg::FALSE_BOOL(), false);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2019-12-25 23:05:09 +03:00
|
|
|
fn convert_number_type_value_from_env() {
|
2019-12-22 13:13:29 +03:00
|
|
|
env::set_var("I8", "10");
|
|
|
|
env::set_var("I16", "10");
|
|
|
|
env::set_var("I32", "10");
|
|
|
|
env::set_var("I64", "10");
|
|
|
|
env::set_var("I128", "10");
|
|
|
|
env::set_var("ISIZE","10");
|
|
|
|
env::set_var("U8", "10");
|
|
|
|
env::set_var("U16", "10");
|
|
|
|
env::set_var("U32", "10");
|
|
|
|
env::set_var("U64", "10");
|
|
|
|
env::set_var("U128", "10");
|
|
|
|
env::set_var("USIZE","10");
|
|
|
|
env::set_var("F32", "10");
|
|
|
|
env::set_var("F64","10");
|
|
|
|
|
|
|
|
config! {
|
|
|
|
I8: i8,
|
|
|
|
I16: i16,
|
|
|
|
I32: i32,
|
|
|
|
I64: i64,
|
|
|
|
I128: i128,
|
|
|
|
ISIZE: isize,
|
|
|
|
U8: u8,
|
|
|
|
U16: u16,
|
|
|
|
U32: u32,
|
|
|
|
U64: u64,
|
|
|
|
U128: u128,
|
|
|
|
USIZE: usize,
|
|
|
|
F32: f32,
|
|
|
|
F64: f64,
|
|
|
|
}
|
|
|
|
|
2019-12-25 23:05:09 +03:00
|
|
|
cfg::init();
|
2019-12-22 13:13:29 +03:00
|
|
|
assert_eq!(cfg::I8(), 10);
|
|
|
|
assert_eq!(cfg::I16(), 10);
|
|
|
|
assert_eq!(cfg::I32(), 10);
|
|
|
|
assert_eq!(cfg::I64(), 10);
|
|
|
|
assert_eq!(cfg::ISIZE(), 10);
|
|
|
|
assert_eq!(cfg::U8(), 10);
|
|
|
|
assert_eq!(cfg::U16(), 10);
|
|
|
|
assert_eq!(cfg::U32(), 10);
|
|
|
|
assert_eq!(cfg::U64(), 10);
|
|
|
|
assert_eq!(cfg::USIZE(), 10);
|
|
|
|
assert_eq!(cfg::F32(), 10.0);
|
|
|
|
assert_eq!(cfg::F64(), 10.0);
|
|
|
|
}
|
2019-12-23 00:11:29 +03:00
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-12-25 23:05:09 +03:00
|
|
|
fn change_configuration_module_name() {
|
2019-12-23 00:11:29 +03:00
|
|
|
config! {
|
2019-12-23 14:56:19 +03:00
|
|
|
#![mod_name = custom_config_name]
|
2019-12-23 00:11:29 +03:00
|
|
|
|
|
|
|
DEBUG: bool => true,
|
|
|
|
}
|
|
|
|
|
2019-12-25 23:05:09 +03:00
|
|
|
custom_config_name::init();
|
2019-12-23 00:11:29 +03:00
|
|
|
assert_eq!(custom_config_name::DEBUG(), true);
|
2019-12-25 10:04:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
2019-12-25 23:05:09 +03:00
|
|
|
fn configuration_with_namespace() {
|
2020-01-07 14:54:17 +03:00
|
|
|
env::set_var("DB_HOST", "t");
|
2019-12-25 15:31:10 +03:00
|
|
|
|
2019-12-25 10:04:20 +03:00
|
|
|
config! {
|
|
|
|
DEBUG: bool => true,
|
|
|
|
|
2020-01-07 14:54:17 +03:00
|
|
|
DB {
|
2019-12-25 15:31:10 +03:00
|
|
|
HOST: bool,
|
2019-12-25 10:04:20 +03:00
|
|
|
PORT: bool => true,
|
|
|
|
USERNAME: bool => true,
|
|
|
|
}
|
2019-12-25 15:31:10 +03:00
|
|
|
|
|
|
|
APP {}
|
2019-12-25 10:04:20 +03:00
|
|
|
}
|
|
|
|
|
2019-12-25 23:05:09 +03:00
|
|
|
cfg::init();
|
2019-12-25 10:04:20 +03:00
|
|
|
assert_eq!(cfg::DEBUG(), true);
|
2020-01-07 14:54:17 +03:00
|
|
|
assert_eq!(cfg::DB::HOST(), true);
|
2019-12-25 15:31:10 +03:00
|
|
|
}
|
|
|
|
|
2020-01-10 23:42:41 +03:00
|
|
|
#[test]
|
|
|
|
fn configuration_with_nested_namespaces() {
|
|
|
|
config! {
|
|
|
|
FIRST {
|
|
|
|
SECOND {
|
|
|
|
THIRD {
|
|
|
|
FOO: u32 => 50,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg::init();
|
|
|
|
assert_eq!(cfg::FIRST::SECOND::THIRD::FOO(), 50);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "meta_namespace")]
|
|
|
|
#[test]
|
|
|
|
fn configuration_namespaces_with_custom_meta() {
|
|
|
|
config! {
|
|
|
|
FIRST {
|
|
|
|
#[cfg(feature = "meta_namespace")]
|
|
|
|
SECOND {
|
|
|
|
THIRD {
|
|
|
|
FOO: u32 => 50,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg::init();
|
|
|
|
assert_eq!(cfg::FIRST::SECOND::THIRD::FOO(), 50);
|
|
|
|
}
|
2019-12-25 15:31:10 +03:00
|
|
|
|
|
|
|
#[test]
|
2019-12-25 23:05:09 +03:00
|
|
|
fn configuration_variables_and_namespace_in_lowercase() {
|
|
|
|
env::set_var("TESTING", "t");
|
2019-12-25 15:31:10 +03:00
|
|
|
env::set_var("NAMESPACE_FOO", "t");
|
|
|
|
|
|
|
|
config! {
|
2019-12-25 23:05:09 +03:00
|
|
|
testing: bool,
|
2019-12-25 15:31:10 +03:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
foo: bool,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-25 23:05:09 +03:00
|
|
|
cfg::init();
|
|
|
|
assert_eq!(cfg::testing(), true);
|
2019-12-25 15:31:10 +03:00
|
|
|
assert_eq!(cfg::namespace::foo(), true);
|
2019-12-26 08:26:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn custom_environment_name_for_variable() {
|
|
|
|
env::set_var("MY_CUSTOM_NAME", "95");
|
|
|
|
|
|
|
|
config! {
|
|
|
|
#[env_name = "MY_CUSTOM_NAME"]
|
|
|
|
PER_PAGE: i32,
|
|
|
|
|
|
|
|
APP {
|
|
|
|
#[env_name = "MY_CUSTOM_NAME"]
|
|
|
|
RECIPES_PER_PAGE: i32,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg::init();
|
|
|
|
assert_eq!(cfg::PER_PAGE(), 95);
|
|
|
|
assert_eq!(cfg::APP::RECIPES_PER_PAGE(), 95);
|
2019-12-27 23:57:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn stranger_meta_data() {
|
|
|
|
env::set_var("MY_CUSTOM_NAME", "95");
|
|
|
|
|
|
|
|
config! {
|
|
|
|
#[cfg(feature = "postgres")]
|
|
|
|
#[env_name = "MY_CUSTOM_NAME"]
|
|
|
|
DATABASE_URL: String,
|
|
|
|
|
|
|
|
#[cfg(not(feature = "postgres"))]
|
|
|
|
#[env_name = "MY_CUSTOM_NAME"]
|
|
|
|
DATABASE_URL: i32,
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg::init();
|
|
|
|
#[cfg(not(feature = "postgres"))]
|
|
|
|
assert_eq!(cfg::DATABASE_URL(), 95);
|
|
|
|
|
|
|
|
#[cfg(feature = "postgres")]
|
|
|
|
assert_eq!(cfg::DATABASE_URL(), "95");
|
2020-01-06 23:12:06 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn setting_default_env_variable() {
|
|
|
|
config! {
|
2020-01-07 16:14:45 +03:00
|
|
|
DEFAULT_ENV_STRING: String => "localhost",
|
2020-01-06 23:12:06 +03:00
|
|
|
DEFAULT_ENV_BOOLEAN: bool => true,
|
|
|
|
DEFAULT_ENV_UINT: u32 => 40,
|
|
|
|
DEFAULT_ENV_FLOAT: f64 => 40.9,
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg::init();
|
|
|
|
|
|
|
|
assert_eq!(env::var("DEFAULT_ENV_STRING"), Ok("localhost".to_string()));
|
|
|
|
assert_eq!(env::var("DEFAULT_ENV_BOOLEAN"), Ok("true".to_string()));
|
|
|
|
assert_eq!(env::var("DEFAULT_ENV_UINT"), Ok("40".to_string()));
|
|
|
|
assert_eq!(env::var("DEFAULT_ENV_FLOAT"), Ok("40.9".to_string()));
|
|
|
|
}
|
|
|
|
|
2020-01-07 14:54:17 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn concatenate_environment_variables() {
|
|
|
|
env::set_var("POSTGRES_USERNAME", "user");
|
|
|
|
env::set_var("POSTGRES_PASSWORD", "pass");
|
|
|
|
env::set_var("POSTGRES_HOST", "localhost");
|
|
|
|
env::set_var("POSTGRES_DB", "test");
|
|
|
|
|
|
|
|
config! {
|
|
|
|
DATABASE_URL < (
|
|
|
|
"postgres://",
|
|
|
|
POSTGRES_USERNAME,
|
|
|
|
":",
|
|
|
|
POSTGRES_PASSWORD,
|
|
|
|
"@",
|
|
|
|
POSTGRES_HOST,
|
|
|
|
"/",
|
|
|
|
POSTGRES_DB,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg::init();
|
|
|
|
assert_eq!(cfg::DATABASE_URL(), String::from("postgres://user:pass@localhost/test"));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn setting_default_concat_env_variable() {
|
|
|
|
env::set_var("SETTING_DEFAULT_CONCAT_ENV_VARIABLE", "custom");
|
|
|
|
|
|
|
|
config! {
|
|
|
|
DEFAULT_CONCAT_ENV < (
|
|
|
|
"string",
|
|
|
|
"/",
|
|
|
|
SETTING_DEFAULT_CONCAT_ENV_VARIABLE,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg::init();
|
|
|
|
assert_eq!(env::var("DEFAULT_CONCAT_ENV"), Ok("string/custom".to_string()));
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[test]
|
2020-01-16 22:05:35 +03:00
|
|
|
#[should_panic(expected = "Environment variable \"PG_USERNAME\" is missing")]
|
2020-01-07 14:54:17 +03:00
|
|
|
fn concatenate_not_defined_environment_variables() {
|
|
|
|
config! {
|
|
|
|
DATABASE_URL < (
|
|
|
|
"postgres://",
|
|
|
|
PG_USERNAME,
|
|
|
|
":",
|
|
|
|
PG_PASSWORD,
|
|
|
|
"@",
|
|
|
|
PG_HOST,
|
|
|
|
"/",
|
|
|
|
PG_DB,
|
|
|
|
),
|
|
|
|
}
|
|
|
|
cfg::init();
|
|
|
|
}
|
|
|
|
|
2020-01-07 15:23:50 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn default_value_for_concatenate_env_parameter() {
|
|
|
|
config! {
|
|
|
|
CONCATENATED_DATABASE_URL < (
|
|
|
|
"postgres://",
|
2020-01-07 16:14:45 +03:00
|
|
|
NOT_DEFINED_PG_USERNAME => "user",
|
2020-01-07 15:23:50 +03:00
|
|
|
":",
|
2020-01-07 16:14:45 +03:00
|
|
|
NOT_DEFINED_PG_PASSWORD => "pass",
|
2020-01-07 15:23:50 +03:00
|
|
|
"@",
|
2020-01-07 16:14:45 +03:00
|
|
|
NOT_DEFINED_PG_HOST => "localhost:5432",
|
2020-01-07 15:23:50 +03:00
|
|
|
"/",
|
2020-01-07 16:14:45 +03:00
|
|
|
NOT_DEFINED_PG_DB => "test",
|
2020-01-07 15:23:50 +03:00
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg::init();
|
|
|
|
assert_eq!(
|
|
|
|
env::var("CONCATENATED_DATABASE_URL"),
|
|
|
|
Ok("postgres://user:pass@localhost:5432/test".to_string())
|
|
|
|
);
|
|
|
|
}
|
2020-01-07 16:14:45 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn envname_meta_for_concatenated_env_variable() {
|
|
|
|
config! {
|
|
|
|
#[env_name = "CUSTOM_CONCAT_ENVNAME"]
|
|
|
|
CONCAT_ENVVAR < (
|
|
|
|
"postgres://",
|
|
|
|
NOT_DEFINED_PG_USERNAME => "user",
|
|
|
|
":",
|
|
|
|
NOT_DEFINED_PG_PASSWORD => "pass",
|
|
|
|
"@",
|
|
|
|
NOT_DEFINED_PG_HOST => "localhost:5432",
|
|
|
|
"/",
|
|
|
|
NOT_DEFINED_PG_DB => "test",
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg::init();
|
|
|
|
assert_eq!(
|
|
|
|
env::var("CUSTOM_CONCAT_ENVNAME"),
|
|
|
|
Ok("postgres://user:pass@localhost:5432/test".to_string())
|
|
|
|
);
|
|
|
|
assert_eq!(env::var("CONCAT_ENVVAR"), Err(VarError::NotPresent));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn concatenated_environment_variable_in_namespace() {
|
|
|
|
config! {
|
|
|
|
CONCATED_NAMESPACE {
|
|
|
|
CONCAT_ENVVAR < (
|
|
|
|
"postgres://",
|
|
|
|
NOT_DEFINED_PG_USERNAME => "user",
|
|
|
|
":",
|
|
|
|
NOT_DEFINED_PG_PASSWORD => "pass",
|
|
|
|
"@",
|
|
|
|
NOT_DEFINED_PG_HOST => "localhost:5432",
|
|
|
|
"/",
|
|
|
|
NOT_DEFINED_PG_DB => "test",
|
|
|
|
),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cfg::init();
|
|
|
|
assert_eq!(
|
|
|
|
env::var("CONCATED_NAMESPACE_CONCAT_ENVVAR"),
|
|
|
|
Ok("postgres://user:pass@localhost:5432/test".to_string())
|
|
|
|
);
|
|
|
|
assert_eq!(env::var("CONCAT_ENVVAR"), Err(VarError::NotPresent));
|
|
|
|
}
|