diff --git a/e2e/assert.sh b/e2e/assert.sh index 1b6271e657..d7e5385236 100755 --- a/e2e/assert.sh +++ b/e2e/assert.sh @@ -58,6 +58,72 @@ assert() { fi } +assert_json() { + local actual + actual="$(quiet_assert_succeed "$1")" + if jq -e . >/dev/null <<<"$actual"; then + ok "[$1] output is valid JSON" + else + fail "[$1] output is not valid JSON" + fi + + actual_json="$(jq . <<<"$actual")" + expected_json="$(jq . <<<"$2")" + if [[ $actual_json == "$expected_json" ]]; then + ok "[$1] output is equal to '$2'" + else + diff --side-by-side <(jq . <<<"$expected_json") <(jq . <<<"$actual_json") || true + fail "JSON output from [$1] is different from expected" + fi +} + +assert_json_partial_array() { + local command="$1" fields="$2" expected="$3" + + local actual + actual="$(quiet_assert_succeed "$command")" + + local filter="map({$fields})" + local actual_filtered expected_filtered + + actual_filtered="$(jq -S "$filter" <<<"$actual")" + expected_filtered="$(jq -S "$filter" <<<"$expected")" + + if [[ "$actual_filtered" == "$expected_filtered" ]]; then + ok "[$command] partial array match successful" + else + echo "Expected:" + echo "$expected_filtered" + echo "Got:" + echo "$actual_filtered" + fail "[$command] partial array match failed" + fi +} + +assert_json_partial_object() { + local command="$1" fields="$2" expected="$3" + + local actual + actual="$(quiet_assert_succeed "$command")" + + # shellcheck disable=SC2016 + local filter='with_entries(select(.key as $k | ($fields | split(",")) | contains([$k])))' + local actual_filtered expected_filtered + + actual_filtered="$(jq -S --arg fields "$fields" "$filter" <<<"$actual")" + expected_filtered="$(jq -S --arg fields "$fields" "$filter" <<<"$expected")" + + if [[ "$actual_filtered" == "$expected_filtered" ]]; then + ok "[$command] partial object match successful" + else + echo "Expected:" + echo "$expected_filtered" + echo "Got:" + echo "$actual_filtered" + fail "[$command] partial object match failed" + fi +} + assert_not() { local actual debug "$ $1" diff --git a/e2e/tasks/test_task_info b/e2e/tasks/test_task_info index 2f733aaf20..ef2d58957f 100644 --- a/e2e/tasks/test_task_info +++ b/e2e/tasks/test_task_info @@ -11,3 +11,67 @@ Usage Spec: bin "build"' assert_contains "mise task info build.sh --json" '"name": "build"' + +assert_json_partial_object "mise task info build --json" "name,source,description" '{ + "name": "build", + "source": "'"$PWD"'/mise-tasks/build.sh", + "description": "" +}' + +mise tasks add lint --depends "build -v" -- 'echo "linting!"' +assert_json "mise task info lint --json" "$( + cat <tasks.toml run = 'echo "testing!"' [test-with-args] run = 'echo "{{arg()}} {{flag(name="force")}} {{option(name="user")}}"' +depends = ["test -v", "test"] EOF mkdir -p .mise/tasks @@ -45,3 +46,124 @@ filetask3 ./mytasks/filetask3.sh lint ./mise.toml test ./tasks.toml test-with-args ./tasks.toml" + +assert_json "mise task ls --json" "$( + cat <(); miseprintln!("{}", serde_json::to_string_pretty(&array_items)?);