summaryrefslogtreecommitdiff
path: root/lib/elixir/test/elixir/option_parser_test.exs
blob: 7f38e4264431d63d35e8a262c50f475dbcc5409e (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
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
Code.require_file("test_helper.exs", __DIR__)

defmodule OptionParserTest do
  use ExUnit.Case, async: true

  doctest OptionParser

  test "parses --key value option" do
    assert OptionParser.parse(["--source", "form_docs/", "other"], switches: [source: :string]) ==
             {[source: "form_docs/"], ["other"], []}
  end

  test "parses --key=value option" do
    assert OptionParser.parse(["--source=form_docs/", "other"], switches: [source: :string]) ==
             {[source: "form_docs/"], ["other"], []}
  end

  test "parses overrides options by default" do
    assert OptionParser.parse(
             ["--require", "foo", "--require", "bar", "baz"],
             switches: [require: :string]
           ) == {[require: "bar"], ["baz"], []}
  end

  test "parses multi-word option" do
    config = [switches: [hello_world: :boolean]]
    assert OptionParser.next(["--hello-world"], config) == {:ok, :hello_world, true, []}
    assert OptionParser.next(["--no-hello-world"], config) == {:ok, :hello_world, false, []}

    assert OptionParser.next(["--no-hello-world"], strict: []) ==
             {:undefined, "--no-hello-world", nil, []}

    assert OptionParser.next(["--no-hello_world"], strict: []) ==
             {:undefined, "--no-hello_world", nil, []}

    config = [strict: [hello_world: :boolean]]
    assert OptionParser.next(["--hello-world"], config) == {:ok, :hello_world, true, []}
    assert OptionParser.next(["--no-hello-world"], config) == {:ok, :hello_world, false, []}
    assert OptionParser.next(["--hello_world"], config) == {:undefined, "--hello_world", nil, []}

    assert OptionParser.next(["--no-hello_world"], config) ==
             {:undefined, "--no-hello_world", nil, []}
  end

  test "parses more than one key-value pair options using switches" do
    opts = [switches: [source: :string, docs: :string]]

    assert OptionParser.parse(["--source", "from_docs/", "--docs", "show"], opts) ==
             {[source: "from_docs/", docs: "show"], [], []}

    assert OptionParser.parse(["--source", "from_docs/", "--doc", "show"], opts) ==
             {[source: "from_docs/", doc: "show"], [], []}

    assert OptionParser.parse(["--source", "from_docs/", "--doc=show"], opts) ==
             {[source: "from_docs/", doc: "show"], [], []}

    assert OptionParser.parse(["--no-bool"], strict: []) == {[], [], [{"--no-bool", nil}]}
  end

  test "parses more than one key-value pair options using strict" do
    opts = [strict: [source: :string, docs: :string]]

    assert OptionParser.parse(["--source", "from_docs/", "--docs", "show"], opts) ==
             {[source: "from_docs/", docs: "show"], [], []}

    assert OptionParser.parse(["--source", "from_docs/", "--doc", "show"], opts) ==
             {[source: "from_docs/"], ["show"], [{"--doc", nil}]}

    assert OptionParser.parse(["--source", "from_docs/", "--doc=show"], opts) ==
             {[source: "from_docs/"], [], [{"--doc", nil}]}

    assert OptionParser.parse(["--no-bool"], strict: []) == {[], [], [{"--no-bool", nil}]}
  end

  test "collects multiple invalid options" do
    argv = ["--bad", "opt", "foo", "-o", "bad", "bar"]

    assert OptionParser.parse(argv, switches: [bad: :integer]) ==
             {[], ["foo", "bar"], [{"--bad", "opt"}]}
  end

  test "parse/2 raises when using both options: switches and strict" do
    assert_raise ArgumentError, ":switches and :strict cannot be given together", fn ->
      OptionParser.parse(["--elixir"], switches: [ex: :string], strict: [elixir: :string])
    end
  end

  test "parse/2 raises an exception on invalid switch types/modifiers" do
    assert_raise ArgumentError, "invalid switch types/modifiers: :bad", fn ->
      OptionParser.parse(["--elixir"], switches: [ex: :bad])
    end

    assert_raise ArgumentError, "invalid switch types/modifiers: :bad, :bad_modifier", fn ->
      OptionParser.parse(["--elixir"], switches: [ex: [:bad, :bad_modifier]])
    end
  end

  test "parse!/2 raises an exception for an unknown option using strict" do
    msg = "1 error found!\n--doc-bar : Unknown option. Did you mean --docs-bar?"

    assert_raise OptionParser.ParseError, msg, fn ->
      argv = ["--source", "from_docs/", "--doc-bar", "show"]
      OptionParser.parse!(argv, strict: [source: :string, docs_bar: :string])
    end

    assert_raise OptionParser.ParseError, "1 error found!\n--foo : Unknown option", fn ->
      argv = ["--source", "from_docs/", "--foo", "show"]
      OptionParser.parse!(argv, strict: [source: :string, docs: :string])
    end
  end

  test "parse!/2 raises an exception when an option is of the wrong type" do
    assert_raise OptionParser.ParseError, fn ->
      argv = ["--bad", "opt", "foo", "-o", "bad", "bar"]
      OptionParser.parse!(argv, switches: [bad: :integer])
    end
  end

  test "parse_head!/2 raises an exception when an option is of the wrong type" do
    message = "1 error found!\n--number : Expected type integer, got \"lib\""

    assert_raise OptionParser.ParseError, message, fn ->
      argv = ["--number", "lib", "test/enum_test.exs"]
      OptionParser.parse_head!(argv, strict: [number: :integer])
    end
  end

  describe "arguments" do
    test "parses until --" do
      assert OptionParser.parse(
               ["--source", "foo", "--", "1", "2", "3"],
               switches: [source: :string]
             ) == {[source: "foo"], ["1", "2", "3"], []}

      assert OptionParser.parse_head(
               ["--source", "foo", "--", "1", "2", "3"],
               switches: [source: :string]
             ) == {[source: "foo"], ["1", "2", "3"], []}

      assert OptionParser.parse(
               ["--source", "foo", "bar", "--", "-x"],
               switches: [source: :string]
             ) == {[source: "foo"], ["bar", "-x"], []}

      assert OptionParser.parse_head(
               ["--source", "foo", "bar", "--", "-x"],
               switches: [source: :string]
             ) == {[source: "foo"], ["bar", "--", "-x"], []}
    end

    test "parses - as argument" do
      argv = ["--foo", "-", "-b", "-"]
      opts = [strict: [foo: :boolean, boo: :string], aliases: [b: :boo]]
      assert OptionParser.parse(argv, opts) == {[foo: true, boo: "-"], ["-"], []}
    end

    test "parses until first non-option arguments" do
      argv = ["--source", "from_docs/", "test/enum_test.exs", "--verbose"]

      assert OptionParser.parse_head(argv, switches: [source: :string]) ==
               {[source: "from_docs/"], ["test/enum_test.exs", "--verbose"], []}
    end
  end

  describe "aliases" do
    test "supports boolean aliases" do
      assert OptionParser.parse(["-d"], aliases: [d: :docs], switches: [docs: :boolean]) ==
               {[docs: true], [], []}
    end

    test "supports non-boolean aliases" do
      assert OptionParser.parse(
               ["-s", "from_docs/"],
               aliases: [s: :source],
               switches: [source: :string]
             ) == {[source: "from_docs/"], [], []}
    end

    test "supports --key=value aliases" do
      assert OptionParser.parse(
               ["-s=from_docs/", "other"],
               aliases: [s: :source],
               switches: [source: :string]
             ) == {[source: "from_docs/"], ["other"], []}
    end

    test "parses -ab as -a -b" do
      opts = [aliases: [a: :first, b: :second], switches: [second: :integer]]
      assert OptionParser.parse(["-ab=1"], opts) == {[first: true, second: 1], [], []}
      assert OptionParser.parse(["-ab", "1"], opts) == {[first: true, second: 1], [], []}

      opts = [aliases: [a: :first, b: :second], switches: [first: :boolean, second: :boolean]]
      assert OptionParser.parse(["-ab"], opts) == {[first: true, second: true], [], []}
      assert OptionParser.parse(["-ab3"], opts) == {[first: true], [], [{"-b", "3"}]}
      assert OptionParser.parse(["-ab=bar"], opts) == {[first: true], [], [{"-b", "bar"}]}
      assert OptionParser.parse(["-ab3=bar"], opts) == {[first: true], [], [{"-b", "3=bar"}]}
      assert OptionParser.parse(["-3ab"], opts) == {[], ["-3ab"], []}
    end
  end

  describe "types" do
    test "parses configured booleans" do
      assert OptionParser.parse(["--docs=false"], switches: [docs: :boolean]) ==
               {[docs: false], [], []}

      assert OptionParser.parse(["--docs=true"], switches: [docs: :boolean]) ==
               {[docs: true], [], []}

      assert OptionParser.parse(["--docs=other"], switches: [docs: :boolean]) ==
               {[], [], [{"--docs", "other"}]}

      assert OptionParser.parse(["--docs="], switches: [docs: :boolean]) ==
               {[], [], [{"--docs", ""}]}

      assert OptionParser.parse(["--docs", "foo"], switches: [docs: :boolean]) ==
               {[docs: true], ["foo"], []}

      assert OptionParser.parse(["--no-docs", "foo"], switches: [docs: :boolean]) ==
               {[docs: false], ["foo"], []}

      assert OptionParser.parse(["--no-docs=foo", "bar"], switches: [docs: :boolean]) ==
               {[], ["bar"], [{"--no-docs", "foo"}]}

      assert OptionParser.parse(["--no-docs=", "bar"], switches: [docs: :boolean]) ==
               {[], ["bar"], [{"--no-docs", ""}]}
    end

    test "does not set unparsed booleans" do
      assert OptionParser.parse(["foo"], switches: [docs: :boolean]) == {[], ["foo"], []}
    end

    test "keeps options on configured keep" do
      argv = ["--require", "foo", "--require", "bar", "baz"]

      assert OptionParser.parse(argv, switches: [require: :keep]) ==
               {[require: "foo", require: "bar"], ["baz"], []}

      assert OptionParser.parse(["--require"], switches: [require: :keep]) ==
               {[], [], [{"--require", nil}]}
    end

    test "parses configured strings" do
      assert OptionParser.parse(["--value", "1", "foo"], switches: [value: :string]) ==
               {[value: "1"], ["foo"], []}

      assert OptionParser.parse(["--value=1", "foo"], switches: [value: :string]) ==
               {[value: "1"], ["foo"], []}

      assert OptionParser.parse(["--value"], switches: [value: :string]) ==
               {[], [], [{"--value", nil}]}

      assert OptionParser.parse(["--no-value"], switches: [value: :string]) ==
               {[no_value: true], [], []}
    end

    test "parses configured counters" do
      assert OptionParser.parse(["--verbose"], switches: [verbose: :count]) ==
               {[verbose: 1], [], []}

      assert OptionParser.parse(["--verbose", "--verbose"], switches: [verbose: :count]) ==
               {[verbose: 2], [], []}

      argv = ["--verbose", "-v", "-v", "--", "bar"]
      opts = [aliases: [v: :verbose], strict: [verbose: :count]]
      assert OptionParser.parse(argv, opts) == {[verbose: 3], ["bar"], []}
    end

    test "parses configured integers" do
      assert OptionParser.parse(["--value", "1", "foo"], switches: [value: :integer]) ==
               {[value: 1], ["foo"], []}

      assert OptionParser.parse(["--value=1", "foo"], switches: [value: :integer]) ==
               {[value: 1], ["foo"], []}

      assert OptionParser.parse(["--value", "WAT", "foo"], switches: [value: :integer]) ==
               {[], ["foo"], [{"--value", "WAT"}]}
    end

    test "parses configured integers with keep" do
      argv = ["--value", "1", "--value", "2", "foo"]

      assert OptionParser.parse(argv, switches: [value: [:integer, :keep]]) ==
               {[value: 1, value: 2], ["foo"], []}

      argv = ["--value=1", "foo", "--value=2", "bar"]

      assert OptionParser.parse(argv, switches: [value: [:integer, :keep]]) ==
               {[value: 1, value: 2], ["foo", "bar"], []}
    end

    test "parses configured floats" do
      assert OptionParser.parse(["--value", "1.0", "foo"], switches: [value: :float]) ==
               {[value: 1.0], ["foo"], []}

      assert OptionParser.parse(["--value=1.0", "foo"], switches: [value: :float]) ==
               {[value: 1.0], ["foo"], []}

      assert OptionParser.parse(["--value", "WAT", "foo"], switches: [value: :float]) ==
               {[], ["foo"], [{"--value", "WAT"}]}
    end

    test "correctly handles negative integers" do
      opts = [switches: [option: :integer], aliases: [o: :option]]
      assert OptionParser.parse(["arg1", "-o43"], opts) == {[option: 43], ["arg1"], []}
      assert OptionParser.parse(["arg1", "-o", "-43"], opts) == {[option: -43], ["arg1"], []}
      assert OptionParser.parse(["arg1", "--option=-43"], opts) == {[option: -43], ["arg1"], []}

      assert OptionParser.parse(["arg1", "--option", "-43"], opts) ==
               {[option: -43], ["arg1"], []}
    end

    test "correctly handles negative floating-point numbers" do
      opts = [switches: [option: :float], aliases: [o: :option]]
      assert OptionParser.parse(["arg1", "-o43.2"], opts) == {[option: 43.2], ["arg1"], []}
      assert OptionParser.parse(["arg1", "-o", "-43.2"], opts) == {[option: -43.2], ["arg1"], []}

      assert OptionParser.parse(["arg1", "--option=-43.2"], switches: [option: :float]) ==
               {[option: -43.2], ["arg1"], []}

      assert OptionParser.parse(["arg1", "--option", "-43.2"], opts) ==
               {[option: -43.2], ["arg1"], []}
    end
  end

  describe "next" do
    test "with strict good options" do
      config = [strict: [str: :string, int: :integer, bool: :boolean]]
      assert OptionParser.next(["--str", "hello", "..."], config) == {:ok, :str, "hello", ["..."]}
      assert OptionParser.next(["--int=13", "..."], config) == {:ok, :int, 13, ["..."]}
      assert OptionParser.next(["--bool=false", "..."], config) == {:ok, :bool, false, ["..."]}
      assert OptionParser.next(["--no-bool", "..."], config) == {:ok, :bool, false, ["..."]}
      assert OptionParser.next(["--bool", "..."], config) == {:ok, :bool, true, ["..."]}
      assert OptionParser.next(["..."], config) == {:error, ["..."]}
    end

    test "with strict unknown options" do
      config = [strict: [bool: :boolean]]

      assert OptionParser.next(["--str", "13", "..."], config) ==
               {:undefined, "--str", nil, ["13", "..."]}

      assert OptionParser.next(["--int=hello", "..."], config) ==
               {:undefined, "--int", "hello", ["..."]}

      assert OptionParser.next(["-no-bool=other", "..."], config) ==
               {:undefined, "-no-bool", "other", ["..."]}
    end

    test "with strict bad type" do
      config = [strict: [str: :string, int: :integer, bool: :boolean]]
      assert OptionParser.next(["--str", "13", "..."], config) == {:ok, :str, "13", ["..."]}

      assert OptionParser.next(["--int=hello", "..."], config) ==
               {:invalid, "--int", "hello", ["..."]}

      assert OptionParser.next(["--int", "hello", "..."], config) ==
               {:invalid, "--int", "hello", ["..."]}

      assert OptionParser.next(["--bool=other", "..."], config) ==
               {:invalid, "--bool", "other", ["..."]}
    end

    test "with strict missing value" do
      config = [strict: [str: :string, int: :integer, bool: :boolean]]
      assert OptionParser.next(["--str"], config) == {:invalid, "--str", nil, []}
      assert OptionParser.next(["--int"], config) == {:invalid, "--int", nil, []}
      assert OptionParser.next(["--bool=", "..."], config) == {:invalid, "--bool", "", ["..."]}

      assert OptionParser.next(["--no-bool=", "..."], config) ==
               {:invalid, "--no-bool", "", ["..."]}
    end
  end

  test "split" do
    assert OptionParser.split(~S[]) == []
    assert OptionParser.split(~S[foo]) == ["foo"]
    assert OptionParser.split(~S[foo bar]) == ["foo", "bar"]
    assert OptionParser.split(~S[  foo  bar  ]) == ["foo", "bar"]
    assert OptionParser.split(~S[foo\ bar]) == ["foo bar"]
    assert OptionParser.split(~S[foo" bar"]) == ["foo bar"]
    assert OptionParser.split(~S[foo\" bar\"]) == ["foo\"", "bar\""]
    assert OptionParser.split(~S[foo "\ bar\""]) == ["foo", "\\ bar\""]
    assert OptionParser.split(~S[foo '\"bar"\'\ ']) == ["foo", "\\\"bar\"'\\ "]
  end

  describe "to_argv" do
    test "converts options back to switches" do
      assert OptionParser.to_argv(foo_bar: "baz") == ["--foo-bar", "baz"]

      assert OptionParser.to_argv(bool: true, bool: false, discarded: nil) ==
               ["--bool", "--no-bool"]
    end

    test "handles :count switch type" do
      original = ["--counter", "--counter"]
      {opts, [], []} = OptionParser.parse(original, switches: [counter: :count])
      assert original == OptionParser.to_argv(opts, switches: [counter: :count])
    end
  end
end

defmodule OptionsParserDeprecationsTest do
  use ExUnit.Case, async: true

  @warning ~r[not passing the :switches or :strict option to OptionParser is deprecated]

  def assert_deprecated(fun) do
    assert ExUnit.CaptureIO.capture_io(:stderr, fun) =~ @warning
  end

  test "parses boolean option" do
    assert_deprecated(fn ->
      assert OptionParser.parse(["--docs"]) == {[docs: true], [], []}
    end)
  end

  test "parses more than one boolean option" do
    assert_deprecated(fn ->
      assert OptionParser.parse(["--docs", "--compile"]) == {[docs: true, compile: true], [], []}
    end)
  end

  test "parses more than one boolean options as the alias" do
    assert_deprecated(fn ->
      assert OptionParser.parse(["-d", "--compile"], aliases: [d: :docs]) ==
               {[docs: true, compile: true], [], []}
    end)
  end

  test "parses --key value option" do
    assert_deprecated(fn ->
      assert OptionParser.parse(["--source", "form_docs/"]) == {[source: "form_docs/"], [], []}
    end)
  end

  test "does not interpret undefined options with value as boolean" do
    assert_deprecated(fn ->
      assert OptionParser.parse(["--no-bool"]) == {[no_bool: true], [], []}
    end)

    assert_deprecated(fn ->
      assert OptionParser.parse(["--no-bool=...", "other"]) == {[no_bool: "..."], ["other"], []}
    end)
  end

  test "parses -ab as -a -b" do
    assert_deprecated(fn ->
      assert OptionParser.parse(["-ab"], aliases: [a: :first, b: :second]) ==
               {[first: true, second: true], [], []}
    end)
  end

  test "parses mixed options" do
    argv = ["--source", "from_docs/", "--compile", "-x"]

    assert_deprecated(fn ->
      assert OptionParser.parse(argv, aliases: [x: :x]) ==
               {[source: "from_docs/", compile: true, x: true], [], []}
    end)
  end

  test "parses more than one key-value pair options" do
    assert_deprecated(fn ->
      assert OptionParser.parse(["--source", "from_docs/", "--docs", "show"]) ==
               {[source: "from_docs/", docs: "show"], [], []}
    end)
  end

  test "multi-word option" do
    assert_deprecated(fn ->
      assert OptionParser.next(["--hello-world"], []) == {:ok, :hello_world, true, []}
    end)

    assert_deprecated(fn ->
      assert OptionParser.next(["--no-hello-world"], []) == {:ok, :no_hello_world, true, []}
    end)

    assert_deprecated(fn ->
      assert OptionParser.next(["--hello_world"], []) == {:undefined, "--hello_world", nil, []}
    end)

    assert_deprecated(fn ->
      assert OptionParser.next(["--no-hello_world"], []) ==
               {:undefined, "--no-hello_world", nil, []}
    end)
  end
end