summaryrefslogtreecommitdiff
path: root/lib/tests/cli.test.zsh
blob: 9ee5cd219f7be9e0d6a07b9fe985b8d6ff1d21ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
#!/usr/bin/zsh -df

run_awk() {
  local -a dis_plugins=(${=1})
  local input_text="$2"

  (( ! DEBUG )) || set -xv

  local awk_subst_plugins="\
    gsub(/[ \t]+(${(j:|:)dis_plugins})[ \t]+/, \" \") # with spaces before or after
    gsub(/[ \t]+(${(j:|:)dis_plugins})$/, \"\")       # with spaces before and EOL
    gsub(/^(${(j:|:)dis_plugins})[ \t]+/, \"\")       # with BOL and spaces after

    gsub(/\((${(j:|:)dis_plugins})[ \t]+/, \"(\")     # with parenthesis before and spaces after
    gsub(/[ \t]+(${(j:|:)dis_plugins})\)/, \")\")     # with spaces before or parenthesis after
    gsub(/\((${(j:|:)dis_plugins})\)/, \"()\")        # with only parentheses

    gsub(/^(${(j:|:)dis_plugins})\)/, \")\")          # with BOL and closing parenthesis
    gsub(/\((${(j:|:)dis_plugins})$/, \"(\")          # with opening parenthesis and EOL
  "
    # Disable plugins awk script
    local awk_script="
  # if plugins=() is in oneline form, substitute disabled plugins and go to next line
  /^[ \t]*plugins=\([^#]+\).*\$/ {
    $awk_subst_plugins
    print \$0
    next
  }

  # if plugins=() is in multiline form, enable multi flag and disable plugins if they're there
  /^[ \t]*plugins=\(/ {
    multi=1
    $awk_subst_plugins
    print \$0
    next
  }

  # if multi flag is enabled and we find a valid closing parenthesis, remove plugins and disable multi flag
  multi == 1 && /^[^#]*\)/ {
    multi=0
    $awk_subst_plugins
    print \$0
    next
  }

  multi == 1 && length(\$0) > 0 {
    $awk_subst_plugins
    if (length(\$0) > 0) print \$0
    next
  }

  { print \$0 }
  "

  command awk "$awk_script" <<< "$input_text"

  (( ! DEBUG )) || set +xv
}

# runs awk against stdin, checks if the resulting file is not empty and then checks if the file has valid zsh syntax
run_awk_and_test() {
  local description="$1"
  local plugins_to_disable="$2"
  local input_text="$3"
  local expected_output="$4"

  local tmpfile==(:)

  {
    print -u2 "Test: $description"
    DEBUG=0 run_awk "$plugins_to_disable" "$input_text" >| $tmpfile

    if [[ ! -s "$tmpfile" ]]; then
      print -u2 "\e[31mError\e[0m: output file empty"
      return 1
    fi

    if ! zsh -n $tmpfile; then
      print -u2 "\e[31mError\e[0m: zsh syntax error"
      diff -u $tmpfile <(echo "$expected_output")
      return 1
    fi

    if ! diff -u --color=always $tmpfile <(echo "$expected_output"); then
      if (( DEBUG )); then
        print -u2 ""
        DEBUG=1 run_awk "$plugins_to_disable" "$input_text"
        print -u2 ""
      fi
      print -u2 "\e[31mError\e[0m: output file does not match expected output"
      return 1
    fi

    print -u2 "\e[32mSuccess\e[0m"
  } always {
    print -u2 ""
    command rm -f "$tmpfile"
  }
}

# These tests are for the `omz plugin disable` command
run_awk_and_test \
  "it should delete a single plugin in oneline format" \
  "git" \
  "plugins=(git)" \
  "plugins=()"

run_awk_and_test \
  "it should delete a single plugin in multiline format" \
  "github" \
"plugins=(
  github
)" \
"plugins=(
)"

run_awk_and_test \
  "it should delete multiple plugins in oneline format" \
  "github git z" \
  "plugins=(github git z)" \
  "plugins=()"

run_awk_and_test \
  "it should delete multiple plugins in multiline format" \
  "github git z" \
"plugins=(
  github
  git
  z
)" \
"plugins=(
)"

run_awk_and_test \
  "it should delete a single plugin among multiple in oneline format" \
  "git" \
  "plugins=(github git z)" \
  "plugins=(github z)"

run_awk_and_test \
  "it should delete a single plugin among multiple in multiline format" \
  "git" \
"plugins=(
  github
  git
  z
)" \
"plugins=(
  github
  z
)"

run_awk_and_test \
  "it should delete multiple plugins in mixed format" \
  "git z" \
"plugins=(github
git z)" \
"plugins=(github
)"

run_awk_and_test \
  "it should delete multiple plugins in mixed format 2" \
  "github z" \
"plugins=(github
  git
z)" \
"plugins=(
  git
)"