Skip to content

Commit

Permalink
Add a bin/rspec helper that can run specs inside components
Browse files Browse the repository at this point in the history
For when a change affects multiple components or for tools relying on
bin/rspec this helper will make things a lot easier.

Will take in both paths and options and run the specs for each
component speparately keeping the options.

Example:

    $ bin/rspec core/spec/models/spree/exchange_spec.rb api/spec/features/checkout_spec.rb -f doc

Will give the following output:

```
$ cd core; bundle exec rspec -f doc /Users/elia/Code/Nebulab/solidus/core/spec/models/spree/exchange_spec.rb; cd -
Run options: include {:focus=>true}

All examples were filtered out; ignoring {:focus=>true}

Randomized with seed 190

Spree::Exchange
  #description
    describes the return items' change in options
  #perform!
    creates shipments for the order with the return items exchange inventory units
    when it cannot create shipments for all items
      raises an UnableToCreateShipments error
  #display_amount
    is the total amount of all return items
  .param_key
    is expected to eq "spree_exchange"
  #to_key
    is expected to be nil

Finished in 2.22 seconds (files took 3.27 seconds to load)
6 examples, 0 failures

Randomized with seed 190

$ cd api; bundle exec rspec -f doc /Users/elia/Code/Nebulab/solidus/api/spec/features/checkout_spec.rb; cd -

Randomized with seed 55051

Api Feature Specs
  is able to checkout with the update request
  is able to checkout with individualized requests
  is able to checkout with the create request

Finished in 3.15 seconds (files took 3.3 seconds to load)
3 examples, 0 failures

Randomized with seed 55051

```

The first line is always the executed command that can be copied and
pasted as it is.
  • Loading branch information
elia committed Feb 3, 2020
1 parent 547b9d2 commit 3626abb
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 16 deletions.
22 changes: 6 additions & 16 deletions bin/build
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,28 @@ export DB=${DB:-postgresql}

bin/setup

# Solidus API
echo "***********************"
echo "* Testing Solidus API *"
echo "***********************"
cd api
bundle exec rspec spec
bin/rspec api/spec

# Solidus Backend
echo "***************************"
echo "* Testing Solidus Backend *"
echo "***************************"
cd ../backend
bundle exec rspec spec
bundle exec teaspoon --require=../backend/spec/teaspoon_env.rb
bin/rspec backend/spec
bundle exec teaspoon --require=backend/spec/teaspoon_env.rb

# Solidus Core
echo "************************"
echo "* Testing Solidus Core *"
echo "************************"
cd ../core
bundle exec rspec spec
bin/rspec core/spec

# Solidus Frontend
echo "****************************"
echo "* Testing Solidus Frontend *"
echo "****************************"
cd ../frontend
bundle exec rspec spec
bin/rspec frontend/spec

# Solidus Sample
echo "**************************"
echo "* Testing Solidus Sample *"
echo "**************************"
cd ../sample
bundle exec rspec spec
bin/rspec sample/spec
49 changes: 49 additions & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/usr/bin/env ruby

ROOT = File.expand_path('..', __dir__)

LIBS = %w[
api
backend
core
frontend
sample
]

# Is it a spec file or an RSpec option?
is_spec = ->(path) {
path = path.split(':', 2).first # ignore line info
File.directory?(path) ? path.include?('/spec') : path.end_with?('_spec.rb')
}

# Find the Solidus library for this path
find_lib = ->(path) {
LIBS.find { |lib| path.start_with? File.join(ROOT, lib) +'/' }
}

spec_files, options = ARGV.partition(&is_spec) # Separate specs and options
spec_files.map! { |f| File.expand_path(f) } # Let all paths be absolute
specs = {}

if spec_files.any?
specs = spec_files.group_by(&find_lib)
else
# If no files are provided run all specs in each lib.
LIBS.each { |lib| specs[lib] = [] }
end

# If we weren't able to find the solidus lib for some files
# then we have them under the nil key.
if specs.key?(nil)
abort "Couldn't find a lib for these files: #{specs[nil].join(' ')}"
end

# Run specs for each lib separately
specs.each do |lib, files|
Dir.chdir(lib) do
command = ['bundle', 'exec', 'rspec', *options, *files]
warn "$ cd #{lib}; #{command.join(' ')}; cd -"
system *command
exit $?.exitstatus unless $?.success?
end
end

0 comments on commit 3626abb

Please sign in to comment.