jenkins-bot has submitted this change and it was merged.

Change subject: Provide Rake task to serve as a CI entrypoint
......................................................................


Provide Rake task to serve as a CI entrypoint

Implemented a Rake task that runs Cucumber in a standard way with
respect to our Jenkins configuration.

So one can invoke Rake task from anywhere in the working copy,
Environment learned 'test_dir', the base directory from which to lookup
for environments.yml and tests/features.  If relative, paths are made
absolute so the Environment properly load the YAML file and to have
cucumber to point to the proper place.

Add bunch of spec to cover the test_dir parameter.

Update README.md with a couple examples to add the rake task.

Bug: T128190
Signed-off-by: Antoine Musso <[email protected]>
Change-Id: I627d0603487ab88e375fe5aa4fca2f8bb2a07790
---
M CREDITS
M README.md
M lib/mediawiki_selenium.rb
M lib/mediawiki_selenium/cucumber/hooks.rb
M lib/mediawiki_selenium/environment.rb
A lib/mediawiki_selenium/rake_task.rb
M spec/environment_spec.rb
7 files changed, 109 insertions(+), 15 deletions(-)

Approvals:
  Zfilipin: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/CREDITS b/CREDITS
index 6931d6e..83502d1 100644
--- a/CREDITS
+++ b/CREDITS
@@ -5,6 +5,7 @@
 For further details on licensing, see the LICENSE file.
 
 == Developers ==
+* Antoine Musso
 * Chris McMahon
 * Dan Duvall
 * Jeff Hall
diff --git a/README.md b/README.md
index 521fd83..4c61190 100644
--- a/README.md
+++ b/README.md
@@ -204,6 +204,27 @@
 
     SCREENSHOT_FAILURES=true SCREENSHOT_FAILURES_PATH="/tmp/screenshots" 
bundle exec cucumber
 
+### CI Rake task
+
+To utilize the CI rake task, add the following to your `Rakefile`:
+
+    require 'mediawiki_selenium/rake_task'
+    MediawikiSelenium::RakeTask.new
+
+It defaults to look for `environments.yml` and `features` under 
`tests/browser`.
+You can specify the directory:
+
+    require 'mediawiki_selenium/rake_task'
+    MediawikiSelenium::RakeTask.new(test_dir: modules/ve-mw/tests/browser)
+
+CI specific options are passed to cucumber when the rake task detects the
+environment variable WORKSPACE is set. It will emit JUnit results under
+`$WORKSPACE/log/junit`. To reproduce that behavior one can:
+
+    export WORKSPACE=/tmp/myplace
+    mkdir -p $WORKSPACE/log/junit
+    bundle exec rake spec
+
 ## Updating Your Gemfile
 
 In your repository, the `Gemfile` specifies dependencies and `Gemfile.lock` 
defines
diff --git a/lib/mediawiki_selenium.rb b/lib/mediawiki_selenium.rb
index 61691b8..7660c0c 100644
--- a/lib/mediawiki_selenium.rb
+++ b/lib/mediawiki_selenium.rb
@@ -10,6 +10,7 @@
   autoload :LoginHelper, 'mediawiki_selenium/helpers/login_helper'
   autoload :PageFactory, 'mediawiki_selenium/page_factory'
   autoload :Raita, 'mediawiki_selenium/raita'
+  autoload :RakeTask, 'mediawiki_selenium/rake_task'
   autoload :RemoteBrowserFactory, 'mediawiki_selenium/remote_browser_factory'
   autoload :ScreenshotHelper, 'mediawiki_selenium/helpers/screenshot_helper'
   autoload :StrictPending, 'mediawiki_selenium/cucumber/strict_pending'
diff --git a/lib/mediawiki_selenium/cucumber/hooks.rb 
b/lib/mediawiki_selenium/cucumber/hooks.rb
index d9971bb..82ad5d2 100644
--- a/lib/mediawiki_selenium/cucumber/hooks.rb
+++ b/lib/mediawiki_selenium/cucumber/hooks.rb
@@ -3,6 +3,8 @@
 end
 
 AfterConfiguration do |config|
+  MediawikiSelenium::Environment.default_test_directory = config.paths.first 
|| '.'
+
   # Install a formatter that can be used to show feature-related warnings
   pretty_format, io = config.formats.find { |(format, _io)| format == 'pretty' 
}
   config.formats << ['MediawikiSelenium::WarningsFormatter', io] if 
pretty_format
diff --git a/lib/mediawiki_selenium/environment.rb 
b/lib/mediawiki_selenium/environment.rb
index 5faa16a..5c9b5d5 100644
--- a/lib/mediawiki_selenium/environment.rb
+++ b/lib/mediawiki_selenium/environment.rb
@@ -63,7 +63,7 @@
     include Comparable
 
     class << self
-      attr_accessor :default_configuration
+      attr_accessor :default_configuration, :default_test_directory
 
       # Instantiates a new environment using the given set of default
       # configuration from `environments.yml` in the current working
@@ -71,20 +71,23 @@
       #
       # @param name [String] Name of the environment.
       # @param extra [Hash] Additional configuration to use.
+      # @param test_dir [String] Path from which to search upward for
+      # `environments.yml`
       #
-      def load(name, extra = {})
+      def load(name, extra = {}, test_dir = nil)
         name = name.to_s
         configs = []
 
         unless name.empty?
-          envs = YAML.load_file(default_configuration)
+          envs = YAML.load_file(search_for_configuration(test_dir || 
default_test_directory))
           raise ConfigurationError, "unknown environment `#{name}`" unless 
envs.include?(name)
           configs << envs[name]
         end
 
         configs << extra
 
-        new(*configs)
+        env = new(*configs)
+        env
       end
 
       # Instantiates a new environment from the values of `ENV` and the
@@ -93,12 +96,29 @@
       #
       # @see load
       #
-      def load_default
-        load(ENV['MEDIAWIKI_ENVIRONMENT'] || 'default', ENV)
+      def load_default(test_dir = nil)
+        load(ENV['MEDIAWIKI_ENVIRONMENT'] || 'default', ENV, test_dir)
+      end
+
+      # Searches for `environments.yml` in the given path. If it isn't found,
+      # the search continues upward in the directory hierarchy.
+      #
+      # @param path [String] Path to search for configuration
+      #
+      # @return [String] Qualified path to the configuration file
+      #
+      def search_for_configuration(path)
+        return default_configuration if path.nil? || path.empty?
+
+        file_path = File.join(path, default_configuration)
+        return file_path if File.exist?(file_path)
+
+        search_for_configuration(File.dirname(path))
       end
     end
 
     self.default_configuration = 'environments.yml'
+    self.default_test_directory = 'tests/browser'
 
     def initialize(*configs)
       @_config = configs.map { |config| normalize_config(config) 
}.reduce(:merge)
diff --git a/lib/mediawiki_selenium/rake_task.rb 
b/lib/mediawiki_selenium/rake_task.rb
new file mode 100644
index 0000000..e5150fd
--- /dev/null
+++ b/lib/mediawiki_selenium/rake_task.rb
@@ -0,0 +1,29 @@
+require 'cucumber/rake/task'
+require 'mediawiki_selenium'
+require 'uri'
+
+module MediawikiSelenium
+  class RakeTask < Cucumber::Rake::Task
+    def initialize(name = :selenium, test_dir: 
Environment.default_test_directory)
+      target = File.expand_path(test_dir, Rake.original_dir)
+      env = Environment.load_default(target)
+
+      workspace = env.lookup(:workspace, default: nil)
+      site = URI.parse(env.lookup(:mediawiki_url)).host
+      browser = env.browser_name
+
+      options = Shellwords.escape(test_dir)
+
+      if workspace
+        options +=
+          ' --backtrace --verbose --color --format pretty'\
+          " --format Cucumber::Formatter::Sauce --out 
'#{workspace}/log/junit'"\
+          " --tags @#{site}"
+      end
+
+      super(name) do |t|
+        t.cucumber_opts = "#{options} --tags @#{browser}"
+      end
+    end
+  end
+end
diff --git a/spec/environment_spec.rb b/spec/environment_spec.rb
index 833882a..df412ed 100644
--- a/spec/environment_spec.rb
+++ b/spec/environment_spec.rb
@@ -23,18 +23,16 @@
     let(:mediawiki_user) { 'mw user' }
     let(:mediawiki_password) { 'mw password' }
 
-    def mock_environment_name(name)
-      expect(ENV).to 
receive(:[]).with('MEDIAWIKI_ENVIRONMENT').and_return(name)
-    end
-
     describe '.load' do
       subject { Environment.load(name, extra) }
 
       let(:name) { 'foo' }
       let(:extra) { {} }
+      let(:env_file) { 'environments.yml' }
 
       before do
-        expect(YAML).to receive(:load_file).with('environments.yml').
+        allow(Environment).to 
receive(:search_for_configuration).and_return(env_file)
+        allow(YAML).to receive(:load_file).with(env_file).
           and_return('foo' => { 'x' => 'a', 'y' => 'b' })
       end
 
@@ -63,21 +61,43 @@
           expect(subject[:y]).to eq('b')
         end
       end
+
+      context 'when a test_dir is given' do
+        subject { Environment.load('foo', {}, test_dir) }
+
+        let(:test_dir) { '/srv/workspace/job' }
+        let(:env_file) { File.join(test_dir, 'environments.yml') }
+
+        it 'loads configuration in the given test_dir' do
+          expect(Environment).to 
receive(:search_for_configuration).with(test_dir).
+            and_return(env_file)
+          expect(YAML).to receive(:load_file).with(env_file).
+            and_return('foo' => { 'x' => 'a', 'y' => 'b' })
+          subject
+        end
+      end
+
     end
 
     describe '.load_default' do
       subject { Environment.load_default }
 
+      let(:mediawiki_environment) { 'foo' }
+
+      before do
+        expect(ENV).to 
receive(:[]).with('MEDIAWIKI_ENVIRONMENT').and_return(mediawiki_environment)
+      end
+
       it 'loads the environment configuration specified by 
MEDIAWIKI_ENVIRONMENT' do
-        mock_environment_name('foo')
-        expect(Environment).to receive(:load).with('foo', ENV)
+        expect(Environment).to receive(:load).with(mediawiki_environment, ENV, 
nil)
         subject
       end
 
       context 'where MEDIAWIKI_ENVIRONMENT is not defined' do
+        let(:mediawiki_environment) { nil }
+
         it 'looks for a "default" environment' do
-          mock_environment_name(nil)
-          expect(Environment).to receive(:load).with('default', ENV)
+          expect(Environment).to receive(:load).with('default', ENV, nil)
           subject
         end
       end

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

Gerrit-MessageType: merged
Gerrit-Change-Id: I627d0603487ab88e375fe5aa4fca2f8bb2a07790
Gerrit-PatchSet: 17
Gerrit-Project: mediawiki/selenium
Gerrit-Branch: master
Gerrit-Owner: Zfilipin <[email protected]>
Gerrit-Reviewer: Dduvall <[email protected]>
Gerrit-Reviewer: Hashar <[email protected]>
Gerrit-Reviewer: Zfilipin <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to