File: test_cmake.rb

package info (click to toggle)
ruby-mini-portile2 2.8.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 332 kB
  • sloc: ruby: 1,834; ansic: 38; sh: 8; makefile: 4
file content (266 lines) | stat: -rw-r--r-- 8,157 bytes parent folder | download | duplicates (2)
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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
require File.expand_path('../helper', __FILE__)

class TestCMake < TestCase
  attr_accessor :assets_path, :tar_path, :recipe

  def before_all
    super
    @assets_path = File.expand_path("../assets", __FILE__)
    @tar_path = File.expand_path("../../tmp/test-cmake-1.0.tar.gz", __FILE__)

    # remove any previous test files
    FileUtils.rm_rf("tmp")

    create_tar(@tar_path, @assets_path, "test-cmake-1.0")
    start_webrick(File.dirname(@tar_path))

    @logger = StringIO.new # IO to keep recipe logs in case we need to debug
    @recipe = init_recipe

    git_dir = File.join(@assets_path, "git")
    with_custom_git_dir(git_dir) do
      recipe.cook
    end
  rescue => e
    puts @logger.string
    raise e
  end

  def after_all
    super
    stop_webrick
    # leave test files for inspection
  end

  def exe_name
    case
      when MiniPortile.windows? then "hello.exe"
      else "hello"
    end
  end

  def test_cmake_inherits_from_base
    assert(MiniPortileCMake <= MiniPortile)
  end

  def test_configure
    cmakecache = File.join(work_dir, "CMakeCache.txt")
    assert File.exist?(cmakecache), cmakecache

    assert_includes(IO.read(cmakecache), "CMAKE_INSTALL_PREFIX:PATH=#{recipe.path}")
  end

  def test_compile
    binary = File.join(work_dir, exe_name)
    assert File.exist?(binary), binary
  end

  def test_install
    binary = File.join(recipe.path, "bin", exe_name)
    assert File.exist?(binary), binary
  end

  def init_recipe
    MiniPortileCMake.new("test-cmake", "1.0").tap do |recipe|
      recipe.logger = @logger
      recipe.files << "http://localhost:#{HTTP_PORT}/#{ERB::Util.url_encode(File.basename(@tar_path))}"
      recipe.patch_files << File.join(@assets_path, "patch 1.diff")
    end
  end
end

class TestCMakeConfig < TestCMake
  def test_make_command_configuration
    MiniPortile.stub(:mswin?, false) do
      without_env("MAKE") do
        #assert_equal("make", MiniPortileCMake.new("test", "1.0.0").make_cmd)
        assert_equal("xyzzy", MiniPortileCMake.new("test", "1.0.0", make_command: "xyzzy").make_cmd)
      end
      with_env("MAKE"=>"asdf") do
        assert_equal("asdf", MiniPortileCMake.new("test", "1.0.0").make_cmd)
        assert_equal("asdf", MiniPortileCMake.new("test", "1.0.0", make_command: "xyzzy").make_cmd)
      end
    end

    MiniPortile.stub(:mswin?, true) do
      assert_equal("nmake", MiniPortileCMake.new("test", "1.0.0").make_cmd)
    end
  end

  def test_configure_defaults_with_macos
    with_env({ "CC" => nil, "CXX" => nil }) do
      MiniPortile.stub(:darwin?, true) do
        with_stubbed_target(os: 'darwin22', cpu: 'arm64') do
          with_compilers(c_compiler: 'clang', cxx_compiler: 'clang++') do
            Open3.stub(:capture2, cmake_help_mock('Unix')) do
              assert_equal(
                [
                  "-DCMAKE_SYSTEM_NAME=Darwin",
                  "-DCMAKE_SYSTEM_PROCESSOR=arm64",
                  "-DCMAKE_C_COMPILER=clang",
                  "-DCMAKE_CXX_COMPILER=clang++",
                  "-DCMAKE_BUILD_TYPE=Release"
                ],
                @recipe.configure_defaults)
            end
          end
        end
      end
    end
  end

  def test_configure_defaults_with_freebsd
    with_env({ "CC" => nil, "CXX" => nil }) do
      with_stubbed_target(os: 'freebsd14') do
        with_compilers(c_compiler: 'cc', cxx_compiler: 'c++') do
          Open3.stub(:capture2, cmake_help_mock('Unix')) do
            assert_equal(
              [
                "-DCMAKE_SYSTEM_NAME=FreeBSD",
                "-DCMAKE_SYSTEM_PROCESSOR=x86_64",
                "-DCMAKE_C_COMPILER=cc",
                "-DCMAKE_CXX_COMPILER=c++",
                "-DCMAKE_BUILD_TYPE=Release"
              ],
              @recipe.configure_defaults)
          end
        end
      end
    end
  end

  def test_configure_defaults_with_manual_system_name
    MiniPortile.stub(:darwin?, false) do
      with_stubbed_target do
        with_compilers do
          Open3.stub(:capture2, cmake_help_mock('Unix')) do
            @recipe.stub(:system_name, 'Custom') do
              assert_equal(
                [
                  "-DCMAKE_SYSTEM_NAME=Custom",
                  "-DCMAKE_SYSTEM_PROCESSOR=x86_64",
                  "-DCMAKE_C_COMPILER=gcc",
                  "-DCMAKE_CXX_COMPILER=g++",
                  "-DCMAKE_BUILD_TYPE=Release"
                ],
                @recipe.configure_defaults)
            end
          end
        end
      end
    end
  end

  def test_configure_defaults_with_unix_makefiles
    MiniPortile.stub(:linux?, true) do
      MiniPortile.stub(:darwin?, false) do
        with_stubbed_target do
          with_compilers do
            Open3.stub(:capture2, cmake_help_mock('Unix')) do
              MiniPortile.stub(:mingw?, true) do
                assert_equal(default_x86_compile_flags,
                             @recipe.configure_defaults)
              end
            end
          end
        end
      end
    end
  end

  def test_configure_defaults_with_msys_makefiles
    MiniPortile.stub(:linux?, true) do
      MiniPortile.stub(:darwin?, false) do
        with_stubbed_target do
          with_compilers do
            Open3.stub(:capture2, cmake_help_mock('MSYS')) do
              MiniPortile.stub(:mingw?, true) do
                assert_equal(['-G', 'MSYS Makefiles'] + default_x86_compile_flags, @recipe.configure_defaults)
              end
            end
          end
        end
      end
    end
  end

  def test_configure_defaults_with_nmake_makefiles
    MiniPortile.stub(:linux?, true) do
      MiniPortile.stub(:darwin?, false) do
        with_stubbed_target do
          with_compilers do
            Open3.stub(:capture2, cmake_help_mock('NMake')) do
              MiniPortile.stub(:mswin?, true) do
                assert_equal(['-G', 'NMake Makefiles'] + default_x86_compile_flags, @recipe.configure_defaults)
              end
            end
          end
        end
      end
    end
  end

  def test_cmake_command_configuration
    without_env("CMAKE") do
      assert_equal("cmake", MiniPortileCMake.new("test", "1.0.0").cmake_cmd)
      assert_equal("xyzzy", MiniPortileCMake.new("test", "1.0.0", cmake_command: "xyzzy").cmake_cmd)
    end
    with_env("CMAKE"=>"asdf") do
      assert_equal("asdf", MiniPortileCMake.new("test", "1.0.0").cmake_cmd)
      assert_equal("asdf", MiniPortileCMake.new("test", "1.0.0", cmake_command: "xyzzy").cmake_cmd)
    end
  end

  def test_cmake_build_type_configuration
    without_env("CMAKE_BUILD_TYPE") do
      assert_equal("Release", MiniPortileCMake.new("test", "1.0.0").cmake_build_type)
      assert_equal("xyzzy", MiniPortileCMake.new("test", "1.0.0", cmake_build_type: "xyzzy").cmake_build_type)
    end
    with_env("CMAKE_BUILD_TYPE"=>"Debug") do
      assert_equal("Debug", MiniPortileCMake.new("test", "1.0.0").cmake_build_type)
      assert_equal("Debug", MiniPortileCMake.new("test", "1.0.0", cmake_build_type: "xyzzy").cmake_build_type)
    end
  end

  private

  def with_stubbed_target(os: 'linux', cpu: 'x86_64')
    MiniPortile.stub(:target_os, os) do
      MiniPortile.stub(:target_cpu, cpu) do
        yield
      end
    end
  end

  def with_compilers(c_compiler: 'gcc', cxx_compiler: 'g++')
    @recipe.stub(:cc_cmd, c_compiler) do
      @recipe.stub(:cxx_cmd, cxx_compiler) do
        yield
      end
    end
  end

  def default_x86_compile_flags
    [
      "-DCMAKE_SYSTEM_NAME=Linux",
      "-DCMAKE_SYSTEM_PROCESSOR=x86_64",
      "-DCMAKE_C_COMPILER=gcc",
      "-DCMAKE_CXX_COMPILER=g++",
      "-DCMAKE_BUILD_TYPE=Release"
    ]
  end

  def cmake_help_mock(generator_type)
    open3_mock = MiniTest::Mock.new
    cmake_script = <<~SCRIPT
      echo "The following generators are available on this platform (* marks default):"
      echo "* #{generator_type} Makefiles               = Generates standard #{generator_type.upcase} makefiles."
    SCRIPT

    exit_status = MiniTest::Mock.new
    exit_status.expect(:success?, true)
    expected_output = [cmake_script, exit_status]
    open3_mock.expect(:call, expected_output, ['cmake --help'])
    open3_mock
  end
end