loading...

One of my favorite testing/productivity tools: guard

ben profile image Ben Halpern ・1 min read

guard is not a new library. It has been a staple in many areas of Ruby development for years. Many of us use it, but I felt like shedding a bit of light on it as a great too folks should know about in general.

GitHub logo guard / guard

Guard is a command line tool to easily handle events on file system modifications.

Guard

IMPORTANT: Please upgrade to Ruby >= 2.4 before installing Guard! To install for older versions, update Bundler at least 1.12: gem update bundler and Bundler should correctly resolve to earlier gems for your given Ruby version.

❗️ Guard is currently accepting more maintainers. Please read this if you're interested in joining the team.

Gem Version Build Status Code Climate Test Coverage Inline docs Reviewed by Hound

Guard Icon

Guard automates various tasks by running custom rules whenever file or directories are modified

It's frequently used by software developers, web designers, writers and other specialists to avoid mundane, repetitive actions and commands such as "relaunching" tools after changing source files or configurations.

Common use cases include: an IDE replacement, web development tools, designing "smart" and "responsive" build systems/workflows, automating various project tasks and installing/monitoring various system services.

For a full categorized list of known…

Guard automates various tasks by running custom rules whenever file or directories are modified.

At DEV, we use guard to run tests as files are saved in order to optimize our test-driven-development practices.

I say "we" and "our" without total certainty that it is used across the team and among contributors because it is not needed in our flow. This is probably one of the better things about its use with rspec. It is the cherry on top.

It's not a heavy-handed default, but so engrained in my own workflow, I almost forget it's not the default.

The gem is most popular in conjunction with Rails and testing, but I'd advise folks to check out its usefulness in any situation where saving a file can kick off a task. The guard ecosystem is vast.

Are there similar tools in other language environments folks check out?

Posted on by:

ben profile

Ben Halpern

@ben

A Canadian software developer who thinks he’s funny. He/Him.

Discussion

markdown guide
 

I've often used entr for this sort of thing:

eradman.com/entrproject/

It's a simple command line tool. You pipe a list of files to entr, and append a command that you'd like to run when any of those files change.

It's nice and Unix-y, language agnostic, quick to set up, and can be used for more than just test watching. Good tool.

 
 
 

We use NCrunch for .NET ecosystem. So nice coz it auto runs unit tests on file changes without saving, and also has an intelligent option where it would only run tests that were impacted by your code changes. It isn't free but well worth the price. 🙂

 

Does it wait a few seconds or so before it runs your tests?

 

Yeah, about a second or two coz it auto builds the project before running the tests.

 

guard is not a new library.

I think that's an understatement.
It's incredibly old. Github would make you believe its only 7 years old when I believe the repo was previously hosted on SourceForge.

 

I can't even think about coding in RoR without guard these days. Our CI/CD pipeline isn't the fastest, so the feedback loop between submitting a PR and receiving the dreaded github warnings that the PR didn't pass all the tests is sometimes over 20 minutes.
Having to go through that for a silly typo, to just get another error almost an hour later kills my soul, but fortunately when I joined the team they had guard setup already so adopting it early on was purely a matter of valuing my own time. I can say with certainty that guard and the rails console are two of my most often used tools any given day.

 

I love all these sorts of tools to automate things on "the machine building the machine"! PHPStorm has this functionality built into the IDE (at least when working with PHP). They call it "auto-test". You can customise the time between save and (re)run of tests as well. Very useful :)

On a related but separate note, you can also add custom file watchers to do things like regenerating static cache files when the source changes.

 

Modd is another file watcher written in Go. I use it to automate the build and test of my apps.