From 8dda399606400654529c62f486511f5f6caf4559 Mon Sep 17 00:00:00 2001 From: Stephen Sherratt Date: Thu, 25 Jul 2024 11:36:37 +1000 Subject: [PATCH] Don't require cram to use bash as its shell --- tests/completion_tests/basic/test.t | 11 ++++--- tests/completion_tests/dune | 2 +- .../{helper.sh => print_completions.sh} | 29 +++++++++++++++++-- 3 files changed, 32 insertions(+), 10 deletions(-) rename tests/completion_tests/{helper.sh => print_completions.sh} (55%) mode change 100644 => 100755 diff --git a/tests/completion_tests/basic/test.t b/tests/completion_tests/basic/test.t index 68fa548..b761e0c 100644 --- a/tests/completion_tests/basic/test.t +++ b/tests/completion_tests/basic/test.t @@ -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 @@ -29,7 +27,7 @@ --baz $ x "basic --bar" \ - > " ^" + > " ^" --bar $ x "basic --bar " \ @@ -47,11 +45,12 @@ --foo --help -h + $ x "basic --foo --bar" \ > " ^ " --foo $ x "basic --bar --foo" \ - > " ^ " + > " ^ " --bar --baz diff --git a/tests/completion_tests/dune b/tests/completion_tests/dune index 18ca932..520cd63 100644 --- a/tests/completion_tests/dune +++ b/tests/completion_tests/dune @@ -1,3 +1,3 @@ (cram - (deps helper.sh) + (deps print_completions.sh) (applies_to :whole_subtree)) diff --git a/tests/completion_tests/helper.sh b/tests/completion_tests/print_completions.sh old mode 100644 new mode 100755 similarity index 55% rename from tests/completion_tests/helper.sh rename to tests/completion_tests/print_completions.sh index 1d8fa4f..ff96b27 --- a/tests/completion_tests/helper.sh +++ b/tests/completion_tests/print_completions.sh @@ -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 @@ -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+=("") @@ -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"