One of the more powerful tools on my toolbelt is a red-green development cycle. With this cycle of purposefully writing tests which fail (red), then writing the code required to make them pass (resulting in green tests), allows me to keep my mind focused on the current task at hand, and saves me time.
For example, take the annual Advent of Code set of challenges: given a problem and example input with output, create working code which solves the problem for any given input.
Small challenges such as Advent of Code provide the perfect scenario for Test Driven Development (TDD). Given an Advent of Code challenge, I would always write a test which asserts some output. Since those tests would fail, I would then proceed to write code which returns the correct answer.
I can’t emphasize the time saved from automatically running my tests whenever any file is saved in my working directory. The time savings has payed off time and time again because it has saved me a number of keystrokes off of running tests manually each time. Saving my source or test file automatically causes my tests to start running in another terminal pane.
The one way I’m able to accomplish this is by utilizing two tools:
entr. These two shell tools used in combination allow for a powerful red-green development process.
ag (commonly known as the Silver Searcher, succeeded by
rg (RipGrep)) is a program which lists files recursively.
entr is a program which runs a given shell command whenever a file is modified. These two tools piped together like
ag -l | entr -c rails test enable a TDD workflow because
ag provides a recursive list of files, then
entr watches those files and runs the provided command. In this case, the command to run every time is
rails test. The
-c is a parameter for
entr to clear the console each time the command is rerun.
I would highly recommend trying out these two tools in combination next time you encounter your next TDD situation – it’s certainly saved me a lot of time repeating keyboard commands. Even if you don’t do TDD regularly or at all, I would recommend trying out these new tools.
For a quicker install
brew install the_silver_searcher entr on Mac OS.
apt-get install silversearcher-ag entr on Ubuntu-like systems.