Dduvall has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/179048

Change subject: Support for default environment configurations
......................................................................

Support for default environment configurations

Default configuration for different types of environments can now be
defined in an `environments.yml` file. The contents of the file should
be a YAML hash containing key/hash pairs corresponding to a environment
name and set of environment variable defaults. If a value for
`MEDIAWIKI_ENVIRONMENT` is provided, the set of defaults defined in the
file will be merged with any given via environment variables directly,
the latter taking precedence.

Change-Id: Ifb358ec205b36ff0fbde9b4b16501f5ed354c28c
---
M lib/mediawiki_selenium/configuration_error.rb
M lib/mediawiki_selenium/environment.rb
M lib/mediawiki_selenium/support/env.rb
M spec/environment_spec.rb
4 files changed, 72 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/selenium 
refs/changes/48/179048/1

diff --git a/lib/mediawiki_selenium/configuration_error.rb 
b/lib/mediawiki_selenium/configuration_error.rb
index 2547bce..6dc29c9 100644
--- a/lib/mediawiki_selenium/configuration_error.rb
+++ b/lib/mediawiki_selenium/configuration_error.rb
@@ -1,13 +1,4 @@
 module MediawikiSelenium
   class ConfigurationError < StandardError
-    attr_reader :name
-
-    def initialize(name)
-      @name = name
-    end
-
-    def to_s
-      "missing configuration for #{name}"
-    end
   end
 end
diff --git a/lib/mediawiki_selenium/environment.rb 
b/lib/mediawiki_selenium/environment.rb
index c5a2998..afe4b91 100644
--- a/lib/mediawiki_selenium/environment.rb
+++ b/lib/mediawiki_selenium/environment.rb
@@ -1,3 +1,5 @@
+require "yaml"
+
 module MediawikiSelenium
   # Provides an interface that unifies environmental configuration, page
   # objects, and browser setup. Additionally, it provides a DSL for switching
@@ -10,8 +12,36 @@
     attr_reader :config
     protected :config
 
-    def initialize(config)
-      @config = normalize_config(config)
+    class << self
+      attr_accessor :default_configuration
+
+      # Instantiates a new environment using the given set of default
+      # configuration from `environments.yml` in the current working
+      # directory, and the additional hash of environment variables.
+      #
+      # @param name [String] Name of the environment.
+      # @param extra [Hash] Additional configuration to use.
+      #
+      def load(name, extra = {})
+        name = name.to_s
+        configs = []
+
+        unless name.empty?
+          envs = YAML.load_file(default_configuration)
+          raise ConfigurationError, "unknown environment `#{name}`" unless 
envs.include?(name)
+          configs << envs[name]
+        end
+
+        configs << extra
+
+        new(*configs)
+      end
+    end
+
+    self.default_configuration = "environments.yml"
+
+    def initialize(*configs)
+      @config = configs.map { |config| normalize_config(config) 
}.reduce(:merge)
       @factory_cache = {}
     end
 
@@ -177,7 +207,7 @@
         if options.include?(:default)
           options[:default].is_a?(Proc) ? options[:default].call : 
options[:default]
         else
-          raise ConfigurationError, key
+          raise ConfigurationError, "missing configuration for `#{key}`"
         end
       else
         value
diff --git a/lib/mediawiki_selenium/support/env.rb 
b/lib/mediawiki_selenium/support/env.rb
index a829a10..1040ebc 100644
--- a/lib/mediawiki_selenium/support/env.rb
+++ b/lib/mediawiki_selenium/support/env.rb
@@ -18,7 +18,7 @@
 require "mediawiki_selenium/support/modules/sauce_helper"
 require "mediawiki_selenium/support/modules/strict_pending"
 
-World { MediawikiSelenium::Environment.new(ENV) }
+World { MediawikiSelenium::Environment.load(ENV["MEDIAWIKI_ENVIRONMENT"], ENV) 
}
 
 World(MediawikiSelenium::ApiHelper)
 World(MediawikiSelenium::PageFactory)
diff --git a/spec/environment_spec.rb b/spec/environment_spec.rb
index 1a27c2c..40cdc81 100644
--- a/spec/environment_spec.rb
+++ b/spec/environment_spec.rb
@@ -23,6 +23,44 @@
     let(:mediawiki_user) { "mw user" }
     let(:mediawiki_password) { "mw password" }
 
+    describe ".load" do
+      subject { Environment.load(name, extra) }
+
+      let(:name) { "foo" }
+      let(:extra) { {} }
+
+      before do
+        expect(YAML).to receive(:load_file).with("environments.yml").
+          and_return("foo" => { "x" => "a", "y" => "b" })
+      end
+
+      it "returns a new environment" do
+        expect(subject).to be_a(Environment)
+      end
+
+      it "uses the given configuration in `environments.yml`" do
+        expect(subject[:x]).to eq("a")
+        expect(subject[:y]).to eq("b")
+      end
+
+      context "when the given environment does not exist in 
`environments.yml`" do
+        let(:name) { "bar" }
+
+        it "raises a ConfigurationError" do
+          expect { subject }.to raise_error(ConfigurationError, "unknown 
environment `bar`")
+        end
+      end
+
+      context "when extra configuration is given" do
+        let(:extra) { { x: "c" } }
+
+        it "overwrites the loaded configuration" do
+          expect(subject[:x]).to eq("c")
+          expect(subject[:y]).to eq("b")
+        end
+      end
+    end
+
     describe "#==" do
       subject { env == other }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/179048
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ifb358ec205b36ff0fbde9b4b16501f5ed354c28c
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

Reply via email to