Skip to content

Commit

Permalink
feat: update JSON output for task info/ls (#4034)
Browse files Browse the repository at this point in the history
Co-authored-by: jdx <[email protected]>
  • Loading branch information
hverlin and jdx authored Jan 10, 2025
1 parent cf07782 commit 659287c
Show file tree
Hide file tree
Showing 6 changed files with 306 additions and 24 deletions.
66 changes: 66 additions & 0 deletions e2e/assert.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
64 changes: 64 additions & 0 deletions e2e/tasks/test_task_info
Original file line number Diff line number Diff line change
Expand Up @@ -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 <<EOF
{
"name": "lint",
"aliases": [],
"description": "",
"source": "$(pwd)/mise.toml",
"depends": [
[
"build",
"-v"
]
],
"depends_post": [],
"wait_for": [],
"env": {},
"dir": null,
"hide": false,
"raw": false,
"sources": [],
"outputs": [],
"shell": null,
"quiet": false,
"silent": false,
"tools": {},
"run": [
"'echo \"linting!\"'"
],
"file": null,
"usage_spec": {
"name": "lint",
"bin": "lint",
"cmd": {
"full_cmd": [],
"usage": "",
"subcommands": {},
"args": [],
"flags": [],
"mounts": [],
"hide": false,
"help": "",
"name": "lint",
"aliases": [],
"hidden_aliases": [],
"before_help_long": "- Depends: build -v",
"examples": []
},
"config": {
"props": {}
},
"usage": "",
"complete": {}
}
}
EOF
)"
122 changes: 122 additions & 0 deletions e2e/tasks/test_task_ls
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ cat <<EOF >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
Expand Down Expand Up @@ -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 <<EOF
[
{
"name": "filetask2",
"aliases": [],
"description": "",
"source": "$(pwd)/mytasks/filetask2",
"depends": [],
"depends_post": [],
"wait_for": [],
"env": {},
"dir": null,
"hide": false,
"raw": false,
"sources": [],
"outputs": [],
"shell": null,
"quiet": false,
"silent": false,
"tools": {},
"run": [],
"file": "$(pwd)/mytasks/filetask2"
},
{
"name": "filetask3",
"aliases": [],
"description": "",
"source": "$(pwd)/mytasks/filetask3.sh",
"depends": [],
"depends_post": [],
"wait_for": [],
"env": {},
"dir": null,
"hide": false,
"raw": false,
"sources": [],
"outputs": [],
"shell": null,
"quiet": false,
"silent": false,
"tools": {},
"run": [],
"file": "$(pwd)/mytasks/filetask3.sh"
},
{
"name": "lint",
"aliases": [],
"description": "",
"source": "$(pwd)/mise.toml",
"depends": [],
"depends_post": [],
"wait_for": [],
"env": {},
"dir": null,
"hide": false,
"raw": false,
"sources": [],
"outputs": [],
"shell": null,
"quiet": false,
"silent": false,
"tools": {},
"run": [
"echo \"linting!\""
],
"file": null
},
{
"name": "test",
"aliases": [],
"description": "",
"source": "$(pwd)/tasks.toml",
"depends": [],
"depends_post": [],
"wait_for": [],
"env": {},
"dir": null,
"hide": false,
"raw": false,
"sources": [],
"outputs": [],
"shell": null,
"quiet": false,
"silent": false,
"tools": {},
"run": [
"echo \"testing!\""
],
"file": null
},
{
"name": "test-with-args",
"aliases": [],
"description": "",
"source": "$(pwd)/tasks.toml",
"depends": [
["test","-v"],
"test"
],
"depends_post": [],
"wait_for": [],
"env": {},
"dir": null,
"hide": false,
"raw": false,
"sources": [],
"outputs": [],
"shell": null,
"quiet": false,
"silent": false,
"tools": {},
"run": [
"echo \"{{arg()}} {{flag(name=\"force\")}} {{option(name=\"user\")}}\""
],
"file": null
}
]
EOF
)"
22 changes: 20 additions & 2 deletions e2e/tasks/test_task_usage
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# tests that "test.sh" gets mapped to "test" except when an existing task is there named "test"
assert "mise task add --file test.sh --description sh"
assert "mise task ls" "test sh"
assert "mise task ls --json" "[
assert_json_partial_array "mise task ls --json" "name,description,source" "[
{
\"name\": \"test\",
\"description\": \"sh\",
Expand All @@ -15,7 +15,7 @@ assert "mise task ls --usage" 'cmd "test" help="sh"'
assert "mise task add --file test --description no-sh"
assert "mise task ls" "test no-sh
test.sh sh"
assert "mise task ls --json" "[
assert_json_partial_array "mise task ls --json" "name,description,source" "[
{
\"name\": \"test\",
\"description\": \"no-sh\",
Expand All @@ -27,5 +27,23 @@ assert "mise task ls --json" "[
\"source\": \"$PWD/mise-tasks/test.sh\"
}
]"

assert_json_partial_array "mise task ls --json" "name,description,source" "$(
cat <<EOF
[
{
"name": "test",
"description": "no-sh",
"source": "$PWD/mise-tasks/test"
},
{
"name": "test.sh",
"description": "sh",
"source": "$PWD/mise-tasks/test.sh"
}
]
EOF
)"

assert "mise task ls --usage" 'cmd "test" help="no-sh"
cmd "test.sh" help="sh"'
13 changes: 9 additions & 4 deletions src/cli/tasks/info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,17 +102,22 @@ impl TasksInfo {
let (spec, _) = task.parse_usage_spec(None, env)?;
let o = json!({
"name": task.display_name(),
"aliases": task.aliases.join(", "),
"description": task.description.to_string(),
"aliases": task.aliases,
"description": task.description,
"source": task.config_source,
"depends": task.depends.iter().join(", "),
"depends_post": task.depends_post.iter().join(", "),
"depends": task.depends,
"depends_post": task.depends_post,
"wait_for": task.wait_for,
"env": task.env,
"dir": task.dir,
"hide": task.hide,
"raw": task.raw,
"sources": task.sources,
"outputs": task.outputs,
"shell": task.shell,
"quiet": task.quiet,
"silent": task.silent,
"tools": task.tools,
"run": task.run(),
"file": task.file,
"usage_spec": spec,
Expand Down
Loading

0 comments on commit 659287c

Please sign in to comment.