Firestarter: Automated Testing and More in Emacs

Like most of my Emacs-related posts, today’s is a short example of a useful package, one called Firestarter. It’s a recent addition to the plethora of available Elisp packages out there, not even two months old at this time of writing. Firestarter’s GitHub page provides a nice, terse summary of its purpose:

firestarter lets you execute all kinds of things after saving a buffer. You can run shell commands, execute interactive Emacs commands and evaluate arbitrary pieces of Emacs Lisp. Support for shell commands includes asynchronous process control, format chars in command strings and report of process on common process termination states.

I want to show you some simple ways in which I’ve found Firestarter useful.

My first application of Firestarter was to automate testing for PHP Mode. We have a collection of unit tests for PHP Mode which use ERT, the Emacs Lisp Regression Testing library that comes with Emacs. Once we have loaded our file of tests we can run them via M-x ert-run-tests-interactively. I like to run our unit tests whenever I’m making changes to PHP Mode; it is hardly a chore to run ERT manually, but I prefer to automate things whenever I can. And if I’m being honest, there have been times where I’ve written some patches in the late hours of the night, half-asleep, and lacking the self-discipline in that scenario to make sure all the tests pass. With Firestarter I can ensure the tests always run by adding a file-local variable to the PHP Mode sources, which looks like this:


;; Local Variables:
;; firestarter: ert-run-tests-interactively
;; End:

With this variable in the source files, and with firestarter-mode enabled, Firestarter will run that command any time I save those files. This is valuable not because it saves me any time, but because it prevents me from forgetting to run the unit tests after any modifications, a safety net, if you will.

Firestarter can do more than run Emacs commands. I can also use it to run shell commands. Here’s a simple, practical example: The top-level directory of the game I’m currently working on has a file of directory-level variables which looks like this:

((nil
  (firestarter-type . failure)
  (firestarter . "make game")))

In my first example firestarter had a symbol for its value, in which case Firestarter runs the Emacs command named by that symbol. But when given a string, as in this example, Firestarter treats it as a shell command. So now whenever I introduce game-breaking bugs add a new feature and save my work, Firestarter automatically runs make game, which in turn invokes GNU Make to rebuild the game. The other variable, firestarter-type, lets me control Firestarter’s output based on the results of make game. By giving it the value failure I will only see the output from make game if something goes wrong. A successful rebuild of the game will happen silently.

And that’s all for my simple introduction. Firestarter offers more customization than what I’ve demonstrated here, such as its format string variables which you can use to create more complex commands. If you’re looking for an easy way to automatically do something whenever you save a buffer or file then check out Firestarter. As I said, it hasn’t been out for long, but it has already become an important piece of my Emacs toolbox.

Advertisements

One thought on “Firestarter: Automated Testing and More in Emacs

Add Your Thoughts

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s