From ed376f0ac362794b8655c3b86b2054ce896e560f Mon Sep 17 00:00:00 2001 From: Alessandro Fazzi Date: Sun, 26 Dec 2021 17:53:59 +0100 Subject: [PATCH] Handle edge case when wp cli param-dump returns empty string --- .github/workflows/ruby.yml | 9 +++++---- lib/wordmove/wpcli.rb | 6 +++++- spec/wpcli_spec.rb | 13 +++++++++++++ 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index c2b098e..92a94fd 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -27,10 +27,11 @@ jobs: path: .vendor/bundle key: ${{ hashFiles('wordmove.gemspec') }} - name: Install bundler - run: gem install bundler:2.3.3 - - name: Install dependencies - run : | + run: | + echo $(pwd) bundle config set path .vendor/bundle - bundle install + gem install bundler:2.3.3 + - name: Install dependencies + run : bundle install - name: Run tests run: bundle exec rake diff --git a/lib/wordmove/wpcli.rb b/lib/wordmove/wpcli.rb index c386431..3142e08 100644 --- a/lib/wordmove/wpcli.rb +++ b/lib/wordmove/wpcli.rb @@ -67,7 +67,9 @@ def load_from_yml(context) YAML.load_file(yml_path).with_indifferent_access['path'] end - # Returns the wordpress path as per wp-cli configuration + # Returns the wordpress path as per wp-cli configuration. + # A possible scenario is that the used wpcli command could return an empty + # string: we thus rescue parse errors in order to ignore this config source # # @return [String, nil] The wordpress path as per wp-cli configuration or nil # @!scope class @@ -75,6 +77,8 @@ def load_from_yml(context) def load_from_wpcli wpcli_config = JSON.parse(`wp cli param-dump --with-values`, symbolize_names: true) wpcli_config.dig(:path, :current) + rescue JSON::ParserError => _e + nil end end end diff --git a/spec/wpcli_spec.rb b/spec/wpcli_spec.rb index a02edda..ba643a8 100644 --- a/spec/wpcli_spec.rb +++ b/spec/wpcli_spec.rb @@ -32,6 +32,19 @@ it 'returns the configured path' do expect(subject.wpcli_config_path(a_context)).to eq('/path/to/pudding') end + + context 'when wp-cli param-dump returns empty string' do + before do + allow(subject) + .to receive(:`) + .with('wp cli param-dump --with-values') + .and_return('') + end + + it 'will fallback to movefile config without raising errors' do + expect { subject.wpcli_config_path(a_context) }.to_not raise_error(JSON::ParserError) + end + end end end end