Commit 78acec0e authored by Sanad Liaquat's avatar Sanad Liaquat Committed by Ramya Authappan

Write number of examples to file

With the --count-examples-only switch, write the number of examples
to file in a no_of_examples directory. Also include the tag when
--tag is used.
parent c128cb00
...@@ -41,6 +41,9 @@ module QA ...@@ -41,6 +41,9 @@ module QA
end end
end end
# Save the scenario class name
QA::Runtime::Scenario.define(:klass, self.class.name)
Specs::Runner.perform do |specs| Specs::Runner.perform do |specs|
specs.tty = true specs.tty = true
specs.tags = self.class.focus specs.tags = self.class.focus
......
...@@ -32,6 +32,9 @@ module QA ...@@ -32,6 +32,9 @@ module QA
# Given *gitlab_address* = 'http://gitlab-abc123.test/' #=> http://about.gitlab-abc123.test/ # 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 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 # Setup knapsack and download latest report
# #
......
...@@ -7,6 +7,7 @@ module QA ...@@ -7,6 +7,7 @@ module QA
module Specs module Specs
class Runner < Scenario::Template class Runner < Scenario::Template
attr_accessor :tty, :tags, :options attr_accessor :tty, :tags, :options
RegexMismatchError = Class.new(StandardError)
DEFAULT_TEST_PATH_ARGS = ['--', File.expand_path('./features', __dir__)].freeze DEFAULT_TEST_PATH_ARGS = ['--', File.expand_path('./features', __dir__)].freeze
DEFAULT_STD_ARGS = [$stderr, $stdout].freeze DEFAULT_STD_ARGS = [$stderr, $stdout].freeze
...@@ -72,16 +73,48 @@ module QA ...@@ -72,16 +73,48 @@ module QA
elsif Runtime::Scenario.attributes[:count_examples_only] elsif Runtime::Scenario.attributes[:count_examples_only]
args.unshift('--dry-run') args.unshift('--dry-run')
out = StringIO.new out = StringIO.new
RSpec::Core::Runner.run(args.flatten, $stderr, out).tap do |status| RSpec::Core::Runner.run(args.flatten, $stderr, out).tap do |status|
abort if status.nonzero? abort if status.nonzero?
end 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 else
RSpec::Core::Runner.run(args.flatten, *DEFAULT_STD_ARGS).tap do |status| RSpec::Core::Runner.run(args.flatten, *DEFAULT_STD_ARGS).tap do |status|
abort if status.nonzero? abort if status.nonzero?
end end
end 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 end
end end
...@@ -14,6 +14,7 @@ RSpec.describe QA::Specs::Runner do ...@@ -14,6 +14,7 @@ RSpec.describe QA::Specs::Runner do
allow(QA::Runtime::Browser).to receive(:configure!) allow(QA::Runtime::Browser).to receive(:configure!)
QA::Runtime::Scenario.define(:gitlab_address, "http://gitlab.test") QA::Runtime::Scenario.define(:gitlab_address, "http://gitlab.test")
QA::Runtime::Scenario.define(:klass, "QA::Scenario::Test::Instance::All")
end end
it_behaves_like 'excludes orchestrated, transient, and geo' it_behaves_like 'excludes orchestrated, transient, and geo'
...@@ -43,6 +44,43 @@ RSpec.describe QA::Specs::Runner do ...@@ -43,6 +44,43 @@ RSpec.describe QA::Specs::Runner do
subject.perform subject.perform
end 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 after do
QA::Runtime::Scenario.attributes.delete(:count_examples_only) QA::Runtime::Scenario.attributes.delete(:count_examples_only)
end 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