#!/usr/bin/env bash

assert() {
	actual="$($1)"
	actual="${actual%$'\n'}"
	expected="${2%$'\n'}"
	if [[ "$actual" != "$expected" ]]; then
		echo "assertion failed, expected '$expected', got '$actual'"
		exit 1
	fi
}
assert_raises() {
	if ! $1; then
		echo "assertion failed: $1"
		exit 1
	fi
}

mise i shfmt@3.6.0 shfmt@3.5.0

assert "mise local" "#:schema ../../schema/mise.json
[env]
FOO = \"bar\"
mise.file = ['.test-env']
mise.path = [\"/root\", \"./cwd\"]

[tools]
tiny = \"latest\"
#golang = {version=\"1.19.5\", foo=\"bar\"}

[plugins]
tiny-ref = \"https://github.com/mise-plugins/rtx-tiny#c532b140abd4ca00d3e76651b9bd32a980bd483c\"

[tasks.configtask]
run = 'echo \"configtask:\"'
[tasks.lint]
run = 'echo \"linting!\"'
[tasks.test]
run = 'echo \"testing!\"'
"

mise local shfmt@3.5.0
assert "mise local" "#:schema ../../schema/mise.json
[env]
FOO = \"bar\"
mise.file = ['.test-env']
mise.path = [\"/root\", \"./cwd\"]

[tools]
tiny = \"latest\"
shfmt = \"3.5.0\"
#golang = {version=\"1.19.5\", foo=\"bar\"}

[plugins]
tiny-ref = \"https://github.com/mise-plugins/rtx-tiny#c532b140abd4ca00d3e76651b9bd32a980bd483c\"

[tasks.configtask]
run = 'echo \"configtask:\"'
[tasks.lint]
run = 'echo \"linting!\"'
[tasks.test]
run = 'echo \"testing!\"'
"

mise exec -- shfmt --version >&2
if [[ "$(mise exec -- shfmt --version)" != "v3.5.0" ]]; then
	exit 1
fi

mise local shfmt@3.6.0
assert "mise local" "#:schema ../../schema/mise.json
[env]
FOO = \"bar\"
mise.file = ['.test-env']
mise.path = [\"/root\", \"./cwd\"]

[tools]
tiny = \"latest\"
shfmt = \"3.6.0\"
#golang = {version=\"1.19.5\", foo=\"bar\"}

[plugins]
tiny-ref = \"https://github.com/mise-plugins/rtx-tiny#c532b140abd4ca00d3e76651b9bd32a980bd483c\"

[tasks.configtask]
run = 'echo \"configtask:\"'
[tasks.lint]
run = 'echo \"linting!\"'
[tasks.test]
run = 'echo \"testing!\"'
"

mise exec -- shfmt --version >&2
if [[ "$(mise exec -- shfmt --version)" != "v3.6.0" ]]; then
	exit 1
fi

mise local --rm shfmt
assert "mise local" "#:schema ../../schema/mise.json
[env]
FOO = \"bar\"
mise.file = ['.test-env']
mise.path = [\"/root\", \"./cwd\"]

[tools]
tiny = \"latest\"
#golang = {version=\"1.19.5\", foo=\"bar\"}

[plugins]
tiny-ref = \"https://github.com/mise-plugins/rtx-tiny#c532b140abd4ca00d3e76651b9bd32a980bd483c\"

[tasks.configtask]
run = 'echo \"configtask:\"'
[tasks.lint]
run = 'echo \"linting!\"'
[tasks.test]
run = 'echo \"testing!\"'
"

export MISE_DEFAULT_CONFIG_FILENAME=.MISSING

mise local
assert "mise local" "#python 3.11.1 3.10.9 # foo
shellcheck sub-0.1:0.10.0
shfmt      3.6.0 # test comment
#node    20.0.0
"

mise local shfmt@3.5.0
assert "mise local" "#python 3.11.1 3.10.9 # foo
shellcheck sub-0.1:0.10.0
shfmt      3.5.0 # test comment
#node    20.0.0
"

mise exec -- shfmt --version >&2
if [[ "$(mise exec -- shfmt --version)" != "v3.5.0" ]]; then
	exit 1
fi

mise local shfmt@3.6.0
assert "mise local" "#python 3.11.1 3.10.9 # foo
shellcheck sub-0.1:0.10.0
shfmt      3.6.0 # test comment
#node    20.0.0
"

mise exec -- shfmt --version >&2
if [[ "$(mise exec -- shfmt --version)" != "v3.6.0" ]]; then
	exit 1
fi
