This is right out of the pickaxe 2nd edition
pages 336 - 337.
__FILE__ String The name of the
current source file. [r/o]
The constant __FILE__ and the variable $0 are often used
together to run code only if it appears in the file run directly by the user. For
example, library writers often use this to include tests in their libraries
that will run if the library source is run directly, but not if the source is
required into another program.
# Library code
#...
If __FILE__ == $0
#tests …
end
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On
Behalf Of Paul Carvalho
Sent: Thursday, April 20, 2006
12:05 PM
To: [email protected]
Subject: [Wtr-general] Trying to
understand the UnitTests directorymanipulation
I'm finally getting around to taking a closer look at
the Watir UnitTests and thought that I would start with the 'all_tests.rb' and
'core_tests.rb' scripts. I'm having some trouble trying to understand
*why* the paths are manipulated the way they are.
Both files are basically the same, just the [variable] list of test files
called is different. Here's the 'all_tests.rb' script:
---
TOPDIR = File.join(File.dirname(__FILE__), '..')
$LOAD_PATH.unshift TOPDIR
require 'unittests/setup.rb'
Dir.chdir TOPDIR
$all_tests.each {|x| require x}
---
Here's my understanding of what this script is doing:
1) Line one sets the variable = './..' ( using a much more verbose
way of doing it ;) )
- Incidentally, I was unable to find any reference to the use of '__FILE__'
anywhere. Is that some special kind of dummy variable? Can someone
please tell me how/why this works?
2) Line two prepends './..' to the Ruby load path
- I don't understand the design choice behind this. i.e. I don't know why
the first two lines can't just be squished into this one line - as in:
$LOAD_PATH.unshift './..'
( I must be missing something important further down the scripts that I haven't
gotten to yet. )
3) skip line three for the moment..
4) Line four changes the current directory to the parent of the current (i.e.
essentially 'cd ..')
5) Back to Line three - now that we are one directory back/up, let's _require_
the file that *used to be* in the current directory, but now is in the
subdirectory called 'unittests'
6) Finally, call/require all the test files (where each element in the variable
array contains the path 'unittests/' before the filename)
Did I get that right?
So my question is this:
- Why do the UnitTest scripts go to all the trouble of prepending the parent
path to the Ruby Load Path, then change to the parent path, only to then call
all the files in the desired subdirectory?
Is there something that I'm missing regarding the value of directory *position*
when running Ruby/Watir scripts? I am not doing this directory one-two
step in my scripts and am wondering if it is something that I should add or
not.
Please let me know what I am missing here. Thanks.
Paul.