#!/usr/bin/env bash
# Read go version from go.mod (idiomatic version file, opt-in)
# The "ignored without opt-in" case is covered by e2e/core/test_go_slow.

# Enable idiomatic version files for go
mise settings set idiomatic_version_file_enable_tools go

# the `toolchain` directive is an exact pin of the toolchain the module builds
# with, and takes precedence over the `go` directive. A `go` directive alongside
# it is not read at all, so this must not warn -- `go` + `toolchain` together is
# what `go mod tidy` writes, and it is the shape users should end up with.
cat >go.mod <<GOMOD
module example.com/m

go 1.21
toolchain go1.21.4
GOMOD
assert "mise current go" "1.21.4"
assert_not_contains "mise current go 2>&1" "deprecated"

# a `toolchain` pin alone is enough, and also does not warn
cat >go.mod <<GOMOD
module example.com/m

toolchain go1.21.4
GOMOD
assert "mise current go" "1.21.4"
assert_not_contains "mise current go 2>&1" "deprecated"

# without a toolchain line, an exact `go` line implies `toolchain go1.21.4`. This is
# what `go mod tidy` leaves when the two lines match, so it is read and does not warn.
cat >go.mod <<GOMOD
module example.com/m

go 1.21.4
GOMOD
assert "mise current go" "1.21.4"
assert_not_contains "mise current go 2>&1" "deprecated"

# a `go X.Y` line is only a minimum compatible version, so it is not read
cat >go.mod <<GOMOD
module example.com/m

go 1.21
GOMOD
assert "mise current go" ""
assert_not_contains "mise current go 2>&1" "deprecated"

# no usable directive at all -> the file is skipped
cat >go.mod <<GOMOD
module example.com/m
GOMOD
assert "mise current go" ""

# an exact `go` line still resolves, since it is the module's implied toolchain
cat >go.mod <<GOMOD
module example.com/m

go 1.21.4
GOMOD
assert "mise current go" "1.21.4"

# ...and `toolchain` still resolves
cat >go.mod <<GOMOD
module example.com/m

go 1.21
toolchain go1.21.4
GOMOD
assert "mise current go" "1.21.4"
