Class: Autotest::Growl

Inherits:
Object
  • Object
show all
Defined in:
lib/autotest/growl.rb

Constant Summary collapse

VERSION =
'0.1.2'
GEM_PATH =
File.expand_path(File.join(File.dirname(__FILE__), '../..'))
@@remote_notification =
false
@@hide_label =
false
@@clear_term =
true
@@wrap_lines =
false
@@pretty_print =
false

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clear_before_run=(boolean) ⇒ Object

Whether to clear the terminal before each run (default) or not.



46
47
48
# File 'lib/autotest/growl.rb', line 46

def self.clear_before_run=(boolean)
  @@clear_term = boolean
end

.growl(title, message, icon, priority = 0, stick = "") ⇒ Object

Display a message through Growl.



74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/autotest/growl.rb', line 74

def self.growl title, message, icon, priority=0, stick=""
  growl = File.join(GEM_PATH, 'growl', 'growlnotify')
  image = File.join(ENV['HOME'], '.autotest-growl', "#{icon}.png")
  image = File.join(GEM_PATH, 'img', "#{icon}.png") unless File.exists?(image)
  message = message.gsub(/,\s?/, "\n") if @@wrap_lines && !@@pretty_print
  message = pretty_print(message) if @@pretty_print

  if @@remote_notification
    system "#{growl} -H localhost -n autotest --image '#{image}' -p #{priority} -m \"#{message}\" '#{title}' #{stick}"
  else
    system "#{growl} -n autotest --image '#{image}' -p #{priority} -m \"#{message}\" '#{title}' #{stick}"
  end
end

.hide_label=(boolean) ⇒ Object

Whether to display the label (default) or not.



40
41
42
# File 'lib/autotest/growl.rb', line 40

def self.hide_label=(boolean)
  @@hide_label = boolean
end

.pretty_print(msg) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'lib/autotest/growl.rb', line 62

def self.pretty_print(msg)
  out = ''
  msg.split(/,\s?/).each do |line|
    matches = line.match(/(\d+)\s(\w+)/)
    str = "#{matches[1].rjust(6)} #{matches[2]}\n"
    out << str
  end
  out
end

.pretty_print=(boolean) ⇒ Object

Whether to try and make a more readable format or not (default).



58
59
60
# File 'lib/autotest/growl.rb', line 58

def self.pretty_print=(boolean)
  @@pretty_print = boolean
end

.remote_notification=(boolean) ⇒ Object

Whether to use remote or local notificaton (default).



34
35
36
# File 'lib/autotest/growl.rb', line 34

def self.remote_notification=(boolean)
  @@remote_notification = boolean
end

.wrap_lines=(boolean) ⇒ Object

Whether to wrap the message at the commas or not (default).



52
53
54
# File 'lib/autotest/growl.rb', line 52

def self.wrap_lines=(boolean)
  @@wrap_lines = boolean
end

Instance Method Details

#ran_commandObject

Parse the test results and send them to Growl.



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'lib/autotest/growl.rb', line 100

Autotest.add_hook :ran_command do |autotest|
  gist = autotest.results.grep(/\d+\s+(example|test|scenario|step)s?/).map {|s|
    s.gsub(/(\e.*?m|\n)/, '')
  }.join(" / ")

  if gist == ''
    growl @label + 'Cannot run specs/tests/scenarios', '', 'error', 2
  else
    case gist
      when /[1-9]\d*\s+(failure|error)/
        growl @label + 'Tests failed', gist, 'failed', 2
      when /pending/
        growl @label + 'Tests pending', gist, 'pending', -1
      when /failed/
        growl @label + 'Scenarios failed', gist, 'failed', 2
      when /undefined/
        growl @label + 'Scenarios undefined', gist, 'pending', -1
      when /scenario|step/
        growl @label + 'Scenarios passed', gist, 'passed', -2
      else
        growl @label + 'Tests passed', gist, 'passed', -2
    end
  end
  false
end

#run_commandObject

Set the label and clear the terminal.



90
91
92
93
94
95
96
# File 'lib/autotest/growl.rb', line 90

Autotest.add_hook :run_command do
  @label = (@@hide_label) ? ('') : (File.basename(Dir.pwd).upcase + ': ')
  @run_scenarios = false
  print "\n"*2 + '-'*80 + "\n"*2
  print "\e[2J\e[f" if @@clear_term # clear the terminal
  false
end