# frozen_string_literal: true

require "test_helper"

class TestRbSys < Minitest::Test
  def test_that_it_has_a_version_number
    refute_nil ::RbSys::VERSION
  end

  def test_includes_rbconfig_env
    makefile = create_makefile

    assert_match(/RBCONFIG_/, makefile.read)
  end

  def test_invokes_cargo_rustc
    makefile = create_makefile

    assert_match(/\$\(CARGO\) rustc/, makefile.read)
  end

  def test_invokes_custom_env
    skip("Skipping for mswin") if win_target?

    makefile = create_makefile do |b|
      b.env = {"NO_LINK_RUTIE" => "true"}
    end

    assert_match(/export NO_LINK_RUTIE := true/, makefile.read)
  end

  def test_doesnt_contain_blocked_link_args
    makefile = create_makefile
    lines = makefile.read.split("\n")
    cargo_command = lines.find { |l| l.include? "rustc" }

    assert !cargo_command.include?("compress-debug-sections")
  end

  def test_uses_custom_profile
    makefile = create_makefile do |b|
      b.profile = :dev
    end

    assert(makefile.read.include?("RB_SYS_CARGO_PROFILE ?= dev"), "Expected to find RB_SYS_CARGO_PROFILE ?= dev") unless win_target?
    assert_match(/--profile \$\(RB_SYS_CARGO_PROFILE\)/, makefile.read)
  end

  def test_uses_extra_features
    makefile = create_makefile do |b|
      b.features = ["foo", "bar"]
    end

    assert(makefile.read.include?("RB_SYS_CARGO_FEATURES ?= foo,bar"), "Expected to find RB_SYS_CARGO_PROFILE ?= foo,bar") unless win_target?
    assert_match(/--features \$\(RB_SYS_CARGO_FEATURES\)/, makefile.read)
  end

  def test_uses_extra_rustc_args
    makefile = create_makefile do |b|
      b.extra_rustc_args = ["-C", "debuginfo=42"]
    end

    assert_match(/-C debuginfo=42$/, makefile.read)
  end

  def test_installs_extra_rustup_targets
    makefile = create_makefile do |b|
      b.extra_rustup_targets = ["wasm32-unknown-unknown", "wasm32-wasi-unknown"]
    end

    makefile_output = makefile.read

    assert_match(/rustup target add wasm32-unknown-unknown$/, makefile_output)
    assert_match(/rustup target add wasm32-wasi-unknown$/, makefile_output)
  end

  def test_uses_extra_cargo_args
    makefile = create_makefile do |b|
      b.extra_cargo_args = ["--crate-type", "cdylib"]
    end

    assert_match(/--crate-type cdylib/, makefile.read)
    assert_includes(makefile.read, "rustc $(RB_SYS_EXTRA_CARGO_ARGS)")
  end

  def test_uses_extra_rustflags
    skip("Skipping for mswin") if win_target?

    makefile = create_makefile do |b|
      b.extra_rustflags = ["--cfg=foo"]
    end

    content = makefile.read

    assert content.include?("export RUSTFLAGS := $(RB_SYS_GLOBAL_RUSTFLAGS) $(RB_SYS_EXTRA_RUSTFLAGS) $(RUSTFLAGS)")
    assert content.include?("RB_SYS_EXTRA_RUSTFLAGS ?= --cfg=foo")
  end

  def test_uses_custom_target
    makefile = create_makefile do |b|
      b.target = "wasm32-unknown-unknown"
    end

    assert_match(/CARGO_BUILD_TARGET \?= wasm32-unknown-unknown/, makefile.read) unless win_target?
    assert_match(/--target \$\(CARGO_BUILD_TARGET\)/, makefile.read)
  end

  private

  def create_makefile(&blk)
    require "mkmf"
    require "rb_sys/mkmf"
    cargo_dir = Dir.mktmpdir("rb_sys_test")

    Dir.chdir(cargo_dir) do
      create_rust_makefile("foo_ext", &blk)
      Pathname.new(File.join(cargo_dir, "Makefile"))
    end
  end
end
