1require 'rubygems/test_case'
2require 'rubygems/ext'
3
4class TestGemExtCmakeBuilder < Gem::TestCase
5
6  def setup
7    super
8
9    `cmake #{Gem::Ext::Builder.redirector}`
10
11    skip 'cmake not present' unless $?.success?
12
13    @ext = File.join @tempdir, 'ext'
14    @dest_path = File.join @tempdir, 'prefix'
15
16    FileUtils.mkdir_p @ext
17    FileUtils.mkdir_p @dest_path
18  end
19
20  def test_self_build
21    File.open File.join(@ext, 'CMakeLists.txt'), 'w' do |cmakelists|
22      cmakelists.write <<-eo_cmake
23cmake_minimum_required(VERSION 2.8)
24install (FILES test.txt DESTINATION bin)
25      eo_cmake
26    end
27
28    FileUtils.touch File.join(@ext, 'test.txt')
29
30    output = []
31
32    Dir.chdir @ext do
33      Gem::Ext::CmakeBuilder.build nil, nil, @dest_path, output
34    end
35
36    output = output.join "\n"
37
38    assert_match \
39      %r%^cmake \. -DCMAKE_INSTALL_PREFIX=#{Regexp.escape @dest_path}%, output
40    assert_match %r%#{Regexp.escape @ext}%, output
41    assert_contains_make_command '', output
42    assert_contains_make_command 'install', output
43    assert_match %r%test\.txt%, output
44  end
45
46  def test_self_build_fail
47    output = []
48
49    error = assert_raises Gem::InstallError do
50      Dir.chdir @ext do
51        Gem::Ext::CmakeBuilder.build nil, nil, @dest_path, output
52      end
53    end
54
55    output = output.join "\n"
56
57    shell_error_msg = %r{(CMake Error: .*)}
58    sh_prefix_cmake = "cmake . -DCMAKE_INSTALL_PREFIX="
59
60    expected = %r(cmake failed:
61
62#{Regexp.escape sh_prefix_cmake}#{Regexp.escape @dest_path}
63#{shell_error_msg}
64)
65
66    assert_match expected, error.message
67
68    assert_match %r%^#{sh_prefix_cmake}#{Regexp.escape @dest_path}%, output
69    assert_match %r%#{shell_error_msg}%, output
70  end
71
72  def test_self_build_has_makefile
73    File.open File.join(@ext, 'Makefile'), 'w' do |makefile|
74      makefile.puts "all:\n\t@echo ok\ninstall:\n\t@echo ok"
75    end
76
77    output = []
78
79    Dir.chdir @ext do
80      Gem::Ext::CmakeBuilder.build nil, nil, @dest_path, output
81    end
82
83    output = output.join "\n"
84
85    assert_contains_make_command '', output
86    assert_contains_make_command 'install', output
87  end
88
89end
90
91