From 9dea6910b4ee6b3b0f54e5c4e47f13cbb2dfdc0e Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Thu, 7 Jul 2022 12:28:17 +0000 Subject: [PATCH 1/2] - Fix file/folder completion when they contain spaces --- lib/completely/templates/template.erb | 2 +- spec/approvals/cli/generated-script | 8 ++++---- spec/approvals/cli/generated-script-alt | 8 ++++---- spec/approvals/cli/generated-wrapped-script | 8 ++++---- spec/approvals/cli/test/completely-tester.sh | 8 ++++---- spec/approvals/completions/function | 6 +++--- spec/approvals/completions/script | 6 +++--- spec/approvals/completions/script-only-spaces | 4 ++-- spec/fixtures/tester/default.bash | 8 ++++---- 9 files changed, 29 insertions(+), 29 deletions(-) diff --git a/lib/completely/templates/template.erb b/lib/completely/templates/template.erb index 67fe0fb..dcf03b2 100644 --- a/lib/completely/templates/template.erb +++ b/lib/completely/templates/template.erb @@ -19,7 +19,7 @@ % patterns.each do |pattern| % next if pattern.empty? <%= pattern.case_string %>) - COMPREPLY=($(compgen <%= pattern.compgen %> -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen <%= pattern.compgen %> -- "$cur" ) ;; % end diff --git a/spec/approvals/cli/generated-script b/spec/approvals/cli/generated-script index 7c5816e..93f465e 100644 --- a/spec/approvals/cli/generated-script +++ b/spec/approvals/cli/generated-script @@ -10,19 +10,19 @@ _mygit_completions() { case "$compline" in 'status'*) - COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur" ) ;; 'commit'*) - COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur" ) ;; 'init'*) - COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--bare" -- "$cur" ) ;; *) - COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version status init commit" -- "$cur" ) ;; esac diff --git a/spec/approvals/cli/generated-script-alt b/spec/approvals/cli/generated-script-alt index d1f8061..34487c4 100644 --- a/spec/approvals/cli/generated-script-alt +++ b/spec/approvals/cli/generated-script-alt @@ -10,19 +10,19 @@ _mycomps() { case "$compline" in 'status'*) - COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur" ) ;; 'commit'*) - COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur" ) ;; 'init'*) - COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--bare" -- "$cur" ) ;; *) - COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version status init commit" -- "$cur" ) ;; esac diff --git a/spec/approvals/cli/generated-wrapped-script b/spec/approvals/cli/generated-wrapped-script index 6e66271..969ffc8 100644 --- a/spec/approvals/cli/generated-wrapped-script +++ b/spec/approvals/cli/generated-wrapped-script @@ -11,19 +11,19 @@ give_comps() { echo $'' echo $' case "$compline" in' echo $' \'status\'*)' - echo $' COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur"))' + echo $' while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur" )' echo $' ;;' echo $'' echo $' \'commit\'*)' - echo $' COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur"))' + echo $' while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur" )' echo $' ;;' echo $'' echo $' \'init\'*)' - echo $' COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur"))' + echo $' while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--bare" -- "$cur" )' echo $' ;;' echo $'' echo $' *)' - echo $' COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur"))' + echo $' while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version status init commit" -- "$cur" )' echo $' ;;' echo $'' echo $' esac' diff --git a/spec/approvals/cli/test/completely-tester.sh b/spec/approvals/cli/test/completely-tester.sh index 41b9720..3327fbd 100644 --- a/spec/approvals/cli/test/completely-tester.sh +++ b/spec/approvals/cli/test/completely-tester.sh @@ -18,19 +18,19 @@ _mygit_completions() { case "$compline" in 'status'*) - COMPREPLY=($(compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --verbose --branch $(git branch 2> /dev/null)" -- "$cur" ) ;; 'commit'*) - COMPREPLY=($(compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A file -W "--help --message --all -a --quiet -q" -- "$cur" ) ;; 'init'*) - COMPREPLY=($(compgen -A directory -W "--bare" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--bare" -- "$cur" ) ;; *) - COMPREPLY=($(compgen -W "--help --version status init commit" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version status init commit" -- "$cur" ) ;; esac diff --git a/spec/approvals/completions/function b/spec/approvals/completions/function index be0046a..f683e42 100644 --- a/spec/approvals/completions/function +++ b/spec/approvals/completions/function @@ -11,15 +11,15 @@ send_completions() { echo $'' echo $' case "$compline" in' echo $' \'generate\'*)' - echo $' COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur"))' + echo $' while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--help --force" -- "$cur" )' echo $' ;;' echo $'' echo $' \'init\'*)' - echo $' COMPREPLY=($(compgen -W "--help" -- "$cur"))' + echo $' while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help" -- "$cur" )' echo $' ;;' echo $'' echo $' *)' - echo $' COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur"))' + echo $' while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version init generate" -- "$cur" )' echo $' ;;' echo $'' echo $' esac' diff --git a/spec/approvals/completions/script b/spec/approvals/completions/script index 1ff89e5..c6e0c58 100644 --- a/spec/approvals/completions/script +++ b/spec/approvals/completions/script @@ -10,15 +10,15 @@ _completely_completions() { case "$compline" in 'generate'*) - COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--help --force" -- "$cur" ) ;; 'init'*) - COMPREPLY=($(compgen -W "--help" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help" -- "$cur" ) ;; *) - COMPREPLY=($(compgen -W "--help --version init generate" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version init generate" -- "$cur" ) ;; esac diff --git a/spec/approvals/completions/script-only-spaces b/spec/approvals/completions/script-only-spaces index 92a49ef..e4d3402 100644 --- a/spec/approvals/completions/script-only-spaces +++ b/spec/approvals/completions/script-only-spaces @@ -10,11 +10,11 @@ _completely_completions() { case "$compline" in 'generate'*) - COMPREPLY=($(compgen -A directory -W "--help --force" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -A directory -W "--help --force" -- "$cur" ) ;; 'init'*) - COMPREPLY=($(compgen -W "--help" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help" -- "$cur" ) ;; esac diff --git a/spec/fixtures/tester/default.bash b/spec/fixtures/tester/default.bash index 8a5d834..8a0d7a3 100644 --- a/spec/fixtures/tester/default.bash +++ b/spec/fixtures/tester/default.bash @@ -10,19 +10,19 @@ _cli_completions() { case "$compline" in 'command childcommand'*) - COMPREPLY=($(compgen -W "--quiet --verbose -q -v" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--quiet --verbose -q -v" -- "$cur" ) ;; 'command subcommand'*) - COMPREPLY=($(compgen -W "--force --quiet" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--force --quiet" -- "$cur" ) ;; 'command'*) - COMPREPLY=($(compgen -W "subcommand childcommand" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "subcommand childcommand" -- "$cur" ) ;; *) - COMPREPLY=($(compgen -W "--help --version command conquer" -- "$cur")) + while read; do COMPREPLY+=( "$REPLY" ); done < <( compgen -W "--help --version command conquer" -- "$cur" ) ;; esac From 2f2c63b69442105a9e7e3c256b0474788fd4a7de Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Thu, 7 Jul 2022 12:55:08 +0000 Subject: [PATCH 2/2] fix tester command and add coverage for spaced paths --- lib/completely/templates/tester-template.erb | 5 ++++- lib/completely/tester.rb | 2 +- spec/approvals/cli/test/completely-tester.sh | 5 ++++- spec/approvals/tester/script | 5 ++++- spec/approvals/tester/script_path | 5 ++++- spec/completely/integration.yml | 3 ++- spec/completely/zsh_spec.rb | 4 ++-- spec/fixtures/integration/dir with spaces/.keep | 0 spec/fixtures/integration/file with spaces.txt | 1 + 9 files changed, 22 insertions(+), 8 deletions(-) create mode 100644 spec/fixtures/integration/dir with spaces/.keep create mode 100644 spec/fixtures/integration/file with spaces.txt diff --git a/lib/completely/templates/tester-template.erb b/lib/completely/templates/tester-template.erb index bcd9223..58a87a9 100644 --- a/lib/completely/templates/tester-template.erb +++ b/lib/completely/templates/tester-template.erb @@ -16,4 +16,7 @@ COMP_POINT=${#COMP_LINE} COMP_CWORD=<%= cword %> <%= function_name %> -echo "${COMPREPLY[*]}" +for suggestion in "${COMPREPLY[@]}"; do + echo "$suggestion" +done + diff --git a/lib/completely/tester.rb b/lib/completely/tester.rb index cb74712..3f88de6 100644 --- a/lib/completely/tester.rb +++ b/lib/completely/tester.rb @@ -14,7 +14,7 @@ def test(compline) f << tester_script(compline) f.flush `bash #{f.path}` - end.split " " + end.split "\n" end def tester_script(compline) diff --git a/spec/approvals/cli/test/completely-tester.sh b/spec/approvals/cli/test/completely-tester.sh index 3327fbd..42b7bde 100644 --- a/spec/approvals/cli/test/completely-tester.sh +++ b/spec/approvals/cli/test/completely-tester.sh @@ -48,4 +48,7 @@ COMP_POINT=${#COMP_LINE} COMP_CWORD=2 _mygit_completions -echo "${COMPREPLY[*]}" +for suggestion in "${COMPREPLY[@]}"; do + echo "$suggestion" +done + diff --git a/spec/approvals/tester/script b/spec/approvals/tester/script index 327178b..4e1517a 100644 --- a/spec/approvals/tester/script +++ b/spec/approvals/tester/script @@ -16,4 +16,7 @@ COMP_POINT=${#COMP_LINE} COMP_CWORD=1 _cli_completions -echo "${COMPREPLY[*]}" +for suggestion in "${COMPREPLY[@]}"; do + echo "$suggestion" +done + diff --git a/spec/approvals/tester/script_path b/spec/approvals/tester/script_path index 6c39f1c..8acc416 100644 --- a/spec/approvals/tester/script_path +++ b/spec/approvals/tester/script_path @@ -16,4 +16,7 @@ COMP_POINT=${#COMP_LINE} COMP_CWORD=1 _cli_completions -echo "${COMPREPLY[*]}" +for suggestion in "${COMPREPLY[@]}"; do + echo "$suggestion" +done + diff --git a/spec/completely/integration.yml b/spec/completely/integration.yml index 0afad2a..95d3de6 100644 --- a/spec/completely/integration.yml +++ b/spec/completely/integration.yml @@ -7,9 +7,10 @@ ftp: - compline: "ftp download " expected: [--help, --override, another-dir, dummy-dir, ftp.yaml, gradual.yaml] + expected: [--help, --override, another-dir, dir with spaces, dummy-dir, file with spaces.txt, ftp.yaml, gradual.yaml] - compline: "ftp upload " - expected: [--confirm, --help, another-dir, dummy-dir] + expected: [--confirm, --help, another-dir, dir with spaces, dummy-dir] - compline: "ftp connect ssh " expected: [--keyfile] diff --git a/spec/completely/zsh_spec.rb b/spec/completely/zsh_spec.rb index 50c811b..5464303 100644 --- a/spec/completely/zsh_spec.rb +++ b/spec/completely/zsh_spec.rb @@ -20,14 +20,14 @@ describe "completions script and test script" do it "returns completions without erroring" do - expect(subject).to eq "somedir --help --force" + expect(subject).to eq "somedir\n--help\n--force" end context "on bash" do let(:shell) { 'bash' } it "returns the same output" do - expect(subject).to eq "somedir --help --force" + expect(subject).to eq "somedir\n--help\n--force" end end end diff --git a/spec/fixtures/integration/dir with spaces/.keep b/spec/fixtures/integration/dir with spaces/.keep new file mode 100644 index 0000000..e69de29 diff --git a/spec/fixtures/integration/file with spaces.txt b/spec/fixtures/integration/file with spaces.txt new file mode 100644 index 0000000..54ae4aa --- /dev/null +++ b/spec/fixtures/integration/file with spaces.txt @@ -0,0 +1 @@ + <-- more spaces \ No newline at end of file