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
|
require File.dirname(__FILE__) + '/../abstract_unit'
class TestAvailability < Test::Unit::TestCase
def setup
@fixture_path = File.expand_path(File.join(File.dirname(__FILE__), '..', 'fixtures'))
@avail = Mkrf::Availability.new(:includes => @fixture_path)
end
def teardown
FileUtils.rm_f 'mkrf.log'
end
def test_has_library_should_return_true_when_lib_already_loaded
@avail = Mkrf::Availability.new(:loaded_libs => ['sample_library'])
assert @avail.has_library?('sample_library')
end
def test_has_library_should_fail_on_bogus_lib
assert !@avail.has_library?('bogus_library')
end
def test_can_link
@avail.send(:with_headers, 'stdio.h') do
assert @avail.can_link?(@avail.send(:simple_reference, "printf"))
end
end
def test_create_source
assert_creates_file(Mkrf::Availability::TEMP_SOURCE_FILE) do
@avail.send(:create_source, "puts 'Hello World!'")
end
source = File.open(Mkrf::Availability::TEMP_SOURCE_FILE).read
assert_equal "puts 'Hello World!'", source
ensure
FileUtils.rm_f Mkrf::Availability::TEMP_SOURCE_FILE
end
def test_has_header_should_fail_on_bogus_header
assert !@avail.has_header?('some_fake_header.h')
end
def test_has_header_should_work_with_basic_headers
assert @avail.has_header?('stdmkrf.h')
end
def test_has_header_should_check_many_paths
assert !@avail.has_header?('header_down_a_directory.h')
assert @avail.has_header?('header_down_a_directory.h',
File.join(@fixture_path, 'down_a_directory'))
end
def test_has_header_should_add_define_with_valid_header
assert @avail.has_header?('stdmkrf.h')
assert @avail.defines.include?('HAVE_STDMKRF_H'), "Defines: #{@avail.defines.inspect}"
end
def test_include_header
assert @avail.has_header?('stdmkrf.h')
assert !@avail.headers.include?('stdmkrf.h')
@avail.include_header('stdmkrf.h')
assert @avail.headers.include?('stdmkrf.h')
end
# This should really use a trivial lib compiled in fixtures..
def test_include_library
assert @avail.has_library?('z')
assert !@avail.loaded_libs.include?('z')
@avail.include_library('z')
assert @avail.loaded_libs.include?('z')
end
def test_method_missing_should_go_down_chain_when_not_catching_stackable_attributes
assert_raises(NoMethodError) { @avail.not_a_stackable_attribute }
assert_raises(NoMethodError) { @avail.with_not_a_stackable_attribute }
end
def test_find_executable_should_return_nil_when_not_found
assert_nil @avail.find_executable('fake_executable')
end
def test_find_executable_should_default_to_search_env_path
old_path = ENV['PATH']
ENV['PATH'] = @fixture_path
expected = File.join(@fixture_path, 'some_binary')
assert_equal expected, @avail.find_executable('some_binary')
ensure
ENV['PATH'] = old_path
end
def test_find_executable_should_search_given_paths_if_supplied
expected = File.join(@fixture_path, 'some_binary')
assert_equal expected, @avail.find_executable('some_binary', @fixture_path)
end
def test_logging
@avail.logger.level = Logger::INFO
assert @avail.include_library('z')
assert @avail.include_library('z')
assert !@avail.include_library('bogus_lib')
assert !@avail.include_header('some_fake_header.h')
assert @avail.include_header('stdio.h')
assert !@avail.has_function?('blah_blah_blah')
assert @avail.has_function?('printf')
source = File.open('mkrf.log').read
[ 'Checking for library: z',
'Library found: z',
'Library already loaded: z',
'Library not found: bogus_lib',
'Header not found: some_fake_header.h',
'Header found: stdio.h',
'Function not found: blah_blah_blah()',
'Function found: printf()'
].each do |log_items|
assert_match log_items, source
end
end
end
class TestAvailabilityDefaults < Test::Unit::TestCase
def setup
@avail = Mkrf::Availability.new
@config = Config::CONFIG
end
def test_default_libs_should_be_from_rbconfig
assert_equal @config["LIBS"].chomp(" "), @avail.library_compile_string
end
def test_default_compiler_should_be_from_rbconfig
assert_equal @config["CC"], @avail.send(:instance_variable_get, :@compiler)
end
def test_default_include_dir_should_be_from_rbconfig
#Fix to correspond to the definition in lib/mkrf/availability.rb (Debian patch)
if Config::CONFIG['rubyarchhdrdir']
expected = [Config::CONFIG['rubyarchhdrdir'],Config::CONFIG['rubyhdrdir'],
Config::CONFIG['rubyhdrdir'] + "/" + Config::CONFIG['arch'],
Config::CONFIG["archdir"],Config::CONFIG['sitelibdir'], "."]
else
expected = [Config::CONFIG['rubyhdrdir'],
Config::CONFIG['rubyhdrdir'] + "/" + Config::CONFIG['arch'],
Config::CONFIG["archdir"],Config::CONFIG['sitelibdir'], "."]
end
assert_equal expected, @avail.send(:instance_variable_get, :@includes)
end
end
|