<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>a timocracy of one: Capturing a system call in ruby with backticks, while setting an environment variable</title>
    <link>http://www.timocracy.com/articles/2008/02/20/capturing-a-system-call-in-ruby-with-backticks-while-setting-an-environment-variable</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>Tim Connor's Blog</description>
    <item>
      <title>Capturing a system call in ruby with backticks, while setting an environment variable</title>
      <description>&lt;p&gt;As you may know system() and `` both make system calls in ruby, but the second one captures the &lt;span class="caps"&gt;STDOUT&lt;/span&gt; for you, essentially, which is handy sometimes, like when testing.  They don&amp;#8217;t necessarily both do this by just a straight call to the commandline, but various internal utilities that make such thing possible in Windows too, for instance.&lt;/p&gt;


Well the problem with this is it can mess with your setting of an &lt;span class="caps"&gt;ENV&lt;/span&gt; as a precursor to your command.  For instance, this is valid on my platform:
&lt;pre&gt;&lt;code&gt;
RAILS_ENV=test rake metric_fetcher
&lt;/code&gt;&lt;/pre&gt;
and works via system():
&lt;pre&gt;&lt;code&gt;
system("RAILS_ENV=test rake metric_fetcher")
&lt;/code&gt;&lt;/pre&gt;
but chokes in backticks, probably due to the aforementioned jiggering around for Windows compat:
&lt;pre&gt;&lt;code&gt;
`RAILS_ENV=test rake metric_fetcher`
&lt;/code&gt;&lt;/pre&gt;
Now you generally can just move the command back to the beginning of the line:
&lt;pre&gt;&lt;code&gt;
`rake RAILS_ENV=test metric_fetcher`
&lt;/code&gt;&lt;/pre&gt;
but if for some bizarre reason that doesn&amp;#8217;t work for you (it&amp;#8217;s probably something else going, wrong, though, to be honest) you can do this:
&lt;pre&gt;&lt;code&gt;
ENV['RAILS_ENV']='test'; `rake metric_fetcher`
&lt;/code&gt;&lt;/pre&gt;

So ultimately, I can do this hacky wonder:
&lt;pre&gt;&lt;code&gt;
assert `rake RAILS_ENV=test metric_fetcher`.empty?
&lt;/code&gt;&lt;/pre&gt;

	&lt;p&gt;if my rake task succeeds silently and fails noisily (which is a useful characteristic for cron&amp;#8217;ed tasks).&lt;/p&gt;


	&lt;p&gt;Thanks to Defiler and ocotpod for proving to me I really did have my head up my ass and that all but the leading env var in backticks form should work as expected.(fixture set-up issues as usual).&lt;/p&gt;


	&lt;p&gt;&lt;strong&gt;Addendum&lt;/strong&gt; And if you are calling rake like this in a test and getting odd problems &amp;#8211; consider turning transaction fixtures off.&lt;/p&gt;</description>
      <pubDate>Wed, 20 Feb 2008 10:26:00 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:71e4799c-c086-4af7-b5d0-82b604137655</guid>
      <author>Tim Connor</author>
      <link>http://www.timocracy.com/articles/2008/02/20/capturing-a-system-call-in-ruby-with-backticks-while-setting-an-environment-variable</link>
      <category>Rails</category>
      <category>ruby</category>
      <category>rake</category>
      <category>testing</category>
    </item>
    <item>
      <title>"Capturing a system call in ruby with backticks, while setting an environment variable" by Tim Connor</title>
      <description>&lt;p&gt;fyi: I just added an article with the &amp;#8220;proper&amp;#8221; way of doing this by actually &lt;a href="http://www.timocracy.com/articles/2008/02/21/calling-invoking-rails-rake-tasks-from-within-ruby-for-testing-try-2" rel="nofollow"&gt;invoking the rake task from ruby&lt;/a&gt;&lt;/p&gt;</description>
      <pubDate>Thu, 21 Feb 2008 14:10:13 -0800</pubDate>
      <guid isPermaLink="false">urn:uuid:11009495-b3b2-4382-91e9-40ea0de28f4f</guid>
      <link>http://www.timocracy.com/articles/2008/02/20/capturing-a-system-call-in-ruby-with-backticks-while-setting-an-environment-variable#comment-210</link>
    </item>
  </channel>
</rss>
