summaryrefslogtreecommitdiff
path: root/test/rubygems/test_gem_ext_cmake_builder.rb
blob: b9b57084d4fd0dfc736a5d8ba0ea364a0735c8d3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# frozen_string_literal: true

require_relative "helper"
require "rubygems/ext"

class TestGemExtCmakeBuilder < Gem::TestCase
  def setup
    super

    # Details: https://github.com/ruby/rubygems/issues/1270#issuecomment-177368340
    pend "CmakeBuilder doesn't work on Windows." if Gem.win_platform?

    require "open3"

    begin
      _, status = Open3.capture2e("cmake")
      pend "cmake not present" unless status.success?
    rescue Errno::ENOENT
      pend "cmake not present"
    end

    @ext = File.join @tempdir, "ext"
    @dest_path = File.join @tempdir, "prefix"

    FileUtils.mkdir_p @ext
    FileUtils.mkdir_p @dest_path
  end

  def test_self_build
    File.open File.join(@ext, "CMakeLists.txt"), "w" do |cmakelists|
      cmakelists.write <<-EO_CMAKE
cmake_minimum_required(VERSION 3.26)
project(self_build NONE)
install (FILES test.txt DESTINATION bin)
      EO_CMAKE
    end

    FileUtils.touch File.join(@ext, "test.txt")

    output = []

    builder = Gem::Ext::CmakeBuilder.new
    builder.build nil, @dest_path, output, [], @dest_path, @ext

    output = output.join "\n"

    assert_match(/^current directory: #{Regexp.escape @ext}/, output)
    assert_match(/cmake.*-DCMAKE_RUNTIME_OUTPUT_DIRECTORY\\=#{Regexp.escape @dest_path}/, output)
    assert_match(/cmake.*-DCMAKE_LIBRARY_OUTPUT_DIRECTORY\\=#{Regexp.escape @dest_path}/, output)
    assert_match(/#{Regexp.escape @ext}/, output)
  end

  def test_self_build_presets
    File.open File.join(@ext, "CMakeLists.txt"), "w" do |cmakelists|
      cmakelists.write <<-EO_CMAKE
cmake_minimum_required(VERSION 3.26)
project(self_build NONE)
install (FILES test.txt DESTINATION bin)
      EO_CMAKE
    end

    File.open File.join(@ext, "CMakePresets.json"), "w" do |presets|
      presets.write <<-EO_CMAKE
{
  "version": 6,
  "configurePresets": [
    {
      "name": "debug",
      "displayName": "Debug",
      "generator": "Ninja",
      "binaryDir": "build/debug",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Debug"
      }
    },
    {
      "name": "release",
      "displayName": "Release",
      "generator": "Ninja",
      "binaryDir": "build/release",
      "cacheVariables": {
        "CMAKE_BUILD_TYPE": "Release"
      }
    }
  ]
}
      EO_CMAKE
    end

    FileUtils.touch File.join(@ext, "test.txt")

    output = []

    builder = Gem::Ext::CmakeBuilder.new
    builder.build nil, @dest_path, output, [], @dest_path, @ext

    output = output.join "\n"

    assert_match(/The gem author provided a list of presets that can be used to build the gem./, output)
    assert_match(/Available configure presets/, output)
    assert_match(/\"debug\"   - Debug/, output)
    assert_match(/\"release\" - Release/, output)
    assert_match(/^current directory: #{Regexp.escape @ext}/, output)
    assert_match(/cmake.*-DCMAKE_RUNTIME_OUTPUT_DIRECTORY\\=#{Regexp.escape @dest_path}/, output)
    assert_match(/cmake.*-DCMAKE_LIBRARY_OUTPUT_DIRECTORY\\=#{Regexp.escape @dest_path}/, output)
    assert_match(/#{Regexp.escape @ext}/, output)
  end

  def test_self_build_fail
    output = []

    builder = Gem::Ext::CmakeBuilder.new
    error = assert_raise Gem::InstallError do
      builder.build nil, @dest_path, output, [], @dest_path, @ext
    end

    assert_match "cmake_configure failed", error.message

    shell_error_msg = /(CMake Error: .*)/
    output = output.join "\n"
    assert_match(/#{shell_error_msg}/, output)
    assert_match(/CMake Error: The source directory .* does not appear to contain CMakeLists.txt./, output)
  end

  def test_self_build_has_makefile
    File.open File.join(@ext, "CMakeLists.txt"), "w" do |cmakelists|
      cmakelists.write <<-EO_CMAKE
cmake_minimum_required(VERSION 3.26)
project(self_build NONE)
install (FILES test.txt DESTINATION bin)
      EO_CMAKE
    end

    output = []

    builder = Gem::Ext::CmakeBuilder.new
    builder.build nil, @dest_path, output, [], @dest_path, @ext

    output = output.join "\n"

    # The default generator will create a Makefile in the build directory
    makefile = File.join(@ext, "build", "Makefile")
    assert(File.exist?(makefile))
  end
end