How would you test these?

I’m relatively new to Ruby, but learning fast. As an exercise, I’m
porting
a family of untested bash scripts to Ruby, and I’m running into some
things
that I’m not sure how best to test.

I know how to write the code, but I want to be able to write it
test-first,
and I’m not sure of the best way to test it.

I’m using minitest, if that makes a difference.

  1. Compute the total size of a directory and its contents (bash script
    uses
    “du” for this)

  2. Make a clean copy of a directory and its contents, removing any
    version
    control directories (.svn, .git)

  3. Shell out to run a few commands using fakeroot. Bonus points if
    there’s
    a way to test that the commands have the desired effects.

  4. Get a list of files matching a given pattern on a remote server
    (using
    ssh).

  5. Copy a file up to a remote server (using scp).

  6. Run a script on a remote server (again, ssh).

I know I could write some mock-heavy tests here to make sure my program
is
running the right shell commands and such, but that doesn’t seem to
fully
test that the program does what I want.

How would you approach writing tests for scenarios like these?

Thanks,
Randy

On Wed, Feb 22, 2012 at 4:54 PM, Randy C. [email protected]
wrote:

I’m relatively new to Ruby, but learning fast. As an exercise, I’m porting
a family of untested bash scripts to Ruby, and I’m running into some things
that I’m not sure how best to test.

I know how to write the code, but I want to be able to write it test-first,
and I’m not sure of the best way to test it.

How would you approach writing tests for scenarios like these?

Hmm, how about using your current scripts to compare the outcome of
the new scripts with? For example, the file lists should be identical
(modulo sorting) so something like this could work

$ diff -U5 <(old-script.sh … | sort) <(new-script.rb … | sort)

Of course, you could also write more fine granular tests, e.g. place a
file on the remote machine and check whether the lower level listing
function finds the file with the proper pattern. In any case if you
separate output from the logic you can test the logic more easily.

Kind regards

robert