Skip to content

Commit 6c4eb0d

Browse files
authored
Merge pull request #55 from hlship/hls/20251017-completions-tests
Minor corrections to zsh completions generation
2 parents f7265a4 + f654562 commit 6c4eb0d

File tree

10 files changed

+277
-46
lines changed

10 files changed

+277
-46
lines changed
Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,18 @@
11
(ns net.lewisship.cli-tools.builtins
22
"Built-in commands, available to any tool."
3-
{:command-category "Built-in"
4-
:command-category-order 100}
53
(:require [net.lewisship.cli-tools.impl :as impl]
64
[net.lewisship.cli-tools :as cli :refer [defcommand]]))
75

86
(defcommand help
97
"List available commands.
108
119
If a search term is provided, the --commands option is ignored."
12-
[output-level (cli/select-option "-c" "--commands FILTER" "Print commands: " #{:none :root :all}
10+
[output-level (cli/select-option "-c" "--commands FILTER" "Print commands:"
11+
#{:none :root :all}
1312
:default :default)
1413
:args
1514
search-term ["SEARCH" "Filter shown commands to those that match this term"
1615
:optional true]]
1716
(if search-term
1817
(impl/print-search-results search-term)
1918
(impl/print-tool-help output-level)))
20-

src/net/lewisship/cli_tools/completions.clj

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,9 @@
5858
(defn- extract-command
5959
[fn-prefix [command-name command-map]]
6060
(let [{:keys [fn]} command-map
61-
title (binding [ansi/*color-enabled* false]
62-
(impl/extract-command-title command-map))
61+
title (-> (impl/extract-command-title command-map)
62+
ansi/compose
63+
string/trim)
6364
fn-name (simplify fn-prefix command-name)]
6465
;; TODO: Support messy group/command combos
6566
(if fn
@@ -110,7 +111,8 @@
110111
io/output-stream
111112
io/writer)]
112113
(try
113-
(binding [*out* w]
114+
(binding [*out* w
115+
ansi/*color-enabled* false]
114116
(print-tool tool-name command-root groups))
115117
(catch Throwable t
116118
(abort 1 [:red
@@ -119,5 +121,5 @@
119121
(class t))]))))
120122
(perr [:cyan "Wrote " output-path]))
121123
;; Just write to standard output
122-
(print-tool tool-name command-root groups)))))
123-
124+
(binding [ansi/*color-enabled* false]
125+
(print-tool tool-name command-root groups))))))

src/net/lewisship/cli_tools/impl.clj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -724,7 +724,7 @@
724724
frequencies)]
725725
[:faint
726726
(when command-count
727-
(list
727+
(str
728728
(h/numberword command-count) " "
729729
(inflect/pluralize-noun command-count "sub-command")))
730730
(when (and command-count group-count)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#compdef _messy messy
2+
3+
_messy() {
4+
local line state
5+
6+
_arguments -C \
7+
"1: :->cmds" \
8+
"*::arg:->args"
9+
10+
case "$state" in
11+
cmds)
12+
_values "messy command" \
13+
"completions[Generate zsh command completions]" \
14+
"simple[Simple command]" \
15+
"messy[Messy command]" \
16+
"help[List available commands]"
17+
;;
18+
args)
19+
case $line[1] in
20+
completions) _messy_completions ;;
21+
22+
simple) _messy_simple ;;
23+
24+
messy) _messy_messy ;;
25+
26+
help) _messy_help ;;
27+
28+
esac
29+
;;
30+
esac
31+
}
32+
33+
_messy_completions() {
34+
_arguments -s \
35+
'(-h --help)'{-h,--help}$'[This command summary]'
36+
}
37+
38+
_messy_simple() {
39+
_arguments -s \
40+
'(-h --help)'{-h,--help}$'[This command summary]'
41+
}
42+
43+
_messy_messy() {
44+
_arguments -s \
45+
'(-h --help)'{-h,--help}$'[This command summary]'
46+
}
47+
48+
_messy_help() {
49+
_arguments -s \
50+
'(-c --commands)'{-c,--commands}$'[Print commands: all, none, root]':FILTER \
51+
'(-h --help)'{-h,--help}$'[This command summary]'
52+
}
53+
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#compdef _simple simple
2+
3+
_simple() {
4+
local line state
5+
6+
_arguments -C \
7+
"1: :->cmds" \
8+
"*::arg:->args"
9+
10+
case "$state" in
11+
cmds)
12+
_values "simple command" \
13+
"colors[Shows available foreground and background colors]" \
14+
"completions[Generate zsh command completions]" \
15+
"help[List available commands]"
16+
;;
17+
args)
18+
case $line[1] in
19+
colors) _simple_colors ;;
20+
21+
completions) _simple_completions ;;
22+
23+
help) _simple_help ;;
24+
25+
esac
26+
;;
27+
esac
28+
}
29+
30+
_simple_colors() {
31+
_arguments -s \
32+
'(-h --help)'{-h,--help}$'[This command summary]'
33+
}
34+
35+
_simple_completions() {
36+
_arguments -s \
37+
'(-h --help)'{-h,--help}$'[This command summary]'
38+
}
39+
40+
_simple_help() {
41+
_arguments -s \
42+
'(-c --commands)'{-c,--commands}$'[Print commands: all, none, root]':FILTER \
43+
'(-h --help)'{-h,--help}$'[This command summary]'
44+
}
45+
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#compdef _subgroup subgroup
2+
3+
_subgroup() {
4+
local line state
5+
6+
_arguments -C \
7+
"1: :->cmds" \
8+
"*::arg:->args"
9+
10+
case "$state" in
11+
cmds)
12+
_values "subgroup command" \
13+
"completions[Generate zsh command completions]" \
14+
"help[List available commands]" \
15+
"subgroup[one sub-command]"
16+
;;
17+
args)
18+
case $line[1] in
19+
completions) _subgroup_completions ;;
20+
21+
help) _subgroup_help ;;
22+
23+
subgroup) _subgroup_subgroup ;;
24+
25+
esac
26+
;;
27+
esac
28+
}
29+
30+
_subgroup_completions() {
31+
_arguments -s \
32+
'(-h --help)'{-h,--help}$'[This command summary]'
33+
}
34+
35+
_subgroup_help() {
36+
_arguments -s \
37+
'(-c --commands)'{-c,--commands}$'[Print commands: all, none, root]':FILTER \
38+
'(-h --help)'{-h,--help}$'[This command summary]'
39+
}
40+
41+
_subgroup_subgroup() {
42+
local state line
43+
44+
_arguments -C \
45+
"1: :->cmds" \
46+
"*::arg:->args"
47+
48+
case "$state" in
49+
cmds)
50+
_values "subgroup subgroup subcommands" \
51+
"example[Does something interesting]"
52+
;;
53+
args)
54+
case $line[1] in
55+
example) _subgroup_subgroup_example ;;
56+
esac
57+
;;
58+
esac
59+
}
60+
61+
62+
_subgroup_subgroup_example() {
63+
_arguments -s \
64+
'(-v --verbose)'{-v,--verbose}$'[Extra output]' \
65+
'(-l --limit)'{-l,--limit}$'[Maximum globnars to frobnicate]':NUMBER \
66+
'(-h --help)'{-h,--help}$'[This command summary]'
67+
}
68+
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(ns net.lewisship.cli-tools.aux
22
(:require [clojure.test :refer [is]]
3-
[net.lewisship.cli-tools.test :refer [with-exit]]))
3+
[net.lewisship.cli-tools :as cli-tools]
4+
[net.lewisship.cli-tools.test :refer [with-exit capture-result]]))
45

56
(defmacro with-exit-errors
67
[expected-errors & body]
@@ -9,3 +10,8 @@
910
(reset! *errors# errors#))]
1011
(with-exit 1 ~@body))
1112
(is (= @*errors# ~expected-errors))))
13+
14+
(defn dispatch-with-result
15+
[options]
16+
(capture-result
17+
(cli-tools/dispatch (assoc options :cache-dir nil))))
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
(ns net.lewisship.cli-tools.completion-group
2+
(:require [net.lewisship.cli-tools :refer [defcommand]]))
3+
4+
(defcommand example
5+
"Does something interesting."
6+
[verbose? ["-v" "--verbose" "Extra output"]
7+
limit ["-l" "--limit NUMBER" "Maximum globnars to frobnicate"]]
8+
;; Make linter happy
9+
{:verbose? verbose?
10+
:limit limit})
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
(ns net.lewisship.cli-tools.completions-test
2+
"Tests related to command completions."
3+
(:require [clojure.java.io :as io]
4+
[clojure.string :as string]
5+
[clojure.test :refer [deftest is]]
6+
[net.lewisship.cli-tools.aux :refer [dispatch-with-result]]))
7+
8+
(defn- expected [file]
9+
(-> (str "expected/" file)
10+
io/resource
11+
slurp
12+
string/split-lines))
13+
14+
(defn- dispatch
15+
([options]
16+
(-> options
17+
(assoc :arguments ["completions"])
18+
dispatch-with-result
19+
:out-lines))
20+
([to options]
21+
(let [result (-> options
22+
(assoc :arguments ["completions"])
23+
dispatch-with-result
24+
:out)]
25+
(-> (str "test-resources/expected/" to)
26+
io/file
27+
(spit result))
28+
(println result))))
29+
30+
(deftest simple-completions
31+
(is (match? (expected "simple-completions.txt")
32+
(dispatch '{:tool-name "simple"
33+
:namespaces [net.lewisship.cli-tools.colors
34+
net.lewisship.cli-tools.completions]}))))
35+
36+
(deftest subgroup-completion
37+
(is (match? (expected "subgroup-completions.txt")
38+
(dispatch
39+
'{:tool-name "subgroup"
40+
:namespaces [net.lewisship.cli-tools.completions]
41+
:groups
42+
{"subgroup" {:namespaces [net.lewisship.cli-tools.completion-group]}}}))))
43+
44+
(deftest messy-completions
45+
;; where command name and group name collide
46+
;; Not sure the current behavior is correct
47+
(is (match? (expected "messy-completions.txt")
48+
(dispatch
49+
{:tool-name "messy"
50+
:namespaces '[net.lewisship.cli-tools.completions
51+
net.lewisship.messy-commands]
52+
:groups {"messy" {:namespaces '[net.lewisship.messy]
53+
:doc "Messy command and group at same time"}}}))))

0 commit comments

Comments
 (0)