Skip to content

Commit

Permalink
Don't require cram to use bash as its shell
Browse files Browse the repository at this point in the history
  • Loading branch information
gridbugs committed Jul 25, 2024
1 parent 1ee2a66 commit 8dda399
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 10 deletions.
11 changes: 5 additions & 6 deletions tests/completion_tests/basic/test.t
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
$ . ../helper.sh
$ ./main.exe > completion.sh
$ . ./completion.sh
$ x() { completion_test _basic_complete "$1" "$2"; }
$ x() { ../print_completions.sh ./completion.sh _basic_complete "$1" "$2"; }

$ x "basic " \
> " ^"
> " ^"
--bar
--baz
--foo
Expand All @@ -29,7 +27,7 @@
--baz

$ x "basic --bar" \
> " ^"
> " ^"
--bar

$ x "basic --bar " \
Expand All @@ -47,11 +45,12 @@
--foo
--help
-h

$ x "basic --foo --bar" \
> " ^ "
--foo

$ x "basic --bar --foo" \
> " ^ "
> " ^ "
--bar
--baz
2 changes: 1 addition & 1 deletion tests/completion_tests/dune
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
(cram
(deps helper.sh)
(deps print_completions.sh)
(applies_to :whole_subtree))
29 changes: 26 additions & 3 deletions tests/completion_tests/helper.sh → tests/completion_tests/print_completions.sh
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
#!/usr/bin/env bash

# Usage: ./print_completions.sh COMPLETION_SCRIPT COMPLETION_FUNCTION COMMAND_LINE CURSOR_LINE
# Prints the completion suggestions if a user was to press TAB on a
# given command line with the cursor at the position indicated by
# CURSOR_LINE. CURSOR_LINE is expected to be a string where the first
# non-space character is at the position of the cursor. This is so
# that test scripts can be formatted with COMMAND_LINE and CURSOR_LINE
# on separate lines to make it visually clear which character of the
# command line is under the cursor.
# COMPLETION_SCRIPT is the name of a script that defines bash
# completion rules, and COMPLETION_FUNCTION is the name of the
# function defined in that script registered with `complete`.

set -u

# Print the completion suggestions of a given completion function on
Expand All @@ -15,9 +27,9 @@ set -u
# is lined up with the character in the command line where the cursor
# is located.
completion_test() {
completion_function="$1"
COMP_LINE="$2"
cursor_line="$3"
local completion_function="$1"
local COMP_LINE="$2"
local cursor_line="$3"
read -r -a COMP_WORDS <<< "$COMP_LINE"
if [ ${#COMP_WORDS[@]} == 1 ]; then
COMP_WORDS+=("")
Expand Down Expand Up @@ -47,3 +59,14 @@ completion_test() {
$completion_function "$first_word" "$curr_word" "$prev_word"
printf "%s\n" "${COMPREPLY[@]}"
}

main () {
local completion_script="$1"
local completion_function="$2"
local COMP_LINE="$3"
local cursor_line="$4"
. "$completion_script"
completion_test "$completion_function" "$COMP_LINE" "$cursor_line"
}

main "$1" "$2" "$3" "$4"

0 comments on commit 8dda399

Please sign in to comment.