You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

117 lines
3.0 KiB

  1. {-# LANGUAGE OverloadedStrings #-}
  2. module HPath.IO.DeleteDirRecursiveSpec where
  3. import Test.Hspec
  4. import System.IO.Error
  5. (
  6. ioeGetErrorType
  7. )
  8. import System.Posix.Files.ByteString
  9. (
  10. getSymbolicLinkStatus
  11. )
  12. import GHC.IO.Exception
  13. (
  14. IOErrorType(..)
  15. )
  16. import Utils
  17. upTmpDir :: IO ()
  18. upTmpDir = do
  19. setTmpDir "DeleteDirRecursiveSpec"
  20. createTmpDir
  21. setupFiles :: IO ()
  22. setupFiles = do
  23. createRegularFile' "file"
  24. createDir' "dir"
  25. createRegularFile' "dir/.keep"
  26. createSymlink' "dirSym" "dir/"
  27. createDir' "noPerms"
  28. createRegularFile' "noPerms/.keep"
  29. createDir' "noWritable"
  30. createRegularFile' "noWritable/.keep"
  31. cleanupFiles :: IO ()
  32. cleanupFiles = do
  33. deleteFile' "file"
  34. deleteFile' "dir/.keep"
  35. deleteDir' "dir"
  36. deleteFile' "dirSym"
  37. deleteFile' "noPerms/.keep"
  38. deleteDir' "noPerms"
  39. deleteFile' "noWritable/.keep"
  40. deleteDir' "noWritable"
  41. spec :: Spec
  42. spec = beforeAll_ (upTmpDir >> setupFiles) $ afterAll_ cleanupFiles $
  43. describe "HPath.IO.deleteDirRecursive" $ do
  44. -- successes --
  45. it "deleteDirRecursive, empty directory, all fine" $ do
  46. createDir' "testDir"
  47. deleteDirRecursive' "testDir"
  48. getSymbolicLinkStatus "testDir"
  49. `shouldThrow`
  50. (\e -> ioeGetErrorType e == NoSuchThing)
  51. it "deleteDirRecursive, empty directory with null permissions, all fine" $ do
  52. createDir' "noPerms/testDir"
  53. noPerms "noPerms/testDir"
  54. deleteDirRecursive' "noPerms/testDir"
  55. it "deleteDirRecursive, non-empty directory, all fine" $ do
  56. createDir' "nonEmpty"
  57. createDir' "nonEmpty/dir1"
  58. createDir' "nonEmpty/dir2"
  59. createDir' "nonEmpty/dir2/dir3"
  60. createRegularFile' "nonEmpty/file1"
  61. createRegularFile' "nonEmpty/dir1/file2"
  62. deleteDirRecursive' "nonEmpty"
  63. getSymbolicLinkStatus "nonEmpty"
  64. `shouldThrow`
  65. (\e -> ioeGetErrorType e == NoSuchThing)
  66. -- posix failures --
  67. it "deleteDirRecursive, can't open parent directory" $ do
  68. createDir' "noPerms/foo"
  69. noPerms "noPerms"
  70. (deleteDirRecursive' "noPerms/foo")
  71. `shouldThrow`
  72. (\e -> ioeGetErrorType e == PermissionDenied)
  73. normalDirPerms "noPerms"
  74. deleteDir' "noPerms/foo"
  75. it "deleteDirRecursive, can't write to parent directory" $ do
  76. createDir' "noWritable/foo"
  77. noWritableDirPerms "noWritable"
  78. (deleteDirRecursive' "noWritable/foo")
  79. `shouldThrow`
  80. (\e -> ioeGetErrorType e == PermissionDenied)
  81. normalDirPerms "noWritable"
  82. deleteDir' "noWritable/foo"
  83. it "deleteDirRecursive, wrong file type (symlink to directory)" $
  84. deleteDirRecursive' "dirSym"
  85. `shouldThrow`
  86. (\e -> ioeGetErrorType e == InappropriateType)
  87. it "deleteDirRecursive, wrong file type (regular file)" $
  88. deleteDirRecursive' "file"
  89. `shouldThrow`
  90. (\e -> ioeGetErrorType e == InappropriateType)
  91. it "deleteDirRecursive, directory does not exist" $
  92. deleteDirRecursive' "doesNotExist"
  93. `shouldThrow`
  94. (\e -> ioeGetErrorType e == NoSuchThing)