[Haskell-cafe] Problems using tasty-golden
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)
(try $ openBinaryFile path ReadMode :: IO (Either IOException Handle))
(either (const $ pure ()) hClose)
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 819 bytes
Desc: Digital signature
More information about the Haskell-Cafe