[aklug] Re: Simplest way to do this in bash?

From: Christopher Howard <choward@indicium.us>
Date: Sat Jan 16 2010 - 10:26:28 AKST

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

bryanm@acsalaska.net wrote:
> On Fri, January 15, 2010 1:11 am, Christopher Howard wrote:
>> My bash skills are a little weak. Okay, they are pretty much
>> non-existent past the common one-liners I use every day. So I would
>> appreciate any general suggestions or code:
>>
>> Basic idea: I'm writing some tests for my Java class to make sure that,
>> as I continue to develop the codebase, my methods are still giving the
>> output I expect. So I want to write a bash script that executes a test
>> class, and then compares STDOUT to the contents of a file (which
>> contains the output I expect). If the two are exactly the same, then
>> script moves on to the next test, otherwise echoes a failure message and
>> dies with a bad return value.
>>
>> Like so
>>
>> #
>> # ... compiles all the code ...
>> # ... I can write that part ...
>> #
>>
>> # execute java test program
>> java -cp ".;../" TestMyClass
>>
>> # then compare STDOUT of previous cmd
>> # to contents of file TestMyClass.dat
>>
>> # If contents are different then
>> echo "TestMyClass failed!"
>> # then die with bad return value
>>
>> # If contents are the same
>> # continue script...
>
> This is not difficult. First, redirect output to a file:
> java -cp ".;../" TestMyClass >MyClass.out
>
> Then, use diff(1) to do the comparison:
> diff MyClass.expected MyClass.out
>
> I won't spoil the fun by quoting the man page here. :)
>
> --
> Bryan Medsker
> bryanm@acsalaska.net
>
> ---------
> To unsubscribe, send email to <aklug-request@aklug.org>
> with 'unsubscribe' in the message body.
>

I found it is unnecessary to do the diff compare, as I can just use
assert statements in the code itself to determine if the output is what
it should be. So my script simply compiles and the executes the code,
like so:

 #!/usr/bin/env bash

 javac -cp ".:../" TestColorList.java || exit 1;
 java -ea -cp ".:../" TestColorList || exit 1;

 javac -cp ".:../" TestBoard.java || exit 1;
 java -ea -cp ".:../" TestBoard || exit 1;

Since this meets the needs of my small project, it did not seem
necessary to learn a unit testing framework like junit.

- --
Christopher Howard
http://indicium.us
http://theologia.indicium.us
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v2.0.11 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAktSEuQACgkQQ5FLNdi0BcVjrwCgk2ajZEUsv9/va99ID4AD0bsE
CPgAmwcSd+l8H8Q9byX2XiBmL+m0MCYJ
=AMgO
-----END PGP SIGNATURE-----
---------
To unsubscribe, send email to <aklug-request@aklug.org>
with 'unsubscribe' in the message body.
Received on Sat Jan 16 10:25:56 2010

This archive was generated by hypermail 2.1.8 : Sat Jan 16 2010 - 10:25:56 AKST