Rspec Autotesting with libnotify( on Ubuntu)

Posted by PunNeng, Mon Oct 15 01:01:00 UTC 2007

ก็เคย set บน os x ไปแล้ว คราวนี้จะมา set บน ubuntu กันบ้าง

พระเอกคราวนี้เห็นจะเป็น libnotify

เริ่มต้นเหมือนเดิม ติดตั้ง rspec zentest redgreen ด้วย gem แล้วก็ plugin ของ rails ที่ชื่อ rspec และ rspec_on_rails ไปก่อน แล้วมาติดตั้งเจ้า libnotify

$ sudo apt-get install libnotify-bin

จากนั้นก็สร้าง .autotest เหมือนเดิม ไว้ที่ home

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
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"
  PENDING_STOCK_ICON = "gtk-dialog-warning"

  Autotest.add_hook :ran_command do |at|
    if at.results.empty? # break out gracefully if a test file throws an uncaught exception
      notify ERROR_STOCK_ICON, "Syntax error", ""
    else
      output = format_input( at.results )
      display_notification( output )
    end
  end

  private

  def self.notify stock_icon, title, message
    options = "-t #{EXPIRATION_IN_SECONDS * 1000} -i #{stock_icon}"
    system "notify-send #{options} '#{title}' '#{message}'"
  end

  # isolate the example, failure, pending line like "12 examples, 4 failures, 5 pending"
  # and remove color codes
  def self.format_input( input )
    output = input.detect{ |result| result.include? "example"}
    output = remove_color_codes( output )
  end

  def self.display_notification( output )
    examples, failures, pending = output.split(", ")
    if failures.to_i > 0
      notify ERROR_STOCK_ICON, "Tests Failed", output
    elsif pending.to_i > 0
      notify PENDING_STOCK_ICON, "Tests Pending", output
    else
      notify SUCCESS_STOCK_ICON, "Tests Passed", output
    end
  end


  def self.remove_color_codes( string )
    string.match( /m(.+)\e/)[1]
  end

end

หน้าตาก็จะประมาณนี้

autotest_on_ubuntu

ปล. post อันที่แล้วก็ว่าไปแล้วทีนึง คราวนี้ขอโฆษณาให้คุณวุธ(ไม่รู้สะกดชื่อถูกหรือเปล่า)เต็มๆ ผมไปนั่งร้านกาแฟบ่อยครับ ทุกเสาร์อาทิตย์ แล้วก็ใช้ ubuntu นี่แหละครับ ร้านกาแฟเดียวกันที่ mk เคยเอาลง blog ชื่อร้านว่า Cofe' De Woody อยากคุยเรื่อง rails กับผมก็มาเจอกันได้ :)
ปอ. ผมไม่ทำ rails บน windows นะครับ :) ถ้าอยากใช้ rspec ก็ไปที่ ph7 นะครับ ผมก็เอาตัว libnotify มาจากที่นี่แหละครับ

Filed Under: Ruby on Rails | Tags: autotest howto rspec ruby on rails ubuntu

Comments

Have your say

A name is required. You may use HTML in your comments.




codegent: we're hiring