TESTS: also test directories with no permissions at all
This commit is contained in:
parent
98e8104602
commit
c76c27288d
120
test/Spec.hs
120
test/Spec.hs
@ -29,8 +29,13 @@ import System.Exit
|
|||||||
import System.Process
|
import System.Process
|
||||||
import System.Posix.Files.ByteString
|
import System.Posix.Files.ByteString
|
||||||
(
|
(
|
||||||
groupReadMode
|
accessModes
|
||||||
|
, groupExecuteMode
|
||||||
|
, groupReadMode
|
||||||
|
, nullFileMode
|
||||||
|
, otherExecuteMode
|
||||||
, otherReadMode
|
, otherReadMode
|
||||||
|
, ownerExecuteMode
|
||||||
, ownerReadMode
|
, ownerReadMode
|
||||||
, setFileMode
|
, setFileMode
|
||||||
, unionFileModes
|
, unionFileModes
|
||||||
@ -43,7 +48,7 @@ import System.Posix.Files.ByteString
|
|||||||
|
|
||||||
|
|
||||||
main :: IO ()
|
main :: IO ()
|
||||||
main = hspec $ before_ fixPermissions $ do
|
main = hspec $ before_ fixPermissions $ after_ revertPermissions $ do
|
||||||
let tests = [copyFileSpec
|
let tests = [copyFileSpec
|
||||||
, copyDirRecursiveSpec
|
, copyDirRecursiveSpec
|
||||||
, createDirSpec
|
, createDirSpec
|
||||||
@ -59,8 +64,7 @@ main = hspec $ before_ fixPermissions $ do
|
|||||||
|
|
||||||
-- TODO: deleteFile, deleteDir, deleteDirRecursive, getDirsFiles, getFileType
|
-- TODO: deleteFile, deleteDir, deleteDirRecursive, getDirsFiles, getFileType
|
||||||
where
|
where
|
||||||
fixPermissions = do
|
noWriteDirs = ["test/copyFileSpec/outputDirNoWrite"
|
||||||
let dirlist = ["test/copyFileSpec/outputDirNoWrite"
|
|
||||||
,"test/copyDirRecursiveSpec/noWritePerm"
|
,"test/copyDirRecursiveSpec/noWritePerm"
|
||||||
,"test/createDirSpec/noWritePerms"
|
,"test/createDirSpec/noWritePerms"
|
||||||
,"test/createRegularFileSpec/noWritePerms"
|
,"test/createRegularFileSpec/noWritePerms"
|
||||||
@ -68,7 +72,20 @@ main = hspec $ before_ fixPermissions $ do
|
|||||||
,"test/moveFile/noWritePerm"
|
,"test/moveFile/noWritePerm"
|
||||||
,"test/recreateSymlinkSpec/noWritePerm"
|
,"test/recreateSymlinkSpec/noWritePerm"
|
||||||
]
|
]
|
||||||
sequence_ $ fmap noWritable dirlist
|
noPermsDirs = ["test/copyFileSpec/noPerms"
|
||||||
|
,"test/copyDirRecursiveSpec/noPerms"
|
||||||
|
,"test/createDirSpec/noPerms"
|
||||||
|
,"test/createRegularFileSpec/noPerms"
|
||||||
|
,"test/renameFile/noPerms"
|
||||||
|
,"test/moveFile/noPerms"
|
||||||
|
,"test/recreateSymlinkSpec/noPerms"
|
||||||
|
]
|
||||||
|
fixPermissions = do
|
||||||
|
sequence_ $ fmap noWritableDirPerms noWriteDirs
|
||||||
|
sequence_ $ fmap noPerms noPermsDirs
|
||||||
|
revertPermissions = do
|
||||||
|
sequence_ $ fmap normalDirPerms noWriteDirs
|
||||||
|
sequence_ $ fmap normalDirPerms noPermsDirs
|
||||||
|
|
||||||
|
|
||||||
-------------
|
-------------
|
||||||
@ -105,6 +122,19 @@ copyFileSpec =
|
|||||||
`shouldThrow`
|
`shouldThrow`
|
||||||
(\e -> ioeGetErrorType e == PermissionDenied)
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
it "copyFile, cannot open output directory" $
|
||||||
|
copyFile' "test/copyFileSpec/inputFile"
|
||||||
|
"test/copyFileSpec/noPerms/outputFile"
|
||||||
|
`shouldThrow`
|
||||||
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
it "copyFile, cannot open source directory" $
|
||||||
|
copyFile' "test/copyFileSpec/noPerms/inputFile"
|
||||||
|
"test/copyFileSpec/outputFile"
|
||||||
|
`shouldThrow`
|
||||||
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
|
||||||
it "copyFile, wrong input type (symlink)" $
|
it "copyFile, wrong input type (symlink)" $
|
||||||
copyFile' "test/copyFileSpec/inputFileSymL"
|
copyFile' "test/copyFileSpec/inputFileSymL"
|
||||||
"test/copyFileSpec/outputFile"
|
"test/copyFileSpec/outputFile"
|
||||||
@ -166,6 +196,18 @@ copyDirRecursiveSpec =
|
|||||||
`shouldThrow`
|
`shouldThrow`
|
||||||
(\e -> ioeGetErrorType e == PermissionDenied)
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
it "copyDirRecursive, cannot open destination dir" $
|
||||||
|
copyDirRecursive' "test/copyDirRecursiveSpec/inputDir"
|
||||||
|
"test/copyDirRecursiveSpec/noPerms/foo"
|
||||||
|
`shouldThrow`
|
||||||
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
it "copyDirRecursive, cannot open source dir" $
|
||||||
|
copyDirRecursive' "test/copyDirRecursiveSpec/noPerms/inputDir"
|
||||||
|
"test/copyDirRecursiveSpec/foo"
|
||||||
|
`shouldThrow`
|
||||||
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
it "copyDirRecursive, destination dir already exists" $
|
it "copyDirRecursive, destination dir already exists" $
|
||||||
copyDirRecursive' "test/copyDirRecursiveSpec/inputDir"
|
copyDirRecursive' "test/copyDirRecursiveSpec/inputDir"
|
||||||
"test/copyDirRecursiveSpec/alreadyExistsD"
|
"test/copyDirRecursiveSpec/alreadyExistsD"
|
||||||
@ -217,6 +259,11 @@ createDirSpec =
|
|||||||
`shouldThrow`
|
`shouldThrow`
|
||||||
(\e -> ioeGetErrorType e == PermissionDenied)
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
it "createDir, can't open destination directory" $
|
||||||
|
createDir' "test/createDirSpec/noPerms/newDir"
|
||||||
|
`shouldThrow`
|
||||||
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
it "createDir, destination directory already exists" $
|
it "createDir, destination directory already exists" $
|
||||||
createDir' "test/createDirSpec/alreadyExists"
|
createDir' "test/createDirSpec/alreadyExists"
|
||||||
`shouldThrow`
|
`shouldThrow`
|
||||||
@ -237,6 +284,11 @@ createRegularFileSpec =
|
|||||||
`shouldThrow`
|
`shouldThrow`
|
||||||
(\e -> ioeGetErrorType e == PermissionDenied)
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
it "createRegularFile, can't write to destination directory" $
|
||||||
|
createRegularFile' "test/createRegularFileSpec/noPerms/newDir"
|
||||||
|
`shouldThrow`
|
||||||
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
it "createRegularFile, destination file already exists" $
|
it "createRegularFile, destination file already exists" $
|
||||||
createRegularFile' "test/createRegularFileSpec/alreadyExists"
|
createRegularFile' "test/createRegularFileSpec/alreadyExists"
|
||||||
`shouldThrow`
|
`shouldThrow`
|
||||||
@ -277,6 +329,18 @@ renameFileSpec =
|
|||||||
`shouldThrow`
|
`shouldThrow`
|
||||||
(\e -> ioeGetErrorType e == PermissionDenied)
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
it "renameFile, can't open destination directory" $
|
||||||
|
renameFile' "test/renameFile/myFile"
|
||||||
|
"test/renameFile/noPerms/renamedFile"
|
||||||
|
`shouldThrow`
|
||||||
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
it "renameFile, can't open source directory" $
|
||||||
|
renameFile' "test/renameFile/noPerms/myFile"
|
||||||
|
"test/renameFile/renamedFile"
|
||||||
|
`shouldThrow`
|
||||||
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
-- custom failures --
|
-- custom failures --
|
||||||
it "renameFile, destination file already exists" $
|
it "renameFile, destination file already exists" $
|
||||||
renameFile' "test/renameFile/myFile"
|
renameFile' "test/renameFile/myFile"
|
||||||
@ -331,6 +395,18 @@ moveFileSpec =
|
|||||||
`shouldThrow`
|
`shouldThrow`
|
||||||
(\e -> ioeGetErrorType e == PermissionDenied)
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
it "moveFile, can't open destination directory" $
|
||||||
|
moveFile' "test/moveFile/myFile"
|
||||||
|
"test/moveFile/noPerms/movedFile"
|
||||||
|
`shouldThrow`
|
||||||
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
it "moveFile, can't open source directory" $
|
||||||
|
moveFile' "test/moveFile/noPerms/myFile"
|
||||||
|
"test/moveFile/movedFile"
|
||||||
|
`shouldThrow`
|
||||||
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
-- custom failures --
|
-- custom failures --
|
||||||
it "moveFile, destination file already exists" $
|
it "moveFile, destination file already exists" $
|
||||||
moveFile' "test/moveFile/myFile"
|
moveFile' "test/moveFile/myFile"
|
||||||
@ -383,6 +459,18 @@ recreateSymlinkSpec =
|
|||||||
`shouldThrow`
|
`shouldThrow`
|
||||||
(\e -> ioeGetErrorType e == PermissionDenied)
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
it "recreateSymLink, can't open destination directory" $
|
||||||
|
recreateSymlink' "test/recreateSymlinkSpec/myFileL"
|
||||||
|
"test/recreateSymlinkSpec/noPerms/movedFile"
|
||||||
|
`shouldThrow`
|
||||||
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
|
it "recreateSymLink, can't open source directory" $
|
||||||
|
recreateSymlink' "test/recreateSymlinkSpec/noPerms/myFileL"
|
||||||
|
"test/recreateSymlinkSpec/movedFile"
|
||||||
|
`shouldThrow`
|
||||||
|
(\e -> ioeGetErrorType e == PermissionDenied)
|
||||||
|
|
||||||
it "recreateSymLink, destination file already exists" $
|
it "recreateSymLink, destination file already exists" $
|
||||||
recreateSymlink' "test/recreateSymlinkSpec/myFileL"
|
recreateSymlink' "test/recreateSymlinkSpec/myFileL"
|
||||||
"test/recreateSymlinkSpec/alreadyExists"
|
"test/recreateSymlinkSpec/alreadyExists"
|
||||||
@ -483,12 +571,30 @@ recreateSymlink' inputFileP outputFileP = do
|
|||||||
whenM (doesFileExist outputFile) (deleteFile outputFile)
|
whenM (doesFileExist outputFile) (deleteFile outputFile)
|
||||||
|
|
||||||
|
|
||||||
noWritable :: ByteString -> IO ()
|
noWritableDirPerms :: ByteString -> IO ()
|
||||||
noWritable path = do
|
noWritableDirPerms path = do
|
||||||
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||||
file <- (pwd P.</>) <$> P.parseRel path
|
file <- (pwd P.</>) <$> P.parseRel path
|
||||||
setFileMode (P.fromAbs file) perms
|
setFileMode (P.fromAbs file) perms
|
||||||
where
|
where
|
||||||
perms = ownerReadMode
|
perms = ownerReadMode
|
||||||
|
`unionFileModes` ownerExecuteMode
|
||||||
`unionFileModes` groupReadMode
|
`unionFileModes` groupReadMode
|
||||||
|
`unionFileModes` groupExecuteMode
|
||||||
`unionFileModes` otherReadMode
|
`unionFileModes` otherReadMode
|
||||||
|
`unionFileModes` otherExecuteMode
|
||||||
|
|
||||||
|
|
||||||
|
noPerms :: ByteString -> IO ()
|
||||||
|
noPerms path = do
|
||||||
|
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||||
|
file <- (pwd P.</>) <$> P.parseRel path
|
||||||
|
setFileMode (P.fromAbs file) nullFileMode
|
||||||
|
|
||||||
|
|
||||||
|
normalDirPerms :: ByteString -> IO ()
|
||||||
|
normalDirPerms path = do
|
||||||
|
pwd <- fromJust <$> getEnv "PWD" >>= P.parseAbs
|
||||||
|
file <- (pwd P.</>) <$> P.parseRel path
|
||||||
|
setFileMode (P.fromAbs file) accessModes
|
||||||
|
|
||||||
|
@ -0,0 +1,8 @@
|
|||||||
|
dadasasddas
|
||||||
|
sda
|
||||||
|
|
||||||
|
!!1
|
||||||
|
sda
|
||||||
|
|
||||||
|
|
||||||
|
11
|
@ -0,0 +1 @@
|
|||||||
|
dadasasddas
|
4
test/copyDirRecursiveSpec/noPerms/inputDir/inputFile2
Normal file
4
test/copyDirRecursiveSpec/noPerms/inputDir/inputFile2
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
dadasasddas
|
||||||
|
das
|
||||||
|
sda
|
||||||
|
sda
|
0
test/copyFileSpec/noPerms/inputFile
Normal file
0
test/copyFileSpec/noPerms/inputFile
Normal file
0
test/createDirSpec/noPerms/.keep
Normal file
0
test/createDirSpec/noPerms/.keep
Normal file
0
test/createRegularFileSpec/noPerms/.keep
Normal file
0
test/createRegularFileSpec/noPerms/.keep
Normal file
0
test/moveFile/noPerms/myFile
Normal file
0
test/moveFile/noPerms/myFile
Normal file
4
test/recreateSymlinkSpec/noPerms/myFile
Normal file
4
test/recreateSymlinkSpec/noPerms/myFile
Normal file
@ -0,0 +1,4 @@
|
|||||||
|
ads
|
||||||
|
sad
|
||||||
|
das
|
||||||
|
sda
|
1
test/recreateSymlinkSpec/noPerms/myFileL
Symbolic link
1
test/recreateSymlinkSpec/noPerms/myFileL
Symbolic link
@ -0,0 +1 @@
|
|||||||
|
myFile
|
0
test/renameFile/noPerms/myFile
Normal file
0
test/renameFile/noPerms/myFile
Normal file
Loading…
Reference in New Issue
Block a user