Relative File Path in Rspec

Relative File Path in RSpec

As far as I know (from looking every month or two) there is no better way that building something in spec_helper that uses the __FILE__ value to grab the path to a know bit of content, then build your own helpers on top of that.

You can obviously use a path relative to __FILE__ in the individual *_spec.rb file as well.

Referencing file location in RSpec Rake task vs. rspec runner

It's because of

File.open("person_invalid_address_examples.yaml", "r")

It opens the file where the rspec is running.
In your case you should define file more apparently something like this:

file_path = File.expand_path("person_invalid_address_examples.yaml", File.dirname(__FILE__))
File.open(file_path, "r")

Ruby require 'file' and relative location

You don't. requires are relative to the current directory, which in your case was GUI_Tests/Tests. If you did this instead:

cd ..
spec -r upload_tool -fs Test/test_spec.rb

You would have to use this:

require 'windows_gui' # without '../'

The most common way to get around that problem is using File.dirname(__FILE__):

require File.join(File.dirname(__FILE__), '..', 'windows_gui')

NOTE: in Ruby 1.9.2 require changed it's defaults: Ruby: require vs require_relative - best practice to workaround running in both Ruby <1.9.2 and >=1.9.2

How does ruby know where to find rspec in require rspec

This post explains it in detail. Here's the important bits:

To help you use the code inside of your gems, RubyGems overrides Ruby’s require method. (It does this in core_ext/kernel_require.rb). The comment is pretty clear:

 ##
# When RubyGems is required, Kernel#require is replaced with our own which
# is capable of loading gems on demand.
#
# When you call <tt>require 'x'</tt>, this is what happens:
# * If the file can be loaded from the existing Ruby loadpath, it
# is.
# * Otherwise, installed gems are searched for a file that matches.
# If it's found in gem 'y', that gem is activated (added to the
# loadpath).
#

The rest of the post walks us through the require process step by step, looking through the code; but the gist of it is, if original require (through $LOAD_PATH) fails, it will scan the specification files of all installed gems and see if your require is supposed to work with it; if so, it activates (adds to $LOAD_PATH) that gem and also all gems that it depends on; finally it will re-invoke the original require.

File paths for parsing in Ruby using Rake

__dir_ method returns directory of the current file you're in, so this should work for you:

File.read(__dir__ + "/../metadata.json")

How do I include image path in my spec file?

According to the doc

1) some input element on the page should have a name, id, or label_text matching "Main image"

2) you should use relative (not absolute) path of the file, in your case /spec/images/avatar4.jpg

Opening relative paths from gem

Short answer

If I understand it correctly, you don't need to change anything.

Inside app.rb and your gem, relative paths will be understood relatively to Dir.pwd.

If you run ruby app.rb from inside /home/user/my_app :

  • Dir.pwd will be /home/user/my_app
  • both app.rb and my_gem will look for 'data.txt' inside /home/user/my_app.

Useful methods, just in case

Dir.chdir

If for some reason Dir.pwd isn't the desired folder, you could change directory :

Dir.chdir('/home/user/my_app') do
# relative paths will be based from /home/user/my_app
# call your gem from here
end

Get the directory of current file :

__dir__ will help you get the directory :

Returns the canonicalized absolute path of the directory of the file
from which this method is called.

Get the current file :

__FILE__ will return the current file. (Note : uppercase.)

Concatenate file paths :

If you need to concatenate file paths, use File.expand_path or File.join. Please don't concatenate strings.

If you don't trust that the relative path will be correctly resolved, you could send an absolute path to your method :

my_gem.load_from_file(File.expand_path('data.txt'))


Related Topics



Leave a reply



Submit