[racket] testing impure stuff

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Mon Dec 23 14:29:31 EST 2013


On Dec 23, 2013, at 2:10 PM, Manfred Lotz <manfred.lotz at arcor.de> wrote:

> On Mon, 23 Dec 2013 13:53:05 -0500
> Greg Hendershott
> <greghendershott at gmail.com> wrote:
> 
>> On Sun, Dec 22, 2013 at 2:49 PM, Neil Van Dyke
>> <neil at neilvandyke.org> wrote:
>>> Manfred Lotz wrote at 12/22/2013 01:54 PM:
>>>> Or perhaps even better create my directory structure on the fly and
>>>> build my test cases upon this?
>>> Yes, like that.  It can be tedious to develop, but then your test
>>> suite is more likely to work when you or someone else needs it to.
>> 
>> I agree. Even if you use a file system mock, it's better to test on
>> real file systems, too. You're more likely to encounter real-world
>> situations you need to handle in your code -- and in your file system
>> mock.
>> 
>> For instance running `fold-files` on certain paths will likely give
>> you some items for which you lack permissions. Either you need to
>> distinguish them using `file-or-directory-permissions` and not try to
>> read them at all, or, use `with-handlers` to catch the resulting
>> exceptions.
>> 
>> That's an example of something you might not think to include in a
>> file system mock, at least not until you've encountered it in a real
>> file system.
>> ____________________
>>  Racket Users list:
>>  http://lists.racket-lang.org/users
>> 
> 
> Yep, I also think it is best to create test directory structure and
> take this for testing.
> 
> One question: How would I resume from a permission denied when using
> in-directory.
> 
> 
> Let's say I have this:
> 
> 
> (for ([f (in-directory  dir)] #:when (myfilter f))
>      (do-something f))
> 
> Now I would like to report a permission denied error, and then
> continue getting the next file.
> 
> I saw that there is 'with-handlers' or 'call-with-exception-handler'
> but I did not find how to resume.


I'd set a with-handler around do-something like below, which 
keeps the loop intact: 


#lang racket ;; foo.rkt 

(require 2htdp/batch-io)

(for ([f (in-directory)] #:when (regexp-match #rx"\\.rkt" f))
  (with-handlers ((exn:fail:contract? (lambda (e) (log-warning (exn-message e)))))
     (displayln (read-file (string-append (path->string f) ".html")))))


Run this program as 'racket -W warning foo.rkt' 

Posted on the users mailing list.