autotest的notify in Gnome

autotest是个方便的测试工具,ZenTest的组件之一,对Rspec支持很好。

在Peedcode的教学视频中很多时候看到作者用autotest时,测试结果会以桌面系统Notify的形式通知用户,每次看到都觉得好羡慕。其实在Gnome环境下的同学不用羡慕,因为在Gnome下也可以,把你Home目录下的.autotest文件(附件有),加入以下代码,并把附件中的图片文件放到Home下(把dot改为.)。

require 'rnotify'
require 'gtk2'
 
module Autotest::RNotify
  class Notification
   attr_accessor :verbose, :image_root, :tray_icon, :notification,
   :image_pass, :image_pending, :image_fail
 
   def initialize(timeout = 5000,
   image_root = "#{ENV['HOME']}/.autotest_images” ,
   verbose = false)
    self.verbose = verbose
    self.image_root = image_root
 
    puts ‘Autotest Hook: loading Notify’ if verbose
    Notify.init(’Autotest’) || raise(’Failed to initialize Notify’)
 
    puts ‘Autotest Hook: initializing tray icon’ if verbose
    self.tray_icon = Gtk::StatusIcon.new
    tray_icon.icon_name = ‘face-monkey’
    tray_icon.tooltip = ‘RSpec Autotest’
 
    puts ‘Autotest Hook: Creating Notifier’ if verbose
    self.notification = Notify::Notification.new(’X', nil, nil, tray_icon)
    notification.timeout = timeout
 
    Thread.new { Gtk.main }
    sleep 1
    tray_icon.embedded? || raise(’Failed to set up tray icon’)
   end
 
    def notify(icon, tray, title, message)
    notification.update(title, message, nil)
notification.pixbuf_icon = icon
tray_icon.tooltip = “Last Result: #{message}”
tray_icon.icon_name = tray
notification.show
end
 
def passed(title, message)
self.image_pass ||= Gdk::Pixbuf.new(”#{image_root}/pass.png”, 48,48)
notify(image_pass, ‘face-smile’, title, message)
end
 
def pending(title, message)
self.image_pending ||= Gdk::Pixbuf.new(”#{image_root}/pending.png”,48,48)
notify(image_pending, ‘face-plain’, title, message)
end
 
def failed(title, message)
self.image_fail ||= Gdk::Pixbuf.new(”#{image_root}/fail.png”, 48,48)
notify(image_fail, ‘face-sad’, title, message)
end
 
def quit
puts ‘Autotest Hook: Shutting Down…’ if verbose
#Notify.uninit
Gtk.main_quit
end
end
 
Autotest.add_hook :initialize do |at|
@notify = Notification.new
end
 
Autotest.add_hook :ran_command do |at|
results = at.results.last
 
puts “the results is #{results}”
 
unless results.nil?
output = results[/(\d+)\s+example?,\s*(\d+)\s+failures?(,\s*(\d+)\s+peding)?/]
 
if $~[2].to_i > 0
@notify.failed(”Tests Failed”, output)
elsif $~[4].to_i > 0
@notify.pending(”Tests Pending”, output)
else
unless at.tainted
@notify.passed(”All Tests Passed”, output)
else
@notify.passed(”Tests Passed”, output)
end
end
end
end
 
  Autotest.add_hook :quit do |at|
   @notify.quit
  end
end

ohoh~~我们来看看效果怎样,在任意一个加入rspec on rails插件的Project下(当然是写了spec的)执行autotest:
notify.png

是不是很有趣呢?当然这个依赖于库ruby-gnome2,ruby-libnotify,都是用各自Linux发行班的包管理软件可以轻松得到的,比如我的arch就是,yaourt ruby-gnome2 ruby-libnotify。这个其实也可以用在unit test下,不过需要修改Autotest.add_hook :ran_command中的正则。

Ruby一个可爱的地方就是配置文件也是代码,代码还是代码。autotest的扩展性真好,这也是得益于Ruby的哲学,要修改一个类的行为就打开它,定义它,很优雅。Code is the best firend of Programmer 。

参考资料:
http://www.ikhono.net/2007/12/16/gnome-autotest-notifications

dot_autotest_images.zip

This entry was posted on 星期四, 03月 13th, 2008 at 1:46 pm and is filed under Promgramming, Ruby. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

2 Comments so far

Be the first to comment on this entry.

Have your say

You must be logged in to post a comment.

Recent Posts:
  1. Blog已搬迁 - 2008-08-22
  2. a few interesting rails plugin - 2008-08-06
  3. Get Start Java Network App Dev - 2008-08-06
  4. 思考着Blog搬家 - 2008-07-28
  5. a bug in attachment_fu - 2008-07-28
  1. 我的梦想

    我的梦想是成为世界级的软件开发者。。。 联系我请mailto: chenk85 AT gmail.com 或者加我MSN: chenk85 AT live.cn
  2. 标签

  3.  

    03月 2008
    « Feb   Apr »
     1
    2345678
    9101112131415
    16171819202122
    23242526272829
    3031  
  4. 文章分类

  5. 存档页

  6. 评头论足

    • 功能