On 4/20/06, Paul Carvalho <[EMAIL PROTECTED]> wrote:
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?

You have the explanation of __FILE__ from Mark.

The important fact you need to understand is that directory that contains this file and the current directory (i.e. ".") are not necessarily the same. If you do this:

  > cd unittests
  > ruby core_tests.rb

then they are the same. However, you can also run them this way:

  > cd \
  > ruby c:\workspace\watir\unittests\core_tests.rb

(assuming this path is where the tests are.) Now the current directory is "c:\".

The code is written so that the TOPDIR is correct no matter how the tests are run.

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. )

The two lines could indeed be combined in one. In other Watir tests, this in fact is what is done.

But note that it is very important that they are prepended. Typically, you simply append something to the load path. That ensures it is in the path. Prepending will ensure that *this particular version* is what is loaded ( i.e. tested). If it was appended, it would be testing the version of Watir installed, not necessary the version that is in the same hierarchy as the tests. (In fact, this would almost always be the wrong version when you are making changes to Watir.)

3) skip line three for the moment..

4) Line four changes the current directory to the parent of the current ( i.e. essentially 'cd ..')

No. It changes the current directory to the parent of the *file* regardless of current was. (This is why we saved TOPDIR.)

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.

Looking over the script now, it seems to me that line four is superflous. It was important when creating the all_tests array (in setup.rb), but it is not important when consuming it (all that matters then is the load path, which has already been set up).
 
Bret
_______________________________________________
Wtr-general mailing list
[email protected]
http://rubyforge.org/mailman/listinfo/wtr-general

Reply via email to