You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@buildr.apache.org by vb...@apache.org on 2008/10/20 22:48:16 UTC

svn commit: r706402 - /incubator/buildr/trunk/spec/addon/drb_spec.rb

Author: vborja
Date: Mon Oct 20 13:48:15 2008
New Revision: 706402

URL: http://svn.apache.org/viewvc?rev=706402&view=rev
Log:
Added many specs for drb addon

Modified:
    incubator/buildr/trunk/spec/addon/drb_spec.rb

Modified: incubator/buildr/trunk/spec/addon/drb_spec.rb
URL: http://svn.apache.org/viewvc/incubator/buildr/trunk/spec/addon/drb_spec.rb?rev=706402&r1=706401&r2=706402&view=diff
==============================================================================
--- incubator/buildr/trunk/spec/addon/drb_spec.rb (original)
+++ incubator/buildr/trunk/spec/addon/drb_spec.rb Mon Oct 20 13:48:15 2008
@@ -18,64 +18,228 @@
 require 'stringio'
 Sandbox.require_optional_extension 'buildr/drb'
 
+
 describe Buildr::DRbApplication do 
+
+  module DRbHelper 
+    attr_accessor :app, :drb, :cfg
+
+    def use_argv(*args)
+      cfg.update :argv => args
+    end
+
+    def use_stdio(stdin = nil, stdout = nil, stderr = nil)
+      stdin ||= StringIO.new
+      stdout ||= StringIO.new
+      stderr ||= StringIO.new
+      cfg.update :in => stdin, :out => stdout, :err => stderr
+    end
+    
+    def remote_run
+      drb.remote_run(cfg)
+    end
+
+    def output
+      cfg[:out].string
+    end
+
+    def write_buildfile(content = nil)
+      write 'buildfile', content || <<-BF
+          define('foo') do
+
+            task('hello') do 
+              $stdout.puts 'hi'
+            end
+
+            task('empty')
+
+            task('no') do
+              task('empty' => 'delete_me')
+              task('empty') { $stout.puts 'no' }
+            end
+
+            task('delete_me') do
+            end
+          end
+      BF
+    end
+  end
+
+  include DRbHelper
   
   before(:each) do
-    @drb = Buildr::DRbApplication
+    @cfg = {
+      :dir => Dir.pwd, :argv => [],
+      :in => $stdin, :out => $stdout, :err => $stderr
+    }
+    @drb = Buildr::DRbApplication.clone
+    @app = Buildr.application.extend @drb
   end
   
   describe '.run' do
     it 'starts server if no server is running' do
-      @drb.should_receive(:connect).and_raise DRb::DRbConnError
-      @drb.should_receive(:run_server!)
-      @drb.should_not_receive(:run_client)
-      @drb.run
+      drb.should_receive(:connect).and_raise DRb::DRbConnError
+      drb.should_receive(:run_server!)
+      drb.should_not_receive(:run_client)
+      drb.run
     end
 
     it 'connects to an already started server' do
-      @drb.should_receive(:connect).and_return "client"
-      @drb.should_receive(:run_client).with "client"
-      @drb.should_not_receive(:run_server!)
-      @drb.run
+      drb.should_receive(:connect).and_return "client"
+      drb.should_receive(:run_client).with "client"
+      drb.should_not_receive(:run_server!)
+      drb.run
     end
   end
 
   describe '.remote_run' do
     
-    before(:each) do 
-      @cfg = { 
-        :dir => Dir.pwd, :argv => [],
-        :in => StringIO.new, :out => StringIO.new, :err => StringIO.new
-      }
-    end
-
     describe 'stdout' do
       it 'is redirected to client' do 
+        use_stdio
         Buildr.application.should_receive(:remote_run) do 
           $stdout.puts "HELLO"
         end
-        @drb.remote_run(@cfg)
-        @cfg[:out].string.should eql("HELLO\n")
+        remote_run
+        output.should eql("HELLO\n")
       end
     end
 
     describe 'stderr' do 
       it 'is redirected to client' do
+        use_stdio
         Buildr.application.should_receive(:remote_run) do 
           $stderr.puts "HELLO"
         end
-        @drb.remote_run(@cfg)
-        @cfg[:err].string.should eql("HELLO\n")
+        remote_run
+        cfg[:err].string.should eql("HELLO\n")
       end
     end
 
     describe 'stdin' do
       it 'is redirected to client' do
-        @cfg[:in].should_receive(:gets).and_return("HELLO\n")
+        use_stdio
+        cfg[:in].should_receive(:gets).and_return("HELLO\n")
+        result = nil
+        Buildr.application.should_receive(:remote_run) do 
+          result = $stdin.gets
+        end
+        remote_run
+        result.should eql("HELLO\n")
+      end
+    end
+
+    describe 'server ARGV' do
+      it 'is replaced with client argv' do
+        use_argv 'hello'
         Buildr.application.should_receive(:remote_run) do 
-          $stdin.gets.should eql("HELLO\n")
+          ARGV.should eql(['hello'])
         end
-        @drb.remote_run(@cfg)
+        remote_run
+      end
+    end
+
+    describe 'without buildfile loaded' do
+      before(:each) do
+        app.instance_eval { @rakefile = nil }
+        write_buildfile
+      end
+      
+      it 'should load the buildfile' do
+        app.should_receive(:top_level)
+        lambda { remote_run }.should run_task('foo')
+      end
+    end
+
+    describe 'with unmodified buildfile' do
+      
+      before(:each) do 
+        write_buildfile
+        app.options.rakelib = []
+        app.send :load_buildfile
+        app.send :buildfile_reloaded!
+      end
+      
+      it 'should not reload the buildfile' do
+        app.should_not_receive(:reload_buildfile)
+        app.should_receive(:top_level)
+        remote_run
+      end
+
+      it 'should invoke tasks specified by client' do
+        times = 0
+        task(:hello) { times += 1 }
+        use_argv 'hello'
+        2.times { remote_run }
+        times.should eql(2)
+      end
+
+      it 'should not define projects again' do
+        use_stdio
+        use_argv 'foo:hello'
+        lambda { 2.times { remote_run } }.should_not run_task('foo')
+        output.should eql("hi\nhi\n")
+      end
+      
+    end
+
+    describe 'with modified buildfile' do
+      
+      before(:each) do 
+        write_buildfile
+        app.options.rakelib = []
+        app.send :load_buildfile
+        app.send :buildfile_reloaded!
+        app.instance_eval { @last_loaded = Time.now - 10 }
+        write_buildfile <<-BF
+          define('foo') do
+            task('hello') do 
+              $stdout.puts 'bye'
+            end
+            task('empty')
+            define('bar') do
+              
+            end
+          end
+        BF
+      end
+
+      it 'should reload the buildfile' do
+        app.should_receive(:reload_buildfile)
+        app.should_receive(:top_level)
+        remote_run
+      end
+
+      it 'should redefine projects' do
+        lambda { remote_run }.should run_tasks('foo', 'foo:bar')
+      end
+
+      it 'should remove tasks deleted from buildfile' do
+        app.lookup('foo:delete_me').should_not be_nil
+        remote_run
+        app.lookup('foo:delete_me').should be_nil
+      end
+      
+      it 'should restore tasks actions' do
+        actions = app.lookup('foo:empty').instance_eval { @actions }
+        actions.should be_empty # no action
+        app.lookup('foo:no').invoke # enhance the empty task
+        actions = app.lookup('foo:empty').instance_eval { @actions }
+        actions.should_not be_empty
+        remote_run # cause to reload the buildfile
+        actions = app.lookup('foo:empty').instance_eval { @actions }
+        actions.should be_empty # as defined on the new buildfile
+      end
+
+      it 'should restore task prerequisites' do
+        pres = app.lookup('foo:empty').send(:prerequisites).map(&:to_s)
+        pres.should be_empty # no action
+        app.lookup('foo:no').invoke # enhance the empty task
+        pres = app.lookup('foo:empty').send(:prerequisites).map(&:to_s)
+        pres.should_not be_empty
+        remote_run # cause to reload the buildfile
+        pres = app.lookup('foo:empty').send(:prerequisites).map(&:to_s)
+        pres.should be_empty # as defined on the new buildfile
       end
     end