Automate Everything: Ruby, Linux and other hints, tips and tricks.

Notificaciones de Autospec en Gnome

Últimamente ando muy metido en TDD y ya que he conseguido echar a andar las notificaciones de autospec con Ubuntu (Jaunty 9.04) me gustaría compartirlo por aquí porque el post en el que lo encontré ya no está disponible.


Uso XFCE pero debe funcionar igualmente con Gnome. Este es un screenshot del resultado final:

autospec notification XFCE/Gnome

autospec notification XFCE/Gnome


En primer lugar, necesitas la gema ZenTest. Probablemente ya la tengas:

$ sudo gem install ZenTest
$ sudo gem install redgreen


Ahora instala el paquete libnotify-bin:

$ sudo apt-get install libnotify-bin


Y aquí está el truco, crea un fichero llamado ~/.autotest con esto:

#!/bin/ruby
require 'redgreen'
require 'autotest/timestamp'
 
module Autotest::GnomeNotify
  def self.notify title, msg, img
    system "notify-send '#{title}' '#{msg}' -i #{img} -t 3000"
  end
 
  Autotest.add_hook :ran_command do |at|
    image_root = "~/.autotest_images"
    results = [at.results].flatten.join("\n")
    results.gsub!(/\\e\[\d+m/,'')
    output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?(,\s(\d+)\spending?|)/)
    full_sentence, green, failures, garbage, pending = $~.to_a.map(&:to_i)
    if output
      if failures > 0
        notify "FAIL", "#{output}", "#{image_root}/fail.png"
      elsif pending > 0
        notify "Pending", "#{output}", "#{image_root}/pending.png"
      else
        notify "Pass", "#{output}", "#{image_root}/pass.png"
      end
    end
  end
end

Como puedes ver uso fail.png y pass.png para añadir esos smiles. Puedes descargarlos aquí y copiarlos a ~/.autotest_images/.

autotest_images.zip

Haz que tus tests sean de color verde y pilla una cerveza : )

Update: Añadida la gema redgreen y cambiado “require redgreen” como sugirieron Rodigo Flores y rakk. Gracias!

Update: Corregido > escapado por error en el código. Gracias a Martin y Hunter por los comentarios.


Update: El script ahora soporta ejemplos pendientes. Gracias a Andy.


Update: Eliminado un end que sobraba. Gracias a Andy de nuevo.

  • You need to install the redgreen gem also. The file .autotest only work if instead of “autotest/redgreen”, you put “redgreen/autotest”.

    Thanks for the hints

  • Hi Rodrigo,

    Thanks for the comment. I don’t have any gem called ‘redgreen’ installed. It seems to come with Zentest:

    /usr/lib/ruby/gems/1.8/gems/ZenTest-4.0.0/lib/autotest/redgreen.rb

    I can also do this:

    $ irb
    >> require 'autotest'
    => true
    >> require 'autotest/redgreen'
    => true
    >> require 'autotest/timestamp'
    => true
    >>
    

    Maybe we have a version mismatch.

  • sudo gem install redgreen

    - require ‘autotest/redgreen’
    + require ‘redgreen’

    for ZenTest (4.1.4)

  • Right rakk! I had ZenTest 4.0.0. Updating the post now.

  • I couldn’t find the images in the zip file. here is a link to a page with red and green smiley faces. http://getsatisfaction.com/railstutorial/topics/gnome_and_autospec

  • Hi Jesse. The images should be inside a hidden directory called “.autotest_images”. Maybe you missed them because the dir is hidden.

  • I am quite new to rails, did exactly what you did, but neither autotest nor autospec opens the graphical display. Autotest reacts to changes on the file system as expected, but still, no gui. I have gnome and
    autotest-rails (4.1.0)
    redgreen (1.2.2)
    rspec (1.3.0)
    rspec-rails (1.3.2, 1.3.0)
    ZenTest (4.3.1)
    any idea? Thx a lot.

  • ah, something else: it says
    ./.autotest:17: warning: `&’ interpreted as argument prefix
    ./.autotest:17: warning: unused literal ignored
    thx again

  • Martin:
    I had the same problem, but I got it working by replacing the “>” on line 17 with a >

  • Martin:
    What I meant to say was I fixed it by replacing the “& g t” on line 17 with a >. (As my previous post demonstrated, “& g t” is the character entity for >)

  • I can confirm that Hunter’s solution of replacing & g t with > works to fix the issue.

  • ./.autotest:17: warning: `&’ interpreted as argument prefix
    ./.autotest:17: warning: unused literal ignored
    ./.autotest:6: warning: redefine notify

    are still warnings I get.

    Line 17 is if $~[2].to_i > 0

    and Line 6 def self.notify title, msg, img

    Otherwise it seems to work, I get the pictures ;-) But how am I supposed to use autospec? Just start a terminal and run it and ignore the terminal afterwards? or how do I ideally run it continuously?

  • Hi Benjamin, you can follow http://www.railstutorial.org/ to see autotest in action :-)

  • you can also see screenshots from my environment here
    http://uczareac.blogspot.com/2010/06/ruby-autotest-setup-notes-autospec.html

  • im running ubuntu 10.04 i followed all the directions above and my autospec isnt giving me popups … any idea?

  • The post’s script didn’t work for me, found a super-simple way of installing notifications – http://github.com/nuxlli/autotest-notification

  • Great! Thanks!

  • $ autotest
    loading autotest/rails
    /home/deim/.rvm/gems/ruby-1.9.2-head/gems/test-unit-2.1.1/lib/test/unit/ui/console/testrunner.rb:295:in `’: uninitialized constant Test::Unit::UI::Console::Diff (NameError)

    Whats wrong?

  • @mystdeim
    I have the same problem with you,
    you can change the require ‘redgreen’ to gem ‘redgreen’
    at your ~/.autotest

  • Hey, wanted to say thanks for this, it’s helping me get going with the railstutorial.org autotest setup! I was trying to get a “Pending” reading as well, but I can’t seem to get it, however I could at least get the “x Pending” to show in the notification by adding a little to the regEx in line 14:

    output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?,\s(\d+)\spending?/)

  • okay this has been driving me crazy but my autotest broke after I had it working for a while… the redgreem gem seems to require ‘test/unit/ui/console/testrunner’ and the location and/or name of this module has changed in Ruby > 1.8.7.. I used RVM to install 1.8.7, 1.9.1 and 1.9.2 and when i do “locate testrunner” it is found in the 1.8.7 ruby directories but not the other two… I couldn’t find this redgreen gem on github either, so we need to get the author of that to update it for newer Rubies maybe?

    Also: I tried the ‘autotest-notification’ gem and it just hangs in my 1.9.2 ruby gemset as well…

    I am very new to code and development period, so I unfortunately am not equipped to fork the redgreem gem and fix it myself or I would.. it seems to be a real lynch-pin to getting a nice rails TDD setup in Linux!

  • EDIT: that regex breaks if there are no tests pending, so you have to use

    output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?(,\s(\d+)\spending?|)/)

    …just in case anybody tried it.

  • Thank you very much Andy. You Regexp worked as expected : ). I’ll edit the post to include it as well.

  • One too many “end”s on there now. ;)

  • Hi!
    I become this, when i tried it with your script:
    .autotest:28: syntax error, unexpected kEND, expecting $end (SyntaxError)…
    Like Andy sad…

  • Koli
    I have the same problem. I think, there is one unnecessary ‘end’ in .autotest, I delete it and tried to run it and what i got
    “:29:in `require’: no such file to load — test/unit/ui/console/testrunner (LoadError)”

  • On my system the notification works only with ZenTest v4.1.4

  • Hey!

    I’m assuming a lot of people are (like me) coming here from chapter 3 of the railstutorial.org guide in an attempt to setup testing… please can we have some help with this ‘testrunner (LoadError)’ problem?

    :29:in ‘require’: no such file to load — test/unit/ui/console/testrunner (LoadError)
    from /home/me/.rvm/gems/ruby-1.9.2.p0/gems/redgreen-1.2.2/lib/redgreen.rb:2:in ”

    Going into the ~/.autotest and REMOVING the ‘require redgreen’ results in autotest running fine, albeit without displaying any feedback whatsoever (although the ‘Waiting since’ timestamp updates when I make any changes to code, implying that it’s actually doing the tests…)

    Running autotest without any .autotest file in my home directory at all results in ‘loading autotest/rails’ and nothing more. No “wiating since” or timestamp at all.

    *pulls hair out at lack of information about this online*

    ANY help at all would be MUCH appreciated!

  • Uninstalled redgreen and installed mynyml-redgreen, now autotest runs fine with ‘require redgreen’ but still don’t see the notify window.

    Verified notify works with:
    $ notify-send “Foo” “Bar” -i “~/.autotest_images/pass.png”
    which shows a notify without the image, but
    $ notify-send “Foo” “Bar” -i “/home/me/.autotest_images/pass.png”
    shows a notify WITH the image. Changed it within ~/.autotest but made no difference. Still getting no notify window triggered by autotest at all.

  • Hello Ellicks. I did not suffer from that problem. Anyway, if you can confirm that using the mynyml-redgreen gem and using the full path fixed it, I’m willing to add it to the post.

    Thanks for he feedback!

  • Well as far as I can see, mynyml redgreen doesnt need the ‘testrunner’ that redgreen seems to want so bad, but it hasn’t really *fixed* anything because I’m still not getting popups at all.

    system “notify-send ‘hey’ ‘message’” BEFORE the ‘autotest.add_hook’ line displays a notification when I run autotest, but the same line WITHIN the .add_hook block never show’s a notification at all… making me think it’s never actually adding the hook…

  • I love being a n00b. After hours of mucking around, I realised that it MUST be performing SOME tests when I modify code because the timestamp updates. It give me no output regarding those tests tho. PERHAPS it’s running NO tests!

    I copied part of a spec/controllers/blah.rb file to test/unit/blah.rb and ran autotest again — it immediately complained about something or other.

    Clearly autotest is not running the rspec tests I’ve setup, but whatever generic tests it does by default.

    My question: How do I get autotest to run the rspec tests?

  • Running autotest will run the Rails generic tests. autospec is what you have to execute for running your RSpec tests.

  • Interesting… I’m sure I read somewhere that autospec was contained within autotest… and I spent ages following other peoples forum advice which finally culminated in adding:
    Autotest.add_discovery { “rspec2″ }
    to autotest/discover.rb, which results in autotest printing:
    loading autotest/rspec2
    style: Rspec2
    upon execution, and executing my RSpec tests (although only upon execution.. the ‘auto’ part prints a timestamp but doesnt run my rspec tests)

    Where can I get this ‘autospec’ you’re referring to? There is no gem ‘autospec’…

  • http://exviva.posterous.com/running-autotest-with-rails-3 says:

    “One thing I really missed in my workflow was ZenTest and autotest (which I believe used to be autospec for RSpec, but now got unified with autotest).”

  • I’m getting this, after following your directions… Any thoughts on what configuration might be wrong here?

    $ autotest
    loading autotest/rails
    :29:in `require’: no such file to load — test/unit/ui/console/testrunner (LoadError)
    from :29:in `require’
    from /home/paul/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/redgreen-1.2.2/lib/redgreen.rb:2:in `’
    from :33:in `require’
    from :33:in `rescue in require’
    from :29:in `require’
    from /home/paul/.autotest:2:in `’
    from /home/paul/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/ZenTest-4.4.0/lib/autotest.rb:183:in `load’
    from /home/paul/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/ZenTest-4.4.0/lib/autotest.rb:183:in `block in initialize’
    from /home/paul/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/ZenTest-4.4.0/lib/autotest.rb:182:in `each’
    from /home/paul/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/ZenTest-4.4.0/lib/autotest.rb:182:in `initialize’
    from /home/paul/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/autotest-rails-4.1.0/lib/autotest/rails.rb:7:in `initialize’
    from /home/paul/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/ZenTest-4.4.0/lib/autotest.rb:124:in `new’
    from /home/paul/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/ZenTest-4.4.0/lib/autotest.rb:124:in `run’
    from /home/paul/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/gems/ZenTest-4.4.0/bin/autotest:55:in `’
    from /home/paul/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/bin/autotest:19:in `load’
    from /home/paul/.rvm/gems/ruby-1.9.2-p0@rails3tutorial/bin/autotest:19:in `’

  • I suspect a lot of people viewing this come to this thread from the railstutorial.org in order to get autotest working on a Linux box (I have Ubuntu 10.04). I was in that situation and had the same problem as described above. I finally got this working and wanted to share :)

    The excellent rails tutorial at railstutorial.org takes the view that you should install specific versions of some things, including autotest, presumably to avoid problems like this. It is ironic that this approach seems to cause this one.

    The rails tutorial tells you to do:
    gem install autotest -v 4.3.2

    Simply change this to the latest version
    gem install autotest

    (I don’t believe you have to uninstall it first, it worked for me).

    You then need to add a new require line to the .autotest script above, so all require lines in it will be:

    require ‘test/unit/ui/console/testrunner’
    require ‘redgreen’
    require ‘autotest/timestamp’

    and voila… it works.

  • I came from railstutorial.org as well and I followed the instructions in your post.. but it didn’t work.

    I was having the same problem as Ellicks above. The timestamps were being shown but nothing else. That was when I ran autotest.

    It seems that autospec no longer exists (?). The solution is to run “autotest rspec” and then it will work :-)

  • Hey Fridz, I tried your example, and I got an error due to the type of quote marks you had when I copied and pasted. I changed to the right style, and your fix worked like a charm!

  • Fridz:
    Thanks, your information helped, but I had to do one extra step (possibly because I’m using Ruby 1.9.2): gem install ‘test-unit’

  • Thanks for everyone’s help so far. I am not sure if I am having a problem or not; maybe someone could clear it up.

    The tests run correctly and the images display, but in the terminal I am left with # Waiting Since …

    Am I supposed to ^C ^C to quit? or should it be doing this automatically?

  • Hi all,

    i have the following gems installed
    rspec (2.0.1)
    rspec-core (2.0.1)
    rspec-expectations (2.0.1)
    rspec-mocks (2.0.1)
    rspec-rails (2.0.1)
    test-unit (2.1.1)
    redgreen (1.2.2)
    ZenTest (4.4.0)
    autotest-rails-pure (4.1.0)
    autotest (4.4.2)

    i also have libnotify-bin installed in my Ubuntu 10.04. as well as adding “require ‘test/unit/ui/console/testrunner’” in my .autotest file.

    i have tested the notify-send command and it works well and i run the test case with “rspec spec” without any problem.

    but when i run autotest. there is not popup in my ubuntu gnome. and when i quit the autotest by ^C^C. the following error is shown.

    ykyuen@ykyuen-laptop:~/Documents/RailsApplications/sample_app$ autotest
    loading autotest/rails

    # Waiting since 2010-11-05 00:05:52

    ^CInterrupt a second time to quit
    ^C/home/ykyuen/.rvm/gems/ruby-1.9.2-p0/gems/test-unit-2.1.1/lib/test/unit/ui/console/testrunner.rb:29:in `initialize’: wrong number of arguments (3 for 2) (ArgumentError)
    from /home/ykyuen/.rvm/gems/ruby-1.9.2-p0/gems/redgreen-1.2.2/lib/redgreen.rb:17:in `initialize’
    from /home/ykyuen/.rvm/gems/ruby-1.9.2-p0/gems/test-unit-2.1.1/lib/test/unit/ui/testrunnerutilities.rb:24:in `new’
    from /home/ykyuen/.rvm/gems/ruby-1.9.2-p0/gems/test-unit-2.1.1/lib/test/unit/ui/testrunnerutilities.rb:24:in `run’
    from /home/ykyuen/.rvm/gems/ruby-1.9.2-p0/gems/test-unit-2.1.1/lib/test/unit/autorunner.rb:307:in `run’
    from /home/ykyuen/.rvm/gems/ruby-1.9.2-p0/gems/test-unit-2.1.1/lib/test/unit/autorunner.rb:52:in `run’
    from /home/ykyuen/.rvm/gems/ruby-1.9.2-p0/gems/test-unit-2.1.1/lib/test/unit.rb:326:in `block in ‘

  • Hi all, i have changed the require ‘red/green’ to gem ‘red/green’. now no error is thrown but the autotest never fired the rspec tests.

    ykyuen@ykyuen-laptop:~/Documents/RailsApplications/sample_app$ autotest
    loading autotest/rails
    …(nothing happen)

    and then i try using the autotest_notification but thre result is the same.

    please help.

    Regards,
    Kit

  • i found the solution!! i can start the rspec test by entering “bundle exec autotest”

    Reference: http://exviva.posterous.com/running-autotest-with-rails-3

  • Kit’s solution did not fix things for me.

    The ‘redgreen’ gem is breaking things now. I did finally get things working. I’m planning on creating a Live CD Linux distro specifically for those reading the Rails Tutorial book so folks can avoid the headache.

    There is an ongoing discussion about this, and solutions:

    http://getsatisfaction.com/railstutorial/topics/dont_make_readers_have_to_figure_out_how_to_install_the_software?utm_content=topic_link&utm_medium=email&utm_source=reply_notification

  • This is the correct version of redgreen for ruby 1.9.1 and upper:

    https://github.com/mynyml/redgreen#readme

  • Dastan,
    I have the same problem.I solved through two steps:
    1,sudo gem install test-unit -v 1.2.3
    2,add “require ‘test/unit/ui/console/testrunner’” for .autotest

  • @kenshin – had the same problem, your solution worked:
    changed “require ‘redgreen’” to “gem ‘redgreen’”

  • Had the same problem coming from rails tutorial. Followed Fridz instructions and those mods that came after it. Specifically, installed current version of autotest, as Fridz suggested; added the 2 requires; added test-unit v 1.2.3, and then changed the “requires redgreen” to “gem redgreen”. Still got a “warning: loading in progress, circular require considered harmful”, although autotest ran. Experimented and wound up deleting both the “gem redgreen” and “require test/ui/console/testrunner” lines. Now it works without warnings!

  • I came from the tutorial too.

    Was getting the line 29 error. I added:
    require ‘test/unit/ui/console/testrunner’
    per Kevin and Fridz

    gem install test-unit

    which installed version 2.1.2, and everything worked fine. My guess is the extra gems installed for Mac OS in the book install test-unit for you. For clarification, autotest, redgreen, and test-unit should NOT be in the gemfile, correct? Basically would the rule of thumb be anything that comes down from a git clone of your repo that requires gems, should have those gems installed via gemfile, right?

You can follow any responses to this entry through the RSS 2.0 feed.

Trackbacks / Pingbacks