From 0feaf53a9d793b3970ff93a9fb24cd2893f3919e Mon Sep 17 00:00:00 2001 From: James Crayne Date: Wed, 27 Apr 2016 02:12:22 -0400 Subject: utils for writing tests --- testkiki/testkiki.hs | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) (limited to 'testkiki/testkiki.hs') diff --git a/testkiki/testkiki.hs b/testkiki/testkiki.hs index c45764f..606f268 100644 --- a/testkiki/testkiki.hs +++ b/testkiki/testkiki.hs @@ -16,6 +16,7 @@ import System.Exit import System.IO --import System.Posix.ByteString.FilePath import Control.Applicative +import Data.List import Control.Monad import qualified Data.ByteString.Char8 as B import Data.Time.Clock @@ -23,6 +24,7 @@ import Data.Time.Clock.POSIX import Data.IORef import Crypto.Hash.SHA1 (hash) import System.IO.Unsafe (unsafePerformIO) +import ProcessUtils #if !MIN_VERSION_base(4,7,0) setEnv k v = System.Posix.Env.setEnv k v True @@ -305,3 +307,44 @@ doTests tkConfig = hspec $ do appendpaths config str = TKS { gnupghome = gnupghome config ++ str , chroot = chroot config ++ str } + + + +safeFileInfo :: FilePath -> IO (UTCTime,FilePath) +safeFileInfo file = do + mtime <- getModificationTime file + let folder = takeDirectory file + fileDotOld = file ++ ".old" + readProcess "cp" ["-aR",file,fileDotOld] "" + return (mtime,fileDotOld) + +compareSha1 :: FilePath -> IO Bool +compareSha1 file = do + let folder = takeDirectory file + fileDotOld = file ++ ".old" + hash1 <- hash <$> B.readFile fileDotOld + hash2 <- hash <$> B.readFile file + return (hash1 == hash2) + +getMTimes :: FilePath -> IO (UTCTime, UTCTime) +getMTimes file = (,) <$> getModificationTime oldfile <*> getModificationTime file + where oldfile = file ++ ".old" + +getLineCounts :: FilePath -> IO (Int, Int) +getLineCounts file = (,) <$> l oldfile <*> l file + where oldfile = file ++ ".old" + l x = length . B.lines <$> B.readFile x + +linesSubtractedAndAdded :: FilePath -> IO (Int,Int) +linesSubtractedAndAdded file = counts <$> readPipe [("diff",["-u",oldfile,file]),("sed",["-n","4,$ p"])] "" + where oldfile = file ++ ".old" + counts x = ( length $ filter ("-" `isPrefixOf`) (lines x) + , length $ filter ("+" `isPrefixOf`) (lines x) ) + +saveOutputOfRun :: FilePath -> IO String -> IO ((FilePath -> IO a) -> (IO a)) +saveOutputOfRun fileToFill action = do + action >>= writeFile (fileToFill ++ ".old") + return $ \getSomething -> do + bRanAgain <- doesFileExist fileToFill + when (not bRanAgain) $ action >>= writeFile fileToFill + getSomething fileToFill -- cgit v1.2.3