package core_unix

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file test_validate_command.ml

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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
open! Core
open! Import

let group subcommands = Command.group ~summary:"" subcommands
let basic param = Command.basic ~summary:"" param

let exec path_to_exe child_subcommand =
  Command.exec ~summary:"" ~path_to_exe ~child_subcommand ()
;;

let get_output_or_error command args ~f =
  (match f command args with
   | Ok () -> ()
   | Error error -> print_s [%sexp (error : Error.t)]
   | exception exn -> print_s [%sexp "Raised", (exn : exn)]);
  expect_test_output [%here]
;;

(* Opam cannot distinguish between dependencies of the implementation and dependencies of
   the tests, so [core > patdiff -> core tests] would be a dependency cycle at the opam
   package level.

   Instead we approximate a diff as a shared prefix, a shared suffix, and one big
   substitution in between. Good enough for the simple diffs in this test file. *)
let hacky_diff_because_patdiff_breaks_the_public_release ~prev ~next =
  let shared_prefix = String.common_prefix2 prev next in
  let shared_suffix = String.common_suffix2 prev next in
  let get_unshared_lines_and_prepend str ~prepend =
    String.sub
      str
      ~pos:(String.length shared_prefix)
      ~len:String.(length str - length shared_prefix - length shared_suffix)
    |> String.split_lines
    |> List.map ~f:(fun s -> prepend ^ s)
  in
  [ String.split_lines shared_prefix
  ; get_unshared_lines_and_prepend prev ~prepend:"-|"
  ; get_unshared_lines_and_prepend next ~prepend:"+|"
  ; String.split_lines shared_suffix
  ]
  |> List.concat
  |> String.concat ~sep:"\n"
;;

let test command args =
  let validate_command_line =
    get_output_or_error command args ~f:(fun command args ->
      Command_test_helpers.validate_command_line (Command_unix.shape command)
      |> Or_error.bind ~f:(fun f -> f args))
  in
  let validate_command =
    get_output_or_error command args ~f:Command_test_helpers.validate_command
  in
  match String.( = ) validate_command_line validate_command with
  | true -> print_endline validate_command (* either would work, since they're the same *)
  | false ->
    print_endline "Diff of validate_command (-) vs validate_command_line (+):";
    hacky_diff_because_patdiff_breaks_the_public_release
      ~prev:validate_command
      ~next:validate_command_line
    |> print_endline
;;

module _ = struct
  let test =
    let command = basic (Command.Param.return Fn.id) in
    group [ "foo1", group [ "bar1", command; "qux", command ]; "foo2", command ] |> test
  ;;

  let%expect_test "subcommand__missing_subcommand" =
    test [ "foo1" ];
    [%expect
      {|
        CMD foo1 SUBCOMMAND

      === subcommands ===

        bar1                       .
        qux                        .
        help                       . explain a given subcommand (perhaps recursively)

      missing subcommand for command CMD foo1
      (command.ml.Exit_called (status 1)) |}]
  ;;

  let%expect_test "subcommand__present_subcommand" =
    test [ "foo1"; "bar1" ];
    [%expect {| |}]
  ;;

  let%expect_test "subcommand__ambiguous_prefix" =
    test [ "foo" ];
    [%expect
      {|
        CMD SUBCOMMAND

      === subcommands ===

        foo1                       .
        foo2                       .
        version                    . print version information
        help                       . explain a given subcommand (perhaps recursively)

      subcommand foo is an ambiguous prefix: foo1, foo2
      (command.ml.Exit_called (status 1)) |}]
  ;;

  let%expect_test "subcommand__unambiguous_prefix" =
    test [ "foo1"; "q" ];
    [%expect {| |}]
  ;;
end

module _ = struct
  let%expect_test "misc__check_command_doesn't_actually_run" =
    let command =
      basic
        (let%map_open.Command s = flag "-print" (required string) ~doc:"" in
         fun () -> print_endline s)
    in
    test command [ "-print"; "don't print me" ];
    [%expect {| |}]
  ;;

  let%expect_test "misc__cannot_validate_exec_commands" =
    let exec_dev_null = exec (`Absolute "/dev/null") [] in
    test exec_dev_null [];
    [%expect
      {|
        ("[Exec _] commands are not validated to avoid unexpected external dependencies."
         (exec_info (
           (summary     "")
           (working_dir ELIDED-IN-TEST)
           (path_to_exe /dev/null)
           (child_subcommand ())))) |}]
  ;;
end

module _ = struct
  open Command.Param

  let unit_flag ?aliases ?full_flag_required name arg_type =
    flag ?aliases ?full_flag_required name arg_type ~doc:"" |> map ~f:ignore
  ;;

  let test params args =
    let param = List.fold params ~init:(return ()) ~f:(map2 ~f:const) in
    test (basic (map param ~f:const)) args
  ;;

  let%expect_test "params__full_flag_required" =
    let test_full_flag_required args =
      test [ unit_flag "-foo" (required int) ~full_flag_required:() ] args
    in
    (* Validating by reconstruction cannot test full-flag-required *)
    test_full_flag_required [ "-f"; "1" ];
    [%expect
      {|
      Diff of validate_command (-) vs validate_command_line (+):
      -|Error parsing command line:
      -|
      -|  unknown flag -f
      -|
      -|For usage information, run
      -|
      -|  CMD -help
      -|
      -|(command.ml.Exit_called (status 1)) |}]
  ;;

  module _ = struct
    let test_prefix = test [ unit_flag "-foobar" no_arg; unit_flag "-fubar" no_arg ]

    let%expect_test "params_prefixes__unambiguous_prefix" =
      test_prefix [ "-fo" ];
      [%expect {| |}]
    ;;

    let%expect_test "params_prefixes__ambiguous_prefix" =
      test_prefix [ "-f" ];
      [%expect
        {|
          Error parsing command line:

            flag -f is an ambiguous prefix: -foobar, -fubar

          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}]
    ;;
  end

  module _ = struct
    let test_flags args = test [ unit_flag "-a" no_arg; unit_flag "-b" (listed int) ] args

    let%expect_test "params_arg_types__not_passing_nonrequired_flags" =
      test_flags [ "-a" ];
      [%expect {| |}]
    ;;

    let%expect_test "params_arg_types__passing_invalid_anonymous_args" =
      test_flags [ "-a"; "_" ];
      [%expect
        {|
          Error parsing command line:

            too many anonymous arguments

          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}]
    ;;

    let%expect_test "params_arg_types__passing_wrong_arg_type" =
      (* Validating by reconstruction cannot test arg type, only arity *)
      test_flags [ "-b"; "_" ];
      [%expect
        {|
        Diff of validate_command (-) vs validate_command_line (+):
        -|Error parsing command line:
        -|
        -|  failed to parse -b value "_".
        -|  (Failure "Int.of_string: \"_\"")
        -|
        -|For usage information, run
        -|
        -|  CMD -help
        -|
        -|(command.ml.Exit_called (status 1)) |}]
    ;;

    let%expect_test "params_arg_types__passing_correct_arg_type" =
      test_flags [ "-b"; "1" ];
      [%expect {||}]
    ;;

    let%expect_test "params_arg_types__passing_flag_but_no_arg" =
      test_flags [ "-b" ];
      [%expect
        {|
          Error parsing command line:

            missing argument for flag -b

          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}]
    ;;

    let%expect_test "params_arg_types__passing_arg_which_looks_like_a_flag" =
      (* Validating by reconstruction cannot test arg type, only arity *)
      test_flags [ "-b"; "-b" ];
      [%expect
        {|
          Diff of validate_command (-) vs validate_command_line (+):
          -|Error parsing command line:
          -|
          -|  failed to parse -b value "-b".
          -|  (Failure "Int.of_string: \"-b\"")
          -|
          -|For usage information, run
          -|
          -|  CMD -help
          -|
          -|(command.ml.Exit_called (status 1)) |}]
    ;;

    let%expect_test "params_arg_types__passing_flag_twice_but_arg_once" =
      test_flags [ "-b"; "-b"; "_" ];
      (* Wrong error raised: Validating by reconstruction cannot
         test arg type, only arity *)
      [%expect
        {|
        Diff of validate_command (-) vs validate_command_line (+):
        Error parsing command line:


        -|failed to parse -b value "-b".
        -|  (Failure "Int.of_string: \"-b\"")
        +|too many anonymous arguments


        For usage information, run

          CMD -help

        (command.ml.Exit_called (status 1)) |}]
    ;;
  end

  module _ = struct
    let test_alias = test [ unit_flag "-a" no_arg ~aliases:[ "-b" ] ]

    let%expect_test "params_aliases__original_flag_passed" =
      test_alias [ "-a" ];
      [%expect {| |}]
    ;;

    let%expect_test "params_aliases__alias_passed" =
      test_alias [ "-b" ];
      [%expect {| |}]
    ;;

    let%expect_test "params_aliases__non_alias_passed" =
      test_alias [ "-c" ];
      [%expect
        {|
          Error parsing command line:

            unknown flag -c

          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}]
    ;;

    let%expect_test "params_aliases__internal_alias_exluded_from_help" =
      test [] [ "--help" ];
      [%expect
        {|
            CMD

          === flags ===

            [-build-info]              . print info about this build and exit
            [-version]                 . print the version of this build and exit
            [-help], -?                . print this help text and exit

          (command.ml.Exit_called (status 0)) |}]
    ;;
  end

  module _ = struct
    let test_anons_vs_flag_args =
      let unit_anon f = anon (f ("A" %: int) |> map_anons ~f:ignore) in
      test [ unit_anon Fn.id; unit_flag "-a" (optional int); unit_flag "-b" no_arg ]
    ;;

    let%expect_test "params_anonymous_flags_and_args__just_anon" =
      test_anons_vs_flag_args [ "1" ];
      [%expect {| |}]
    ;;

    let%expect_test "params_anonymous_flags_and_args__anon_and_arg" =
      test_anons_vs_flag_args [ "1"; "-a"; "2" ];
      [%expect {| |}]
    ;;

    let%expect_test "params_anonymous_flags_and_args__no_required_anon" =
      test_anons_vs_flag_args [ "-a"; "2" ];
      [%expect
        {|
          Error parsing command line:

            missing anonymous argument: A

          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}]
    ;;

    let%expect_test "params_anonymous_flags_and_args__anon_and_no-arg_flag" =
      test_anons_vs_flag_args [ "-b"; "3" ];
      [%expect {| |}]
    ;;
  end

  module _ = struct
    let test_escape args =
      let escape =
        [ flag ~doc:"" "--" escape |> map ~f:(Option.map ~f:(List.map ~f:Int.of_string)) ]
      in
      test escape args
    ;;

    let%expect_test "params_escape__incorrect_escaped_arg_type" =
      test_escape [ "--"; "foo" ];
      (* Wrong error raised: Validation by reconstruction cannot
         handle side-effects like escape args *)
      [%expect
        {|
          Diff of validate_command (-) vs validate_command_line (+):
          Error parsing command line:


          -|(Failure "Int.of_string: \"foo\"")
          +|too many anonymous arguments


          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}]
    ;;

    let%expect_test "params_escape__correct_escaped_arg_type" =
      (* Validation by reconstruction cannot handle side-effects like escape args *)
      test_escape [ "--"; "1" ];
      [%expect
        {|
          Diff of validate_command (-) vs validate_command_line (+):
          +|Error parsing command line:
          +|
          +|  too many anonymous arguments
          +|
          +|For usage information, run
          +|
          +|  CMD -help
          +|
          +|(command.ml.Exit_called (status 1)) |}]
    ;;
  end

  module _ = struct
    let print ~make_args n result =
      "$ CMD" :: make_args n |> String.concat ~sep:" " |> print_endline;
      print_endline result
    ;;

    let test_param_types param args =
      test
        [ (let%map_open.Command _ = param in
           Fn.id)
        ]
        args
    ;;

    module _ = struct
      let make_args = List.init ~f:(const [ "-a"; "1" ]) >> List.concat
      let print = print ~make_args

      let test f n =
        let param = unit_flag "-a" (f int) in
        let args = make_args n in
        test_param_types param args
      ;;

      let%expect_test "params_counting_flags__listed" =
        let test n =
          test listed n;
          print n [%expect.output]
        in
        test 0;
        [%expect {| $ CMD |}];
        test 1;
        [%expect {| $ CMD -a 1 |}];
        test 2;
        [%expect {| $ CMD -a 1 -a 1 |}]
      ;;

      let%expect_test "params_counting_flags__optional" =
        let test n =
          test optional n;
          print n [%expect.output]
        in
        test 0;
        [%expect {| $ CMD |}];
        test 1;
        [%expect {| $ CMD -a 1 |}];
        test 2;
        [%expect
          {|
          $ CMD -a 1 -a 1
          Error parsing command line:

            flag -a passed more than once

          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}]
      ;;

      let%expect_test "params_counting_flags__required" =
        let test n =
          test required n;
          print n [%expect.output]
        in
        test 0;
        [%expect
          {|
          $ CMD
          Error parsing command line:

            missing required flag: -a

          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}];
        test 1;
        [%expect {| $ CMD -a 1 |}];
        test 2;
        [%expect
          {|
          $ CMD -a 1 -a 1
          Error parsing command line:

            flag -a passed more than once

          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}]
      ;;
    end

    module _ = struct
      let make_args = List.init ~f:(Int.succ >> Int.to_string)
      let print = print ~make_args

      let test f n =
        let param = anon (f ("A" %: int)) in
        let args = make_args n in
        test_param_types param args
      ;;

      let%expect_test "params_counting_anonymous_args__sequence" =
        let test n =
          test sequence n;
          print n [%expect.output]
        in
        test 0;
        [%expect {| $ CMD |}];
        test 1;
        [%expect {| $ CMD 1 |}];
        test 2;
        [%expect {| $ CMD 1 2 |}]
      ;;

      let%expect_test "params_counting_anonymous_args__maybe" =
        let test n =
          test maybe n;
          print n [%expect.output]
        in
        test 0;
        [%expect {| $ CMD |}];
        test 1;
        [%expect {| $ CMD 1 |}];
        test 2;
        [%expect
          {|
          $ CMD 1 2
          Error parsing command line:

            too many anonymous arguments

          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}]
      ;;

      let%expect_test "params_counting_anonymous_args__required" =
        let test n =
          test Fn.id n;
          print n [%expect.output]
        in
        test 0;
        [%expect
          {|
          $ CMD
          Error parsing command line:

            missing anonymous argument: A

          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}];
        test 1;
        [%expect {| $ CMD 1 |}];
        test 2;
        [%expect
          {|
          $ CMD 1 2
          Error parsing command line:

            too many anonymous arguments

          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}]
      ;;

      let%expect_test "params_counting_anonymous_args__required_and_maybe" =
        let test n =
          test_param_types
            (let%map_open.Command _ = anon ("A" %: int)
             and _ = anon (maybe ("B" %: int)) in
             Fn.id)
            (make_args n);
          print n [%expect.output]
        in
        test 0;
        [%expect
          {|
          $ CMD
          Error parsing command line:

            missing anonymous argument: A

          For usage information, run

            CMD -help

          (command.ml.Exit_called (status 1)) |}];
        test 1;
        [%expect {| $ CMD 1 |}];
        test 2;
        [%expect {| $ CMD 1 2 |}]
      ;;
    end
  end
end