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.
 
 
 
 

109 lines
3.1 KiB

  1. {-# LANGUAGE OverloadedStrings #-}
  2. module HPath.IO.AppendFileSpec where
  3. import Test.Hspec
  4. import System.IO.Error
  5. (
  6. ioeGetErrorType
  7. )
  8. import GHC.IO.Exception
  9. (
  10. IOErrorType(..)
  11. )
  12. import Utils
  13. upTmpDir :: IO ()
  14. upTmpDir = do
  15. setTmpDir "AppendFileSpec"
  16. createTmpDir
  17. setupFiles :: IO ()
  18. setupFiles = do
  19. createRegularFile' "fileWithContent"
  20. createRegularFile' "fileWithoutContent"
  21. createSymlink' "inputFileSymL" "fileWithContent"
  22. createDir' "alreadyExistsD"
  23. createRegularFile' "noPerms"
  24. noPerms "noPerms"
  25. createDir' "noPermsD"
  26. createRegularFile' "noPermsD/inputFile"
  27. noPerms "noPermsD"
  28. writeFile' "fileWithContent" "BLKASL"
  29. cleanupFiles :: IO ()
  30. cleanupFiles = do
  31. deleteFile' "fileWithContent"
  32. deleteFile' "fileWithoutContent"
  33. deleteFile' "inputFileSymL"
  34. deleteDir' "alreadyExistsD"
  35. normalFilePerms "noPerms"
  36. deleteFile' "noPerms"
  37. normalDirPerms "noPermsD"
  38. deleteFile' "noPermsD/inputFile"
  39. deleteDir' "noPermsD"
  40. spec :: Spec
  41. spec = beforeAll_ (upTmpDir >> setupFiles) $ afterAll_ cleanupFiles $
  42. describe "HPath.IO.appendFile" $ do
  43. -- successes --
  44. it "appendFile file with content, everything clear" $ do
  45. appendFile' "fileWithContent" "blahfaselllll"
  46. out <- readFile' "fileWithContent"
  47. out `shouldBe` "BLKASLblahfaselllll"
  48. it "appendFile file with content, everything clear" $ do
  49. appendFile' "fileWithContent" "gagagaga"
  50. out <- readFile' "fileWithContent"
  51. out `shouldBe` "BLKASLblahfaselllllgagagaga"
  52. it "appendFile file with content, everything clear" $ do
  53. appendFile' "fileWithContent" ""
  54. out <- readFile' "fileWithContent"
  55. out `shouldBe` "BLKASLblahfaselllllgagagaga"
  56. it "appendFile file without content, everything clear" $ do
  57. appendFile' "fileWithoutContent" "blahfaselllll"
  58. out <- readFile' "fileWithoutContent"
  59. out `shouldBe` "blahfaselllll"
  60. it "appendFile, everything clear" $ do
  61. appendFile' "fileWithoutContent" "gagagaga"
  62. out <- readFile' "fileWithoutContent"
  63. out `shouldBe` "blahfaselllllgagagaga"
  64. it "appendFile symlink, everything clear" $ do
  65. appendFile' "inputFileSymL" "blahfaselllll"
  66. out <- readFile' "inputFileSymL"
  67. out `shouldBe` "BLKASLblahfaselllllgagagagablahfaselllll"
  68. it "appendFile symlink, everything clear" $ do
  69. appendFile' "inputFileSymL" "gagagaga"
  70. out <- readFile' "inputFileSymL"
  71. out `shouldBe` "BLKASLblahfaselllllgagagagablahfaselllllgagagaga"
  72. -- posix failures --
  73. it "appendFile to dir, inappropriate type" $ do
  74. appendFile' "alreadyExistsD" ""
  75. `shouldThrow` (\e -> ioeGetErrorType e == InappropriateType)
  76. it "appendFile, no permissions to file" $ do
  77. appendFile' "noPerms" ""
  78. `shouldThrow` (\e -> ioeGetErrorType e == PermissionDenied)
  79. it "appendFile, no permissions to file" $ do
  80. appendFile' "noPermsD/inputFile" ""
  81. `shouldThrow` (\e -> ioeGetErrorType e == PermissionDenied)
  82. it "appendFile, file does not exist" $ do
  83. appendFile' "gaga" ""
  84. `shouldThrow` (\e -> ioeGetErrorType e == NoSuchThing)