<commit_before>require "unindent"

describe "CLI" do
  before :all do
    # use local scripts
    ENV["PATH"] = "#{File.join(File.dirname(__FILE__),"..","bin")}:#{ENV["PATH"]}"
  end

  def run(command, options={})
    result = `#{command}`
    raise "FAILED #{command} : #{result}" if $?.success? == !!options[:fail]
    result
  end

  def write(file, content)
    File.open(file, 'w'){|f| f.write content }
  end

  around do |example|
    dir = "spec/tmp"
    run "rm -rf #{dir}"
    run "mkdir #{dir}"

    # use fake home for .ssh hacks
    run "mkdir #{dir}/home"
    ENV["HOME"] = File.expand_path("#{dir}/home")

    Dir.chdir dir do
      run "touch a && git init && git add . && git commit -am 'initial'"
      example.run
    end
  end

  describe "about" do
    it "lists the user" do
      run "git config user.name NAME"
      run("git about").should =~ /git user:\s+NAME/
    end

    it "lists the user as NONE if there is none" do
      run "git config user.name ''"
      run("git about").should =~ /git user:\s+NONE/
    end

    it "lists the email" do
      run "git config user.email EMAIL"
      run("git about").should =~ /git email:\s+EMAIL/
    end

    it "lists the email as NONE if there is none" do
      run "git config user.email ''"
      run("git about").should =~ /git email:\s+NONE/
    end

    it "does not find a project" do
      run("git about").should =~ /GitHub project:\s+NONE/
    end

    context "with github project" do
      before do
        run "mkdir home/.ssh"
        run "touch home/.ssh/id_github_foo"
        run "ln -s home/.ssh/id_github_foo home/.ssh/id_github_current"
      end

      it "finds a project" do
        run("git about").should =~ /GitHub project:\s+foo/
      end
    end
  end

  describe "pair" do
    def expect_config(result, name, initials, email, options={})
      global = "cd /tmp && " if options[:global]
      run("#{global}git config user.name").should == "#{name}\n"
      run("#{global}git config user.initials").should == "#{initials}\n"
      run("#{global}git config user.email").should == "#{email}\n"

      prefix = (options[:global] ? "global: " : "local:  ")
      result.should include "#{prefix}user.name #{name}"
      result.should include "#{prefix}user.initials #{initials}"
      result.should include "#{prefix}user.email #{email}"
    end

    it "prints help" do
      result = run "git-pair --help"
      result.should include("Configures git authors when pair programming")
    end

    it "prints version" do
      result = run "git pair --version"
      result.should =~ /\d+\.\d+\.\d+/
    end

    context "with .pairs file" do
      before do
        write ".pairs", <<-YAML.unindent
          pairs:
            ab: Aa Bb
            bc: Bb Cc
            cd: Cc Dd

          email:
            prefix: the-pair
            domain: the-host.com
          YAML
      end

      describe "global" do
        it "sets pairs globally when global: true is set" do
          write ".pairs", File.read(".pairs") + "\nglobal: true"
          result = run "git pair ab"
          expect_config result, "Aa Bb", "ab", "<EMAIL>", :global => true
        end

        it "sets pairs globally when --global is given" do
          result = run "git pair ab --global"
          result.should include "global: user.name Aa Bb"
          expect_config result, "Aa Bb", "ab", "<EMAIL>", :global => true
        end

        it "unsets global config when no argument is passed" do
          run "git pair ab --global"
          run "git pair ab"
          result = run "git pair --global"
          #result.should include "Unset --global user.name, user.email and user.initials"
          expect_config result, "Aa Bb", "ab", "<EMAIL>"
          result.should_not include("global:")
        end
      end

      it "can set a single user as pair" do
        result = run "git pair ab"
        expect_config result, "Aa Bb", "ab", "<EMAIL>"
      end

      it "can set a 2 users as pair" do
        result = run "git pair ab bc"
        expect_config result, "Aa Bb & Bb Cc", "ab bc", "<EMAIL>"
      end

      it "can set a n users as pair" do
        result = run "git pair ab bc cd"
        expect_config result, "Aa Bb, Bb Cc & Cc Dd", "ab bc cd", "<EMAIL>"
      end

      it "fails when there is no .git in the tree" do
        run "rm -f /tmp/pairs"
        run "cp .pairs /tmp"
        Dir.chdir "/tmp" do
          result = run "git pair ab 2>&1", :fail => true
          result.should include("Not a git repository (or any of the parent directories)")
        end
        run "rm -f /tmp/pairs"
      end

      it "finds .pairs file in lower parent folder" do
        run "mkdir foo"
        Dir.chdir "foo" do
          result = run "git pair ab"
          expect_config result, "Aa Bb", "ab", "<EMAIL>"
        end
      end

      it "unsets local config when no argument is passed" do
        run "git pair ab --global"
        run "git pair bc"
        result = run "git pair"
        result.should include "Unset user.name, user.email, user.initials"
        expect_config result, "Aa Bb", "ab", "<EMAIL>", :global => true
        result.should_not include("local:")
      end

      it "uses hard email when given" do
        write ".pairs", File.read(".pairs").sub(/email:.*/m, "email: <EMAIL>")
        result = run "git pair ab"
        expect_config result, "Aa Bb", "ab", "<EMAIL>"
      end

      it "uses no email prefix when only host is given" do
        write ".pairs", File.read(".pairs").sub(/email:.*/m, "email:\n  domain: foo.com")
        result = run "git pair ab"
        expect_config result, "Aa Bb", "ab", "<EMAIL>"
      end

      it "fails with unknown initials" do
        result = run "git pair xx", :fail => true
        result.should include("Couldn't find author name for initials: xx")
      end

      it "uses alternate email prefix" do
        write ".pairs", File.read(".pairs").sub(/ab:.*/, "ab: Aa Bb; blob")
        result = run "git pair ab"
        expect_config result, "Aa Bb", "ab", "the-pair+blob@the-host.com"
      end
    end

    context "without a .pairs file in the tree" do
      around do |example|
        Dir.chdir "/tmp" do
          run "rm -f .pairs"
          dir = "git_stats_test"
          run "rm -rf #{dir}"
          run "mkdir #{dir}"
          Dir.chdir dir do
            run "git init"
            example.run
          end
          run "rm -rf #{dir}"
        end
      end

      it "fails if it cannot find a pairs file" do
        run "git pair ab", :fail => true
      end

      it "prints instructions" do
        result = run "git pair ab", :fail => true
        result.should include("Could not find a .pairs file. Create a YAML file in your project or home directory.")
      end
    end
  end
end
<commit_msg>test if users with apostrophes are supported<commit_after>require "unindent"

describe "CLI" do
  before :all do
    # use local scripts
    ENV["PATH"] = "#{File.join(File.dirname(__FILE__),"..","bin")}:#{ENV["PATH"]}"
  end

  def run(command, options={})
    result = `#{command}`
    raise "FAILED #{command} : #{result}" if $?.success? == !!options[:fail]
    result
  end

  def write(file, content)
    File.open(file, 'w'){|f| f.write content }
  end

  around do |example|
    dir = "spec/tmp"
    run "rm -rf #{dir}"
    run "mkdir #{dir}"

    # use fake home for .ssh hacks
    run "mkdir #{dir}/home"
    ENV["HOME"] = File.expand_path("#{dir}/home")

    Dir.chdir dir do
      run "touch a && git init && git add . && git commit -am 'initial'"
      example.run
    end
  end

  describe "about" do
    it "lists the user" do
      run "git config user.name NAME"
      run("git about").should =~ /git user:\s+NAME/
    end

    it "lists the user as NONE if there is none" do
      run "git config user.name ''"
      run("git about").should =~ /git user:\s+NONE/
    end

    it "lists the email" do
      run "git config user.email EMAIL"
      run("git about").should =~ /git email:\s+EMAIL/
    end

    it "lists the email as NONE if there is none" do
      run "git config user.email ''"
      run("git about").should =~ /git email:\s+NONE/
    end

    it "does not find a project" do
      run("git about").should =~ /GitHub project:\s+NONE/
    end

    context "with github project" do
      before do
        run "mkdir home/.ssh"
        run "touch home/.ssh/id_github_foo"
        run "ln -s home/.ssh/id_github_foo home/.ssh/id_github_current"
      end

      it "finds a project" do
        run("git about").should =~ /GitHub project:\s+foo/
      end
    end
  end

  describe "pair" do
    def expect_config(result, name, initials, email, options={})
      global = "cd /tmp && " if options[:global]
      run("#{global}git config user.name").should == "#{name}\n"
      run("#{global}git config user.initials").should == "#{initials}\n"
      run("#{global}git config user.email").should == "#{email}\n"

      prefix = (options[:global] ? "global: " : "local:  ")
      result.should include "#{prefix}user.name #{name}"
      result.should include "#{prefix}user.initials #{initials}"
      result.should include "#{prefix}user.email #{email}"
    end

    it "prints help" do
      result = run "git-pair --help"
      result.should include("Configures git authors when pair programming")
    end

    it "prints version" do
      result = run "git pair --version"
      result.should =~ /\d+\.\d+\.\d+/
    end

    context "with .pairs file" do
      before do
        write ".pairs", <<-YAML.unindent
          pairs:
            ab: Aa Bb
            bc: Bb Cc
            cd: Cc Dd

          email:
            prefix: the-pair
            domain: the-host.com
          YAML
      end

      describe "global" do
        it "sets pairs globally when global: true is set" do
          write ".pairs", File.read(".pairs") + "\nglobal: true"
          result = run "git pair ab"
          expect_config result, "Aa Bb", "ab", "<EMAIL>", :global => true
        end

        it "sets pairs globally when --global is given" do
          result = run "git pair ab --global"
          result.should include "global: user.name Aa Bb"
          expect_config result, "Aa Bb", "ab", "<EMAIL>", :global => true
        end

        it "unsets global config when no argument is passed" do
          run "git pair ab --global"
          run "git pair ab"
          result = run "git pair --global"
          #result.should include "Unset --global user.name, user.email and user.initials"
          expect_config result, "Aa Bb", "ab", "<EMAIL>"
          result.should_not include("global:")
        end
      end

      it "can set a single user as pair" do
        result = run "git pair ab"
        expect_config result, "Aa Bb", "ab", "<EMAIL>"
      end

      it "can set a 2 users as pair" do
        result = run "git pair ab bc"
        expect_config result, "Aa Bb & Bb Cc", "ab bc", "<EMAIL>"
      end

      it "can set n users as pair" do
        result = run "git pair ab bc cd"
        expect_config result, "Aa Bb, Bb Cc & Cc Dd", "ab bc cd", "<EMAIL>"
      end

      it "can set a user with apostrophes as pair" do
        write ".pairs", File.read(".pairs").sub("Aa Bb", "<NAME>")
        result = run "git pair ab"
        expect_config result, "<NAME>", "ab", "<EMAIL>"
      end

      it "fails when there is no .git in the tree" do
        run "rm -f /tmp/pairs"
        run "cp .pairs /tmp"
        Dir.chdir "/tmp" do
          result = run "git pair ab 2>&1", :fail => true
          result.should include("Not a git repository (or any of the parent directories)")
        end
        run "rm -f /tmp/pairs"
      end

      it "finds .pairs file in lower parent folder" do
        run "mkdir foo"
        Dir.chdir "foo" do
          result = run "git pair ab"
          expect_config result, "Aa Bb", "ab", "<EMAIL>"
        end
      end

      it "unsets local config when no argument is passed" do
        run "git pair ab --global"
        run "git pair bc"
        result = run "git pair"
        result.should include "Unset user.name, user.email, user.initials"
        expect_config result, "Aa Bb", "ab", "<EMAIL>", :global => true
        result.should_not include("local:")
      end

      it "uses hard email when given" do
        write ".pairs", File.read(".pairs").sub(/email:.*/m, "email: <EMAIL>")
        result = run "git pair ab"
        expect_config result, "Aa Bb", "ab", "<EMAIL>"
      end

      it "uses no email prefix when only host is given" do
        write ".pairs", File.read(".pairs").sub(/email:.*/m, "email:\n  domain: foo.com")
        result = run "git pair ab"
        expect_config result, "Aa Bb", "ab", "<EMAIL>"
      end

      it "fails with unknown initials" do
        result = run "git pair xx", :fail => true
        result.should include("Couldn't find author name for initials: xx")
      end

      it "uses alternate email prefix" do
        write ".pairs", File.read(".pairs").sub(/ab:.*/, "ab: Aa Bb; blob")
        result = run "git pair ab"
        expect_config result, "Aa Bb", "ab", "the-pair+blob@the-host.com"
      end
    end

    context "without a .pairs file in the tree" do
      around do |example|
        Dir.chdir "/tmp" do
          run "rm -f .pairs"
          dir = "git_stats_test"
          run "rm -rf #{dir}"
          run "mkdir #{dir}"
          Dir.chdir dir do
            run "git init"
            example.run
          end
          run "rm -rf #{dir}"
        end
      end

      it "fails if it cannot find a pairs file" do
        run "git pair ab", :fail => true
      end

      it "prints instructions" do
        result = run "git pair ab", :fail => true
        result.should include("Could not find a .pairs file. Create a YAML file in your project or home directory.")
      end
    end
  end
end

{'fraction_non_alphanumeric': 0.13127524523946912, 'fraction_numerical': 0.00043277553375649163, 'mean_word_length': 1.9907247627264884, 'pattern_counts': {'":': 0, '<': 32, '<?xml version=': 0, '>': 44, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '6767155', 'n_tokens_mistral': 4427, 'n_tokens_neox': 4304, 'n_words': 1585}