Forum: Ruby Questions about test-driven development and optparse

Posted by Panagiotis Atmatzidis (Guest)
on 2012-08-06 18:33
(Received via mailing list)
Hello,

I just finished the first version of a script that gathers info from 
'fail2ban.log' [1] and displays it. I'd like to turn it into a gem. 
Reading the official documentation [2] and a book that I use as a guide, 
I've read about the importance of "writing tests", before turning 
anything into a gem.

I wonder though, what kind of test should/could I write since my script 
users optparse. Expected outputs are like:
----
➜  f2bread git:(master) ruby f2bread.rb -l 
../../Local/f2bread/fail2ban.log -i
===============================================================================
Log file: 
'/Users/atma/Dropbox/Programming/Projects/Local/f2bread/fail2ban.log'

First entry: 2011-07-23 02:04:51
Last  entry: 2012-07-25 15:14:47
Time frame:     1 year, 3 days and 07:09:56
Banned IPs:     1072
Countries:      70
Protocol(s):    [ssh-ipfw]
Bans per day:   2.91

Most banned IP(s) by fail2ban:
-------------------------
IP address    -   Attacks
-------------------------
121.31.56.62            7

Most hostile Countries:
--------------------------
Country: Korea(South) - IP(s): 400
Country: China - IP(s): 195
Country: United_States - IP(s): 86
Country: Russian_Federation - IP(s): 36
Country: Germany - IP(s): 32


➜  f2bread git:(master) ruby f2bread.rb -l 
../../Local/f2bread/fail2ban.log -s country -n 5
Country: Korea(South) - IP(s): 400
Country: China - IP(s): 195
Country: United_States - IP(s): 86
Country: Russian_Federation - IP(s): 36
Country: Germany - IP(s): 32
-----

So in this scenario, does 'testing' have a meaning? I mean, I already 
see that 'it works'. Writing a function that will match these and other 
possible outputs looks like a huge regexp hell to me or I'm getting 
something wrong here.

Thanks

[1] https://github.com/atmosx/f2bread
[2] http://guides.rubygems.org/make-your-own-gem/#writing-tests

Panagiotis Atmatzidis
Posted by Bartosz Dziewoński (matmarex)
on 2012-08-06 19:45
(Received via mailing list)
If I were you, I wouldn't use regexps for testing the output here.
That would imply that you're basically only checking that the output
format is right.

You should probably take a simple, short log file, and package it with
the tests. Then run it once for each options combination you want to
test, save the output (which you have manually verified is correct),
and write a test that checks the the output still looks exactly the
same.


You could also just forget about tests here entirely. What you have
here is (when you don't count country code list and documentation) a
100-line script that does just one thing, and you've already verified
it works. Tests are only useful in one case – when you are making
changes to your software and want to make sure they don't introduce
bugs in old code.

Still, it might turn out later that you actually need to change stuff,
and then you'll be wishing you wrote the tests earlier :(

-- Matma Rex
Posted by Panagiotis Atmatzidis (Guest)
on 2012-08-06 22:13
(Received via mailing list)
Hello,

On 6 Αυγ 2012, at 20:44 , Bartosz Dziewoński <matma.rex@gmail.com> 
wrote:

>
> -- Matma Rex
>



Thanks for the info. Your idea of writing tests makes sense... I'll give 
it a try as an exercise.

Thanks!

Panagiotis Atmatzidis
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.