Ease your RSpec upgrade with sed
I’ve been using linux on daily basis since 2008 and changed to OS X a couple of years ago. What I like these systems, is their powerful command line tools. Tools that helps me get my job done quicker and easier than with GUI.
Even though I’ve been using a CLI so long, it’s just so amazing how powerful tools there are, and have been for decades. This time I’d like to share a tip which may save you a lot of time in future (it saved for me)!
So, at the end of the last year and beginning of this year, I got to work with few Rails upgrades from 3.x to 4.x. This meant also that most of the gems needed to be upgraded.
One of the problems was the renamed expectations be_true
/be_false
(to be_truthy
/be_falsey
respectively) in the gem rspec-expectations
on version 3.0.0.beta1. It wasn’t a big change, but depending on the size of your test code base, it may mean a lot of work to you.
But luckily, you can solve it in a few seconds with the sed
sed -i “” “s/be_true/be_truthy/g” spec/**/*_spec.rb
Breaking down the command
-i “”
means in-place editing and by giving empty string for it, we do not want to use backup files. We have version control for that reason.“s/be_true/be_truthy/g”
is the command we give to sed. Thes
at the start tells the sed that we want to use regular expressionbe_true
and replace that withbe_truthy
. In this case, our regular expression is just plain text, but you can use more advanced patterns if you like. Theg
at the end tells that we want replace all occurrences of our search pattern in a single file, not just the first one. You can read more abouts
command from the manualspec/**/*_spec.rb
this is the filter pattern for files I want the change med into. I’m usingzsh
so it may not work as is onbash
.