Don't know how much gulf this can take, but I sure would like as small
a version as I can get.
paths = File.expand_path(File.dirname(__FILE__)).split('/')
paths.size.downto(1) do |i|
f = (paths.slice(0..i)+['test']).join('/')
$TESTDIR = File.join(f,'FIXTURE') if File.directory?(f)
end
raise unless $TESTDIR
Here are a couple options (probably not the shortest though):
paths = File.expand_path(File.dirname(__FILE__)).split('/')
while paths.length > 1
f = (paths+['test/FIXTURE']).join('/')
$TESTDIR = f if File.directory?(f)
paths.pop
end
raise unless $TESTDIR
path = File.expand_path(File.dirname(__FILE__))
until path == '/'
f = File.join(path,'test/FIXTURE')
$TESTDIR = f if File.directory?(f)
path = File.dirname(path)
end
raise unless $TESTDIR
Ryan
···
On 10/22/05, Trans <transfire@gmail.com> wrote:
Don't know how much gulf this can take, but I sure would like as small
a version as I can get.
paths = File.expand_path(File.dirname(__FILE__)).split('/')
paths.size.downto(1) do |i|
f = (paths.slice(0..i)+['test']).join('/')
$TESTDIR = File.join(f,'FIXTURE') if File.directory?(f)
end
raise unless $TESTDIR
Don't know how much gulf this can take, but I sure would like as small
a version as I can get.
paths = File.expand_path(File.dirname(__FILE__)).split('/')
paths.size.downto(1) do |i|
f = (paths.slice(0..i)+['test']).join('/')
$TESTDIR = File.join(f,'FIXTURE') if File.directory?(f)
end
raise unless $TESTDIR
Don't know how much gulf this can take, but I sure would like as small
a version as I can get.
paths = File.expand_path(File.dirname(__FILE__)).split('/')
paths.size.downto(1) do |i|
f = (paths.slice(0..i)+['test']).join('/')
$TESTDIR = File.join(f,'FIXTURE') if File.directory?(f)
end
raise unless $TESTDIR
The best balance between size and readability is probably the
following:
find_test_dir
Of course, the method "find_test_dir" must be implemented somewhere,
but you won't have to read it very often, and you can just use the six
lines you've written above.
However, I would use the 'pathname' library to clarify some of the
code.
> paths = File.expand_path(File.dirname(__FILE__)).split('/')
> paths.size.downto(1) do |i|
> f = (paths.slice(0..i)+['test']).join('/')
> $TESTDIR = File.join(f,'FIXTURE') if File.directory?(f)
> end
> raise unless $TESTDIR
[...]
However, I would use the 'pathname' library to clarify some of the
code.
To wit (untested):
require 'pathname'
def find_test_dir
dir = Pathname.new(__FILE__)
loop do
dir = dir.dirname
test_dir = dir + "test"
return test_dir if test_dir.directory?
raise "Test directory not found" if dir.mountpoint?
end
end
The best balance between size and readability is probably the
following:
find_test_dir
Of course, the method "find_test_dir" must be implemented somewhere,
but you won't have to read it very often, and you can just use the six
lines you've written above.
That' a good point. But my trouble here is that the tests themsselves
can get run out of either of two locations: as attached to the bottom
of the file they test, which is in lib/ or standalone in test/ dir
(same code I just extract it). So there's no convenient place to store
this and still be able to require it without the same trouble, AFAIK.
However, I would use the 'pathname' library to clarify some of the
code.
Yes, in this case it would be good, but I recently discovered Pathname
is terribly slow, so I stopped using it.