1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Set Default Tasks For Rake To Run as a Ruby TIL

This commit is contained in:
jbranchaud
2025-11-10 18:06:12 -06:00
parent 91149fe7cc
commit d1f41884ce
2 changed files with 34 additions and 1 deletions

View File

@@ -0,0 +1,32 @@
# Set Default Tasks For Rake To Run
Let's say our Ruby codebase has a `test` rake task and a `test:system` rake
task. One runs our unit tests and the other runs our system (headless
browser-based) tests. They aren't necessary defined in our `Rakefile`. In fact,
that's how it is in a Rails codebase where these are defined by Rails itself.
We want the default action when [`rake`](https://ruby.github.io/rake/) is
invoked by itself to be to run both of those test tasks.
This can be accomplished by specifying a `default` task and specifying both of
those tasks as prerequisites.
```ruby
task default: ["test", "test:system"]
```
The `default` task itself does nothing. When we invoke it though, it has to run
our prerequisites. So running `rake` results in `test` and then `test:system`
getting run.
If I have something like
[`unicornleap`](https://github.com/dkarter/dotfiles/blob/b5aae6a9edd5766f0cc9100235b0955a9d53aa85/installer/mac-setup.sh#L47-L74)
or
[`confetti`](https://manual.raycast.com/deeplinks#block-702a9613bc82440d853492f553876a20),
then I can have one of those run in the event that all the prerequisites pass.
```ruby
task default: ["test", "test:system"] do
system("unicornleap") if system("which unicornleap > /dev/null 2>&1")
end
```