2022-06-19 07:56:31 +03:00
|
|
|
use super::*;
|
2020-07-19 15:01:46 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dotenv() {
|
2023-01-04 09:31:56 +03: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 15:01:46 +03:00
|
|
|
}
|
2021-03-29 08:38:07 +03:00
|
|
|
|
2024-05-31 02:12:07 +03: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-29 08:38:07 +03:00
|
|
|
}
|
|
|
|
|
2024-05-31 02:12:07 +03:00
|
|
|
#[test]
|
|
|
|
fn set_implicit() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
set dotenv-load
|
2021-03-29 08:38:07 +03:00
|
|
|
|
2024-05-31 02:12:07 +03:00
|
|
|
foo:
|
|
|
|
echo $DOTENV_KEY
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
|
|
|
.stdout("dotenv-value\n")
|
|
|
|
.stderr("echo $DOTENV_KEY\n")
|
|
|
|
.run();
|
2021-03-29 08:38:07 +03:00
|
|
|
}
|
|
|
|
|
2024-05-31 02:12:07 +03:00
|
|
|
#[test]
|
|
|
|
fn set_true() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
set dotenv-load := true
|
2021-03-29 08:38:07 +03:00
|
|
|
|
2024-05-31 02:12:07 +03:00
|
|
|
foo:
|
|
|
|
echo $DOTENV_KEY
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
|
|
|
.stdout("dotenv-value\n")
|
|
|
|
.stderr("echo $DOTENV_KEY\n")
|
|
|
|
.run();
|
2021-03-31 03:30:32 +03:00
|
|
|
}
|
|
|
|
|
2021-07-28 10:33:44 +03:00
|
|
|
#[test]
|
2022-02-02 06:16:35 +03:00
|
|
|
fn no_warning() {
|
2021-07-28 10:33:44 +03:00
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
2024-05-31 02:12:07 +03:00
|
|
|
foo:
|
|
|
|
echo ${DOTENV_KEY:-unset}
|
|
|
|
",
|
2021-07-28 10:33:44 +03:00
|
|
|
)
|
2024-05-31 02:12:07 +03:00
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
2022-02-02 06:16:35 +03:00
|
|
|
.stdout("unset\n")
|
|
|
|
.stderr("echo ${DOTENV_KEY:-unset}\n")
|
2021-07-28 10:33:44 +03:00
|
|
|
.run();
|
|
|
|
}
|
2021-08-09 08:37:35 +03:00
|
|
|
|
|
|
|
#[test]
|
2024-05-31 02:12:07 +03:00
|
|
|
fn dotenv_required() {
|
2021-08-09 08:37:35 +03:00
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
2024-05-31 02:12:07 +03:00
|
|
|
set dotenv-required
|
|
|
|
|
|
|
|
foo:
|
|
|
|
",
|
2021-08-09 08:37:35 +03:00
|
|
|
)
|
2024-05-31 02:12:07 +03:00
|
|
|
.stderr("error: Dotenv file not found\n")
|
|
|
|
.status(1)
|
2021-08-09 08:37:35 +03:00
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn path_resolves() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
2024-05-31 02:12:07 +03:00
|
|
|
foo:
|
|
|
|
@echo $JUST_TEST_VARIABLE
|
|
|
|
",
|
2021-08-09 08:37:35 +03:00
|
|
|
)
|
|
|
|
.tree(tree! {
|
|
|
|
subdir: {
|
2024-01-27 05:10:33 +03:00
|
|
|
".env": "JUST_TEST_VARIABLE=bar"
|
2021-08-09 08:37:35 +03:00
|
|
|
}
|
|
|
|
})
|
2023-01-04 09:31:56 +03:00
|
|
|
.args(["--dotenv-path", "subdir/.env"])
|
2021-08-09 08:37:35 +03:00
|
|
|
.stdout("bar\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn filename_resolves() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
2024-05-31 02:12:07 +03:00
|
|
|
foo:
|
|
|
|
@echo $JUST_TEST_VARIABLE
|
|
|
|
",
|
2021-08-09 08:37:35 +03:00
|
|
|
)
|
|
|
|
.tree(tree! {
|
2024-01-27 05:10:33 +03:00
|
|
|
".env.special": "JUST_TEST_VARIABLE=bar"
|
2021-08-09 08:37:35 +03:00
|
|
|
})
|
2023-01-04 09:31:56 +03:00
|
|
|
.args(["--dotenv-filename", ".env.special"])
|
2021-08-09 08:37:35 +03: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-27 05:10:33 +03:00
|
|
|
@echo $JUST_TEST_VARIABLE
|
2021-08-09 08:37:35 +03:00
|
|
|
",
|
|
|
|
)
|
|
|
|
.tree(tree! {
|
2024-01-27 05:10:33 +03:00
|
|
|
".env.special": "JUST_TEST_VARIABLE=bar"
|
2021-08-09 08:37:35 +03:00
|
|
|
})
|
2023-01-04 09:31:56 +03:00
|
|
|
.args(["--dotenv-filename", ".env.special"])
|
2021-08-09 08:37:35 +03:00
|
|
|
.stdout("bar\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn path_flag_overwrites_no_load() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
2024-05-31 02:12:07 +03:00
|
|
|
set dotenv-load := false
|
2021-08-09 08:37:35 +03:00
|
|
|
|
2024-05-31 02:12:07 +03:00
|
|
|
foo:
|
|
|
|
@echo $JUST_TEST_VARIABLE
|
|
|
|
",
|
2021-08-09 08:37:35 +03:00
|
|
|
)
|
|
|
|
.tree(tree! {
|
|
|
|
subdir: {
|
2024-01-27 05:10:33 +03:00
|
|
|
".env": "JUST_TEST_VARIABLE=bar"
|
2021-08-09 08:37:35 +03:00
|
|
|
}
|
|
|
|
})
|
2023-01-04 09:31:56 +03:00
|
|
|
.args(["--dotenv-path", "subdir/.env"])
|
2021-08-09 08:37:35 +03:00
|
|
|
.stdout("bar\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
2023-10-12 08:04:46 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_set_dotenv_filename_from_justfile() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
r#"
|
|
|
|
set dotenv-filename := ".env.special"
|
|
|
|
|
|
|
|
foo:
|
2024-01-27 05:10:33 +03:00
|
|
|
@echo $JUST_TEST_VARIABLE
|
2023-10-12 08:04:46 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.tree(tree! {
|
2024-01-27 05:10:33 +03:00
|
|
|
".env.special": "JUST_TEST_VARIABLE=bar"
|
2023-10-12 08:04:46 +03:00
|
|
|
})
|
|
|
|
.stdout("bar\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn can_set_dotenv_path_from_justfile() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
r#"
|
2024-05-15 07:31:58 +03:00
|
|
|
set dotenv-path := "subdir/.env"
|
2023-10-12 08:04:46 +03:00
|
|
|
|
|
|
|
foo:
|
2024-01-27 05:10:33 +03:00
|
|
|
@echo $JUST_TEST_VARIABLE
|
2023-10-12 08:04:46 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.tree(tree! {
|
|
|
|
subdir: {
|
2024-01-27 05:10:33 +03:00
|
|
|
".env": "JUST_TEST_VARIABLE=bar"
|
2023-10-12 08:04:46 +03: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-27 05:10:33 +03:00
|
|
|
@echo $JUST_TEST_VARIABLE
|
2023-10-12 08:04:46 +03:00
|
|
|
"#,
|
|
|
|
)
|
|
|
|
.tree(tree! {
|
2024-01-27 05:10:33 +03:00
|
|
|
".env.special": "JUST_TEST_VARIABLE=bar",
|
|
|
|
".env.superspecial": "JUST_TEST_VARIABLE=baz"
|
2023-10-12 08:04:46 +03: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-31 02:12:07 +03:00
|
|
|
"
|
|
|
|
set dotenv-path := 'subdir/.env'
|
2023-10-12 08:04:46 +03:00
|
|
|
|
|
|
|
foo:
|
2024-01-27 05:10:33 +03:00
|
|
|
@echo $JUST_TEST_VARIABLE
|
2024-05-31 02:12:07 +03:00
|
|
|
",
|
2023-10-12 08:04:46 +03:00
|
|
|
)
|
|
|
|
.tree(tree! {
|
|
|
|
subdir: {
|
2024-01-27 05:10:33 +03:00
|
|
|
".env": "JUST_TEST_VARIABLE=bar",
|
|
|
|
".env.special": "JUST_TEST_VARIABLE=baz"
|
2023-10-12 08:04:46 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.args(["--dotenv-path", "subdir/.env.special"])
|
|
|
|
.stdout("baz\n")
|
|
|
|
.status(EXIT_SUCCESS)
|
|
|
|
.run();
|
|
|
|
}
|
2024-05-15 07:31:58 +03:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn dotenv_path_is_relative_to_working_directory() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
set dotenv-path := '.env'
|
|
|
|
|
|
|
|
foo:
|
|
|
|
@echo $DOTENV_KEY
|
|
|
|
",
|
|
|
|
)
|
2024-05-31 02:12:07 +03:00
|
|
|
.write(".env", "DOTENV_KEY=dotenv-value")
|
2024-05-15 07:31:58 +03:00
|
|
|
.tree(tree! { subdir: { } })
|
|
|
|
.current_dir("subdir")
|
|
|
|
.stdout("dotenv-value\n")
|
|
|
|
.run();
|
|
|
|
}
|
2024-05-31 02:12:07 +03: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();
|
|
|
|
}
|
|
|
|
#[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();
|
|
|
|
}
|