<commit_before>require "rubygems/test_case"
require "rubygems/compiler"

class TestGemCompiler < Gem::TestCase
  def setup
    super

    @output_dir = File.join @tempdir, 'output'
    FileUtils.mkdir_p @output_dir
  end

  def test_compile_no_extensions
    gem_file = util_bake_gem

    compiler = Gem::Compiler.new(gem_file, :output => @output_dir)

    e = assert_raises Gem::Compiler::CompilerError do
      use_ui @ui do
        compiler.compile
      end
    end

    assert_equal "There are no extensions to build on this gem file.",
                  e.message
  end

  def test_compile_non_ruby
    gem_file = util_bake_gem { |s| s.platform = Gem::Platform::CURRENT }

    compiler = Gem::Compiler.new(gem_file, :output => @output_dir)

    e = assert_raises Gem::Compiler::CompilerError do
      use_ui @ui do
        compiler.compile
      end
    end

    assert_equal "The gem file seems to be compiled already.", e.message
  end

  def test_compile_succeed
    util_set_arch "i386-mingw32"

    gem_file = util_bake_gem { |spec|
      util_fake_extension spec
    }

    compiler = Gem::Compiler.new(gem_file, :output => @output_dir)

    use_ui @ui do
      compiler.compile
    end

    out = @ui.output.split "\n"

    assert_match %r|Unpacking gem: 'a-1' in temporary directory...|,
                  out.shift

    assert_path_exists File.join(@output_dir, "a-1-x86-mingw32.gem")
  end

  def test_compile_succeed_using_prune
    name = 'a'

    artifact = "#{name}.#{RbConfig::CONFIG["DLEXT"]}"
    old_spec = ''

    gem_file = util_bake_gem(name, 'ports/to_be_deleted_during_ext_build.patch') { |spec|
      old_spec = spec
      util_fake_extension spec, name, <<-EOF
        require 'fileutils'
        FileUtils.rm File.expand_path(File.join(File.dirname(__FILE__), '../../ports/to_be_deleted_during_ext_build.patch'))
        #{util_custom_configure(artifact)}
      EOF
    }

    compiler = Gem::Compiler.new(gem_file, :output => @output_dir, :prune => true)
    output_gem = nil

    use_ui @ui do
      output_gem = compiler.compile
    end

    assert_path_exists File.join(@output_dir, output_gem)
    actual_spec = util_read_spec File.join(@output_dir, output_gem)

    refute actual_spec.files.include? "ports/to_be_deleted_during_ext_build.patch"
  end

  def test_compile_bundle_artifacts
    util_reset_arch

    artifact = "foo.#{RbConfig::CONFIG["DLEXT"]}"

    gem_file = util_bake_gem("foo") { |s|
      util_fake_extension s, "foo", util_custom_configure(artifact)
    }

    compiler = Gem::Compiler.new(gem_file, :output => @output_dir)
    output_gem = nil

    use_ui @ui do
      output_gem = compiler.compile
    end

    assert_path_exists File.join(@output_dir, output_gem)
    spec = util_read_spec File.join(@output_dir, output_gem)

    assert_includes spec.files, "lib/#{artifact}"
  end

  # We need to check that tempdir paths that contain spaces as are handled
  # properly on Windows. In some cases, Dir.tmpdir may returned shortened
  # versions of these components, e.g.  "C:/Users/JOHNDO~1/AppData/Local/Temp"
  # for "C:/Users/<NAME>/AppData/Local/Temp".
  def test_compile_bundle_artifacts_path_with_spaces
    skip("only necessary to test on Windows") unless Gem.win_platform?
    old_tempdir = @tempdir
    old_output_dir = @output_dir

    old_tmp = ENV["TMP"]
    old_temp = ENV["TEMP"]
    old_tmpdir = ENV["TMPDIR"]

    # We want to make sure Dir.tmpdir returns the path containing "DIRWIT~1"
    # so that we're testing whether the compiler expands the path properly. To
    # do this, "dir with spaces" must not be the last path component.
    #
    # This is because Dir.tmpdir calls File.expand_path on ENV[TMPDIR] (or
    # ENV[TEMP], etc.). When "DIRWIT~1" is the last component,
    # File.expand_path will expand this to "dir with spaces". When it's not
    # the last component, it will leave "DIRWIT~1" as-is.
    @tempdir = File.join(@tempdir, "dir with spaces", "tmp")
    FileUtils.mkdir_p(@tempdir)
    @tempdir = File.join(old_tempdir, "DIRWIT~1", "tmp")

    @output_dir = File.join(@tempdir, "output")
    FileUtils.mkdir_p(@output_dir)

    ["TMP", "TEMP", "TMPDIR"].each { |varname| ENV[varname] = @tempdir }

    util_reset_arch

    artifact = "foo.#{RbConfig::CONFIG["DLEXT"]}"

    gem_file = util_bake_gem("foo") { |s|
      util_fake_extension s, "foo", util_custom_configure(artifact)
    }

    compiler = Gem::Compiler.new(gem_file, :output => @output_dir)
    output_gem = nil

    use_ui @ui do
      output_gem = compiler.compile
    end

    assert_path_exists File.join(@output_dir, output_gem)
    spec = util_read_spec File.join(@output_dir, output_gem)

    assert_includes spec.files, "lib/#{artifact}"
  ensure
    FileUtils.rm_rf @tempdir

    ENV["TMP"] = old_tmp
    ENV["TEMP"] = old_temp
    ENV["TMPDIR"] = old_tmpdir

    @tempdir = old_tempdir
    @output_dir = old_output_dir
  end<commit_msg>Avoid run ensure block of test on non Windows platform<commit_after>require "rubygems/test_case"
require "rubygems/compiler"

class TestGemCompiler < Gem::TestCase
  def setup
    super

    @output_dir = File.join @tempdir, 'output'
    FileUtils.mkdir_p @output_dir
  end

  def test_compile_no_extensions
    gem_file = util_bake_gem

    compiler = Gem::Compiler.new(gem_file, :output => @output_dir)

    e = assert_raises Gem::Compiler::CompilerError do
      use_ui @ui do
        compiler.compile
      end
    end

    assert_equal "There are no extensions to build on this gem file.",
                  e.message
  end

  def test_compile_non_ruby
    gem_file = util_bake_gem { |s| s.platform = Gem::Platform::CURRENT }

    compiler = Gem::Compiler.new(gem_file, :output => @output_dir)

    e = assert_raises Gem::Compiler::CompilerError do
      use_ui @ui do
        compiler.compile
      end
    end

    assert_equal "The gem file seems to be compiled already.", e.message
  end

  def test_compile_succeed
    util_set_arch "i386-mingw32"

    gem_file = util_bake_gem { |spec|
      util_fake_extension spec
    }

    compiler = Gem::Compiler.new(gem_file, :output => @output_dir)

    use_ui @ui do
      compiler.compile
    end

    out = @ui.output.split "\n"

    assert_match %r|Unpacking gem: 'a-1' in temporary directory...|,
                  out.shift

    assert_path_exists File.join(@output_dir, "a-1-x86-mingw32.gem")
  end

  def test_compile_succeed_using_prune
    name = 'a'

    artifact = "#{name}.#{RbConfig::CONFIG["DLEXT"]}"
    old_spec = ''

    gem_file = util_bake_gem(name, 'ports/to_be_deleted_during_ext_build.patch') { |spec|
      old_spec = spec
      util_fake_extension spec, name, <<-EOF
        require 'fileutils'
        FileUtils.rm File.expand_path(File.join(File.dirname(__FILE__), '../../ports/to_be_deleted_during_ext_build.patch'))
        #{util_custom_configure(artifact)}
      EOF
    }

    compiler = Gem::Compiler.new(gem_file, :output => @output_dir, :prune => true)
    output_gem = nil

    use_ui @ui do
      output_gem = compiler.compile
    end

    assert_path_exists File.join(@output_dir, output_gem)
    actual_spec = util_read_spec File.join(@output_dir, output_gem)

    refute actual_spec.files.include? "ports/to_be_deleted_during_ext_build.patch"
  end

  def test_compile_bundle_artifacts
    util_reset_arch

    artifact = "foo.#{RbConfig::CONFIG["DLEXT"]}"

    gem_file = util_bake_gem("foo") { |s|
      util_fake_extension s, "foo", util_custom_configure(artifact)
    }

    compiler = Gem::Compiler.new(gem_file, :output => @output_dir)
    output_gem = nil

    use_ui @ui do
      output_gem = compiler.compile
    end

    assert_path_exists File.join(@output_dir, output_gem)
    spec = util_read_spec File.join(@output_dir, output_gem)

    assert_includes spec.files, "lib/#{artifact}"
  end

  # We need to check that tempdir paths that contain spaces as are handled
  # properly on Windows. In some cases, Dir.tmpdir may returned shortened
  # versions of these components, e.g.  "C:/Users/JOHNDO~1/AppData/Local/Temp"
  # for "C:/Users/<NAME>/AppData/Local/Temp".
  def test_compile_bundle_artifacts_path_with_spaces
    skip("only necessary to test on Windows") unless Gem.win_platform?
    old_tempdir = @tempdir
    old_output_dir = @output_dir

    old_tmp = ENV["TMP"]
    old_temp = ENV["TEMP"]
    old_tmpdir = ENV["TMPDIR"]

    # We want to make sure Dir.tmpdir returns the path containing "DIRWIT~1"
    # so that we're testing whether the compiler expands the path properly. To
    # do this, "dir with spaces" must not be the last path component.
    #
    # This is because Dir.tmpdir calls File.expand_path on ENV[TMPDIR] (or
    # ENV[TEMP], etc.). When "DIRWIT~1" is the last component,
    # File.expand_path will expand this to "dir with spaces". When it's not
    # the last component, it will leave "DIRWIT~1" as-is.
    @tempdir = File.join(@tempdir, "dir with spaces", "tmp")
    FileUtils.mkdir_p(@tempdir)
    @tempdir = File.join(old_tempdir, "DIRWIT~1", "tmp")

    @output_dir = File.join(@tempdir, "output")
    FileUtils.mkdir_p(@output_dir)

    ["TMP", "TEMP", "TMPDIR"].each { |varname| ENV[varname] = @tempdir }

    util_reset_arch

    artifact = "foo.#{RbConfig::CONFIG["DLEXT"]}"

    gem_file = util_bake_gem("foo") { |s|
      util_fake_extension s, "foo", util_custom_configure(artifact)
    }

    compiler = Gem::Compiler.new(gem_file, :output => @output_dir)
    output_gem = nil

    use_ui @ui do
      output_gem = compiler.compile
    end

    assert_path_exists File.join(@output_dir, output_gem)
    spec = util_read_spec File.join(@output_dir, output_gem)

    assert_includes spec.files, "lib/#{artifact}"
  ensure
    return unless Gem.win_platform?

    FileUtils.rm_rf @tempdir

    ENV["TMP"] = old_tmp
    ENV["TEMP"] = old_temp
    ENV["TMPDIR"] = old_tmpdir

    @tempdir = old_tempdir
    @output_dir = old_output_dir
  end
{'fraction_non_alphanumeric': 0.10909090909090909, 'fraction_numerical': 0.0032323232323232323, 'mean_word_length': 3.317924116877453, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 19, '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': '801775', 'n_tokens_mistral': 3440, 'n_tokens_neox': 3312, 'n_words': 783}