Skip to content

Commit

Permalink
Add test of fake_git bash completion
Browse files Browse the repository at this point in the history
  • Loading branch information
gridbugs committed Jul 25, 2024
1 parent 8dda399 commit d4ecc88
Show file tree
Hide file tree
Showing 7 changed files with 188 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/climate.mli
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,10 @@ module Command : sig

(** A command that has the side effect of printing the completion
script of the entire command it's contained inside. It's safe to
bury this inside a hidden command group of internal commands. *)
bury this inside a hidden command group of internal
commands. The command takes some arguments to configure the
generated completion script, similar to the arguments of the
function [completion_script_bash]. *)
val print_completion_script_bash : _ t

(** Returns a bash script that can be sourced in a shell to register
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions tests/completion_tests/basic/dune
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(executable
(public_name main)
(public_name basic)
(libraries climate))

(cram
(deps main.exe))
(deps basic.exe))
2 changes: 1 addition & 1 deletion tests/completion_tests/basic/test.t
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$ ./main.exe > completion.sh
$ ./basic.exe > completion.sh
$ x() { ../print_completions.sh ./completion.sh _basic_complete "$1" "$2"; }

$ x "basic " \
Expand Down
6 changes: 6 additions & 0 deletions tests/completion_tests/fake_git/dune
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
(executable
(public_name fake_git)
(libraries climate unix))

(cram
(deps fake_git.exe))
74 changes: 74 additions & 0 deletions tests/completion_tests/fake_git/fake_git.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
open Climate

let ls dir =
let dir_handle = Unix.opendir dir in
let rec loop files =
try
let file = Unix.readdir dir_handle in
loop (file :: files)
with
| End_of_file -> files
in
List.rev (loop [])
;;

(* Fake protocol for defining branches so the list of branches can be
updated dynamically by tests. *)
let list_branches () =
ls "./.git/branches"
|> List.filter (Fun.negate @@ String.starts_with ~prefix:".")
|> List.sort String.compare
;;

let branch_conv =
let open Arg_parser in
{ string with
default_value_name = "BRANCH"
; completion = Some (Completion.reentrant_thunk list_branches)
}
;;

(* A command which mixes reentrant completion with file completion *)
let checkout =
let open Arg_parser in
let+ _create_branch = flag [ "b" ]
and+ _branch_name = pos_req 0 branch_conv
and+ _files = pos_right 1 file in
()
;;

(* A command which mixes reentrant completion with a named argument
with a value completed with an enum *)
let log =
let open Arg_parser in
let+ _pretty =
named_opt [ "pretty"; "p" ] (string_enum [ "full"; "fuller"; "short"; "oneline" ])
and+ _branch_name = pos_req 0 branch_conv in
()
;;

(* A command which mixes positional arguments and subcommands *)
let bisect op =
let open Arg_parser in
match op with
| `Mark ->
let+ _status = pos_opt 0 (string_enum [ "good"; "bad" ]) in
()
| `Start | `Reset -> unit
;;

let () =
let open Command in
group
[ subcommand "checkout" (singleton checkout)
; subcommand "log" (singleton log)
; subcommand "bisect"
@@ group
~default_arg_parser:(bisect `Mark)
[ subcommand "start" (singleton (bisect `Start))
; subcommand "reset" (singleton (bisect `Reset))
]
; subcommand "completions" ~hidden:true print_completion_script_bash
]
|> run
;;
101 changes: 101 additions & 0 deletions tests/completion_tests/fake_git/test.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
$ ./fake_git.exe completions --program-name=fake-git --global-symbol-prefix=_fake_git_ > completion.sh
$ x() { ../print_completions.sh ./completion.sh _fake_git_complete "$1" "$2"; }

Make a fake .git directory with some branches.
$ mkdir -p .git/branches
$ touch .git/branches/foo
$ touch .git/branches/bar

$ x "fake-git " \
> " ^"
checkout
log
bisect

$ x "fake-git checkout " \
> " ^"
bar
foo

$ x "fake-git checkout foo " \
> " ^"
.git
completion.sh
fake_git.exe

$ x "fake-git checkout foo a.txt " \
> " ^"
.git
completion.sh
fake_git.exe

$ x "fake-git checkout foo a.txt " \
> " ^ "
bar
foo

$ x "fake-git checkout foo a.txt " \
> " ^ "
.git
completion.sh
fake_git.exe

$ x "fake-git log " \
> " ^"
bar
foo

$ x "fake-git log -" \
> " ^"
--help
--pretty
-h
-p

$ x "fake-git log --" \
> " ^"
--help
--pretty

$ x "fake-git log --pretty " \
> " ^"
full
fuller
short
oneline

$ x "fake-git log --pretty f" \
> " ^"
full
fuller

$ x "fake-git log --pretty full " \
> " ^"
bar
foo


$ x "fake-git log --pretty full foo " \
> " ^"
--help
--pretty
-h
-p

Test that positional arguments and subcommands are both listed.
$ x "fake-git bisect " \
> " ^"
start
reset
good
bad

$ x "fake-git bisect -" \
> " ^"
--help
-h

$ x "fake-git bisect start " \
> " ^"
--help
-h

0 comments on commit d4ecc88

Please sign in to comment.