Skip to content

Instantly share code, notes, and snippets.

@hasufell hasufell/Spec.hs
Created May 2, 2016

Embed
What would you like to do?
{-# LANGUAGE OverloadedStrings #-}
import Test.Hspec
import HSFM.FileSystem.FileOperations
import Data.Maybe
(
fromJust
)
import qualified HPath as P
import System.Posix.Env.ByteString
(
getEnv
)
import HSFM.FileSystem.Errors
import HSFM.Utils.IO
import System.IO.Error
(
ioeGetErrorType
)
import GHC.IO.Exception
(
IOErrorType(..)
)
import Data.ByteString
(
ByteString
)
main :: IO ()
main = hspec $ do
copyFileSpec
createDirSpec
createRegularFileSpec
-------------
--[ Specs ]--
-------------
copyFileSpec :: Spec
copyFileSpec =
-- TODO: chardev, blockdev, namedpipe, socket
describe "HSFM.FileSystem.FileOperations.copyFile" $ do
it "copyFile, everything clear" $
copyFile' "test/copyFileSpec/inputFile"
"test/copyFileSpec/outputFile"
it "copyFile, input file does not exist" $
copyFile' "test/copyFileSpec/noSuchFile"
"test/copyFileSpec/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == NoSuchThing)
it "copyFile, no permission to open input file" $
copyFile' "test/copyFileSpec/inputNoPerm"
"test/copyFileSpec/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "copyFile, no permission to open output directory" $
copyFile' "test/copyFileSpec/inputFile"
"test/copyFileSpec/outputDirNoPerm/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "copyFile, no permission to write to output directory" $
copyFile' "test/copyFileSpec/inputFile"
"test/copyFileSpec/outputDirNoWrite/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
-- FIXME (via O_NOFOLLOW)
{- it "copyFile, wrong input type (symlink)" $ do -}
{- copyFileSpec "test/copyFileSpec/inputFileSymL" -}
{- "test/copyFileSpec/outputFile" -}
{- `shouldThrow` -}
{- (\e -> ioeGetErrorType e == InappropriateType) -}
it "copyFile, wrong input type (directory)" $
copyFile' "test/copyFileSpec/wrongInput"
"test/copyFileSpec/outputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == InappropriateType)
it "copyFile, output and input are same file" $
copyFile' "test/copyFileSpec/inputFile"
"test/copyFileSpec/inputFile"
`shouldThrow`
(\e -> ioeGetErrorType e == AlreadyExists)
it "copyFile, output file already exists" $
copyFile' "test/copyFileSpec/inputFile"
"test/copyFileSpec/alreadyExists"
`shouldThrow`
(\e -> ioeGetErrorType e == AlreadyExists)
it "copyFile, output file already exists and is a dir" $
copyFile' "test/copyFileSpec/inputFile"
"test/copyFileSpec/alreadyExistsD"
`shouldThrow`
(\e -> ioeGetErrorType e == AlreadyExists)
createDirSpec :: Spec
createDirSpec =
describe "HSFM.FileSystem.FileOperations.createDir" $ do
it "createDir, all fine" $
createDir' "test/createDirSpec/newDir"
it "createDir, can't open destination directory" $
createDir' "test/createDirSpec/noPerms/newDir"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "createDir, can't write to destination directory" $
createDir' "test/createDirSpec/noWritePerms/newDir"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "createDir, destination directory already exists" $
createDir' "test/createDirSpec/alreadyExists"
`shouldThrow`
(\e -> ioeGetErrorType e == AlreadyExists)
createRegularFileSpec :: Spec
createRegularFileSpec =
describe "HSFM.FileSystem.FileOperations.createRegularFile" $ do
it "createRegularFile, all fine" $
createRegularFile' "test/createRegularFileSpec/newDir"
it "createRegularFile, can't open destination directory" $
createRegularFile' "test/createRegularFileSpec/noPerms/newDir"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "createRegularFile, can't write to destination directory" $
createRegularFile' "test/createRegularFileSpec/noWritePerms/newDir"
`shouldThrow`
(\e -> ioeGetErrorType e == PermissionDenied)
it "createRegularFile, destination file already exists" $
createRegularFile' "test/createRegularFileSpec/alreadyExists"
`shouldThrow`
(\e -> ioeGetErrorType e == AlreadyExists)
-----------------
--[ Utilities ]--
-----------------
copyFile' :: ByteString -> ByteString -> IO ()
copyFile' inputFileP outputFileP = do
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
inputFile <- (pwd P.</>) <$> P.parseRel inputFileP
outputFile <- (pwd P.</>) <$> P.parseRel outputFileP
copyFile inputFile outputFile
whenM (doesFileExist outputFile) (deleteFile outputFile)
createDir' :: ByteString -> IO ()
createDir' dest = do
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
outputDir <- (pwd P.</>) <$> P.parseRel dest
createDir outputDir
whenM (doesDirectoryExist outputDir) (deleteDir outputDir)
createRegularFile' :: ByteString -> IO ()
createRegularFile' dest = do
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
outputFile <- (pwd P.</>) <$> P.parseRel dest
createRegularFile outputFile
whenM (doesFileExist outputFile) (deleteFile outputFile)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.