Problems loading yaml file with Rake

Hi,
I have problems loading a yaml file with Rake when testing and getting the following error;
Errno::ENOENT: No such file or directory @ rb_sysopen
But there are no problems when testing from within the test directory. The Rakefile is located at the root of the project.

Hard to say without seeing your rakefile, but my first guess would be a path problem. When you try to load the yaml file, does rake know its location? e.g. if it is in the test directory?

Rakefile content in test directory:

require ‘rake/testtask’

task :default => :test

Rake::TestTask.new do |task|
task.pattern = ‘test_*.rb’
task.warning = true
end

No problems loading yaml file.

Moving Rakefile to root directory of project:

require ‘rake/testtask’

task :default => :test

Rake::TestTask.new do |task|
task.pattern = ‘lib/language_quiz/test_*.rb’
task.warning = true
end

continued:

Errno::ENOENT: No such file or directory @ rb_sysopen

This does not mention the yaml file.

Where is the yaml file being loaded? and how?

Remember that the ‘base folder’ that files are searched on will change depending on where you run the Rakefile from.

All files, including the yaml file are lying in the same directory(language_quiz) for now. The yaml file is loaded via a quiz-class in a quiz.rb file through its initializer i.e @data = YAML.load(File.read(“path to yaml file”))

I don’t know if this helps;
But if I put the load path to the yaml file in the quiz.rb file relative to where the rake file is, “the base folder”, then it finds it.

class Quiz(in quiz.rb)
.
.
.

def initialize
begin
$LOG.debug(“Test loading yaml data…”)
@data = YAML.load(File.read(“path to yaml file relative to the rakefile in the base folder”))
end
.
.
.

end

But then I can’t run the test from within the “language_quiz” folder properly where I find the quiz.rb and the test_quiz.rb file

Probably your best solution is to find the directory of the file in which you are loading the file, e.g. by:

CWD = File.expand_path(File.dirname(__FILE__))

Then you can write:

YAML.load(File.read(File.join(CWD, 'yaml-file')))

You can then name the ‘yaml-file’ relative to the file in which the call is.

1 Like

That did it! Great! Thanks! :smiley:

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs