require File.expand_path('../../spec_helper', __FILE__)

describe "Pod::Downloader" do
  before do
    @pod = Pod::LocalPod.new(fixture_spec('banana-lib/BananaLib.podspec'), temporary_sandbox)
  end

  describe "for Git" do
    extend SpecHelper::TemporaryDirectory
  
    it "check's out a specific commit" do
      @pod.specification.stubs(:source).returns(
        :git => fixture('banana-lib'), :commit => '02467b074d4dc9f6a75b8cd3ab80d9bf37887b01'
      )
      downloader = Pod::Downloader.for_pod(@pod)
      downloader.download
      
      (@pod.root + 'README').read.strip.should == 'first commit'
    end
  
    it "check's out a specific tag" do
      @pod.specification.stubs(:source).returns(
        :git => fixture('banana-lib'), :tag => 'v1.0'
      )
      downloader = Pod::Downloader.for_pod(@pod)
      downloader.download
      (@pod.root + 'README').read.strip.should == 'v1.0'
    end
  
    it "removes the .git directory when cleaning" do
      @pod.specification.stubs(:source).returns(
        :git => fixture('banana-lib')
      )
      downloader = Pod::Downloader.for_pod(@pod)
      downloader.download
      downloader.clean
      (@pod.root + '.git').should.not.exist
    end
  end
  
  describe "for Mercurial" do
    it "check's out a specific revision" do
      @pod.specification.stubs(:source).returns(
        :hg => fixture('mercurial-repo'), :revision => '46198bb3af96'
      )
      downloader = Pod::Downloader.for_pod(@pod)
      downloader.download
      (@pod.root + 'README').read.strip.should == 'first commit'
    end
  
    it "removes the .hg directory when cleaning" do
      @pod.specification.stubs(:source).returns(
        :hg => fixture('mercurial-repo')
      )
      downloader = Pod::Downloader.for_pod(@pod)
      downloader.download
      downloader.clean
      (@pod.root + '.hg').should.not.exist
    end
  end
  
  describe "for Subversion" do
    it "check's out a specific revision" do
      @pod.specification.stubs(:source).returns(
        :svn => "file://#{fixture('subversion-repo')}", :revision => '1'
      )
      downloader = Pod::Downloader.for_pod(@pod)
      downloader.download
      (@pod.root + 'README').read.strip.should == 'first commit'
    end
  
    it "check's out a specific tag" do
      @pod.specification.stubs(:source).returns(
        :svn => "file://#{fixture('subversion-repo')}/tags/tag-1"
      )
      downloader = Pod::Downloader.for_pod(@pod)
      downloader.download
      (@pod.root + 'README').read.strip.should == 'tag 1'
    end
  
    it "removes the .svn directories when cleaning" do
      @pod.specification.stubs(:source).returns(
        :svn => "file://#{fixture('subversion-repo')}/trunk"
      )
      downloader = Pod::Downloader.for_pod(@pod)
      downloader.download
      downloader.clean
      (@pod.root + '.svn').should.not.exist
    end
  end
end

