<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>How they Discovered Something Worth Knowing &#187; Rspec</title>
	<atom:link href="http://www.silverchairsolutions.com/blog/category/rspec/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.silverchairsolutions.com/blog</link>
	<description>reflections on life, technology and brownies</description>
	<lastBuildDate>Wed, 12 May 2010 14:39:21 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>OSX Leopard, RSpec Autotest Growl/growlnotify Workaround</title>
		<link>http://www.silverchairsolutions.com/blog/2007/10/osx-leopard-rspec-autotest-growlgrowlnotify-workaround-2/</link>
		<comments>http://www.silverchairsolutions.com/blog/2007/10/osx-leopard-rspec-autotest-growlgrowlnotify-workaround-2/#comments</comments>
		<pubDate>Wed, 31 Oct 2007 15:06:21 +0000</pubDate>
		<dc:creator>mhagedorn</dc:creator>
				<category><![CDATA[Rspec]]></category>

		<guid isPermaLink="false">http://www.silverchairsolutions.com/blog/?p=19</guid>
		<description><![CDATA[Upon loading up the new release of OSX 10.5 (Leopard), I was very frustrated to find that Growl notifications no longer work when using Autotest. Apparently growlnotify, the command line app which makes the magic happen, isn&#8217;t compatible. Read on to see how I got this work It turns out that there is another way [...]]]></description>
			<content:encoded><![CDATA[<p>Upon loading up the new release of OSX 10.5 (Leopard), I was very frustrated to find that Growl notifications no longer work when using<br />
Autotest.  Apparently growlnotify, the command line app which makes the magic happen, isn&#8217;t compatible.   Read on to see how I got this work</p>

<p><p><span id="more-19"></span></p>
<p>It turns out that there is another way to get notifications into Growl other than the the (broken) growlnotify.   That technique is to use Applescript.<br />
Applescript allows you to do all sorts of neat things to applications in OSX but it is an arcane and hard to understand beast (at least in my<br />
opinion &#8211; feel free to flame me <img src='http://www.silverchairsolutions.com/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  ).  Based on a sample from the Growl website I was able to cobble together a working solution which<br />
restores all the growlnotify goodness we have come to expect from Autotest.  The first thing is to write the Applescript which talks to Growl.</p>
<p>Here is what I came up with (as modified from the Growl website)</p>
<p> &#8212;  growlNotify.applescript<br />
    &#8211;<br />
    &#8212;  Created by Mike Hagedorn on 2007-10-31.<br />
    &#8212;  Copyright (c) 2007 Silverchair Solutions. All rights reserved. http://www.silverchairsolutions.com<br />
    &#8212; based on script from the growl website<br />
    &#8212; works around the fact that growlnotify doesnt work on leopard</p>
<p> &#8212; pass in like osascript  growlNotify.applescript <title> <message> <image><br />
    on run argv<br />
        set rspec<em>title to item 1 of argv<br />
        set rspec</em>message to item 2 of argv<br />
        set rspec<em>image to item 3 of argv</p>
<p>     tell application &#8220;GrowlHelperApp&#8221;<br />
         &#8212; Make a list of all the notification types<br />
         &#8212; that this script will ever send:<br />
         set the allNotificationsList to ¬<br />
          {&#8220;RSpec Notification&#8221; }</p>
<p>      &#8212; Make a list of the notifications<br />
         &#8212; that will be enabled by default.<br />
         &#8212; Those not enabled by default can be enabled later<br />
         &#8212; in the &#8216;Applications&#8217; tab of the growl prefpane.<br />
         set the enabledNotificationsList to ¬<br />
          {&#8220;RSpec Notification&#8221;}</p>
<p>      &#8212; Register our script with growl.<br />
         &#8212; You can optionally (as here) set a default icon<br />
         &#8212; for this script&#8217;s notifications.<br />
         register as application &#8220;Growl-RSpec AppleScript Notifications&#8221; ¬<br />
           all notifications allNotificationsList ¬<br />
           default notifications enabledNotificationsList ¬<br />
           icon of application &#8220;Script Editor&#8221;</p>
<p>      &#8212; Send a Notification&#8230;<br />
         notify with name &#8220;RSpec Notification&#8221; ¬<br />
           title rspec</em>title ¬<br />
           description rspec<em>message ¬<br />
           application name &#8220;Growl-RSpec AppleScript Notifications&#8221; ¬<br />
           image from location  rspec</em>image</p>
<p>     end tell</p>
<p> end run</p>
<p>Put this file somewhere where you can find it (I used ~/Library/Scripts/growlNotify.applescript) then modify the .autotest file you have in your home directory<br />
to not call growlnotify, but the new method you just crafted (i.e. the applescript).  Here is what mine looks like</p>
<p> #<br />
    #  .autotest<br />
    #  Autotest Growl Notifications for Rspec and Test::Unit<br />
    #<br />
    #  Created by Rein Henrichs on 2007-09-12.<br />
    #  Modified by Mike Hagedorn (www.silverchairsolutions.com) to use osascript to work around Leopard issues<br />
    #<br />
    #  Copyright 2007 Rein Henrichs.<br />
    #  http://pastie.caboo.se/96573/download<br />
    # </p>
<p> require &#8216;logger&#8217;<br />
    logfile = File.join(File.dirname(<strong>FILE</strong>), &#8216;.autotest.log&#8217;)<br />
    $logger = Logger.new(logfile)</p>
<p> module Autotest::Growl<br />
      Autotest.add<em>hook :ran</em>command do |at|<br />
        input = Input.new( at.results )<br />
        Growler.display<em>notification( input )<br />
      end<br />
    end  </p>
<p> class String<br />
      def remove</em>color<em>codes!<br />
        self.gsub!(/\e&#91;\d+m/,&#8221;)<br />
        self.strip!<br />
      end<br />
    end</p>
<p> class Growler<br />
      class &lt;&lt; self<br />
        def display</em>notification( input )<br />
          growler = RspecGrowler if input.from<em>rspec?<br />
          growler = TestUnitGrowler if input.from</em>test<em>unit?<br />
          growler.display</em>notification( input.result<em>line )<br />
        rescue => e<br />
          $logger.fatal e<br />
          notify</em>system<em>error( &#8220;Unexpected Error&#8221;, &#8220;Please check your ~/.autotest.log and report anything strange to reinh@reinh.com&#8221; )<br />
        end</p>
<p>     def notify(title, msg, img, pri=0, stick=&#8221;" )<br />
          #system &#8220;growlnotify -n autotest &#8211;image #{img} -p #{pri} -m #{msg.inspect} #{title} #{stick}&#8221;<br />
          system &#8220;osascript ~/Library/Scripts/growlNotify.applescript &#8216;#{title}&#8217; &#8216;#{msg.inspect}&#8217;  &#8216;#{img}&#8217;&#8221;<br />
        end</p>
<p>     def notify</em>system<em>error( title = &#8220;System Error&#8221;, err = &#8220;&#8221;)<br />
          Growler.notify( title, err, &#8216;~/Library/autotest/rails</em>fail.png&#8217;)<br />
        end<br />
      end<br />
    end</p>
<p> class Input<br />
      def initialize(input)<br />
        @input = input<br />
      end</p>
<p>   def empty?; @input.empty?; end</p>
<p>   # TODO: Make these work<br />
      def failing?; end<br />
      def passing?; end<br />
      def error?; end</p>
<p>   def from<em>rspec?; !!rspec</em>result<em>line || empty?; end<br />
      def from</em>test<em>unit?; !!test</em>unit<em>result</em>line; end<br />
      def rspec<em>result</em>line; @input.grep( /\d+\sexample/ ).first; end<br />
      def test<em>unit</em>result<em>line; @input.grep( /\d+\sassertion/ ).first; end<br />
      def result</em>line<br />
        @line ||= case<br />
          when from<em>rspec? then rspec</em>result<em>line<br />
          when from</em>test<em>unit? then test</em>unit<em>result</em>line<br />
          else nil<br />
          end<br />
        @line.remove<em>color</em>codes! if @line<br />
      end<br />
    end</p>
<p> class AutoGrowler &lt; Growler<br />
      class &lt;&lt; self<br />
        def notify<em>failure(input)<br />
          notify( &#8220;Tests Failed&#8221;, input, &#8216;~/Library/autotest/rails</em>fail.png&#8217;, 2 )<br />
        end<br />
        def notify<em>success(input)<br />
          notify( &#8220;Tests Passed&#8221;, input, &#8216;~/Library/autotest/rails</em>ok.png&#8217;, 0 )<br />
        end<br />
      end<br />
    end</p>
<p> class RspecGrowler &lt; AutoGrowler<br />
      class &lt;&lt; self<br />
        def notify<em>pending(input)<br />
          notify( &#8220;Tests Pending&#8221;, input, &#8216;~/Library/autotest/pending.png&#8217;, 1 )<br />
        end</p>
<p>     def notify</em>error<br />
          notify( &#8220;Syntax Error&#8221;, &#8220;It looks like there was a syntax error. Check your autotest results.&#8221;, &#8216;~/Library/autotest/rails<em>fail.png&#8217;)<br />
        end</p>
<p>     def display</em>notification(input)</p>
<p>       notify<em>error and return unless input<br />
          # TODO: replace with some object oriented goodness. elsif FTL!<br />
          examples, failures, pending = input.split(&#8220;, &#8220;)<br />
          if failures.to</em>i > 0<br />
            notify<em>failure input<br />
          elsif pending.to</em>i > 0<br />
            notify<em>pending input<br />
          else<br />
            notify</em>success input<br />
          end<br />
        end<br />
      end<br />
    end</p>
<p> class TestUnitGrowler &lt; AutoGrowler<br />
      class &lt;&lt; self<br />
        def notify<em>error(input)<br />
          notify( &#8220;Tests Errored&#8221;, input, &#8216;~/Library/autotest/rails</em>fail.png&#8217;, 2 )<br />
        end<br />
        def display<em>notification( input )</p>
<p>       # TODO: replace with some object oriented goodness. elsif FTL!<br />
          tests, assertions, failures, errors = input.split(&#8220;, &#8220;)<br />
          if errors.to</em>i > 0<br />
            notify<em>error input<br />
          elsif failures.to</em>i > 0<br />
            notify<em>failure input<br />
          else<br />
            notify</em>success input<br />
          end<br />
        end<br />
      end<br />
    end</p>
<p>Notice that the change here is a small one, only on line 44, reference the commented out growlnotify.  This is working great for me.  Hope this helps<br />
you!</p></p>
]]></content:encoded>
			<wfw:commentRss>http://www.silverchairsolutions.com/blog/2007/10/osx-leopard-rspec-autotest-growlgrowlnotify-workaround-2/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>
