Monday, June 09, 2008

Another Ruby Inconsistency

Maybe it's just me, but I keep finding interpreter issues underlying my code. The latest has to do with accessing the file system. In the context of unit testing,
require 'test/unit'
require 'fileutils'
class TC_File_Access < Test::Unit::TestCase
  def setup
rm_r "temp" if File.exist? "temp"
end
  def test_non_exisitent_intermediate
mkdir "temp"
touch "temp/foo"
assert_raise(Errno::ENOTDIR) { File.new "temp/foo/bar", "w" }
rm_r "temp"
end
end
runs fine on my Mac, bar cannot be created under temp/foo since it's not a directory. Running it under Windows also errors, but the raised error is Errno::ENOENT instead. Of course this plays havoc with tests until the context of the environment is added

class TC_File_Access
  def test_non_exisitent_intermediate
mkdir "temp"
touch "temp/foo"
if Config::CONFIG["target_vendor"] == 'pc'
assert_raise(Errno::ENOENT) { File.new "temp/foo/bar", "w" }
else
assert_raise(Errno::ENOTDIR) { File.new "temp/foo/bar", "w" }
end
rm_r "temp"
end
end
I hate having to add this sort of code, but the underlying OS is pushing these errors to Ruby and its doing the 'right' thing, pushing up what it gets. I guess my complaint is that I wish Ruby could hide these sorts of details and remap the error to a consistent (most correct) value. It'd be a little more code, but it's all about making life easier for the lowly developer, right?

Imperfect worlds are such a pain.

No comments: