建议使用ruby c-extension进行测试的方法

时间:2012-11-05 22:39:06

标签: ruby ruby-c-extension

我正在使用

构建一个rubygem
gem build $gemname.gemspec && gem install $gemname-0.0.1.gem

然而,这似乎并不是测试gem的最佳方法,因为它将它移出局部环境并进入rubygems路径。

建议使用

构建gem
cd ext/$gemname && ruby extconf.rb && make

?还是有更好的做法?

1 个答案:

答案 0 :(得分:0)

我猜测下面的Rakefile也会起作用(并且比上面的命令更容易)。但是,这对测试来说并不明显。

require 'rake/testtask'
require 'rake/clean'

NAME = 'hola'

# rule to build the extension: this says
# that the extension should be rebuilt
# after any change to the files in ext
file "lib/#{NAME}/#{NAME}.so" =>
    Dir.glob("ext/#{NAME}/*{.rb,.c}") do
  Dir.chdir("ext/#{NAME}") do
    # this does essentially the same thing
    # as what RubyGems does
    ruby "extconf.rb"
    sh "make"
  end
  cp "ext/#{NAME}/#{NAME}.so", "lib/#{NAME}"
end

# make the :test task depend on the shared
# object, so it will be built automatically
# before running the tests
task :test => "lib/#{NAME}/#{NAME}.so"

# use 'rake clean' and 'rake clobber' to
# easily delete generated files
CLEAN.include('ext/**/*{.o,.log,.so}')
CLEAN.include('ext/**/Makefile')
CLOBBER.include('lib/**/*.so')

# the same as before
Rake::TestTask.new do |t|
  t.libs << 'test'
end

desc "Run tests"
task :default => :test