#!/usr/bin/env bash

# With an explicit minimum_release_age, core:rust installs `stable` as a dated version. The
# install dependency check of a cargo: tool resolves rust@stable offline and must find that
# dated install instead of looking for a `stable` directory.
# A host cargo-binstall would otherwise be picked before the stub cargo.
export MISE_CARGO_BINSTALL=0
export CARGO_HOME="$HOME/test-cargo-home"
mkdir -p "$CARGO_HOME/bin"
cat >"$CARGO_HOME/bin/cargo" <<'SCRIPT'
#!/usr/bin/env bash
echo "fake cargo ran"
exit 1
SCRIPT
chmod +x "$CARGO_HOME/bin/cargo"

cat >mise.toml <<'TOML'
[settings]
minimum_release_age = "7d"

[tools]
"core:rust" = "stable"
"cargo:mise-fake-crate" = "1.0.0"
TOML

# Without a rust install the dependency check must still fail.
output="$(mise install cargo:mise-fake-crate 2>&1 || true)"
assert_contains "echo '$output'" "requires configured install dependency"

mkdir -p "$MISE_DATA_DIR/installs/rust/1.99.1"
for offline in 0 1; do
  output="$(MISE_OFFLINE=$offline mise install cargo:mise-fake-crate 2>&1 || true)"
  assert_not_contains "echo '$output'" "requires configured install dependency"
  assert_contains "echo '$output'" "fake cargo ran"
done
