<commit_before>require "testing_env"

class TapTest < Homebrew::TestCase
  include FileUtils

  def setup
    @path = Tap::TAP_DIRECTORY/"homebrew/homebrew-foo"
    @path.mkpath
    @tap = Tap.new("Homebrew", "foo")
  end

  def setup_tap_files
    @formula_file = @path/"Formula/foo.rb"
    @formula_file.write <<-EOS.undent
      class Foo < Formula
        url "https://example.com/foo-1.0.tar.gz"
      end
    EOS
    @alias_file = @path/"Aliases/bar"
    @alias_file.parent.mkpath
    ln_s @formula_file, @alias_file
    (@path/"formula_renames.json").write <<-EOS.undent
     { "oldname": "foo" }
    EOS
    (@path/"tap_migrations.json").write <<-EOS.undent
     { "removed-formula": "homebrew/foo" }
    EOS
    @cmd_file = @path/"cmd/brew-tap-cmd.rb"
    @cmd_file.parent.mkpath
    touch @cmd_file
    chmod 0755, @cmd_file
    @manpage_file = @path/"man/man1/brew-tap-cmd.1"
    @manpage_file.parent.mkpath
    touch @manpage_file
  end

  def setup_git_repo
    env = ENV.to_hash
    %w[AUTHOR COMMITTER].each do |role|
      ENV["GIT_#{role}_NAME"] = "brew tests"
      ENV["GIT_#{role}_EMAIL"] = "brew-tests@localhost"
      ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100"
    end

    @path.cd do
      shutup do
        system "git", "init"
        system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo"
        system "git", "add", "--all"
        system "git", "commit", "-m", "init"
      end
    end
  ensure
    ENV.replace(env)
  end

  def teardown
    @path.rmtree
  end

  def test_fetch
    assert_kind_of CoreTap, Tap.fetch("Homebrew", "homebrew")
    tap = Tap.fetch("Homebrew", "foo")
    assert_kind_of Tap, tap
    assert_equal "homebrew/foo", tap.name
  ensure
    Tap.clear_cache
  end

  def test_names
    assert_equal ["homebrew/core", "homebrew/foo"], Tap.names
  end

  def test_attributes
    assert_equal "Homebrew", @tap.user
    assert_equal "foo", @tap.repo
    assert_equal "homebrew/foo", @tap.name
    assert_equal @path, @tap.path
    assert_predicate @tap, :installed?
    assert_predicate @tap, :official?
    refute_predicate @tap, :core_tap?
  end

  def test_issues_url
    t = Tap.new("someone", "foo")
    path = Tap::TAP_DIRECTORY/"someone/homebrew-foo"
    path.mkpath
    cd path do
      shutup { system "git", "init" }
      system "git", "remote", "add", "origin",
        "https://github.com/someone/homebrew-foo"<commit_msg>tap: test fetch/validation of invalid tap names<commit_after>require "testing_env"

class TapTest < Homebrew::TestCase
  include FileUtils

  def setup
    @path = Tap::TAP_DIRECTORY/"homebrew/homebrew-foo"
    @path.mkpath
    @tap = Tap.new("Homebrew", "foo")
  end

  def setup_tap_files
    @formula_file = @path/"Formula/foo.rb"
    @formula_file.write <<-EOS.undent
      class Foo < Formula
        url "https://example.com/foo-1.0.tar.gz"
      end
    EOS
    @alias_file = @path/"Aliases/bar"
    @alias_file.parent.mkpath
    ln_s @formula_file, @alias_file
    (@path/"formula_renames.json").write <<-EOS.undent
     { "oldname": "foo" }
    EOS
    (@path/"tap_migrations.json").write <<-EOS.undent
     { "removed-formula": "homebrew/foo" }
    EOS
    @cmd_file = @path/"cmd/brew-tap-cmd.rb"
    @cmd_file.parent.mkpath
    touch @cmd_file
    chmod 0755, @cmd_file
    @manpage_file = @path/"man/man1/brew-tap-cmd.1"
    @manpage_file.parent.mkpath
    touch @manpage_file
  end

  def setup_git_repo
    env = ENV.to_hash
    %w[AUTHOR COMMITTER].each do |role|
      ENV["GIT_#{role}_NAME"] = "brew tests"
      ENV["GIT_#{role}_EMAIL"] = "<EMAIL>"
      ENV["GIT_#{role}_DATE"] = "Thu May 21 00:04:11 2009 +0100"
    end

    @path.cd do
      shutup do
        system "git", "init"
        system "git", "remote", "add", "origin", "https://github.com/Homebrew/homebrew-foo"
        system "git", "add", "--all"
        system "git", "commit", "-m", "init"
      end
    end
  ensure
    ENV.replace(env)
  end

  def teardown
    @path.rmtree
  end

  def test_fetch
    assert_kind_of CoreTap, Tap.fetch("Homebrew", "homebrew")
    tap = Tap.fetch("Homebrew", "foo")
    assert_kind_of Tap, tap
    assert_equal "homebrew/foo", tap.name

    assert_match "Invalid tap name",
                 assert_raises { Tap.fetch("foo") }.message
    assert_match "Invalid tap name",
                 assert_raises { Tap.fetch("homebrew/homebrew/bar") }.message
    assert_match "Invalid tap name",
                 assert_raises { Tap.fetch("homebrew", "homebrew/baz") }.message
  ensure
    Tap.clear_cache
  end

  def test_names
    assert_equal ["homebrew/core", "homebrew/foo"], Tap.names
  end

  def test_attributes
    assert_equal "Homebrew", @tap.user
    assert_equal "foo", @tap.repo
    assert_equal "homebrew/foo", @tap.name
    assert_equal @path, @tap.path
    assert_predicate @tap, :installed?
    assert_predicate @tap, :official?
    refute_predicate @tap, :core_tap?
  end

  def test_issues_url
    t = Tap.new("someone", "foo")
    path = Tap::TAP_DIRECTORY/"someone/homebrew-foo"
    path.mkpath
    cd path do
      shutup { system "git", "init" }
      system "git", "remote", "add", "origin",
        "https://github.com/someone/homebrew-foo"
{'fraction_non_alphanumeric': 0.14271993853246254, 'fraction_numerical': 0.009220130618517095, 'mean_word_length': 2.8484848484848486, 'pattern_counts': {'":': 4, '<': 20, '<?xml version=': 0, '>': 4, 'https://': 6, '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': '5695329', 'n_tokens_mistral': 1994, 'n_tokens_neox': 1869, 'n_words': 389}