From 63b77d1fd9249c4330d90005277c6884fe0c4397 Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Mon, 28 Nov 2022 14:10:22 +0000 Subject: [PATCH 1/2] - Fix broken script when wildcards follow the first word --- Gemfile | 1 - lib/completely/pattern.rb | 6 ++-- spec/completely/integration.yml | 27 ++++++++++++++++-- spec/completely/pattern_spec.rb | 37 +++++++++++++++++++++++-- spec/fixtures/integration/wildcard.yaml | 17 ++++++++++++ 5 files changed, 80 insertions(+), 8 deletions(-) create mode 100644 spec/fixtures/integration/wildcard.yaml diff --git a/Gemfile b/Gemfile index 664f7d1..e4161df 100644 --- a/Gemfile +++ b/Gemfile @@ -2,7 +2,6 @@ source 'https://rubygems.org' gem 'byebug' gem 'lp' -gem 'rentacop' gem 'rspec' gem 'rspec_approvals' gem 'runfile' diff --git a/lib/completely/pattern.rb b/lib/completely/pattern.rb index 17da567..a276a29 100644 --- a/lib/completely/pattern.rb +++ b/lib/completely/pattern.rb @@ -28,21 +28,21 @@ def actions end def prefix - text.split[0] + text.split(/ |\*/).first end def case_string if text_without_prefix.empty? '*' elsif text_without_prefix.include? '*' - %['#{text_without_prefix.gsub '*', "'*'"}'] + text_without_prefix.gsub(/([^*]+)/, "'\\1'") else "'#{text_without_prefix}'*" end end def text_without_prefix - @text_without_prefix ||= text.split[1..].join ' ' + @text_without_prefix ||= text[/^#{prefix}\s*(.*)/, 1] end def compgen diff --git a/spec/completely/integration.yml b/spec/completely/integration.yml index fc5e96e..eb90815 100644 --- a/spec/completely/integration.yml +++ b/spec/completely/integration.yml @@ -9,7 +9,7 @@ ftp: expected: [download] - compline: "ftp download " - expected: [another-dir, dir with spaces, dummy-dir, file with spaces.txt, ftp.yaml, gradual.yaml] + expected: [another-dir, dir with spaces, dummy-dir, file with spaces.txt, ftp.yaml, gradual.yaml, wildcard.yaml] - compline: "ftp download -" expected: [--help, --override] @@ -70,4 +70,27 @@ gradual: expected: [blue, red] - compline: "cli command subcommand anything --color gr" - expected: [gray, green] \ No newline at end of file + expected: [gray, green] + +wildcard: +- compline: "wildcard " + expected: [download, upload] + +- compline: "wildcard d" + expected: [download] + +- compline: "wildcard --" + expected: [--debug, --env] + +- compline: "wildcard download --" + expected: [--confirm, --contest] + +- compline: "wildcard --env " + expected: [dev, prod] + +- compline: "wildcard --env prod download --contest " + expected: [everything, nothing] + +- compline: "wildcard download --contest " + expected: [everything, nothing] + diff --git a/spec/completely/pattern_spec.rb b/spec/completely/pattern_spec.rb index a076634..074beba 100644 --- a/spec/completely/pattern_spec.rb +++ b/spec/completely/pattern_spec.rb @@ -40,9 +40,25 @@ end describe '#prefix' do - it 'returns the first word from text' do + it 'returns the first word of the pattern' do expect(subject.prefix).to eq 'git' end + + context 'when the pattern includes a * right after the first word' do + let(:text) { 'git*--checkout'} + + it 'returns the first word of the pattern' do + expect(subject.prefix).to eq 'git' + end + end + + context 'when the pattern includes a * anywhere else' do + let(:text) { 'git --checkout*something'} + + it 'returns the first word of the pattern' do + expect(subject.prefix).to eq 'git' + end + end end describe '#case_string' do @@ -68,9 +84,26 @@ end describe '#text_without_prefix' do - it 'returns all but the first word from text' do + it 'returns all but the first word' do expect(subject.text_without_prefix).to eq 'commit' end + + context 'when the pattern includes a * right after the first word' do + let(:text) { 'git*--checkout'} + + it 'returns all but the first word' do + expect(subject.text_without_prefix).to eq '*--checkout' + end + end + + context 'when the pattern includes a * anywhere else' do + let(:text) { 'git --checkout*something'} + + it 'returns all but the first word' do + expect(subject.text_without_prefix).to eq '--checkout*something' + end + end + end describe '#compgen' do diff --git a/spec/fixtures/integration/wildcard.yaml b/spec/fixtures/integration/wildcard.yaml new file mode 100644 index 0000000..5a724de --- /dev/null +++ b/spec/fixtures/integration/wildcard.yaml @@ -0,0 +1,17 @@ +wildcard: +- --debug +- --env +- download +- upload + +wildcard*--env: +- prod +- dev + +wildcard*download: +- --confirm +- --contest + +wildcard*download*--contest: +- everything +- nothing From eeb38735546c5aa2e532177f800b3f76c561a89e Mon Sep 17 00:00:00 2001 From: Danny Ben Shitrit Date: Mon, 28 Nov 2022 14:13:17 +0000 Subject: [PATCH 2/2] fix rubocop layout offenses --- spec/completely/pattern_spec.rb | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/spec/completely/pattern_spec.rb b/spec/completely/pattern_spec.rb index 074beba..5bc3a6c 100644 --- a/spec/completely/pattern_spec.rb +++ b/spec/completely/pattern_spec.rb @@ -45,7 +45,7 @@ end context 'when the pattern includes a * right after the first word' do - let(:text) { 'git*--checkout'} + let(:text) { 'git*--checkout' } it 'returns the first word of the pattern' do expect(subject.prefix).to eq 'git' @@ -53,7 +53,7 @@ end context 'when the pattern includes a * anywhere else' do - let(:text) { 'git --checkout*something'} + let(:text) { 'git --checkout*something' } it 'returns the first word of the pattern' do expect(subject.prefix).to eq 'git' @@ -89,7 +89,7 @@ end context 'when the pattern includes a * right after the first word' do - let(:text) { 'git*--checkout'} + let(:text) { 'git*--checkout' } it 'returns all but the first word' do expect(subject.text_without_prefix).to eq '*--checkout' @@ -97,13 +97,12 @@ end context 'when the pattern includes a * anywhere else' do - let(:text) { 'git --checkout*something'} + let(:text) { 'git --checkout*something' } it 'returns all but the first word' do expect(subject.text_without_prefix).to eq '--checkout*something' end end - end describe '#compgen' do