cli.test.zsh 3.8 KB

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