views:

237

answers:

4
+5  Q: 

Haskell IO Testing

I've been trying to figure out if there is already an accepted method for testing file io operations in Haskell, but I have yet to find any information that is useful for what I am trying to do.

I'm writing a small library that performs various file system operations (recursively traverse a directory and return a list of all files; sync multiple directories so that each directory contains the same files using inodes as the equality test and hardlinks...) and I want to make sure that they actually work, but the only way I can think of to test them is to create a temporary directory with a known structure and compare the results from the functions executed on this temporary directory with the known results. The thing is, I would like to get as much test coverage as possible while still being mainly automated: I don't want to have create the directory structure by hand.

I have searched google and hackage, but the packages that I have seen on hackage do not use any testing -- maybe I just picked the wrong ones -- and anything I find on google does not deal with IO testing.

Any help would be appreciated

Thanks, James

+1  A: 

Maybe you can find a way to make this work for you.

EDIT:

the packages that I have seen on hackage do not use any testing

I have found an unit testing framework for Haskell on Hackage. Including this framework, maybe you could use assertions to verify that the files you require are present in the directories that you want them to be and they correspond to their intended purpose.

luvieere
Thanks for the link, but I've read that page before and it only seems to be for pure functions, and right now I'm concerned with testing the impure parts of my functions.Thank you for the link though
James
Ya, I was planning on using HUnit and some higher level framework like test-framework.What I'm more interested in though, is if there is some way to create a mock virtual directory structure and intercept the calls to filesystem to point to the virtual directory, or if maybe there was a framework that would automatticaly generate the temporary directory structure to be used...It's alright though, I think I have come up with an idea for a small framework that should be fairly trivial to implementThanks though
James
A: 

There is no reason why your test code cannot create a temporary directory, and check its contents after running your impure code.

Dave Hinton
Thats where I'm probably going to end up at, I just wanted to know if there were any standard way of performing such checks, thanks though
James
That actually is how you should do it. Folder/file structure is your test data, structure after operations is your test result. You need to prepare it for your tests. If you don't want to create it manually use Haskell basic IO functions to create this structure in testSetUp, than after tests are done you delete it in testTearDown. Or basically create before test, delete after test.
yoosiba
A: 

HUnit is the usual library for IO-based tests. I don't know of a set of properties/combinators for file actions -- that would be useful.

Don Stewart
Alright, looks like I've got to come up with something then. Thanks for the response
James
A: 

If you want mainly automated testing of monadic code, you might want to look into Monadic QuickCheck. You can write down properties that you think should be true, such as

  • If you create a file with read permission, it will be possible to open the file for reading.

  • If you remove a file, it won't open.

  • Whatever else you think of...

QuickCheck will then generate random tests.

Norman Ramsey
Thanks for the link, I'll check it out
James