Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WP CLI Configuration File Check #597

Merged
merged 19 commits into from
May 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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