Skip to content

Commit

Permalink
Handle edge case when wp cli param-dump returns empty string
Browse files Browse the repository at this point in the history
  • Loading branch information
alessandro-fazzi committed Dec 26, 2021
1 parent d5b20fb commit ed376f0
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
9 changes: 5 additions & 4 deletions .github/workflows/ruby.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 5 additions & 1 deletion lib/wordmove/wpcli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,18 @@ 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
# @!visibility private
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
Expand Down
13 changes: 13 additions & 0 deletions spec/wpcli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ed376f0

Please sign in to comment.