<commit_before>require File.join(File.dirname(__FILE__), "helper")

class NsyncConsumerTest < Test::Unit::TestCase
  def setup
    Nsync.reset_config
    #Grit.debug = true
    @lock_file = "#{TMP_DIR}/nsync_test_#{Process.pid}.lock"
    FileUtils.rm @lock_file, :force => true
    Nsync.config.lock_file = @lock_file

    Nsync.config.version_manager = stub(:version => "foo", :previous_version => "bar")
    Nsync.config.version_manager.stubs(:version=).with(is_a(String))

    # I think mocha is doing something evil and redefing clone
    if Grit::Git.instance_methods.include?("clone")
      Grit::Git.send(:undef_method, :clone)
    end
  end

  context "Initializing Consumer" do
    context "when the repo already exists" do
      setup do
        @repo = TestRepo.new
        FileUtils.rm_rf @repo.bare_consumer_repo_path

        Nsync.config.repo_url = @repo.repo_path
        Nsync.config.repo_path = @repo.bare_consumer_repo_path

        @consumer = Nsync::Consumer.new
      end

      should "not reclone it" do
        Grit::Git.any_instance.expects(:clone).never
        @consumer = Nsync::Consumer.new
      end
    end
        
    context "for a local repo" do
      setup do
        Nsync.config.repo_url = nil
      end

      context "that doesn't exist" do
        setup do
          @repo_path = "#{TMP_DIR}/nsync_non_existent_test_repo"
          FileUtils.rm_rf @repo_path
  
          Nsync.config.repo_path = @repo_path
        end
  
        should "raise a NoSuchPath error" do
          assert_raise Grit::NoSuchPathError do
            Nsync::Consumer.new
          end
        end
      end

      context "that does exist" do
        setup do
          @repo = TestRepo.new
          Nsync.config.repo_path = @repo.repo_path
          @consumer = Nsync::Consumer.new
        end

        should "assign it to repo" do
          assert @consumer.repo.is_a? Grit::Repo
        end
      end 
    end

    context "for a remote repo" do
      context "where the origin does not exist" do
        setup do
          @repo_url = "#{TMP_DIR}/nsync_non_existent_test_repo"
          @repo_path = "#{TMP_DIR}/nsync_non_existent_test_repo_consumer.git"
          FileUtils.rm_rf @repo_url
          FileUtils.rm_rf @repo_path

          Nsync.config.repo_url = @repo_url
          Nsync.config.repo_path = @repo_path
        end

        should "raise a NoSuchPath error" do
          assert_raise Grit::NoSuchPathError do
            Nsync::Consumer.new
          end
        end
      end

      [['local disk', nil], 
        ['github', 'git://github.com/schleyfox/test_repo.git']].each do |name, url|

        context "where the origin does exist on #{name}" do
          setup do
            @repo = TestRepo.new
            FileUtils.rm_rf @repo.bare_consumer_repo_path
  
            Nsync.config.repo_url = url || @repo.repo_path
            Nsync.config.repo_path = @repo.bare_consumer_repo_path
  
            @consumer = Nsync::Consumer.new
          end
  
          should "assign repo to a new bare repo" do
            assert @consumer.repo.is_a? Grit::Repo
            assert @consumer.repo.bare
          end
  
          should "set the origin remote" do
            p @consumer.remotes
            origin = @consumer.remotes.detect{|r| r[0] == "origin" && r[2] == "(fetch)" }
            assert origin
            assert_equal Nsync.config.repo_url, origin[1]
          end
        end
      end  
    end
  end

  context "Updating the Consumer side" do
    setup do
      @repo = TestRepo.new
      Nsync.config.version_manager.stubs(:version => "HEAD^1")
      FileUtils.rm_rf @repo.bare_consumer_repo_path
    end

    context "when local" do<commit_msg>removed a debug line<commit_after>require File.join(File.dirname(__FILE__), "helper")

class NsyncConsumerTest < Test::Unit::TestCase
  def setup
    Nsync.reset_config
    #Grit.debug = true
    @lock_file = "#{TMP_DIR}/nsync_test_#{Process.pid}.lock"
    FileUtils.rm @lock_file, :force => true
    Nsync.config.lock_file = @lock_file

    Nsync.config.version_manager = stub(:version => "foo", :previous_version => "bar")
    Nsync.config.version_manager.stubs(:version=).with(is_a(String))

    # I think mocha is doing something evil and redefing clone
    if Grit::Git.instance_methods.include?("clone")
      Grit::Git.send(:undef_method, :clone)
    end
  end

  context "Initializing Consumer" do
    context "when the repo already exists" do
      setup do
        @repo = TestRepo.new
        FileUtils.rm_rf @repo.bare_consumer_repo_path

        Nsync.config.repo_url = @repo.repo_path
        Nsync.config.repo_path = @repo.bare_consumer_repo_path

        @consumer = Nsync::Consumer.new
      end

      should "not reclone it" do
        Grit::Git.any_instance.expects(:clone).never
        @consumer = Nsync::Consumer.new
      end
    end
        
    context "for a local repo" do
      setup do
        Nsync.config.repo_url = nil
      end

      context "that doesn't exist" do
        setup do
          @repo_path = "#{TMP_DIR}/nsync_non_existent_test_repo"
          FileUtils.rm_rf @repo_path
  
          Nsync.config.repo_path = @repo_path
        end
  
        should "raise a NoSuchPath error" do
          assert_raise Grit::NoSuchPathError do
            Nsync::Consumer.new
          end
        end
      end

      context "that does exist" do
        setup do
          @repo = TestRepo.new
          Nsync.config.repo_path = @repo.repo_path
          @consumer = Nsync::Consumer.new
        end

        should "assign it to repo" do
          assert @consumer.repo.is_a? Grit::Repo
        end
      end 
    end

    context "for a remote repo" do
      context "where the origin does not exist" do
        setup do
          @repo_url = "#{TMP_DIR}/nsync_non_existent_test_repo"
          @repo_path = "#{TMP_DIR}/nsync_non_existent_test_repo_consumer.git"
          FileUtils.rm_rf @repo_url
          FileUtils.rm_rf @repo_path

          Nsync.config.repo_url = @repo_url
          Nsync.config.repo_path = @repo_path
        end

        should "raise a NoSuchPath error" do
          assert_raise Grit::NoSuchPathError do
            Nsync::Consumer.new
          end
        end
      end

      [['local disk', nil], 
        ['github', 'git://github.com/schleyfox/test_repo.git']].each do |name, url|

        context "where the origin does exist on #{name}" do
          setup do
            @repo = TestRepo.new
            FileUtils.rm_rf @repo.bare_consumer_repo_path
  
            Nsync.config.repo_url = url || @repo.repo_path
            Nsync.config.repo_path = @repo.bare_consumer_repo_path
  
            @consumer = Nsync::Consumer.new
          end
  
          should "assign repo to a new bare repo" do
            assert @consumer.repo.is_a? Grit::Repo
            assert @consumer.repo.bare
          end
  
          should "set the origin remote" do
            origin = @consumer.remotes.detect{|r| r[0] == "origin" && r[2] == "(fetch)" }
            assert origin
            assert_equal Nsync.config.repo_url, origin[1]
          end
        end
      end  
    end
  end

  context "Updating the Consumer side" do
    setup do
      @repo = TestRepo.new
      Nsync.config.version_manager.stubs(:version => "HEAD^1")
      FileUtils.rm_rf @repo.bare_consumer_repo_path
    end

    context "when local" do
{'fraction_non_alphanumeric': 0.09210348096979548, 'fraction_numerical': 0.001083570364350535, 'mean_word_length': 2.0753852561432735, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6172627', 'n_tokens_mistral': 2341, 'n_tokens_neox': 2266, 'n_words': 540}