Skip to content

Commit

Permalink
Add tag option to filter specs
Browse files Browse the repository at this point in the history
The rspec tag option allows allows you to include or exclude different specs.  On our CI environment we actively use this to target testing on different components of our system.
  • Loading branch information
jorge-wonolo committed Nov 10, 2021
1 parent 7e6d335 commit 21ed86b
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 4 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ OPTIONS:
--queue-wait-timeout N Time to wait for a queue to be ready before considering it failed (default: 30).
--fail-fast N Abort build with a non-zero status code after N failed examples.
--reproduction Enable reproduction mode: Publish files and examples in the exact order given in the command. Incompatible with --timings.
--tag TAG Run examples with the specified tag, or exclude examples by adding ~ before the tag. - e.g. ~slow - TAG is always converted to a symbol.
-h, --help Show this message.
-v, --version Print the version and exit.
```
Expand Down
12 changes: 11 additions & 1 deletion bin/rspecq
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ def env_set?(var)
["1", "true"].include?(ENV[var])
end

opts = {}
opts = {
:tags => []
}

OptionParser.new do |o|
name = File.basename($PROGRAM_NAME)
Expand Down Expand Up @@ -112,6 +114,13 @@ OptionParser.new do |o|
exit
end

o.on("--tag TAG", "Run examples with the specified tag, or exclude examples " \
"by adding ~ before the tag." \
" - e.g. ~slow" \
" - TAG is always converted to a symbol.") do |tag|
opts[:tags] << tag
end

o.on_tail("-v", "--version", "Print the version and exit.") do
puts "#{name} #{RSpecQ::VERSION}"
exit
Expand Down Expand Up @@ -169,5 +178,6 @@ else
worker.fail_fast = opts[:fail_fast]
worker.seed = Integer(opts[:seed]) if opts[:seed]
worker.reproduction = opts[:reproduction]
worker.tags = opts[:tags]
worker.work
end
11 changes: 8 additions & 3 deletions lib/rspecq/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class Worker
# The RSpec seed
attr_accessor :seed

# Rspec tags
attr_accessor :tags

# Reproduction flag. If true, worker will publish files in the exact order
# given in the command.
attr_accessor :reproduction
Expand All @@ -72,6 +75,7 @@ def initialize(build_id:, worker_id:, redis_opts:)
@max_requeues = 3
@queue_wait_timeout = 30
@seed = srand && srand % 0xFFFF
@tags = []
@reproduction = false

RSpec::Core::Formatters.register(Formatters::JobTimingRecorder, :dump_summary)
Expand Down Expand Up @@ -122,8 +126,10 @@ def work
if populate_timings
RSpec.configuration.add_formatter(Formatters::JobTimingRecorder.new(queue, job))
end

opts = RSpec::Core::ConfigurationOptions.new(["--format", "progress", job])

options = ["--format", "progress", job]
tags.each { |tag| options.push(*["--tag", tag]) }
opts = RSpec::Core::ConfigurationOptions.new(options)
_result = RSpec::Core::Runner.new(opts).run($stderr, $stdout)

queue.acknowledge_job(job)
Expand All @@ -149,7 +155,6 @@ def try_publish_queue!(queue)
)
return
end

RSpec.configuration.files_or_directories_to_run = files_or_dirs_to_run
files_to_run = RSpec.configuration.files_to_run.map { |j| relative_path(j) }

Expand Down
13 changes: 13 additions & 0 deletions test/sample_suites/tagged_suite/spec/tagged_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
RSpec.describe do
it "is slow", :slow do
expect(true).to be true
end

it "is fast", :fast do
expect(true).to be true
end

it "is not tagged"do
expect(true).to be true
end
end
25 changes: 25 additions & 0 deletions test/test_tags.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "test_helpers"

class TestTags < RSpecQTest
def test_inclusion_filter
queue = exec_build("tagged_suite", "--tag=slow")

assert_processed_jobs [
"./spec/tagged_spec.rb",
], queue

assert_equal 1, queue.example_count
end

def test_exclusion_filter
queue = exec_build("tagged_suite", "--tag=~slow")

assert_equal 2, queue.example_count
end

def test_mixed_filter
queue = exec_build("tagged_suite", "--tag=~slow --tag=~fast")

assert_equal 1, queue.example_count
end
end

0 comments on commit 21ed86b

Please sign in to comment.