File: test_completion.sh

package info (click to toggle)
beets 2.5.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,016 kB
  • sloc: python: 46,429; javascript: 8,018; xml: 334; sh: 261; makefile: 125
file content (185 lines) | stat: -rw-r--r-- 2,994 bytes parent folder | download | duplicates (6)
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# Function stub
compopt() { return 0; }

initcli() {
  COMP_WORDS=( "beet" "$@" )
  let COMP_CWORD=${#COMP_WORDS[@]}-1
  COMP_LINE="${COMP_WORDS[@]}"
  let COMP_POINT=${#COMP_LINE}
  _beet
}

completes() {
  for word in "$@"; do
    [[ " ${COMPREPLY[@]} " == *[[:space:]]$word[[:space:]]* ]] || return 1
  done
}

COMMANDS='fields import list update remove
          stats version modify move write
          help'

HELP_OPTS='-h --help'


test_commands() {
  initcli '' &&
  completes $COMMANDS &&

  initcli -v '' &&
  completes $COMMANDS &&

  initcli -l help '' &&
  completes $COMMANDS &&

  initcli -d list '' &&
  completes $COMMANDS &&

  initcli -h '' &&
  completes $COMMANDS &&
  true
}

test_command_aliases() {
  initcli ls &&
  completes list &&

  initcli l &&
  ! completes ls &&

  initcli im &&
  completes import &&
  true
}

test_global_opts() {
  initcli - &&
  completes \
    -l --library \
    -d --directory \
    -h --help \
    -c --config \
    -v --verbose &&
  true
}


test_global_file_opts() {
  # FIXME somehow file completion only works when the completion
  # function is called by the shell completion utilities. So we can't
  # test it here
  initcli --library '' &&
  completes $(compgen -d) &&

  initcli -l '' &&
  completes $(compgen -d) &&

  initcli --config '' &&
  completes $(compgen -d) &&

  initcli -c '' &&
  completes $(compgen -d) &&
  true
}


test_global_dir_opts() {
  initcli --directory '' &&
  completes $(compgen -d) &&

  initcli -d '' &&
  completes $(compgen -d) &&
  true
}


test_fields_command() {
  initcli fields - &&
  completes -h --help &&

  initcli fields '' &&
  completes $(compgen -d) &&
  true
}


test_import_files() {
  initcli import '' &&
  completes $(compgen -d) &&

  initcli import --copy -P '' &&
  completes $(compgen -d) &&

  initcli import --log '' &&
  completes $(compgen -d) &&
  true
}


test_import_options() {
  initcli imp -
  completes \
    -h --help \
    -c --copy -C --nocopy \
    -w --write -W --nowrite \
    -a --autotag -A --noautotag \
    -p --resume -P --noresume \
    -l --log --flat
}


test_list_options() {
  initcli list -
  completes \
    -h --help \
    -a --album \
    -p --path
}

test_list_query() {
  initcli list 'x' &&
  [[ -z "${COMPREPLY[@]}" ]] &&

  initcli list 'art' &&
  completes \
      'artist:' \
      'artpath:' &&

  initcli list 'artits:x' &&
  [[ -z "${COMPREPLY[@]}" ]] &&
  true
}

test_help_command() {
  initcli help '' &&
  completes $COMMANDS &&
  true
}

test_plugin_command() {
  initcli te &&
  completes test &&

  initcli test - &&
  completes -o --option &&
  true
}

run_tests() {
  local tests=$(set | \
    grep --extended-regexp --only-matching '^test_[a-zA-Z_]* \(\) $' |\
    grep --extended-regexp --only-matching '[a-zA-Z_]*'
  )
  local fail=0

  if  [[ -n $@ ]]; then
    tests="$@"
  fi

  for t in $tests; do
    $t || { fail=1 && echo "$t failed" >&2; }
  done
  return $fail
}

run_tests "$@" && echo "completion tests passed"