Skip to content

Commit

Permalink
Merge pull request #597 from maiorano84/master
Browse files Browse the repository at this point in the history
WP CLI: smarter `--path` determination process
  • Loading branch information
alessandro-fazzi committed May 16, 2020
2 parents 110dd6f + df939b3 commit ba959f0
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
29 changes: 27 additions & 2 deletions lib/wordmove/sql_adapter/wpcli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,40 @@ def command
raise UnmetPeerDependencyError, "WP-CLI is not installed or not in your $PATH"
end

"wp search-replace --path=#{local_path} #{from} #{to} --quiet "\
"--skip-columns=guid --all-tables --allow-root"
opts = [
"--path=#{cli_config_path}",
from,
to,
"--quiet",
"--skip-columns=guid",
"--all-tables",
"--allow-root"
]

"wp search-replace #{opts.join(' ')}"
end

private

def wp_in_path?
system('which wp > /dev/null 2>&1')
end

def cli_config_path
load_from_yml || load_from_cli || local_path
end

def load_from_yml
cli_config_path = File.join(local_path, "wp-cli.yml")
return unless File.exist?(cli_config_path)

YAML.load_file(cli_config_path).with_indifferent_access["path"]
end

def load_from_cli
cli_config = JSON.parse(`wp cli param-dump --with-values`, symbolize_names: true)
cli_config.dig(:path, :current)
end
end
end
end
1 change: 1 addition & 0 deletions spec/fixtures/wp-cli.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
path: /path/to/steak
46 changes: 40 additions & 6 deletions spec/sql_adapter/wpcli_spec.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'spec_helper'

describe Wordmove::SqlAdapter::Wpcli do
let(:config_key) { :vhost }
let(:source_config) { { vhost: 'sausage' } }
Expand All @@ -12,15 +14,47 @@
)
end

before do
allow(adapter).to receive(:wp_in_path?).and_return(true)
allow(adapter)
.to receive(:`)
.with('wp cli param-dump --with-values')
.and_return("{}")
end

context "#command" do
before do
allow(adapter).to receive(:wp_in_path?).and_return(true)
context "having wp-cli.yml in local_path" do
let(:local_path) { fixture_folder_root_relative_path }

it "returns the right command as a string" do
expect(adapter.command)
.to eq("wp search-replace --path=/path/to/steak sausage bacon --quiet "\
"--skip-columns=guid --all-tables --allow-root")
end
end

context "without wp-cli.yml in local_path" do
before do
allow(adapter)
.to receive(:`)
.with('wp cli param-dump --with-values')
.and_return("{\"path\":{\"current\":\"\/path\/to\/pudding\"}}")
end
context "but still reachable by wp-cli" do
it "returns the right command as a string" do
expect(adapter.command)
.to eq("wp search-replace --path=/path/to/pudding sausage bacon --quiet "\
"--skip-columns=guid --all-tables --allow-root")
end
end
end

it "returns the right command as a string" do
expect(adapter.command)
.to eq("wp search-replace --path=/path/to/ham sausage bacon --quiet "\
"--skip-columns=guid --all-tables --allow-root")
context "without any wp-cli configuration" do
it "returns the right command with '--path' flag set to local_path" do
expect(adapter.command)
.to eq("wp search-replace --path=/path/to/ham sausage bacon --quiet "\
"--skip-columns=guid --all-tables --allow-root")
end
end
end
end
8 changes: 8 additions & 0 deletions spec/support/fixture_helpers.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
module FixtureHelpers
def fixture_folder_path
File.join(__dir__, '..', 'fixtures')
end

def fixture_folder_root_relative_path
File.join('spec', 'fixtures')
end

def fixture_path_for(filename)
File.join(__dir__, '..', 'fixtures', filename)
end
Expand Down

0 comments on commit ba959f0

Please sign in to comment.