summaryrefslogtreecommitdiff
path: root/testkiki
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2016-04-27 02:12:22 -0400
committerJames Crayne <jim.crayne@gmail.com>2016-04-27 02:12:22 -0400
commit0feaf53a9d793b3970ff93a9fb24cd2893f3919e (patch)
tree63d614e6bfd8ba35878bf0efb465c67da54b09fd /testkiki
parent582ad0423e28e950619f5c61e04fcf4da49b0cd1 (diff)
utils for writing tests
Diffstat (limited to 'testkiki')
-rw-r--r--testkiki/testkiki.hs43
1 files changed, 43 insertions, 0 deletions
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
16import System.IO 16import System.IO
17--import System.Posix.ByteString.FilePath 17--import System.Posix.ByteString.FilePath
18import Control.Applicative 18import Control.Applicative
19import Data.List
19import Control.Monad 20import Control.Monad
20import qualified Data.ByteString.Char8 as B 21import qualified Data.ByteString.Char8 as B
21import Data.Time.Clock 22import Data.Time.Clock
@@ -23,6 +24,7 @@ import Data.Time.Clock.POSIX
23import Data.IORef 24import Data.IORef
24import Crypto.Hash.SHA1 (hash) 25import Crypto.Hash.SHA1 (hash)
25import System.IO.Unsafe (unsafePerformIO) 26import System.IO.Unsafe (unsafePerformIO)
27import ProcessUtils
26 28
27#if !MIN_VERSION_base(4,7,0) 29#if !MIN_VERSION_base(4,7,0)
28setEnv k v = System.Posix.Env.setEnv k v True 30setEnv k v = System.Posix.Env.setEnv k v True
@@ -305,3 +307,44 @@ doTests tkConfig = hspec $ do
305 appendpaths config str = TKS { gnupghome = gnupghome config ++ str 307 appendpaths config str = TKS { gnupghome = gnupghome config ++ str
306 , chroot = chroot config ++ str 308 , chroot = chroot config ++ str
307 } 309 }
310
311
312
313safeFileInfo :: FilePath -> IO (UTCTime,FilePath)
314safeFileInfo file = do
315 mtime <- getModificationTime file
316 let folder = takeDirectory file
317 fileDotOld = file ++ ".old"
318 readProcess "cp" ["-aR",file,fileDotOld] ""
319 return (mtime,fileDotOld)
320
321compareSha1 :: FilePath -> IO Bool
322compareSha1 file = do
323 let folder = takeDirectory file
324 fileDotOld = file ++ ".old"
325 hash1 <- hash <$> B.readFile fileDotOld
326 hash2 <- hash <$> B.readFile file
327 return (hash1 == hash2)
328
329getMTimes :: FilePath -> IO (UTCTime, UTCTime)
330getMTimes file = (,) <$> getModificationTime oldfile <*> getModificationTime file
331 where oldfile = file ++ ".old"
332
333getLineCounts :: FilePath -> IO (Int, Int)
334getLineCounts file = (,) <$> l oldfile <*> l file
335 where oldfile = file ++ ".old"
336 l x = length . B.lines <$> B.readFile x
337
338linesSubtractedAndAdded :: FilePath -> IO (Int,Int)
339linesSubtractedAndAdded file = counts <$> readPipe [("diff",["-u",oldfile,file]),("sed",["-n","4,$ p"])] ""
340 where oldfile = file ++ ".old"
341 counts x = ( length $ filter ("-" `isPrefixOf`) (lines x)
342 , length $ filter ("+" `isPrefixOf`) (lines x) )
343
344saveOutputOfRun :: FilePath -> IO String -> IO ((FilePath -> IO a) -> (IO a))
345saveOutputOfRun fileToFill action = do
346 action >>= writeFile (fileToFill ++ ".old")
347 return $ \getSomething -> do
348 bRanAgain <- doesFileExist fileToFill
349 when (not bRanAgain) $ action >>= writeFile fileToFill
350 getSomething fileToFill