2022-06-18 21:56:31 -07:00
|
|
|
use super::*;
|
2020-07-19 05:01:46 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dotenv() {
|
2023-01-03 22:31:56 -08:00
|
|
|
Test::new()
|
|
|
|
.write(".env", "KEY=ROOT")
|
|
|
|
.write("sub/.env", "KEY=SUB")
|
|
|
|
.write("sub/justfile", "default:\n\techo KEY=${KEY:-unset}")
|
|
|
|
.args(["sub/default"])
|
|
|
|
.stdout("KEY=unset\n")
|
|
|
|
.stderr("echo KEY=${KEY:-unset}\n")
|
|
|
|
.run();
|
2020-07-19 05:01:46 -07:00
|
|
|
}
|
2021-03-28 22:38:07 -07:00
|
|
|
|
2024-05-30 16:12:07 -07:00
|
|
|
#[test]
|
|
|
|
fn set_false() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
r#"
|
|
|
|
set dotenv-load := false
|
|
|
|
|
|
|
|
@foo:
|
|
|
|
if [ -n "${DOTENV_KEY+1}" ]; then echo defined; else echo undefined; fi
|
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
|
|
|
.stdout("undefined\n")
|
|
|
|
.run();
|
2021-03-28 22:38:07 -07:00
|
|
|
}
|
|
|
|
|
2024-05-30 16:12:07 -07:00
|
|
|
#[test]
|
|
|
|
fn set_implicit() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
set dotenv-load
|
2021-03-28 22:38:07 -07:00
|
|
|
|
2024-05-30 16:12:07 -07:00
|
|
|
foo:
|
|
|
|
echo $DOTENV_KEY
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
|
|
|
.stdout("dotenv-value\n")
|
|
|
|
.stderr("echo $DOTENV_KEY\n")
|
|
|
|
.run();
|
2021-03-28 22:38:07 -07:00
|
|
|
}
|
|
|
|
|
2024-05-30 16:12:07 -07:00
|
|
|
#[test]
|
|
|
|
fn set_true() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
set dotenv-load := true
|
2021-03-28 22:38:07 -07:00
|
|
|
|
2024-05-30 16:12:07 -07:00
|
|
|
foo:
|
|
|
|
echo $DOTENV_KEY
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
|
|
|
.stdout("dotenv-value\n")
|
|
|
|
.stderr("echo $DOTENV_KEY\n")
|
|
|
|
.run();
|
2021-03-30 17:30:32 -07:00
|
|
|
}
|
|
|
|
|
2021-07-28 00:33:44 -07:00
|
|
|
#[test]
|
2022-02-01 19:16:35 -08:00
|
|
|
fn no_warning() {
|
2021-07-28 00:33:44 -07:00
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
2024-05-30 16:12:07 -07:00
|
|
|
foo:
|
|
|
|
echo ${DOTENV_KEY:-unset}
|
|
|
|
",
|
2021-07-28 00:33:44 -07:00
|
|
|
)
|
2024-05-30 16:12:07 -07:00
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
2022-02-01 19:16:35 -08:00
|
|
|
.stdout("unset\n")
|
|
|
|
.stderr("echo ${DOTENV_KEY:-unset}\n")
|
2021-07-28 00:33:44 -07:00
|
|
|
.run();
|
|
|
|
}
|
2021-08-08 22:37:35 -07:00
|
|
|
|
|
|
|
#[test]
|
2024-05-30 16:12:07 -07:00
|
|
|
fn dotenv_required() {
|
2021-08-08 22:37:35 -07:00
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
2024-05-30 16:12:07 -07:00
|
|
|
set dotenv-required
|
|
|
|
|
|
|
|
foo:
|
|
|
|
",
|
2021-08-08 22:37:35 -07:00
|
|
|
)
|
2024-05-30 16:12:07 -07:00
|
|
|
.stderr("error: Dotenv file not found\n")
|
|
|
|
.status(1)
|
2021-08-08 22:37:35 -07:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn path_resolves() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
2024-05-30 16:12:07 -07:00
|
|
|
foo:
|
|
|
|
@echo $JUST_TEST_VARIABLE
|
|
|
|
",
|
2021-08-08 22:37:35 -07:00
|
|
|
)
|
|
|
|
.tree(tree! {
|
|
|
|
subdir: {
|
2024-01-26 18:10:33 -08:00
|
|
|
".env": "JUST_TEST_VARIABLE=bar"
|
2021-08-08 22:37:35 -07:00
|
|
|
}
|
|
|
|
})
|
2023-01-03 22:31:56 -08:00
|
|
|
.args(["--dotenv-path", "subdir/.env"])
|
2021-08-08 22:37:35 -07:00
|
|
|
.stdout("bar\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filename_resolves() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
2024-05-30 16:12:07 -07:00
|
|
|
foo:
|
|
|
|
@echo $JUST_TEST_VARIABLE
|
|
|
|
",
|
2021-08-08 22:37:35 -07:00
|
|
|
)
|
|
|
|
.tree(tree! {
|
2024-01-26 18:10:33 -08:00
|
|
|
".env.special": "JUST_TEST_VARIABLE=bar"
|
2021-08-08 22:37:35 -07:00
|
|
|
})
|
2023-01-03 22:31:56 -08:00
|
|
|
.args(["--dotenv-filename", ".env.special"])
|
2021-08-08 22:37:35 -07:00
|
|
|
.stdout("bar\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filename_flag_overwrites_no_load() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
set dotenv-load := false
|
|
|
|
|
|
|
|
foo:
|
2024-01-26 18:10:33 -08:00
|
|
|
@echo $JUST_TEST_VARIABLE
|
2021-08-08 22:37:35 -07:00
|
|
|
",
|
|
|
|
)
|
|
|
|
.tree(tree! {
|
2024-01-26 18:10:33 -08:00
|
|
|
".env.special": "JUST_TEST_VARIABLE=bar"
|
2021-08-08 22:37:35 -07:00
|
|
|
})
|
2023-01-03 22:31:56 -08:00
|
|
|
.args(["--dotenv-filename", ".env.special"])
|
2021-08-08 22:37:35 -07:00
|
|
|
.stdout("bar\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn path_flag_overwrites_no_load() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
2024-05-30 16:12:07 -07:00
|
|
|
set dotenv-load := false
|
2021-08-08 22:37:35 -07:00
|
|
|
|
2024-05-30 16:12:07 -07:00
|
|
|
foo:
|
|
|
|
@echo $JUST_TEST_VARIABLE
|
|
|
|
",
|
2021-08-08 22:37:35 -07:00
|
|
|
)
|
|
|
|
.tree(tree! {
|
|
|
|
subdir: {
|
2024-01-26 18:10:33 -08:00
|
|
|
".env": "JUST_TEST_VARIABLE=bar"
|
2021-08-08 22:37:35 -07:00
|
|
|
}
|
|
|
|
})
|
2023-01-03 22:31:56 -08:00
|
|
|
.args(["--dotenv-path", "subdir/.env"])
|
2021-08-08 22:37:35 -07:00
|
|
|
.stdout("bar\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
2023-10-11 22:04:46 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_set_dotenv_filename_from_justfile() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
r#"
|
|
|
|
set dotenv-filename := ".env.special"
|
|
|
|
|
|
|
|
foo:
|
2024-01-26 18:10:33 -08:00
|
|
|
@echo $JUST_TEST_VARIABLE
|
2023-10-11 22:04:46 -07:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.tree(tree! {
|
2024-01-26 18:10:33 -08:00
|
|
|
".env.special": "JUST_TEST_VARIABLE=bar"
|
2023-10-11 22:04:46 -07:00
|
|
|
})
|
|
|
|
.stdout("bar\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_set_dotenv_path_from_justfile() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
r#"
|
2024-05-14 21:31:58 -07:00
|
|
|
set dotenv-path := "subdir/.env"
|
2023-10-11 22:04:46 -07:00
|
|
|
|
|
|
|
foo:
|
2024-01-26 18:10:33 -08:00
|
|
|
@echo $JUST_TEST_VARIABLE
|
2023-10-11 22:04:46 -07:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.tree(tree! {
|
|
|
|
subdir: {
|
2024-01-26 18:10:33 -08:00
|
|
|
".env": "JUST_TEST_VARIABLE=bar"
|
2023-10-11 22:04:46 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.stdout("bar\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn program_argument_has_priority_for_dotenv_filename() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
r#"
|
|
|
|
set dotenv-filename := ".env.special"
|
|
|
|
|
|
|
|
foo:
|
2024-01-26 18:10:33 -08:00
|
|
|
@echo $JUST_TEST_VARIABLE
|
2023-10-11 22:04:46 -07:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.tree(tree! {
|
2024-01-26 18:10:33 -08:00
|
|
|
".env.special": "JUST_TEST_VARIABLE=bar",
|
|
|
|
".env.superspecial": "JUST_TEST_VARIABLE=baz"
|
2023-10-11 22:04:46 -07:00
|
|
|
})
|
|
|
|
.args(["--dotenv-filename", ".env.superspecial"])
|
|
|
|
.stdout("baz\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn program_argument_has_priority_for_dotenv_path() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
2024-05-30 16:12:07 -07:00
|
|
|
"
|
|
|
|
set dotenv-path := 'subdir/.env'
|
2023-10-11 22:04:46 -07:00
|
|
|
|
|
|
|
foo:
|
2024-01-26 18:10:33 -08:00
|
|
|
@echo $JUST_TEST_VARIABLE
|
2024-05-30 16:12:07 -07:00
|
|
|
",
|
2023-10-11 22:04:46 -07:00
|
|
|
)
|
|
|
|
.tree(tree! {
|
|
|
|
subdir: {
|
2024-01-26 18:10:33 -08:00
|
|
|
".env": "JUST_TEST_VARIABLE=bar",
|
|
|
|
".env.special": "JUST_TEST_VARIABLE=baz"
|
2023-10-11 22:04:46 -07:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.args(["--dotenv-path", "subdir/.env.special"])
|
|
|
|
.stdout("baz\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
2024-05-14 21:31:58 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dotenv_path_is_relative_to_working_directory() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
set dotenv-path := '.env'
|
|
|
|
|
|
|
|
foo:
|
|
|
|
@echo $DOTENV_KEY
|
|
|
|
",
|
|
|
|
)
|
2024-05-30 16:12:07 -07:00
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
2024-05-14 21:31:58 -07:00
|
|
|
.tree(tree! { subdir: { } })
|
|
|
|
.current_dir("subdir")
|
|
|
|
.stdout("dotenv-value\n")
|
|
|
|
.run();
|
|
|
|
}
|
2024-05-30 16:12:07 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dotenv_variable_in_recipe() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
set dotenv-load
|
|
|
|
|
|
|
|
echo:
|
|
|
|
echo $DOTENV_KEY
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
|
|
|
.stdout("dotenv-value\n")
|
|
|
|
.stderr("echo $DOTENV_KEY\n")
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dotenv_variable_in_backtick() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
set dotenv-load
|
|
|
|
X:=`echo $DOTENV_KEY`
|
|
|
|
echo:
|
|
|
|
echo {{X}}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
|
|
|
.stdout("dotenv-value\n")
|
|
|
|
.stderr("echo dotenv-value\n")
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dotenv_variable_in_function_in_recipe() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
set dotenv-load
|
|
|
|
echo:
|
|
|
|
echo {{env_var_or_default('DOTENV_KEY', 'foo')}}
|
|
|
|
echo {{env_var('DOTENV_KEY')}}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
|
|
|
.stdout("dotenv-value\ndotenv-value\n")
|
|
|
|
.stderr("echo dotenv-value\necho dotenv-value\n")
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dotenv_variable_in_function_in_backtick() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
set dotenv-load
|
|
|
|
X:=env_var_or_default('DOTENV_KEY', 'foo')
|
|
|
|
Y:=env_var('DOTENV_KEY')
|
|
|
|
echo:
|
|
|
|
echo {{X}}
|
|
|
|
echo {{Y}}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
|
|
|
.stdout("dotenv-value\ndotenv-value\n")
|
|
|
|
.stderr("echo dotenv-value\necho dotenv-value\n")
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn no_dotenv() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
X:=env_var_or_default('DOTENV_KEY', 'DEFAULT')
|
|
|
|
echo:
|
|
|
|
echo {{X}}
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
|
|
|
.arg("--no-dotenv")
|
|
|
|
.stdout("DEFAULT\n")
|
|
|
|
.stderr("echo DEFAULT\n")
|
|
|
|
.run();
|
|
|
|
}
|
2024-06-13 13:21:00 -07:00
|
|
|
|
2024-05-30 16:12:07 -07:00
|
|
|
#[test]
|
|
|
|
fn dotenv_env_var_override() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
echo:
|
|
|
|
echo $DOTENV_KEY
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
|
|
|
.env("DOTENV_KEY", "not-the-dotenv-value")
|
|
|
|
.stdout("not-the-dotenv-value\n")
|
|
|
|
.stderr("echo $DOTENV_KEY\n")
|
|
|
|
.run();
|
|
|
|
}
|
2024-06-13 13:21:00 -07:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dotenv_path_usable_from_subdir() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
set dotenv-path := '.custom-env'
|
|
|
|
|
|
|
|
@echo:
|
|
|
|
echo $DOTENV_KEY
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.create_dir("sub")
|
|
|
|
.current_dir("sub")
|
|
|
|
.write(".custom-env", "DOTENV_KEY=dotenv-value")
|
|
|
|
.stdout("dotenv-value\n")
|
|
|
|
.run();
|
|
|
|
}
|