mirror of https://github.com/jdx/mise
feat: update JSON output for task info/ls (#4034)
Co-authored-by: jdx <216188+jdx@users.noreply.github.com>
This commit is contained in:
parent
cf07782025
commit
659287cb45
|
@ -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"
|
||||
|
|
|
@ -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
|
||||
)"
|
||||
|
|
|
@ -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
|
||||
|
@ -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
|
||||
)"
|
||||
|
|
|
@ -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\",
|
||||
|
@ -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\",
|
||||
|
@ -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"'
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
use comfy_table::{Attribute, Cell, Row};
|
||||
use eyre::Result;
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::file::display_rel_path;
|
||||
use crate::task::Task;
|
||||
use crate::toolset::Toolset;
|
||||
use crate::ui::table::MiseTable;
|
||||
use comfy_table::{Attribute, Cell, Row};
|
||||
use eyre::Result;
|
||||
use itertools::Itertools;
|
||||
use serde_json::json;
|
||||
|
||||
/// List available tasks to execute
|
||||
/// These may be included from the config file or from the project's .mise/tasks directory
|
||||
|
@ -133,20 +133,27 @@ impl TasksLs {
|
|||
let array_items = tasks
|
||||
.into_iter()
|
||||
.map(|task| {
|
||||
let mut inner = serde_json::Map::new();
|
||||
inner.insert("name".to_string(), task.display_name().into());
|
||||
if !task.aliases.is_empty() {
|
||||
inner.insert("aliases".to_string(), task.aliases.join(", ").into());
|
||||
}
|
||||
if task.hide {
|
||||
inner.insert("hide".to_string(), task.hide.into());
|
||||
}
|
||||
inner.insert("description".to_string(), task.description.into());
|
||||
inner.insert(
|
||||
"source".to_string(),
|
||||
task.config_source.to_string_lossy().into(),
|
||||
);
|
||||
inner
|
||||
json!({
|
||||
"name": task.display_name(),
|
||||
"aliases": task.aliases,
|
||||
"description": task.description,
|
||||
"source": task.config_source,
|
||||
"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,
|
||||
})
|
||||
})
|
||||
.collect::<serde_json::Value>();
|
||||
miseprintln!("{}", serde_json::to_string_pretty(&array_items)?);
|
||||
|
|
Loading…
Reference in New Issue