<commit_before># frozen_string_literal: true

require "utils/git"

describe Git do
  before do
    git = HOMEBREW_SHIMS_PATH/"scm/git"

    HOMEBREW_CACHE.cd do
      system git, "init"

      File.open("blah.rb", "w") { |f| f.write("blah") }
      system git, "add", HOMEBREW_CACHE/"blah.rb"
      system git, "commit", "-m", "'File added'"
      @h1 = `git rev-parse HEAD`

      File.open("blah.rb", "w") { |f| f.write("brew") }
      system git, "add", HOMEBREW_CACHE/"blah.rb"
      system git, "commit", "-m", "'written to File'"
      @h2 = `git rev-parse HEAD`

      File.open("bleh.rb", "w") { |f| f.write("bleh") }
      system git, "add", HOMEBREW_CACHE/"bleh.rb"
      system git, "commit", "-m", "'File added'"
      @h3 = `git rev-parse HEAD`

      File.open("bleh.rb", "w") { |f| f.write("blehbleh") }
      system git, "add", HOMEBREW_CACHE/"bleh.rb"
      system git, "commit", "-m", "'written to File'"
    end
  end

  let(:file) { "blah.rb" }
  let(:file_hash1) { @h1[0..6] }
  let(:file_hash2) { @h2[0..6] }
  let(:files) { ["blah.rb", "bleh.rb"] }
  let(:files_hash1) { [@h3[0..6], ["bleh.rb"]] }
  let(:files_hash2) { [@h2[0..6], ["blah.rb"]] }

  describe "#last_revision_commit_of_file" do
    it "gives last revision commit when before_commit is nil" do
      expect(
        described_class.last_revision_commit_of_file(HOMEBREW_CACHE, file),
      ).to eq(file_hash1)
    end

    it "gives revision commit based on before_commit when it is not nil" do
      expect(
        described_class.last_revision_commit_of_file(HOMEBREW_CACHE,
                                                     file,
                                                     before_commit: file_hash2),
      ).to eq(file_hash2)
    end
  end

  describe "#last_revision_commit_of_files" do
    context "when before_commit is nil" do
      it "gives last revision commit" do
        expect(
          described_class.last_revision_commit_of_files(HOMEBREW_CACHE, files),
        ).to eq(files_hash1)
      end
    end

    context "when before_commit is not nil" do
      it "gives last revision commit" do
        expect(
          described_class.last_revision_commit_of_files(HOMEBREW_CACHE,
                                                        files,
                                                        before_commit: file_hash2),
        ).to eq(files_hash2)
      end
    end
  end

  describe "#last_revision_of_file" do
    it "returns last revision of file" do
      expect(
        described_class.last_revision_of_file(HOMEBREW_CACHE,
                                              HOMEBREW_CACHE/file),
      ).to eq("blah")
    end

    it "returns last revision of file based on before_commit" do
      expect(
        described_class.last_revision_of_file(HOMEBREW_CACHE, HOMEBREW_CACHE/file,
                                              before_commit: "0..3"),
      ).to eq("brew")
    end
  end
end

describe Utils do<commit_msg>utils/git_spec: Make example files easier to reason about<commit_after># frozen_string_literal: true

require "utils/git"

describe Git do
  before do
    git = HOMEBREW_SHIMS_PATH/"scm/git"

    HOMEBREW_CACHE.cd do
      system git, "init"

      File.open("README.md", "w") { |f| f.write("README") }
      system git, "add", HOMEBREW_CACHE/"README.md"
      system git, "commit", "-m", "'File added'"
      @h1 = `git rev-parse HEAD`

      File.open("README.md", "w") { |f| f.write("# README") }
      system git, "add", HOMEBREW_CACHE/"README.md"
      system git, "commit", "-m", "'written to File'"
      @h2 = `git rev-parse HEAD`

      File.open("LICENSE.txt", "w") { |f| f.write("LICENCE") }
      system git, "add", HOMEBREW_CACHE/"LICENSE.txt"
      system git, "commit", "-m", "'File added'"
      @h3 = `git rev-parse HEAD`

      File.open("LICENSE.txt", "w") { |f| f.write("LICENSE") }
      system git, "add", HOMEBREW_CACHE/"LICENSE.txt"
      system git, "commit", "-m", "'written to File'"
    end
  end

  let(:file) { "README.md" }
  let(:file_hash1) { @h1[0..6] }
  let(:file_hash2) { @h2[0..6] }
  let(:files) { ["README.md", "LICENSE.txt"] }
  let(:files_hash1) { [@h3[0..6], ["LICENSE.txt"]] }
  let(:files_hash2) { [@h2[0..6], ["README.md"]] }

  describe "#last_revision_commit_of_file" do
    it "gives last revision commit when before_commit is nil" do
      expect(
        described_class.last_revision_commit_of_file(HOMEBREW_CACHE, file),
      ).to eq(file_hash1)
    end

    it "gives revision commit based on before_commit when it is not nil" do
      expect(
        described_class.last_revision_commit_of_file(HOMEBREW_CACHE,
                                                     file,
                                                     before_commit: file_hash2),
      ).to eq(file_hash2)
    end
  end

  describe "#last_revision_commit_of_files" do
    context "when before_commit is nil" do
      it "gives last revision commit" do
        expect(
          described_class.last_revision_commit_of_files(HOMEBREW_CACHE, files),
        ).to eq(files_hash1)
      end
    end

    context "when before_commit is not nil" do
      it "gives last revision commit" do
        expect(
          described_class.last_revision_commit_of_files(HOMEBREW_CACHE,
                                                        files,
                                                        before_commit: file_hash2),
        ).to eq(files_hash2)
      end
    end
  end

  describe "#last_revision_of_file" do
    it "returns last revision of file" do
      expect(
        described_class.last_revision_of_file(HOMEBREW_CACHE,
                                              HOMEBREW_CACHE/file),
      ).to eq("README")
    end

    it "returns last revision of file based on before_commit" do
      expect(
        described_class.last_revision_of_file(HOMEBREW_CACHE, HOMEBREW_CACHE/file,
                                              before_commit: "0..3"),
      ).to eq("# README")
    end
  end
end

describe Utils do
{'fraction_non_alphanumeric': 0.12146422628951747, 'fraction_numerical': 0.008985024958402662, 'mean_word_length': 2.059033078880407, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 24, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '961162', 'n_tokens_mistral': 2000, 'n_tokens_neox': 1918, 'n_words': 466}