require 'spec_helper' require 'fileutils' describe Box::Release::Base do let(:update_tar) { File.expand_path "../../../fixtures/box-update-20100421-0846.tar", __FILE__ } subject { Box::Release::Memory.new } describe "#file" do context "by default" do its(:file) { should == "/tmp/release.tar" } end it "should use Box::Release.download_directory when defined" do subject.stub :download_directory => "/dummy" subject.file.should == "/dummy/release.tar" end end describe "#download" do before(:each) do FileUtils.rm_f(subject.file) end it "should download a local file" do subject.url = __FILE__ subject.download IO.read(subject.file).should == IO.read(__FILE__) end it "should raise an error if checksum is invalid" do subject.stub :valid_checksum? => false lambda { subject.download }.should raise_error end end describe "valid_checksum? " do before(:each) do subject.stub :file => update_tar end it "should return true if checksum is SHA256 digest of downloaded file" do subject.checksum = "9eeb495a5b273c7dd88aa8dd741df8ecf10b5a34c422b0fe6b7a1b053a518369" subject.should be_valid_checksum end it "should return false if checksum is not the SHA256 digest of downloaded file" do subject.checksum = "dummy" subject.should_not be_valid_checksum end it "should return true if checksum is nil" do subject.checksum = nil subject.should be_valid_checksum end it "should return false if download file isn't found" do subject.checksum = "dummy" subject.stub!(:file_checksum) subject.should_not be_valid_checksum end end describe "to_yaml" do def self.it_shoud_include(attribute) attribute = attribute.to_s it "should the Release #{attribute}" do subject.send("#{attribute}=", "dummy") YAML.load(subject.to_yaml).should include(attribute => "dummy") end end it_shoud_include :name it_shoud_include :description_url it_shoud_include :status_updated_at end describe "yaml_file" do it "should return the path of a file which contains Release#to_yaml" do subject.stub :to_yaml => "to_yaml" IO.read(subject.yaml_file).should == "to_yaml" end end describe "#install" do before(:each) do subject.stub :yaml_file => "/path/to/tempfile" end it "should execute install command with release file and temp file (with attributes) in argument" do subject.stub :install_command => "/usr/bin/dummy", :file => "/tmp/test.tar" Box::Release.should_receive(:execute!).with("/usr/bin/dummy /tmp/test.tar /path/to/tempfile").and_return(true) subject.install end end describe '#newer? ' do it "should be true when other release is nil" do subject.should be_newer(nil) end it "should be true when name is greather than the other one" do subject.name = "version-2" subject.should be_newer(stub(:name => "version-1")) end it "should be false when name is nil" do subject.name = nil subject.should_not be_newer(stub(:name => "version-1")) end end end