Dduvall has uploaded a new change for review.
https://gerrit.wikimedia.org/r/180079
Change subject: Better encapsulation of API-related functionality
......................................................................
Better encapsulation of API-related functionality
Factored out API-related methods from Environment into ApiHelper.
Change-Id: I145617c640e8b59c8b904dc0d26e7ea824960e0f
---
M lib/mediawiki_selenium/environment.rb
M lib/mediawiki_selenium/support/modules/api_helper.rb
M spec/api_helper_spec.rb
M spec/environment_spec.rb
4 files changed, 70 insertions(+), 44 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/selenium
refs/changes/79/180079/1
diff --git a/lib/mediawiki_selenium/environment.rb
b/lib/mediawiki_selenium/environment.rb
index 61e38fd..7790ba7 100644
--- a/lib/mediawiki_selenium/environment.rb
+++ b/lib/mediawiki_selenium/environment.rb
@@ -253,17 +253,13 @@
#
# @param id [Symbol] Alternative wiki ID.
#
- # @yield [wiki_url, api_url]
+ # @yield [wiki_url]
# @yieldparam wiki_url [String] Alternative wiki URL.
- # @yieldparam api_url [String] Alternative API URL.
#
# @return [Environment]
#
def on_wiki(id, &blk)
- url = lookup(:mediawiki_url, id: id)
- api_url = lookup(:mediawiki_api_url, id: id, default: -> {
api_url_from(url) })
-
- with(mediawiki_url: url, mediawiki_api_url: api_url, &blk)
+ with_alternative(:mediawiki_url, id, &blk)
end
# Returns the current value for `:mediawiki_password` or the value for the
@@ -417,10 +413,6 @@
end
private
-
- def api_url_from(wiki_url)
- URI.parse(wiki_url).merge("/w/api.php").to_s
- end
def browser_config
lookup_all(browser_factory.all_binding_keys, default: nil).reject { |k,
v| v.nil? }
diff --git a/lib/mediawiki_selenium/support/modules/api_helper.rb
b/lib/mediawiki_selenium/support/modules/api_helper.rb
index 9ccc44a..45bb421 100644
--- a/lib/mediawiki_selenium/support/modules/api_helper.rb
+++ b/lib/mediawiki_selenium/support/modules/api_helper.rb
@@ -19,5 +19,27 @@
end
end
+ # Extends parent implementation to also override the API URL. If no API
+ # URL is explicitly defined for the given alternative, one is constructed
+ # relative to the wiki URL.
+ #
+ # @yield [wiki_url, api_url]
+ # @yieldparam wiki_url [String] Alternative wiki URL.
+ # @yieldparam api_url [String] Alternative API URL.
+ #
+ # @see Environment#on_wiki
+ #
+ def on_wiki(id, &blk)
+ super(id) do |wiki_url|
+ api_url = lookup(:mediawiki_api_url, id: id, default: -> {
api_url_from(wiki_url) })
+ return with(mediawiki_url: wiki_url, mediawiki_api_url: api_url, &blk)
+ end
+ end
+
+ private
+
+ def api_url_from(wiki_url)
+ URI.parse(wiki_url).merge("/w/api.php").to_s
+ end
end
end
diff --git a/spec/api_helper_spec.rb b/spec/api_helper_spec.rb
index a331d9d..33bece0 100644
--- a/spec/api_helper_spec.rb
+++ b/spec/api_helper_spec.rb
@@ -6,7 +6,8 @@
let(:config) do
{
- mediawiki_url: "http://an.example/wiki/",
+ mediawiki_url: wiki_url,
+ mediawiki_url_b: alternative_wiki_url,
mediawiki_api_url: api_url,
mediawiki_api_url_b: alternative_api_url,
mediawiki_user: "mw user",
@@ -14,11 +15,13 @@
}
end
- let(:api_url) { "http://an.example/api" }
- let(:alternative_api_url) { "" }
-
describe "#api" do
subject { env.api }
+
+ let(:wiki_url) { "http://an.example/wiki/" }
+ let(:api_url) { "http://an.example/api" }
+ let(:alternative_wiki_url) { "http://another.example/wiki/" }
+ let(:alternative_api_url) { "" }
let(:client) { double(MediawikiApi::Client) }
@@ -45,7 +48,7 @@
context "from an altered environment" do
subject { env2.api }
- let(:env2) { env.with_alternative(:mediawiki_api_url, :b) }
+ let(:env2) { env.on_wiki(:b) }
context "with the same API URL" do
let(:alternative_api_url) { api_url }
@@ -72,5 +75,37 @@
end
end
end
+
+ describe "#on_wiki" do
+ let(:wiki_url) { "http://an.example/wiki/" }
+ let(:api_url) { "http://an.example/api" }
+ let(:alternative_wiki_url) { "http://another.example/wiki/" }
+
+ context "and the given alternative API URL is configured" do
+ let(:alternative_api_url) { "http://another.example/api" }
+
+ it "executes in the new environment using the alternative wiki and API
URL" do
+ _ = self
+
+ env.on_wiki(:b) do |wiki_url, api_url|
+ _.expect(wiki_url).to _.eq(_.alternative_wiki_url)
+ _.expect(api_url).to _.eq(_.alternative_api_url)
+ end
+ end
+ end
+
+ context "and no explicit API URL is configured for the wiki" do
+ let(:alternative_api_url) { "" }
+
+ it "constructs one relative to the wiki URL" do
+ _ = self
+
+ env.on_wiki(:b) do |wiki_url, api_url|
+ _.expect(wiki_url).to _.eq(_.alternative_wiki_url)
+ _.expect(api_url).to _.eq("http://another.example/w/api.php")
+ end
+ end
+ end
+ end
end
end
diff --git a/spec/environment_spec.rb b/spec/environment_spec.rb
index 6aead9b..f7664a4 100644
--- a/spec/environment_spec.rb
+++ b/spec/environment_spec.rb
@@ -1,10 +1,11 @@
require "spec_helper"
+require 'pry-byebug'
module MediawikiSelenium
describe Environment do
subject { env }
- let(:env) { Environment.new(config) }
+ let(:env) { Environment.new(config).extend(ApiHelper) }
let(:config) { minimum_config }
let(:minimum_config) do
@@ -320,9 +321,7 @@
let(:config) do
{
mediawiki_url: "http://an.example/wiki",
- mediawiki_api_url: "http://an.example/w/api.php",
mediawiki_url_b: "http://altb.example/wiki",
- mediawiki_api_url_b: "http://altb.example/w/api.php",
mediawiki_url_c: "http://altc.example/wiki",
}
end
@@ -335,32 +334,10 @@
expect(new_env).to receive(:config).and_return(new_config)
end
- it "executes in the new environment using the alternative wiki and API
urls" do
- expect(new_config).to receive(:merge!).with(
- mediawiki_url: "http://altb.example/wiki",
- mediawiki_api_url: "http://altb.example/w/api.php"
- )
- expect(new_env).to receive(:instance_exec).with(
- "http://altb.example/wiki",
- "http://altb.example/w/api.php"
- )
+ it "executes in the new environment using the alternative wiki URL" do
+ expect(new_config).to receive(:merge!).with(mediawiki_url:
"http://altb.example/wiki")
+ expect(new_env).to
receive(:instance_exec).with("http://altb.example/wiki")
subject
- end
-
- context "and no explicit API URL is configured for the wiki" do
- let(:id) { :c }
-
- it "constructs one relative to the wiki URL" do
- expect(new_config).to receive(:merge!).with(
- mediawiki_url: "http://altc.example/wiki",
- mediawiki_api_url: "http://altc.example/w/api.php"
- )
- expect(new_env).to receive(:instance_exec).with(
- "http://altc.example/wiki",
- "http://altc.example/w/api.php"
- )
- subject
- end
end
end
@@ -411,7 +388,7 @@
describe "#wiki_url" do
subject { env.wiki_url(url) }
- let(:env) { Environment.new(mediawiki_url: "http://an.example/wiki/") }
+ let(:env) { Environment.new(mediawiki_url:
"http://an.example/wiki/").extend(ApiHelper) }
context "with no given url" do
let(:url) { nil }
--
To view, visit https://gerrit.wikimedia.org/r/180079
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: I145617c640e8b59c8b904dc0d26e7ea824960e0f
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/selenium
Gerrit-Branch: env-abstraction-layer
Gerrit-Owner: Dduvall <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits