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.
 
 
 
 

86 lines
2.1 KiB

  1. {-# LANGUAGE OverloadedStrings #-}
  2. module HPath.IO.ReadFileSpec 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 "ReadFileSpec"
  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" "Blahfaselgagaga"
  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.readFile" $ do
  43. -- successes --
  44. it "readFile (Strict) file with content, everything clear" $ do
  45. out <- readFile' "fileWithContent"
  46. out `shouldBe` "Blahfaselgagaga"
  47. it "readFile (Strict) symlink, everything clear" $ do
  48. out <- readFile' "inputFileSymL"
  49. out `shouldBe` "Blahfaselgagaga"
  50. it "readFile (Strict) empty file, everything clear" $ do
  51. out <- readFile' "fileWithoutContent"
  52. out `shouldBe` ""
  53. -- posix failures --
  54. it "readFile (Strict) directory, wrong file type" $ do
  55. readFile' "alreadyExistsD"
  56. `shouldThrow` (\e -> ioeGetErrorType e == InappropriateType)
  57. it "readFile (Strict) file, no permissions" $ do
  58. readFile' "noPerms"
  59. `shouldThrow` (\e -> ioeGetErrorType e == PermissionDenied)
  60. it "readFile (Strict) file, no permissions on dir" $ do
  61. readFile' "noPermsD/inputFile"
  62. `shouldThrow` (\e -> ioeGetErrorType e == PermissionDenied)
  63. it "readFile (Strict) file, no such file" $ do
  64. readFile' "lalala"
  65. `shouldThrow` (\e -> ioeGetErrorType e == NoSuchThing)