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

module Pod
  describe Command::Setup do
    extend SpecHelper::Command

    extend SpecHelper::TemporaryRepos

    before do
      environment.stubs(:repos_dir).returns(SpecHelper.tmp_repos_path)
    end

    it "returns the read only URL of the `master` spec-repo" do
      cmd = Command::Setup.new(argv)
      cmd.url.should == 'https://github.com/CocoaPods/Specs.git'
    end

    it "returns the push URL of the `master` spec-repo" do
      config.stubs(:silent).returns(true)
      cmd = Command::Setup.new(argv('--push'))
      cmd.url.should == 'git@github.com:CocoaPods/Specs.git'
    end

    before do
      set_up_test_repo
      Command::Setup.any_instance.stubs(:read_only_url).returns(test_repo_path.to_s)
      environment.stubs(:repos_dir).returns(SpecHelper.temporary_directory)
    end

    it "runs with correct parameters" do
      lambda { run_command('setup') }.should.not.raise
    end

    it "creates the local spec-repos directory and creates a clone of the `master` repo" do
      output = run_command('setup')
      output.should.include "Setup completed"
      output.should.not.include "push"
      url = Dir.chdir(config.repos_dir + 'master') { `git config --get remote.origin.url`.chomp }
      url.should == test_repo_path.to_s
    end

    it "preserves push access for the `master` repo" do
      output = run_command('setup')
      output.should.not.include "push"
      Dir.chdir(config.repos_dir + 'master') { `git remote set-url origin git@github.com:CocoaPods/Specs.git` }
      command('setup').url.should == 'git@github.com:CocoaPods/Specs.git'
    end

    before do
      FileUtils.rm_rf(test_repo_path)
      set_up_old_test_repo
      config.stubs(:repos_dir).returns(SpecHelper.temporary_directory + 'cocoapods/repos')
      Command::Setup.any_instance.stubs(:old_master_repo_dir).returns(SpecHelper.temporary_directory + 'cocoapods/master')
    end

    it "migrates repos from the old directory structure to the new one" do
      source = SpecHelper.temporary_directory + 'cocoapods/master'
      target = config.repos_dir + 'master'

      source.should.exist?
      target.should.not.exist?

      run_command('setup')

      source.should.not.exist?
      target.should.exist?
    end

  end
end
