cli.test.zsh
3919 bytes
1#!/usr/bin/zsh -df
2
3run_awk() {
4 local -a dis_plugins=(${=1})
5 local input_text="$2"
6
7 (( ! DEBUG )) || set -xv
8
9 local awk_subst_plugins="\
10 gsub(/[ \t]+(${(j:|:)dis_plugins})[ \t]+/, \" \") # with spaces before or after
11 gsub(/[ \t]+(${(j:|:)dis_plugins})$/, \"\") # with spaces before and EOL
12 gsub(/^(${(j:|:)dis_plugins})[ \t]+/, \"\") # with BOL and spaces after
13
14 gsub(/\((${(j:|:)dis_plugins})[ \t]+/, \"(\") # with parenthesis before and spaces after
15 gsub(/[ \t]+(${(j:|:)dis_plugins})\)/, \")\") # with spaces before or parenthesis after
16 gsub(/\((${(j:|:)dis_plugins})\)/, \"()\") # with only parentheses
17
18 gsub(/^(${(j:|:)dis_plugins})\)/, \")\") # with BOL and closing parenthesis
19 gsub(/\((${(j:|:)dis_plugins})$/, \"(\") # with opening parenthesis and EOL
20 "
21 # Disable plugins awk script
22 local awk_script="
23 # if plugins=() is in oneline form, substitute disabled plugins and go to next line
24 /^[ \t]*plugins=\([^#]+\).*\$/ {
25 $awk_subst_plugins
26 print \$0
27 next
28 }
29
30 # if plugins=() is in multiline form, enable multi flag and disable plugins if they're there
31 /^[ \t]*plugins=\(/ {
32 multi=1
33 $awk_subst_plugins
34 print \$0
35 next
36 }
37
38 # if multi flag is enabled and we find a valid closing parenthesis, remove plugins and disable multi flag
39 multi == 1 && /^[^#]*\)/ {
40 multi=0
41 $awk_subst_plugins
42 print \$0
43 next
44 }
45
46 multi == 1 && length(\$0) > 0 {
47 $awk_subst_plugins
48 if (length(\$0) > 0) print \$0
49 next
50 }
51
52 { print \$0 }
53 "
54
55 command awk "$awk_script" <<< "$input_text"
56
57 (( ! DEBUG )) || set +xv
58}
59
60# runs awk against stdin, checks if the resulting file is not empty and then checks if the file has valid zsh syntax
61run_awk_and_test() {
62 local description="$1"
63 local plugins_to_disable="$2"
64 local input_text="$3"
65 local expected_output="$4"
66
67 local tmpfile==(:)
68
69 {
70 print -u2 "Test: $description"
71 DEBUG=0 run_awk "$plugins_to_disable" "$input_text" >| $tmpfile
72
73 if [[ ! -s "$tmpfile" ]]; then
74 print -u2 "\e[31mError\e[0m: output file empty"
75 return 1
76 fi
77
78 if ! zsh -n $tmpfile; then
79 print -u2 "\e[31mError\e[0m: zsh syntax error"
80 diff -u $tmpfile <(echo "$expected_output")
81 return 1
82 fi
83
84 if ! diff -u --color=always $tmpfile <(echo "$expected_output"); then
85 if (( DEBUG )); then
86 print -u2 ""
87 DEBUG=1 run_awk "$plugins_to_disable" "$input_text"
88 print -u2 ""
89 fi
90 print -u2 "\e[31mError\e[0m: output file does not match expected output"
91 return 1
92 fi
93
94 print -u2 "\e[32mSuccess\e[0m"
95 } always {
96 print -u2 ""
97 command rm -f "$tmpfile"
98 }
99}
100
101# These tests are for the `omz plugin disable` command
102run_awk_and_test \
103 "it should delete a single plugin in oneline format" \
104 "git" \
105 "plugins=(git)" \
106 "plugins=()"
107
108run_awk_and_test \
109 "it should delete a single plugin in multiline format" \
110 "github" \
111"plugins=(
112 github
113)" \
114"plugins=(
115)"
116
117run_awk_and_test \
118 "it should delete multiple plugins in oneline format" \
119 "github git z" \
120 "plugins=(github git z)" \
121 "plugins=()"
122
123run_awk_and_test \
124 "it should delete multiple plugins in multiline format" \
125 "github git z" \
126"plugins=(
127 github
128 git
129 z
130)" \
131"plugins=(
132)"
133
134run_awk_and_test \
135 "it should delete a single plugin among multiple in oneline format" \
136 "git" \
137 "plugins=(github git z)" \
138 "plugins=(github z)"
139
140run_awk_and_test \
141 "it should delete a single plugin among multiple in multiline format" \
142 "git" \
143"plugins=(
144 github
145 git
146 z
147)" \
148"plugins=(
149 github
150 z
151)"
152
153run_awk_and_test \
154 "it should delete multiple plugins in mixed format" \
155 "git z" \
156"plugins=(github
157git z)" \
158"plugins=(github
159)"
160
161run_awk_and_test \
162 "it should delete multiple plugins in mixed format 2" \
163 "github z" \
164"plugins=(github
165 git
166z)" \
167"plugins=(
168 git
169)"