Fix tests for sdist
We now create the necessary directories and files for the tests on-the-fly.
This commit is contained in:
112
test/Utils.hs
112
test/Utils.hs
@@ -1,4 +1,6 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE PackageImports #-}
|
||||
|
||||
|
||||
module Utils where
|
||||
|
||||
@@ -7,6 +9,10 @@ import Control.Applicative
|
||||
(
|
||||
(<$>)
|
||||
)
|
||||
import Control.Monad
|
||||
(
|
||||
void
|
||||
)
|
||||
import HPath.IO
|
||||
import HPath.IO.Errors
|
||||
import HPath.IO.Utils
|
||||
@@ -36,6 +42,14 @@ import System.Posix.Files.ByteString
|
||||
, unionFileModes
|
||||
)
|
||||
|
||||
import qualified "unix" System.Posix.IO.ByteString as SPI
|
||||
import qualified "unix-bytestring" System.Posix.IO.ByteString as SPB
|
||||
|
||||
|
||||
|
||||
tmpDir :: ByteString
|
||||
tmpDir = "test/HPath/IO/tmp/"
|
||||
|
||||
|
||||
|
||||
-----------------
|
||||
@@ -43,90 +57,118 @@ import System.Posix.Files.ByteString
|
||||
-----------------
|
||||
|
||||
|
||||
withPwd :: ByteString -> (P.Path P.Abs -> IO a) -> IO a
|
||||
withPwd ip f = do
|
||||
createTmpDir :: IO ()
|
||||
createTmpDir = do
|
||||
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||
p <- (pwd P.</>) <$> P.parseRel ip
|
||||
tmp <- P.parseRel tmpDir
|
||||
void $ createDir (pwd P.</> tmp)
|
||||
|
||||
|
||||
deleteTmpDir :: IO ()
|
||||
deleteTmpDir = do
|
||||
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||
tmp <- P.parseRel tmpDir
|
||||
void $ deleteDir (pwd P.</> tmp)
|
||||
|
||||
|
||||
withRawTmpDir :: (P.Path P.Abs -> IO a) -> IO a
|
||||
withRawTmpDir f = do
|
||||
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||
tmp <- P.parseRel tmpDir
|
||||
f (pwd P.</> tmp)
|
||||
|
||||
|
||||
withTmpDir :: ByteString -> (P.Path P.Abs -> IO a) -> IO a
|
||||
withTmpDir ip f = do
|
||||
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||
tmp <- P.parseRel tmpDir
|
||||
p <- (pwd P.</> tmp P.</>) <$> P.parseRel ip
|
||||
f p
|
||||
|
||||
|
||||
withPwd' :: ByteString
|
||||
-> ByteString
|
||||
-> (P.Path P.Abs -> P.Path P.Abs -> IO a)
|
||||
-> IO a
|
||||
withPwd' ip1 ip2 f = do
|
||||
withTmpDir' :: ByteString
|
||||
-> ByteString
|
||||
-> (P.Path P.Abs -> P.Path P.Abs -> IO a)
|
||||
-> IO a
|
||||
withTmpDir' ip1 ip2 f = do
|
||||
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||
p1 <- (pwd P.</>) <$> P.parseRel ip1
|
||||
p2 <- (pwd P.</>) <$> P.parseRel ip2
|
||||
tmp <- P.parseRel tmpDir
|
||||
p1 <- (pwd P.</> tmp P.</>) <$> P.parseRel ip1
|
||||
p2 <- (pwd P.</> tmp P.</>) <$> P.parseRel ip2
|
||||
f p1 p2
|
||||
|
||||
|
||||
removeFileIfExists :: ByteString -> IO ()
|
||||
removeFileIfExists bs =
|
||||
withPwd bs $ \p -> whenM (doesFileExist p) (deleteFile p)
|
||||
withTmpDir bs $ \p -> whenM (doesFileExist p) (deleteFile p)
|
||||
|
||||
|
||||
removeDirIfExists :: ByteString -> IO ()
|
||||
removeDirIfExists bs =
|
||||
withPwd bs $ \p -> whenM (doesDirectoryExist p) (deleteDirRecursive p)
|
||||
withTmpDir bs $ \p -> whenM (doesDirectoryExist p) (deleteDirRecursive p)
|
||||
|
||||
|
||||
copyFile' :: ByteString -> ByteString -> IO ()
|
||||
copyFile' inputFileP outputFileP =
|
||||
withPwd' inputFileP outputFileP copyFile
|
||||
withTmpDir' inputFileP outputFileP copyFile
|
||||
|
||||
|
||||
copyFileOverwrite' :: ByteString -> ByteString -> IO ()
|
||||
copyFileOverwrite' inputFileP outputFileP =
|
||||
withPwd' inputFileP outputFileP copyFileOverwrite
|
||||
withTmpDir' inputFileP outputFileP copyFileOverwrite
|
||||
|
||||
|
||||
copyDirRecursive' :: ByteString -> ByteString -> IO ()
|
||||
copyDirRecursive' inputDirP outputDirP =
|
||||
withPwd' inputDirP outputDirP copyDirRecursive
|
||||
withTmpDir' inputDirP outputDirP copyDirRecursive
|
||||
|
||||
|
||||
copyDirRecursiveOverwrite' :: ByteString -> ByteString -> IO ()
|
||||
copyDirRecursiveOverwrite' inputDirP outputDirP =
|
||||
withPwd' inputDirP outputDirP copyDirRecursiveOverwrite
|
||||
withTmpDir' inputDirP outputDirP copyDirRecursiveOverwrite
|
||||
|
||||
|
||||
createDir' :: ByteString -> IO ()
|
||||
createDir' dest = withPwd dest createDir
|
||||
createDir' dest = withTmpDir dest createDir
|
||||
|
||||
|
||||
createRegularFile' :: ByteString -> IO ()
|
||||
createRegularFile' dest = withPwd dest createRegularFile
|
||||
createRegularFile' dest = withTmpDir dest createRegularFile
|
||||
|
||||
|
||||
createSymlink' :: ByteString -> ByteString -> IO ()
|
||||
createSymlink' dest sympoint = withTmpDir dest
|
||||
(\x -> createSymlink x sympoint)
|
||||
|
||||
|
||||
renameFile' :: ByteString -> ByteString -> IO ()
|
||||
renameFile' inputFileP outputFileP =
|
||||
withPwd' inputFileP outputFileP $ \i o -> do
|
||||
withTmpDir' inputFileP outputFileP $ \i o -> do
|
||||
renameFile i o
|
||||
renameFile o i
|
||||
|
||||
|
||||
moveFile' :: ByteString -> ByteString -> IO ()
|
||||
moveFile' inputFileP outputFileP =
|
||||
withPwd' inputFileP outputFileP $ \i o -> do
|
||||
withTmpDir' inputFileP outputFileP $ \i o -> do
|
||||
moveFile i o
|
||||
moveFile o i
|
||||
|
||||
|
||||
moveFileOverwrite' :: ByteString -> ByteString -> IO ()
|
||||
moveFileOverwrite' inputFileP outputFileP =
|
||||
withPwd' inputFileP outputFileP $ \i o -> do
|
||||
withTmpDir' inputFileP outputFileP $ \i o -> do
|
||||
moveFileOverwrite i o
|
||||
moveFile o i
|
||||
|
||||
|
||||
recreateSymlink' :: ByteString -> ByteString -> IO ()
|
||||
recreateSymlink' inputFileP outputFileP =
|
||||
withPwd' inputFileP outputFileP recreateSymlink
|
||||
withTmpDir' inputFileP outputFileP recreateSymlink
|
||||
|
||||
|
||||
noWritableDirPerms :: ByteString -> IO ()
|
||||
noWritableDirPerms path = withPwd path $ \p ->
|
||||
noWritableDirPerms path = withTmpDir path $ \p ->
|
||||
setFileMode (P.fromAbs p) perms
|
||||
where
|
||||
perms = ownerReadMode
|
||||
@@ -138,34 +180,42 @@ noWritableDirPerms path = withPwd path $ \p ->
|
||||
|
||||
|
||||
noPerms :: ByteString -> IO ()
|
||||
noPerms path = withPwd path $ \p -> setFileMode (P.fromAbs p) nullFileMode
|
||||
noPerms path = withTmpDir path $ \p -> setFileMode (P.fromAbs p) nullFileMode
|
||||
|
||||
|
||||
normalDirPerms :: ByteString -> IO ()
|
||||
normalDirPerms path =
|
||||
withPwd path $ \p -> setFileMode (P.fromAbs p) newDirPerms
|
||||
withTmpDir path $ \p -> setFileMode (P.fromAbs p) newDirPerms
|
||||
|
||||
|
||||
getFileType' :: ByteString -> IO FileType
|
||||
getFileType' path = withPwd path getFileType
|
||||
getFileType' path = withTmpDir path getFileType
|
||||
|
||||
|
||||
getDirsFiles' :: ByteString -> IO [P.Path P.Abs]
|
||||
getDirsFiles' path = withPwd path getDirsFiles
|
||||
getDirsFiles' path = withTmpDir path getDirsFiles
|
||||
|
||||
|
||||
deleteFile' :: ByteString -> IO ()
|
||||
deleteFile' p = withPwd p deleteFile
|
||||
deleteFile' p = withTmpDir p deleteFile
|
||||
|
||||
|
||||
deleteDir' :: ByteString -> IO ()
|
||||
deleteDir' p = withPwd p deleteDir
|
||||
deleteDir' p = withTmpDir p deleteDir
|
||||
|
||||
|
||||
deleteDirRecursive' :: ByteString -> IO ()
|
||||
deleteDirRecursive' p = withPwd p deleteDirRecursive
|
||||
deleteDirRecursive' p = withTmpDir p deleteDirRecursive
|
||||
|
||||
|
||||
canonicalizePath' :: ByteString -> IO (P.Path P.Abs)
|
||||
canonicalizePath' p = withPwd p canonicalizePath
|
||||
canonicalizePath' p = withTmpDir p canonicalizePath
|
||||
|
||||
|
||||
writeFile' :: ByteString -> ByteString -> IO ()
|
||||
writeFile' ip bs =
|
||||
withTmpDir ip $ \p -> do
|
||||
fd <- SPI.openFd (P.fromAbs p) SPI.WriteOnly Nothing
|
||||
SPI.defaultFileFlags
|
||||
SPB.fdWrite fd bs
|
||||
SPI.closeFd fd
|
||||
|
||||
Reference in New Issue
Block a user