Skip to content

cjohansen.no

Pimp my development environment!

Over the past few days I've been adding some bits and pieces that has really pimped by development environment into a slightly more productive one, and certainly a more enjoyable one.

Test goodness: Shoulda

Let's jump right on the puns... I shoulda used Shoulda before. I know. I've been using Context for syntactical sugar in my unit tests for some time now. However, when porting Twibot to Ruby 1.9 the other day I switched to Shoulda simply because it ran smoothly on 1.9 (as opposed to Context). Then I took a few minutes to check out the macros shoulda brings to the table, and I was immediately sold.

I have to admin I'm a little bit sceptical towards automating too much of my tests, but atleast the context/ should (and nested contexts) syntax is very sexy. It makes for extremely readable tests. Besides, the macros shoulda brings in for Rails does make alot of sense:


class UserTest < Test::Unit 
  context "A User instance" do
    setup do
      @user = User.find(:first)
    end

    should "return its full name" do
      assert_equal 'John Doe', @user.full_name
    end

    context "with a profile" do
      setup do
        @user.profile = Profile.find(:first)
      end

      should "return true when sent #has_profile?" do
        assert @user.has_profile?
      end
    end
  end
end

( Via Thoughtbo).

Git branches in the prompt

As most other git users, I use branches extensively. I try as much as possible to develop all new features in their own branch. When they're done and tests are passing I merge them back into the master branch. This means that I'm jumping from branch to branch all the time, and having the branch name in the prompt is very helpful towards avoiding commiting the wrong stuff to the wrong branch.

Ridiculously, this is something I've been thinking about for quite some time, and it really is very simple. The following snippet is all you need to have git branches in your prompt:

PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\
$(git branch &>/dev/null; if [ $? -eq 0 ]; then \
echo " ($(git branch | grep '^*' |sed s/\*\ //))"; fi)\$ '

Which yields a prompt like:

christian@helheim:~/projects/myapp (mybranch)$

Colors

Git

Colors make life in the shell alot more pleasant. They also make Git output (especially diffs) alot more readable. Enabling Git colors are easy:

git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto

Shell

Fortunately, enabling colored ouput elsewhere in my shell is very easy as well. I'm on Ubuntu, which ships with a nice .bashrc. All you need to do here is to uncomment this line:

#force_color_prompt=yes

...and voila! You have colors in the shell as well. Now we can pimp our Git enabled prompt even more:

PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\
\w\[\033[01;36m\]$(git branch &>/dev/null; if [ $? -eq 0 ]; then \
echo " ($(git branch | grep '^*' |sed s/\*\ //))"; fi)\[\033[00m\]\$ '

That shit ain't pleasant to read, but it sure works.

Test::Unit output

You can also have your tests turn red or green in your shell using the RedGreen gem. Simply:

sudo gem install redgreen

And then, in your test_helper.rb:

...
require 'redgreen'

Very nice!

autotest - running tests all the time, automatically

ZenTest is another gem that offers a very useful tool for test driven development. It offers a autotest executable, which, when run, monitors your code and tests and rerun single test cases whenever either the code or test changes. This means you can spend alot less time waiting for your tests to run.

sudo gem install ZenTest
cd ~/projects/myapp
autotest

If you're like me, you probably want to run that command in a shell inside a screen session. That way you can jump over to it whenever you need to know what's going on with your code.

Desktop notification

Even though screen is sweet, getting an automatic notification on your desktop is even sweeter. This way you won't need to leave your editor at all (unless you make mistakes and need to review the failures of course...). For Mac you've got Growl, Windows has Snarl, and on Ubuntu (actually Gnome) you have libnotify.

sudo apt-get install libnotify-bin

With this in place, you only need to put a .autotest file either in your home directory, or in the directory from which you run autotest. The file should contain the following:

module Autotest::GnomeNotify

  # Time notification will be displayed before disappearing automatically
  EXPIRATION_IN_SECONDS = 2
  ERROR_STOCK_ICON = "gtk-dialog-error"
  SUCCESS_STOCK_ICON = "gtk-dialog-info"

  # Convenience method to send an error notification message
  #
  # [stock_icon]   Stock icon name of icon to display
  # [title]        Notification message title
  # [message]      Core message for the notification
  def self.notify stock_icon, title, message
    options = "-t #{EXPIRATION_IN_SECONDS * 1000} -i #{stock_icon}"
    system "notify-send #{options} '#{title}' '#{message}'"
  end
  
  Autotest.add_hook :red do |at|
    notify ERROR_STOCK_ICON, "Tests failed", "#{at.files_to_test.size} tests failed"
  end
  
  Autotest.add_hook :green do |at|
    notify SUCCESS_STOCK_ICON, "All tests passed, good job!", ""
  end
end

Alot of good stuff on ZenTest over here.

Phew, that is a great collection of tools. What other tools do you guys to stay happy and productive?

Possibly related

2006 - 2012 Christian Johansen Creative Commons License