<commit_before>require "testing_env"
require "keg"
require "stringio"

class LinkTests < Homebrew::TestCase
  include FileUtils

  def setup
    keg = HOMEBREW_CELLAR.join("foo", "1.0")
    keg.join("bin").mkpath

    %w[hiworld helloworld goodbye_cruel_world].each do |file|
      touch keg.join("bin", file)
    end

    @keg = Keg.new(keg)
    @dst = HOMEBREW_PREFIX.join("bin", "helloworld")
    @nonexistent = Pathname.new("/some/nonexistent/path")

    @mode = OpenStruct.new

    @old_stdout = $stdout
    $stdout = StringIO.new

    mkpath HOMEBREW_PREFIX/"bin"
    mkpath HOMEBREW_PREFIX/"lib"
  end

  def teardown
    @keg.unlink
    @keg.uninstall

    $stdout = @old_stdout

    rmtree HOMEBREW_PREFIX/"bin"
    rmtree HOMEBREW_PREFIX/"lib"
  end

  def test_linking_keg
    assert_equal 3, @keg.link
    (HOMEBREW_PREFIX/"bin").children.each { |c| assert_predicate c.readlink, :relative? }
  end

  def test_unlinking_keg
    @keg.link
    assert_predicate @dst, :symlink?
    assert_equal 4, @keg.unlink
    refute_predicate @dst, :symlink?
  end

  def test_link_dry_run
    @mode.dry_run = true

    assert_equal 0, @keg.link(@mode)<commit_msg>test_keg: add tests for oldname optlink<commit_after>require "testing_env"
require "keg"
require "stringio"

class LinkTests < Homebrew::TestCase
  include FileUtils

  def setup
    keg = HOMEBREW_CELLAR.join("foo", "1.0")
    keg.join("bin").mkpath

    %w[hiworld helloworld goodbye_cruel_world].each do |file|
      touch keg.join("bin", file)
    end

    @keg = Keg.new(keg)
    @dst = HOMEBREW_PREFIX.join("bin", "helloworld")
    @nonexistent = Pathname.new("/some/nonexistent/path")

    @mode = OpenStruct.new

    @old_stdout = $stdout
    $stdout = StringIO.new

    mkpath HOMEBREW_PREFIX/"bin"
    mkpath HOMEBREW_PREFIX/"lib"
  end

  def teardown
    @keg.unlink
    @keg.uninstall

    $stdout = @old_stdout

    rmtree HOMEBREW_PREFIX/"bin"
    rmtree HOMEBREW_PREFIX/"lib"
  end

  def test_linking_keg
    assert_equal 3, @keg.link
    (HOMEBREW_PREFIX/"bin").children.each { |c| assert_predicate c.readlink, :relative? }
  end

  def test_unlinking_keg
    @keg.link
    assert_predicate @dst, :symlink?
    assert_equal 4, @keg.unlink
    refute_predicate @dst, :symlink?
  end

  def test_oldname_opt_record
    assert_nil @keg.oldname_opt_record
    oldname_opt_record = HOMEBREW_PREFIX/"opt/oldfoo"
    oldname_opt_record.make_relative_symlink(HOMEBREW_CELLAR/"foo/1.0")
    assert_equal oldname_opt_record, @keg.oldname_opt_record
  end

  def test_optlink_relink
    oldname_opt_record = HOMEBREW_PREFIX/"opt/oldfoo"
    oldname_opt_record.make_relative_symlink(HOMEBREW_CELLAR/"foo/1.0")
    keg_record = HOMEBREW_CELLAR.join("foo", "2.0")
    keg_record.join("bin").mkpath
    keg = Keg.new(keg_record)
    keg.optlink
    assert_equal keg_record, oldname_opt_record.resolved_path
    keg.uninstall
    refute_predicate oldname_opt_record, :symlink?
  end

  def test_remove_oldname_opt_record
    oldname_opt_record = HOMEBREW_PREFIX/"opt/oldfoo"
    oldname_opt_record.make_relative_symlink(HOMEBREW_CELLAR/"foo/2.0")
    @keg.remove_oldname_opt_record
    assert_predicate oldname_opt_record, :symlink?
    oldname_opt_record.unlink
    oldname_opt_record.make_relative_symlink(HOMEBREW_CELLAR/"foo/1.0")
    @keg.remove_oldname_opt_record
    refute_predicate oldname_opt_record, :symlink?
  end

  def test_link_dry_run
    @mode.dry_run = true

    assert_equal 0, @keg.link(@mode)
{'fraction_non_alphanumeric': 0.10267471958584987, 'fraction_numerical': 0.005752085130859937, 'mean_word_length': 3.803867403314917, 'pattern_counts': {'":': 0, '<': 5, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2342399', 'n_tokens_mistral': 1401, 'n_tokens_neox': 1310, 'n_words': 223}