#!/usr/bin/env bash

cat <<EOF >mise.toml
[env]
ENV_TMPL_EXEC = "{{ exec(command='echo foo') }}"
EOF
assert "mise env -s zsh | grep ENV_TMPL_EXEC" "export ENV_TMPL_EXEC=foo"

cat <<EOF >mise.toml
[env]
B = "{{ env.A }}"
EOF
A=a assert "mise env -s bash | grep B" "export B=a"

cat <<EOF >mise.toml
[env]
A = "a"
B = "{{ env.A }}"
EOF
assert "mise env -s bash | grep B" "export B=a"

cat <<EOF >mise.toml
[env]
FOO = "foo"

[tasks.foo]
env.BAR = "bar"

run = """
printf '\$FOO: %s\n' \$FOO
printf '\$BAR: %s\n' \$BAR
echo "{% raw %}{{ env.FOO }}{% endraw %}: {{ env.FOO }}"
echo "{% raw %}{{ env.BAR }}{% endraw %}: {{ env.BAR }}"
"""
EOF
assert "mise run --trace foo" "\$FOO: foo
\$BAR: bar
{{ env.FOO }}: foo
{{ env.BAR }}: bar"

cat <<EOF >mise.toml
[env]
FOO = "foo"

[tasks.foo]
env.BAR = "{{ env.FOO }}"
env.BAZ = "{{ env.BAR }}"

run = "printf '\$BAZ: %s\n' \$BAZ"
EOF
assert "mise run --trace foo" "\$BAZ: foo"

cat <<EOF >mise.toml
[env]
FOO = "/foo"
_.source = { path = "env.sh", tools = true }
_.path = "{{ env.FOO }}"
EOF
cat <<EOF >env.sh
#!/usr/bin/env bash
export BAR="\$PATH"
EOF
assert_contains "mise env -s bash | grep BAR" "export BAR='/foo:"
