File: completion_spec.rb

package info (click to toggle)
ruby-clamp 1.5.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 416 kB
  • sloc: ruby: 3,824; sh: 69; makefile: 4
file content (340 lines) | stat: -rw-r--r-- 9,073 bytes parent folder | download
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
# frozen_string_literal: true

require "spec_helper"
require "clamp/completion"
require "tempfile"

RSpec::Matchers.define :be_valid_fish_syntax do
  match do |script|
    Tempfile.open(["completion", ".fish"]) do |f|
      f.write(script)
      f.flush
      system("fish", "--no-execute", f.path, out: File::NULL, err: File::NULL)
    end
  end
end

RSpec::Matchers.define :be_valid_zsh_syntax do
  match do |script|
    Tempfile.open(["completion", ".zsh"]) do |f|
      f.write(script)
      f.flush
      system("zsh", "-n", f.path, out: File::NULL, err: File::NULL)
    end
  end
end

RSpec::Matchers.define :be_valid_bash_syntax do
  match do |script|
    Tempfile.open(["completion", ".bash"]) do |f|
      f.write(script)
      f.flush
      system("bash", "-n", f.path, out: File::NULL, err: File::NULL)
    end
  end
end

describe Clamp::Completion do

  let(:command_class) do
    Class.new(Clamp::Command) do
      option ["-v", "--verbose"], :flag, "be verbose"
      option "--format", "FORMAT", "output format"
      option "--[no-]color", :flag, "use color"
      option "--secret", :flag, "secret option", hidden: true

      subcommand "remote", "manage remotes" do
        subcommand "add", "add a remote" do
          option "--tracking", :flag, "set up tracking"
        end
        subcommand ["remove", "rm"], "remove a remote"
      end

      subcommand "status", "show status"
    end
  end

  describe ".generate_completion" do

    it "raises for unsupported shells" do
      expect { command_class.generate_completion(:powershell, "myapp") }
        .to raise_error(ArgumentError, /unsupported shell/)
    end

  end

  describe "--shell-completions option" do

    include OutputCapture

    let(:simple_command) do
      Class.new(Clamp::Command) do
        option ["-v", "--verbose"], :flag, "be verbose"
        parameter "FILE", "input file"
        def execute; end
      end
    end

    it "generates completions for a command without subcommands" do
      simple_command.run("myapp", ["--shell-completions", "fish"])
      expect(stdout).to include("complete -c myapp")
    end

    it "includes the command's options" do
      simple_command.run("myapp", ["--shell-completions", "fish"])
      expect(stdout).to include("-l verbose")
    end

    it "accepts a full shell path" do
      simple_command.run("myapp", ["--shell-completions", "/usr/bin/fish"])
      expect(stdout).to include("complete -c myapp")
    end

    it "is hidden from help output" do
      expect(simple_command.help("myapp")).not_to include("--shell-completions")
    end

    it "does not include itself in generated completions" do
      simple_command.run("myapp", ["--shell-completions", "fish"])
      expect(stdout).not_to include("--shell-completions")
    end

    it "exits for unsupported shells" do
      expect { simple_command.run("myapp", ["--shell-completions", "powershell"]) }
        .to raise_error(SystemExit)
    end

    it "reports unsupported shells to stderr" do
      simple_command.run("myapp", ["--shell-completions", "powershell"])
    rescue SystemExit # rubocop:disable Lint/SuppressedException
    ensure
      expect(stderr).to include("unsupported shell")
    end

  end

  describe "bash" do

    let(:script) { command_class.generate_completion(:bash, "myapp") }

    it "returns a string" do
      expect(script).to be_a(String)
    end

    it "includes long option switches" do
      expect(script).to include("--verbose")
    end

    it "includes valued option switches" do
      expect(script).to include("--format")
    end

    it "includes short switches" do
      expect(script).to include("-v")
    end

    it "expands --[no-] options to positive form" do
      expect(script).to include("--color")
    end

    it "expands --[no-] options to negative form" do
      expect(script).to include("--no-color")
    end

    it "excludes hidden options" do
      expect(script).not_to include("secret")
    end

    it "includes help option" do
      expect(script).to include("--help")
    end

    it "includes subcommand names" do
      expect(script).to include("remote")
    end

    it "includes other subcommand names" do
      expect(script).to include("status")
    end

    it "includes subcommand aliases" do
      expect(script).to include("rm")
    end

    it "includes nested subcommand options" do
      expect(script).to include("--tracking")
    end

    it "registers the completion function" do
      expect(script).to include("complete -F _clamp_complete_myapp myapp")
    end

    it "lists valued options in takes_value function" do
      expect(script).to include("--format) return 0")
    end

    it "excludes flags from takes_value function" do
      expect(script).not_to include("--verbose) return 0")
    end

    it "passes bash syntax validation" do
      skip "bash not available" unless system("bash", "--version", out: File::NULL, err: File::NULL)
      expect(script).to be_valid_bash_syntax
    end

  end

  describe "zsh" do

    let(:script) { command_class.generate_completion(:zsh, "myapp") }

    it "returns a string" do
      expect(script).to be_a(String)
    end

    it "includes compdef header" do
      expect(script).to include("#compdef myapp")
    end

    it "includes option specs with mutual exclusion" do
      expect(script).to include("(-v --verbose)")
    end

    it "includes brace expansion for short and long switches" do
      expect(script).to include("{-v,--verbose}")
    end

    it "includes option descriptions" do
      expect(script).to include("[be verbose]")
    end

    it "marks valued options as requiring an argument" do
      expect(script).to match(/--format\[output format\]:/)
    end

    it "expands --[no-] options to positive form" do
      expect(script).to include("--color")
    end

    it "expands --[no-] options to negative form" do
      expect(script).to include("--no-color")
    end

    it "excludes hidden options" do
      expect(script).not_to include("secret")
    end

    it "includes help option" do
      expect(script).to include("--help")
    end

    it "includes subcommand names with descriptions" do
      expect(script).to include("remote:manage remotes")
    end

    it "includes subcommand aliases" do
      expect(script).to include("rm:remove a remote")
    end

    it "generates per-subcommand functions" do
      expect(script).to include("_myapp_remote()")
    end

    it "generates nested subcommand functions" do
      expect(script).to include("_myapp_remote_add()")
    end

    it "includes nested subcommand options" do
      expect(script).to include("[set up tracking]")
    end

    it "uses _describe for subcommand listing" do
      expect(script).to include("_describe")
    end

    it "passes zsh syntax validation" do
      skip "zsh not available" unless system("zsh", "--version", out: File::NULL, err: File::NULL)
      expect(script).to be_valid_zsh_syntax
    end

  end

  describe "fish" do

    let(:script) { command_class.generate_completion(:fish, "myapp") }

    it "returns a string" do
      expect(script).to be_a(String)
    end

    it "includes short switches" do
      expect(script).to include("-s v")
    end

    it "includes long switches" do
      expect(script).to include("-l verbose")
    end

    it "includes valued option switches" do
      expect(script).to include("-l format")
    end

    it "expands --[no-] options to positive form" do
      expect(script).to include("-l color")
    end

    it "expands --[no-] options to negative form" do
      expect(script).to include("-l no-color")
    end

    it "excludes --shell-completions from completions" do
      expect(script).not_to match(/^complete\b.*shell-completions/)
    end

    it "includes help option" do
      expect(script).to include("-l help")
    end

    it "includes subcommand names" do
      expect(script).to match(/-a remote\b/)
    end

    it "includes other subcommand names" do
      expect(script).to match(/-a status\b/)
    end

    it "includes subcommand aliases" do
      expect(script).to match(/-a rm\b/)
    end

    it "includes subcommand descriptions" do
      expect(script).to include("manage remotes")
    end

    it "includes nested subcommand options" do
      expect(script).to include("-l tracking")
    end

    it "marks valued options as requiring an argument" do
      format_line = script.lines.find { |l| l.include?("-l format") }
      expect(format_line).to include("-r")
    end

    it "does not mark flag options as requiring an argument" do
      verbose_line = script.lines.find { |l| l.include?("-l verbose") }
      expect(verbose_line).not_to include("-r")
    end

    it "emits inherited options only once" do
      verbose_lines = script.lines.grep(/-l verbose/)
      expect(verbose_lines.length).to eq(1)
    end

    it "passes fish syntax validation" do
      skip "fish not available" unless system("fish", "--version", out: File::NULL, err: File::NULL)
      expect(script).to be_valid_fish_syntax
    end

  end

end