{-# LANGUAGE BangPatterns #-}

-- |
-- Module      : Test.Amazonka.Diff
-- Copyright   : (c) 2013-2021 Brendan Hay
-- License     : Mozilla Public License, v. 2.0.
-- Maintainer  : Brendan Hay <brendan.g.hay+amazonka@gmail.com>
-- Stability   : provisional
-- Portability : non-portable (GHC extensions)
module Test.Amazonka.Diff where

import System.IO
import System.IO.Temp
import System.Process
import Text.Groom

-- | Display the difference between two Haskell values,
-- with control over the diff parameters.
diff :: (Show a, Show b) => a -> b -> IO String
diff :: a -> b -> IO String
diff a
e b
a =
  String -> (String -> Handle -> IO String) -> IO String
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
String -> (String -> Handle -> m a) -> m a
withSystemTempFile String
"diff_expect" ((String -> Handle -> IO String) -> IO String)
-> (String -> Handle -> IO String) -> IO String
forall a b. (a -> b) -> a -> b
$ \String
ep Handle
eh ->
    String -> (String -> Handle -> IO String) -> IO String
forall (m :: * -> *) a.
(MonadIO m, MonadMask m) =>
String -> (String -> Handle -> m a) -> m a
withSystemTempFile String
"diff_actual" ((String -> Handle -> IO String) -> IO String)
-> (String -> Handle -> IO String) -> IO String
forall a b. (a -> b) -> a -> b
$ \String
ap Handle
ah -> do
      Handle -> String -> IO ()
hPutStrLn Handle
eh (a -> String
forall a. Show a => a -> String
groom a
e) IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Handle -> IO ()
hClose Handle
eh
      Handle -> String -> IO ()
hPutStrLn Handle
ah (b -> String
forall a. Show a => a -> String
groom b
a) IO () -> IO () -> IO ()
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> Handle -> IO ()
hClose Handle
ah
      (ExitCode
_, !String
out, !String
err) <-
        String -> [String] -> String -> IO (ExitCode, String, String)
readProcessWithExitCode String
"diff" [String
"-U", String
"3", String
ep, String
ap] []
      String -> IO String
forall (m :: * -> *) a. Monad m => a -> m a
return (String
out String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
"\n" String -> String -> String
forall a. [a] -> [a] -> [a]
++ String
err)