[GULF] Find the test dir

T

Trans

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

T.
 
R

Ryan Leavengood

Don't know how much gulf this can take, but I sure would like as small
a version as I can get.

paths =3D File.expand_path(File.dirname(__FILE__)).split('/')
paths.size.downto(1) do |i|
f =3D (paths.slice(0..i)+['test']).join('/')
$TESTDIR =3D File.join(f,'FIXTURE') if File.directory?(f)
end
raise unless $TESTDIR

I believe you mean golf? ;)

Here are a couple options (probably not the shortest though):

paths =3D File.expand_path(File.dirname(__FILE__)).split('/')
while paths.length > 1
f =3D (paths+['test/FIXTURE']).join('/')
$TESTDIR =3D f if File.directory?(f)
paths.pop
end
raise unless $TESTDIR

path =3D File.expand_path(File.dirname(__FILE__))
until path =3D=3D '/'
f =3D File.join(path,'test/FIXTURE')
$TESTDIR =3D f if File.directory?(f)
path =3D File.dirname(path)
end
raise unless $TESTDIR

Ryan
 
T

Trans

Oops I mean "GOLF" with an O not a U. GOLF. G...O...L...F.

(smtms i wsh nglsh ws mr lk hbrw)

T.
 
D

David A. Black

Hi --

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

require'file/find'
raise unless$TESTDIR=Find.find(File.dirname(__FILE__)){|f|
break File.join(f,"FIXTURE")if test(?d,f)&&/\btest$/.match(f)}


David
 
G

Gavin Sinclair

Trans said:
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.

Cheers,
Gavin
 
G

Gavin Sinclair

Gavin said:
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

# ...

fixture = find_test_dir() + "FIXTURE"


Cheers,
Gavin
 
T

Trans

David said:
require'file/find'
raise unless$TESTDIR=Find.find(File.dirname(__FILE__)){|f|
break File.join(f,"FIXTURE")if test(?d,f)&&/\btest$/.match(f)}

Shorter than I thought possible! And I did not know about file/find.

Thanks,
T.
 
T

Trans

Gavin said:
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.

Thanks,
T.
 
B

Brian Schröder

.hght, drrfn b nc gnnm ht wh gnzm ylpmS

$ echo -e "[...snip...]" | ./2hebrew.rb
lc yw s rtlsnrt wrbhgp t hslgn n
?s knht y t'nD
$ cat 2hebrew.rb
#!/usr/bin/env ruby

ARGF.each do | line | puts line.chomp.gsub(/[aeiou]/i, '').reverse end

cheers,

Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top