1 module Golden.Utils where
3 import Control.Monad (Monad(..))
4 import Data.Either (Either(..))
5 import Data.Function (($))
6 import Data.Semigroup (Semigroup(..))
7 import Data.String (String)
8 import System.IO (IO, FilePath)
9 import System.IO.Unsafe (unsafePerformIO)
10 import qualified Data.ByteString.Lazy as BSL
11 import qualified Data.IORef as IORef
13 import qualified Data.Text.Lazy as TL
14 import qualified Data.Text.Lazy.Encoding as TL
15 import qualified Language.Haskell.TH.Syntax as TH
16 import Paths_symantic_parser
18 getGoldenDir :: FilePath -> FilePath
19 getGoldenDir p = unsafePerformIO $ getDataFileName $ "test/Golden/" <> p
21 goldenDiff :: FilePath -> FilePath -> [String]
22 goldenDiff ref new = ["diff", "-u", "-w", "-B", ref, new]
24 -- | Resetting 'TH.counter' makes 'makeLetName' deterministic,
25 -- except when GHC or executable flags change, like profiling
26 -- or even --accept unfortunately,
27 -- in those case the 'goldensMachine' tests may fail
28 -- due to a different numbering of the 'def' and 'ref' combinators.
29 -- Hence 'ShowLetName' is used with 'False'.
30 resetTHNameCounter :: IO ()
31 resetTHNameCounter = IORef.writeIORef TH.counter 0
33 unLeft :: Either String BSL.ByteString -> IO BSL.ByteString
34 unLeft lr = case lr of
35 Left err -> return $ TL.encodeUtf8 $ TL.pack err