require 'spec_helper' describe ChecksummerFile do let(:line) { "2010-09-14+10:16:26.4888617110 66 d /v1/incoming/ " } let(:line_with_original_path) { "2010-09-14+10:16:26.4888617110 66 d /v1/incoming/ /tmp" } describe "#from_line" do before(:each) do @file = ChecksummerFile.from_line(line) end it "returns a ChecksummerFile" do @file.should be_an_instance_of(ChecksummerFile) end { :modified_at => Time.local(2010, 9, 14, 10, 16, 26), :type => "d", :path => "/v1/incoming" }.each do |key, value| it "sets #{key.inspect} to #{value.inspect}" do @file.send(key).to_s.should == value.to_s end end it "sets original_path to nil when not in response" do @file.original_path.should be_nil end it "sets original_path to the path when found in response" do ChecksummerFile.from_line(line_with_original_path).original_path.should == "/tmp" end it "calls expans_path on all found paths" do File.should_receive(:expand_path).with("/v1/incoming/", ) File.should_receive(:expand_path).with("/tmp") ChecksummerFile.from_line(line_with_original_path) end end describe "#checksum_to!" do let(:file) { ChecksummerFile.new(:path => "/some/text.csv") } let(:time) { Time.local(2010, 9, 10, 11, 12, 13) } let(:md5) { "fde8dad8ea43640b00cdd1e92e532ca9" } before(:each) do FileUtils.stub!(:cp).and_return true FileUtils.stub!(:mkdir_p) FileUtils.stub!(:touch) FileUtils.stub(:ln_sf).and_return true File.stub!(:exists?).and_return false File.stub(:symlink?).and_return false File.stub!(:mtime).and_return time file.stub!(:md5).and_return md5 end it "calls exists with correct parameters" do File.should_receive(:exists?).with("/tmp/data/f/d/e/8/fde8dad8ea43640b00cdd1e92e532ca9") file.checksum_to!("/tmp/data") end it "calls File.symlink? with the correct parameters" do File.should_receive(:symlink?).with("/some/text.csv").and_return false file.checksum_to!("/tmp/data") end it "calls md5_path with correct directory" do file.should_receive(:md5_path).with("/tmp/data").at_least(1).times.and_return md5 file.checksum_to!("/tmp/data") end describe "with the file not being checksummed" do it "copys the file to it's md5" do File.stub!(:exists?).and_return false FileUtils.should_receive(:cp).with("/some/text.csv", "/tmp/data/f/d/e/8/fde8dad8ea43640b00cdd1e92e532ca9") file.checksum_to!("/tmp/data") end it "calls FileUtils::mkdir_p with correct path" do FileUtils.should_receive(:mkdir_p).with("/tmp/data/f/d/e/8") file.checksum_to!("/tmp/data") end it "creates a symlink for the copied file" do FileUtils.should_receive(:ln_sf).with("/tmp/data/f/d/e/8/fde8dad8ea43640b00cdd1e92e532ca9", "/some/text.csv") file.checksum_to!("/tmp/data") end it "returns :copied when copied" do file.checksum_to!("/tmp/data").should == :copied end it "changes the mtime of the symlink to the mtime of the original file" do FileUtils.should_receive(:touch).with("/some/text.csv", :mtime => time) file.checksum_to!("/tmp/data") end end describe "with the file being checksummed already" do before(:each) do File.stub!(:exists?).with("/tmp/data/f/d/e/8/fde8dad8ea43640b00cdd1e92e532ca9").and_return true end it "does not copy the file when already exists" do FileUtils.should_not_receive(:cp) file.checksum_to!("/tmp/data") end it "does not call FileUtils::mkdir_p" do FileUtils.should_not_receive(:mkdir_p) file.checksum_to!("/tmp/data") end it "does symlink the file" do FileUtils.should_receive(:ln_sf).with("/tmp/data/f/d/e/8/fde8dad8ea43640b00cdd1e92e532ca9", "/some/text.csv") file.checksum_to!("/tmp/data") end it "returns :symlinked" do file.checksum_to!("/tmp/data").should == :symlinked end it "changes the mtime of the symlink to the mtime of the original file" do FileUtils.should_receive(:touch).with("/some/text.csv", :mtime => time) file.checksum_to!("/tmp/data") end end describe "with the file being a symlink" do before(:each) do File.stub(:symlink?).with("/some/text.csv").and_return true end it "does not copy the file when already exists" do FileUtils.should_not_receive(:cp) file.checksum_to!("/tmp/data") end it "does not call FileUtils::mkdir_p" do FileUtils.should_not_receive(:mkdir_p) file.checksum_to!("/tmp/data") end it "does not symlink the file" do FileUtils.should_not_receive(:ln_sf) file.checksum_to!("/tmp/data") end it "returns :exists" do file.checksum_to!("/tmp/data").should == :was_symlink end it "changes the mtime of the symlink to the mtime of the original file" do FileUtils.should_not_receive(:touch) file.checksum_to!("/tmp/data") end end end describe "#md5" do let(:md5_module) { double("md5 module", :file => "sometest") } let(:file) do file = ChecksummerFile.new(:path => "/some/path") file.stub!(:md5_module).and_return md5_module file end it "calls md5_module with correct file_path when no md5 set" do md5_module.should_receive(:file).with("/some/path") file.md5 end it "saves the md5 in instance variable" do checksum = double("checksum", :to_s => "the checksum") md5_module.stub(:file).and_return checksum file.md5 file.instance_variable_get(:@md5).should == "the checksum" end it "returns the instance variable when set" do checksum = double("checksum") file.instance_variable_set(:@md5, checksum) file.md5.should == checksum end end describe "#md5_module" do it "returns Digest::MD5 for Ruby 1.9" do ChecksummerFile.new.md5_module.should == Digest::MD5 end end describe "#path=" do it "sets the correct path" do file = ChecksummerFile.new file.path = "/some/path" file.path.should == "/some/path" end it "resets an existing md5" do file = ChecksummerFile.new file.instance_variable_set(:@md5, "somemd5") file.path = "/some/path" file.instance_variable_get(:@md5).should be_nil end end end