Commit 0e60d25a authored by Ramya Authappan's avatar Ramya Authappan

Merge branch 'shl-write-no-of-examples-to-file' into 'master'

Write number of examples to file with --count-examples-only

See merge request gitlab-org/gitlab!79605
parents 35b27507 78acec0e
......@@ -41,6 +41,9 @@ module QA
end
end
# Save the scenario class name
QA::Runtime::Scenario.define(:klass, self.class.name)
Specs::Runner.perform do |specs|
specs.tty = true
specs.tags = self.class.focus
......
......@@ -32,6 +32,9 @@ module QA
# Given *gitlab_address* = 'http://gitlab-abc123.test/' #=> http://about.gitlab-abc123.test/
Runtime::Scenario.define(:about_address, URI(-> { gitlab_address.host = "about.#{gitlab_address.host}"; gitlab_address }.call).to_s) # rubocop:disable Style/Semicolon
# Save the scenario class name
Runtime::Scenario.define(:klass, self.class.name)
##
# Setup knapsack and download latest report
#
......
......@@ -7,6 +7,7 @@ module QA
module Specs
class Runner < Scenario::Template
attr_accessor :tty, :tags, :options
RegexMismatchError = Class.new(StandardError)
DEFAULT_TEST_PATH_ARGS = ['--', File.expand_path('./features', __dir__)].freeze
DEFAULT_STD_ARGS = [$stderr, $stdout].freeze
......@@ -72,16 +73,48 @@ module QA
elsif Runtime::Scenario.attributes[:count_examples_only]
args.unshift('--dry-run')
out = StringIO.new
RSpec::Core::Runner.run(args.flatten, $stderr, out).tap do |status|
abort if status.nonzero?
end
$stdout.puts out.string.match(/(\d+) examples,/)[1]
begin
total_examples = out.string.match(/(\d+) examples?,/)[1]
rescue StandardError
raise RegexMismatchError, 'Rspec output did not match regex'
end
filename = build_filename
File.open(filename, 'w') { |f| f.write(total_examples) } if total_examples.to_i > 0
$stdout.puts "Total examples in #{Runtime::Scenario.klass}: #{total_examples}#{total_examples.to_i > 0 ? ". Saved to file: #{filename}" : ''}"
else
RSpec::Core::Runner.run(args.flatten, *DEFAULT_STD_ARGS).tap do |status|
abort if status.nonzero?
end
end
end
private
def build_filename
filename = Runtime::Scenario.klass.split('::').last(3).join('_').downcase
tags = []
options.reduce do |before, after|
tags << after if %w[--tag -t].include?(before)
after
end
tags = tags.compact.join('_')
filename.concat("_#{tags}") unless tags.empty?
filename.concat('.txt')
FileUtils.mkdir_p('no_of_examples')
File.join('no_of_examples', filename)
end
end
end
end
......@@ -14,6 +14,7 @@ RSpec.describe QA::Specs::Runner do
allow(QA::Runtime::Browser).to receive(:configure!)
QA::Runtime::Scenario.define(:gitlab_address, "http://gitlab.test")
QA::Runtime::Scenario.define(:klass, "QA::Scenario::Test::Instance::All")
end
it_behaves_like 'excludes orchestrated, transient, and geo'
......@@ -43,6 +44,43 @@ RSpec.describe QA::Specs::Runner do
subject.perform
end
it 'writes to file when examples are more than zero' do
allow(RSpec::Core::Runner).to receive(:run).and_return(0)
expect(File).to receive(:open).with('no_of_examples/test_instance_all.txt', 'w') { '22' }
subject.perform
end
it 'does not write to file when zero examples' do
out.string = '0 examples,'
allow(RSpec::Core::Runner).to receive(:run).and_return(0)
expect(File).not_to receive(:open)
subject.perform
end
it 'raises error when Rspec output does not match regex' do
out.string = '0'
allow(RSpec::Core::Runner).to receive(:run).and_return(0)
expect { subject.perform }
.to raise_error(QA::Specs::Runner::RegexMismatchError, 'Rspec output did not match regex')
end
context 'when --tag is specified as an option' do
subject { described_class.new.tap { |runner| runner.options = %w[--tag actioncable] } }
it 'includes the option value in the file name' do
expect_rspec_runner_arguments(['--dry-run', '--tag', '~geo', '--tag', 'actioncable', *described_class::DEFAULT_TEST_PATH_ARGS], [$stderr, anything])
expect(File).to receive(:open).with('no_of_examples/test_instance_all_actioncable.txt', 'w') { '22' }
subject.perform
end
end
after do
QA::Runtime::Scenario.attributes.delete(:count_examples_only)
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment