[Haskell-cafe] Problems using tasty-golden

Roman Cheplyaka roma at ro-che.info
Fri Feb 14 16:51:33 UTC 2014


* Roman Cheplyaka <roma at ro-che.info> [2014-02-14 18:37:07+0200]
> > A bonus question. I redefined vgReadFile from Test.Tasty.Golden.Internal to return Either:
> > 
> > vgReadFileE :: FilePath -> ValueGetter r (Either BSL.ByteString BSL.ByteString)
> > vgReadFileE path =
> >   (fmap Right . liftIO . BSL.hGetContents =<<) $
> >   ValueGetter $
> >   ContT $ \k ->
> >   bracket
> >     (openBinaryFile path ReadMode)
> >     hClose
> >     k
> > 
> > The only modification is the "fmap Right .". How can I return Left if opening the file fails?
> 
> First off, you don't have to do that (unless you have some special needs
> — if so, what are they?). If the file doesn't exist, tasty will catch
> the exception and handle it gracefully.
> 
> If you really want to do it (or are just curious), the easiest way is to
> revert your addition of 'fmap Right' and replace the 'bracket' code with
> 'try' code.

On a second thought, you can't *replace* 'bracket' with 'try', because of
possible exceptions arising from 'k' (also, because of possible async
exceptions). Instead, you need to use both. Something like (untested)

  bracket
    (try $ openBinaryFile path ReadMode :: IO (Either IOException Handle))
    (either (const $ pure ()) hClose)
    k

Roman
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 819 bytes
Desc: Digital signature
URL: <http://www.haskell.org/pipermail/haskell-cafe/attachments/20140214/de4a4942/attachment.sig>


More information about the Haskell-Cafe mailing list