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
|
|
|
|
|
|
|
test! {
|
|
|
|
name: set_false,
|
|
|
|
justfile: r#"
|
|
|
|
set dotenv-load := false
|
|
|
|
|
|
|
|
foo:
|
|
|
|
if [ -n "${DOTENV_KEY+1}" ]; then echo defined; else echo undefined; fi
|
|
|
|
"#,
|
|
|
|
stdout: "undefined\n",
|
|
|
|
stderr: "if [ -n \"${DOTENV_KEY+1}\" ]; then echo defined; else echo undefined; fi\n",
|
|
|
|
}
|
|
|
|
|
|
|
|
test! {
|
|
|
|
name: set_implicit,
|
|
|
|
justfile: r#"
|
|
|
|
set dotenv-load
|
|
|
|
|
|
|
|
foo:
|
|
|
|
echo $DOTENV_KEY
|
|
|
|
"#,
|
|
|
|
stdout: "dotenv-value\n",
|
|
|
|
stderr: "echo $DOTENV_KEY\n",
|
|
|
|
}
|
|
|
|
|
|
|
|
test! {
|
|
|
|
name: set_true,
|
|
|
|
justfile: r#"
|
|
|
|
set dotenv-load := true
|
|
|
|
|
|
|
|
foo:
|
|
|
|
echo $DOTENV_KEY
|
|
|
|
"#,
|
|
|
|
stdout: "dotenv-value\n",
|
|
|
|
stderr: "echo $DOTENV_KEY\n",
|
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(
|
|
|
|
"
|
|
|
|
foo:
|
2022-02-01 19:16:35 -08:00
|
|
|
echo ${DOTENV_KEY:-unset}
|
2021-07-28 00:33:44 -07:00
|
|
|
",
|
|
|
|
)
|
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]
|
|
|
|
fn path_not_found() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
foo:
|
2024-01-26 18:10:33 -08:00
|
|
|
echo $JUST_TEST_VARIABLE
|
2021-08-08 22:37:35 -07:00
|
|
|
",
|
|
|
|
)
|
2023-01-03 22:31:56 -08:00
|
|
|
.args(["--dotenv-path", ".env.prod"])
|
2021-08-08 22:37:35 -07:00
|
|
|
.stderr(if cfg!(windows) {
|
|
|
|
"error: Failed to load environment file: The system cannot find the file specified. (os \
|
|
|
|
error 2)\n"
|
|
|
|
} else {
|
|
|
|
"error: Failed to load environment file: No such file or directory (os error 2)\n"
|
|
|
|
})
|
|
|
|
.status(EXIT_FAILURE)
|
|
|
|
.run();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn path_resolves() {
|
|
|
|
Test::new()
|
|
|
|
.justfile(
|
|
|
|
"
|
|
|
|
foo:
|
2024-01-26 18:10:33 -08:00
|
|
|
@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(
|
|
|
|
"
|
|
|
|
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 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(
|
|
|
|
"
|
|
|
|
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! {
|
|
|
|
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(
|
|
|
|
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",
|
|
|
|
".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
|
|
|
|
",
|
|
|
|
)
|
|
|
|
.tree(tree! { subdir: { } })
|
|
|
|
.current_dir("subdir")
|
|
|
|
.stdout("dotenv-value\n")
|
|
|
|
.run();
|
|
|
|
}
|