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.
That did it! Great! Thanks!