[racket] testing impure stuff

From: Manfred Lotz (manfred.lotz at arcor.de)
Date: Mon Dec 23 15:06:54 EST 2013

On Mon, 23 Dec 2013 14:29:31 -0500
Matthias Felleisen <matthias at ccs.neu.edu>
wrote:

> 
> 
> 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")))))
> 

Hm, it didn't work for me. I have this:

$ ls -l /tmp/testdir
total 0
d--------- 2 manfred manfred 60 Dec 23 20:55 subdir/
-rw-rw-r-- 1 manfred manfred  0 Dec 23 20:54 x.html
-rw-rw-r-- 1 manfred manfred  0 Dec 23 20:54 x.rkt
-rw-rw-r-- 1 manfred manfred  0 Dec 23 20:55 y.rkt


I slightly modified you sample:

#lang racket ;; foo.rkt 

(define start-dir "/tmp/testdir")

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

and get:

/tmp/testdir/y.rkt
/tmp/testdir/x.rkt
directory-list: could not open directory
  path: /tmp/testdir/subdir
  system error: Permission denied; errno=13
  context...:
   /home/manfred/racket/collects/racket/private/for.rkt:1982:28: loop
   /home/manfred/racket/collects/racket/private/for.rkt:1984:30:
for-loop /home/manfred/racket/collects/racket/private/for.rkt:1974:26


Either your sample won't work or I did make a mistake.


-- 
Manfred







Posted on the users mailing list.