
* Roman Cheplyaka
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