开发者

Base/Home directory of the gem

开发者 https://www.devze.com 2023-02-13 00:50 出处:网络
As part of the RSpec, i need to reference a file contained in a gem I am depending on (call it gem foo).I control gem foo, so I can make changes to it as needed.

As part of the RSpec, i need to reference a file contained in a gem I am depending on (call it gem foo). I control gem foo, so I can make changes to it as needed.

Gem 'foo' contains a file that I need to reference with in 开发者_开发问答the a rspec spec. Is there a reasonably stable RubyGem or Bundler API to figure out 'foo' base directory?

Assuming 'foo' is already required in my Gemfile: in Gemfile:

gem 'foo'

I want to do something like this (in something_spec.rb):

filename = File.expand_path('examples/xml/result.xml', Gem.gem_base_path('foo'))

What is gem_base_path API call?


I would recommend creating a function in your gem to do this:

module Foo
  class Configuration
    def self.result_xml_path
      File.realpath("../examples/xml/result.xml")
    end
  end
end

You can then do the following in your spec:

filename = Foo::Configuration.result_xml_path

This is much safer since you are getting all the information from the gem. It also looks cleaner.


This may do what you need without ant need to touch the 'foo' gem:

matches = Gem::Specification.find_all_by_name 'foo'
spec = matches.first
filename = File.expand_path('examples/xml/result.xml', spec.full_gem_path)

I have used this code to make something similar to what you need (namely loading in my specs some factories defined in a gem used by my project)

0

精彩评论

暂无评论...
验证码 换一张
取 消