| |   |
| 1 | # vi: ft=ruby |
| 2 | |
| 3 | module Autotest::GnomeNotify |
| 4 | |
| 5 | # Time notification will be displayed before disappearing automatically |
| 6 | EXPIRATION_IN_SECONDS = 20 |
| 7 | ERROR_STOCK_ICON = "gtk-dialog-error" |
| 8 | SUCCESS_STOCK_ICON = "gtk-dialog-info" |
| 9 | |
| 10 | |
| 11 | # Convenience method to send an error notification message |
| 12 | # |
| 13 | # [stock_icon] Stock icon name of icon to display |
| 14 | # [title] Notification message title |
| 15 | # [message] Core message for the notification |
| 16 | def self.notify stock_icon, title, message |
| 17 | options = "-t #{EXPIRATION_IN_SECONDS * 1000} -i #{stock_icon}" |
| 18 | system "notify-send #{options} '#{title}' '#{message}'" |
| 19 | end |
| 20 | |
| 21 | Autotest.add_hook :red do |at| |
| 22 | title = filter_title(at.results) |
| 23 | message = filter_message(at.results) |
| 24 | notify ERROR_STOCK_ICON, title, message |
| 25 | end |
| 26 | |
| 27 | Autotest.add_hook :green do |at| |
| 28 | title = filter_title(at.results) |
| 29 | if title |
| 30 | message = filter_message(at.results) |
| 31 | notify SUCCESS_STOCK_ICON, title, message |
| 32 | else |
| 33 | title = 'ERROR' |
| 34 | notify ERROR_STOCK_ICON, title, message |
| 35 | end |
| 36 | end |
| 37 | |
| 38 | def self.filter_title(results) |
| 39 | output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?/) |
| 40 | output = swap_position(output) |
| 41 | return output |
| 42 | end |
| 43 | |
| 44 | def self.swap_position(string) |
| 45 | return string if string.nil? |
| 46 | |
| 47 | splitted = string.split(',') |
| 48 | "#{splitted[1]}, #{splitted[0]}" |
| 49 | end |
| 50 | |
| 51 | def self.filter_message(results) |
| 52 | output = '' |
| 53 | |
| 54 | magneta = results[/\n\e\[35m.*\n.*/] |
| 55 | if magneta |
| 56 | output << magneta.sub(/\n\e\[35m/, '').sub(/\e\[0m/, '') |
| 57 | end |
| 58 | |
| 59 | unless magneta # no need to find red if magneta excisted |
| 60 | red = results[/\n\e\[31m.*\n.*/] |
| 61 | if red |
| 62 | output << red.sub(/\n\e\[31m/, '').sub(/\e\[0m/, '') |
| 63 | end |
| 64 | end |
| 65 | |
| 66 | output.gsub!(/['#`<>]/, '') # notify-send don't like those symbols |
| 67 | return output |
| 68 | end |
| 69 | end |
| toggle raw diff |
--- /dev/null
+++ b/.autotest
@@ -0,0 +1,69 @@
+# vi: ft=ruby
+
+module Autotest::GnomeNotify
+
+ # Time notification will be displayed before disappearing automatically
+ EXPIRATION_IN_SECONDS = 20
+ ERROR_STOCK_ICON = "gtk-dialog-error"
+ SUCCESS_STOCK_ICON = "gtk-dialog-info"
+
+
+ # Convenience method to send an error notification message
+ #
+ # [stock_icon] Stock icon name of icon to display
+ # [title] Notification message title
+ # [message] Core message for the notification
+ def self.notify stock_icon, title, message
+ options = "-t #{EXPIRATION_IN_SECONDS * 1000} -i #{stock_icon}"
+ system "notify-send #{options} '#{title}' '#{message}'"
+ end
+
+ Autotest.add_hook :red do |at|
+ title = filter_title(at.results)
+ message = filter_message(at.results)
+ notify ERROR_STOCK_ICON, title, message
+ end
+
+ Autotest.add_hook :green do |at|
+ title = filter_title(at.results)
+ if title
+ message = filter_message(at.results)
+ notify SUCCESS_STOCK_ICON, title, message
+ else
+ title = 'ERROR'
+ notify ERROR_STOCK_ICON, title, message
+ end
+ end
+
+ def self.filter_title(results)
+ output = results.slice(/(\d+)\sexamples?,\s(\d+)\sfailures?/)
+ output = swap_position(output)
+ return output
+ end
+
+ def self.swap_position(string)
+ return string if string.nil?
+
+ splitted = string.split(',')
+ "#{splitted[1]}, #{splitted[0]}"
+ end
+
+ def self.filter_message(results)
+ output = ''
+
+ magneta = results[/\n\e\[35m.*\n.*/]
+ if magneta
+ output << magneta.sub(/\n\e\[35m/, '').sub(/\e\[0m/, '')
+ end
+
+ unless magneta # no need to find red if magneta excisted
+ red = results[/\n\e\[31m.*\n.*/]
+ if red
+ output << red.sub(/\n\e\[31m/, '').sub(/\e\[0m/, '')
+ end
+ end
+
+ output.gsub!(/['#`<>]/, '') # notify-send don't like those symbols
+ return output
+ end
+end |