Autotest and Vim integration

Posted by – May 18, 2009

Yay! I got vim and autotest integration working. When I run autotest, if there is an error, I can have Vim read the errors from autotest and jump me to the right place.

Here is a video of me using it:

Please note that I’m not copying and pasting anything. In vim, I hit a command and Vim automatically picks up errors from autotest and jumps me to the line where the error occurred.

You too can impress your friends with this trick! Here’s how:

  1. Make sure you have vim-ruby installed
  2. Use this as your .autotest file:
    require 'autotest/restart'
    
    Autotest.add_hook :initialize do |at|
      at.unit_diff = 'cat'
    end
    
    Autotest.add_hook :ran_command do |at|
      File.open('/tmp/autotest.txt', 'wb') { |f|
        f.write(at.results.join)
      }
    end
    
  3. Add this to your .vimrc:

    compiler rubyunit
    nmap <Leader>fd :cf /tmp/autotest.txt<cr> :compiler rubyunit<cr>

Now when you get an error in autotest, just type “\fd” in Vim to jump straight to your first error.

The contents of /tmp/autotest.txt will be used in your errorfile. In Vim do “:help quickfix” for more info on what you can do with your new found power.

Caveat: You don’t get unit_diff. I’m working on that. Any help would be much appreciated (I suck at errorformat in Vim).

8 Comments on Autotest and Vim integration

Respond

  1. I would’ve figured to get this working on rspec that you’d just need to change the compiler to rspec but no luck so far… Hopefully I’ll figure this out because this is clearly awesome. Thanks for the great post.

  2. Jinzhu says:

    wow, Really awesome!

  3. Ryan Davis says:

    Welcome to emacs in 2006… you’re catching up!

  4. @Ryan We’re almost there!

  5. Mitko says:

    Really nice.

    Altough doesn’t work correctly on linux.
    The thing is “compiler rubyunit” opens the file “/test/functional/usercontrollertest.rb”, not “/home/{user}/{pathtorailsapp}/test/functional/usercontroller_test.rb”, which is empty file.

  6. ujihisa says:

    nnoremap fd :cf /tmp/autotoest.txt:compiler rubyunit

    would be better than

    nmap fd :cf /tmp/autotest.txt :compiler rubyunit

    because

    * can be mapped into another key (actually I did)
    * if you type numbers before fd unconciously, the command cf would run many times

  7. ujihisa says:

    omg… tags were removed…

  8. djh says:

    You probably want to change the write hook to strip escape sequences if you have autotest print in colour

    Autotest.add_hook :ran_command do |at|
    File.open( ‘/tmp/autotest.txt’, ‘wb’ ) do |f|
    f.write at.results.join.gsub( /\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]/, ” )
    end
    end

    [1] http://serverfault.com/questions/71285/in-centos-4-4-how-can-i-strip-escape-sequences-from-a-text-file

Respond

Comments

Comments