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 @rsync.archive! @rsync.archive.should be_true end it "should include the archive flag when archive is set to true with archive=" do @rsync.archive = true @rsync.archive.should be_true end it "should have the archive flag set to false when ~ is used on the option" do ~ @rsync.archive! @rsync.archive.should be_false end it "should have the archive flag set to false when archive! is chained with no" do @rsync.no.archive! @rsync.archive.should be_false end it "should raise an ArgumentError if a non boolean value (one that is not true, false, or nil) is assigned to a boolean flag" do lambda { @rsync.archive = "string" }.should raise_error(ArgumentError) end it "should set the rsh flag when called with rsh=" do @rsync.rsh = "/usr/bin/ssh" @rsync.rsh.should include("/usr/bin/ssh") end it "should raise a NameError when a non boolean flag is used with rsync.no" do lambda { @rsync.no.rsh = "/usr/bin/ssh" }.should raise_error(NameError) end 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 #destination it "should construct an rsync path when the individual destination properties are set" do @rsync.to_user = "user" @rsync.to_host = "host.com" @rsync.to_path = "/home/user" @rsync.to.should == "user@host.com:/home/user" end it "should have a nil destination if a user is set but not a host and vice versa for a destination" do @rsync.to_user = "user" @rsync.to_path = "/home/user" @rsync.to.should be_nil rsync2 = RbRsync::RbRsync.new rsync2.to_host = "host.com" rsync2.to_path = "/home/user" rsync2.to.should be_nil end it "should have a nil destination if a destination path is not provided" do @rsync.to_user = "user" @rsync.to_host = "host.com" @rsync.to.should be_nil end it "should prefer an explicitly set destination over the components" do @rsync.to_user = "user" @rsync.to_host = "host.com" @rsync.to_path = "/home/user" @rsync.to = "newuser@newhost.com:/home/newuser" @rsync.to.should == "newuser@newhost.com:/home/newuser" end # source it "should construct an rsync path when the individual source properties are set" do @rsync.from_user = "user" @rsync.from_host = "host.com" @rsync.from_path = "/home/user" @rsync.from.should == "user@host.com:/home/user" end it "should have a nil source if a user is set but not a host and vice versa for a source" do @rsync.from_user = "user" @rsync.from_path = "/home/user" @rsync.from.should be_nil rsync2 = RbRsync::RbRsync.new rsync2.from_host = "host.com" rsync2.from_path = "/home/user" rsync2.from.should be_nil end it "should have a nil source if a source path is not provided" do @rsync.from_user = "user" @rsync.from_host = "host.com" @rsync.from.should be_nil end it "should prefer an explicitly set source over the components" do @rsync.from_user = "user" @rsync.from_host = "host.com" @rsync.from_path = "/home/user" @rsync.from = "newuser@newhost.com:/home/newuser" @rsync.from.should == "newuser@newhost.com:/home/newuser" end describe "command method" do def command_should_have_only from, to, *flags argv = @rsync.send(:build_command) flags.each do |flag| argv.should include flag end argv.shift.should eq(RbRsync::RbRsync.rsync_binary) argv.pop.should eq(to) argv.pop.should eq(from) argv = argv.drop_while do |a| flags.include?(a) end # 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" 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" 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 "/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 "/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' @rsync.from.should == '/home/user' @rsync.to.should == 'user@host.com:/home/user' end it "should have no source or destination if the source and destination parameters are omitted from the constructor" do @rsync = RbRsync::RbRsync.new @rsync.from.should be_nil @rsync.to.should be_nil end end describe "build_command" do it "should have the --archive flag when the archive! method is used" do @rsync.archive! @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