spec/rbrsync_spec.rb in rbrsync-0.0.4 vs spec/rbrsync_spec.rb in rbrsync-0.0.5

- old
+ new

@@ -1,13 +1,30 @@ require 'spec_helper' require 'rbrsync' describe RbRsync do + + before do + @fixtures = File.expand_path(File.join(File.dirname(__FILE__), "fixtures")) + end + before :each do @rsync = RbRsync::RbRsync.new end + it "should find path to rsync binary" do + RbRsync::RbRsync.rsync_binary.should == `which rsync`.strip + end + + it "should be able to set the path rsync binary" do + rsync_binary = RbRsync::RbRsync.rsync_binary + RbRsync::RbRsync.rsync_binary = '/test/git' + RbRsync::RbRsync.rsync_binary.should == '/test/git' + RbRsync::RbRsync.rsync_binary = rsync_binary + RbRsync::RbRsync.rsync_binary.should == rsync_binary + end + it "should not have the archive flag set when archive! is not called" do @rsync.archive.should be_nil end it "should include the archive flag when archive! is called" do @@ -50,11 +67,11 @@ it "should allow chaining of boolean flags" do @rsync.archive!.fuzzy! @rsync.archive.should be_true @rsync.fuzzy.should be_true end - + it "should allow underscores as dashes" do @rsync.human_readable! @rsync.human_readable.should be_true end @@ -125,61 +142,68 @@ @rsync.from.should == "newuser@newhost.com:/home/newuser" end describe "command method" do - def command_should_have_only command, from, to, *flags + def command_should_have_only from, to, *flags + argv = @rsync.send(:build_command) + flags.each do |flag| - command.should include flag + argv.should include flag end - command.should =~ /^rsync/ - command.should =~ %r{#{from} #{to}$} + argv.shift.should eq(RbRsync::RbRsync.rsync_binary) + argv.pop.should eq(to) + argv.pop.should eq(from) - command.gsub!(/^rsync/, '') - command.gsub!(%r{#{from} #{to}$}, '') - - flags.each do |flag| - command.sub! flag, '' + argv = argv.drop_while do |a| + flags.include?(a) end - # with all the flags, the rsync command and the source and destination values stripped, there shouldn't be anything else - command.strip.should == "" + # with all the flags, the rsync command and the source and destination values dropped, there shouldn't be anything else + argv.size.should eq(0) end - + it "should set the --archive flag when archive! is used" do @rsync.archive! @rsync.from = "/home/me/" @rsync.to = "user@host.com:/home/user" - - @rsync.command.should == "rsync --archive /home/me/ user@host.com:/home/user" + + command_should_have_only "/home/me/", "user@host.com:/home/user", "--archive" end - + it "should set the --human-readable flag when human_readable! is used" do @rsync.human_readable! @rsync.from = "/home/me/" @rsync.to = "user@host.com:/home/user" - - @rsync.command.should == "rsync --human-readable /home/me/ user@host.com:/home/user" + + command_should_have_only "/home/me/", "user@host.com:/home/user", "--human-readable" end - + it "should set the --archive and --exclude flags when archive! and exclude= are used" do @rsync.archive! @rsync.exclude = "*~" @rsync.from = "/home/me/" @rsync.to = "user@host.com:/home/user" - command_should_have_only @rsync.command, "/home/me/", "user@host.com:/home/user", "--exclude='*~'", "--archive" + command_should_have_only "/home/me/", "user@host.com:/home/user", "--exclude='*~'", "--archive" end it "should set --exclude twice when exclude= is called with an array" do @rsync.exclude = ['*~', '/*'] @rsync.from = "/home/me/" @rsync.to = "user@host.com:/home/user" - command_should_have_only @rsync.command, "/home/me/", "user@host.com:/home/user", "--exclude='*~'", "--exclude='/*'" + command_should_have_only "/home/me/", "user@host.com:/home/user", "--exclude='*~'", "--exclude='/*'" end - + + it "should correctly escape argument values" do + @rsync.log_file = 'rsync.log' + @rsync.log_file_format = "'\\'%t '%f' with %b ''" + @rsync.from = "/home" + @rsync.to = "/home/user" + command_should_have_only "/home", "/home/user", "--log-file='rsync.log'", %q[--log-file-format=''\''\'\''%t '\''%f'\'' with %b '\'''\'''] + end end describe "constructor" do it "should take source and destination parameters" do @rsync = RbRsync::RbRsync.new '/home/user', 'user@host.com:/home/user' @@ -201,7 +225,31 @@ @rsync.from = "-bwah /Users/caleb/Desktop/BBEdit_9.2_Demo.dmg" @rsync.to = "/tmp" @rsync.send(:build_command).should include "--archive" end + end + + describe 'shell_escape' do + it "should escape ' and ;" do + @rsync.send(:shell_escape, "'some \\'quoted 'string 'with a ''").should == %q['\''some \'\''quoted '\''string '\''with a '\'''\''] + end + end + + describe "go!" do + it "should match rsync output" do + @rsync.from = File.join(@fixtures, "from") + @rsync.to = File.join(@fixtures, "to") + @rsync.recursive! + @rsync.verbose! + process = @rsync.go! + + process.status.success?.should be_true + process.out.should match(/total size is .* speedup is .*/) + end + end + + after do + # Cleanup + system "rm -rf #{File.join(@fixtures, 'to', '*')}" end end