SpecsFor
This ruby gem is a CLI (Command Line Interface) tool for developers to easily
find and run rspec on the named files or discovered changed files.
Installation
Install the gem and add to the application's Gemfile by executing:
bundle add 'specs_for'
If bundler is not being used to manage dependencies, install the gem by executing:
gem install specs_for
If your environment uses a private gem proxy/cache and the newest published version is not yet visible, install from a local built gem file:
bundle exec rake build
gem install --local pkg/specs_for-<version>.gem --bindir bin
Usage
Command line
# Run specs for specific files
specs-for lib/my_class.rb app/models/user.rb
# Run specs for all git-changed files
specs-for -c
# Run specs for changed files with an RSpec tag
specs-for -c -I # --tag integration
specs-for -c -T smoke # --tag smoke
# Read filenames from stdin
git diff --name-only HEAD~1 | specs-for -
# Show which spec files would run, without running them
specs-for -s lib/my_class.rb
# Dry-run: print the rspec command without executing it
specs-for -n lib/my_class.rb
Bundler context and standalone usage
specs-for is designed to run as a standalone CLI, independent of the current
project's Gemfile dependency constraints.
- Prefer:
specs-for ... - Avoid:
bundle exec specs-for ...(this forces the current project's Bundler context)
When invoked with bundle exec, RubyGems resolution is controlled by that
project's bundle, which can cause version conflicts unrelated to specs-for.
Path mapping
Source files are mapped to spec files by replacing the first app/ or lib/
segment with spec/ and appending _spec before .rb. Component monorepo
paths preserve the component prefix.
| Source file | Spec file |
|---|---|
lib/foo/bar.rb |
spec/foo/bar_spec.rb |
app/models/user.rb |
spec/models/user_spec.rb |
components/mycomp/lib/foo.rb |
components/mycomp/spec/foo_spec.rb |
Spec files passed as arguments are forwarded to rspec unchanged.
Options
| Flag | Long form | Description |
|---|---|---|
-c |
--changed |
Use git-changed files (git status -s) |
-e |
--exact |
Match FILE exactly (no fuzzy search) |
-I |
--integration |
Add --tag integration |
-M |
--manual-integration |
Add --tag manual_integration |
-T NAME |
--tag NAME |
Add --tag NAME |
-n |
--norun |
Print command without running |
-s |
--show |
Print spec paths without running rspec |
-v |
--verbose |
Verbose output |
- |
Read filenames from STDIN |
Development
After checking out the repo, run bin/setup to install dependencies. Then, run
rake spec to run the tests. You can also run bin/console for an interactive
prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To
release a new version, update the version number in version.rb, and then run
bundle exec rake release, which will create a git tag for the version, push
git commits and the created tag, and push the .gem file to
rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/aks/specs_for.
License
The gem is available as open source under the terms of the MIT License.