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.
 
 
 
 

115 lines
2.7 KiB

  1. {-# LANGUAGE OverloadedStrings #-}
  2. module HPath.IO.DeleteDirSpec 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 "DeleteDirSpec"
  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.deleteDir" $ do
  44. -- successes --
  45. it "deleteDir, empty directory, all fine" $ do
  46. createDir' "testDir"
  47. deleteDir' "testDir"
  48. getSymbolicLinkStatus "testDir"
  49. `shouldThrow`
  50. (\e -> ioeGetErrorType e == NoSuchThing)
  51. it "deleteDir, directory with null permissions, all fine" $ do
  52. createDir' "noPerms/testDir"
  53. noPerms "noPerms/testDir"
  54. deleteDir' "noPerms/testDir"
  55. getSymbolicLinkStatus "testDir"
  56. `shouldThrow`
  57. (\e -> ioeGetErrorType e == NoSuchThing)
  58. -- posix failures --
  59. it "deleteDir, wrong file type (symlink to directory)" $
  60. deleteDir' "dirSym"
  61. `shouldThrow`
  62. (\e -> ioeGetErrorType e == InappropriateType)
  63. it "deleteDir, wrong file type (regular file)" $
  64. deleteDir' "file"
  65. `shouldThrow`
  66. (\e -> ioeGetErrorType e == InappropriateType)
  67. it "deleteDir, directory does not exist" $
  68. deleteDir' "doesNotExist"
  69. `shouldThrow`
  70. (\e -> ioeGetErrorType e == NoSuchThing)
  71. it "deleteDir, directory not empty" $
  72. deleteDir' "dir"
  73. `shouldThrow`
  74. (\e -> ioeGetErrorType e == UnsatisfiedConstraints)
  75. it "deleteDir, can't open parent directory" $ do
  76. createDir' "noPerms/foo"
  77. noPerms "noPerms"
  78. (deleteDir' "noPerms/foo")
  79. `shouldThrow`
  80. (\e -> ioeGetErrorType e == PermissionDenied)
  81. normalDirPerms "noPerms"
  82. deleteDir' "noPerms/foo"
  83. it "deleteDir, can't write to parent directory, still fine" $ do
  84. createDir' "noWritable/foo"
  85. noWritableDirPerms "noWritable"
  86. (deleteDir' "noWritable/foo")
  87. `shouldThrow`
  88. (\e -> ioeGetErrorType e == PermissionDenied)
  89. normalDirPerms "noWritable"
  90. deleteDir' "noWritable/foo"