【问题标题】:Recommended way to build ruby c-extension for testing构建用于测试的 ruby​​ c-extension 的推荐方法
【发布时间】:2012-11-05 22:39:05
【问题描述】:

我正在使用

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

但是,这似乎不是测试 gem 的最佳方法,因为它将其移出本地环境并进入 ruby​​gems 路径。

是否建议使用构建 gem

cd ext/$gemname && ruby extconf.rb && make

?还是有更好的做法?

【问题讨论】:

    标签: ruby ruby-c-extension


    【解决方案1】:

    我猜像下面这样的 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
    

    【讨论】:

      猜你喜欢
      • 2017-08-04
      • 2012-05-05
      • 1970-01-01
      • 1970-01-01
      • 2021-04-18
      • 1970-01-01
      • 1970-01-01
      • 2021-03-24
      • 1970-01-01
      相关资源
      最近更新 更多