summaryrefslogtreecommitdiff
path: root/test/HPath/IO/ReadFileEOFSpec.hs
blob: 6a92b5259ddec45bc16c9cabebb5ef85584524b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
{-# LANGUAGE OverloadedStrings #-}


module HPath.IO.ReadFileEOFSpec where


import Test.Hspec
import System.IO.Error
  (
    ioeGetErrorType
  )
import GHC.IO.Exception
  (
    IOErrorType(..)
  )
import Utils



upTmpDir :: IO ()
upTmpDir = do
  setTmpDir "ReadFileEOFSpec"
  createTmpDir

setupFiles :: IO ()
setupFiles = do
  createRegularFile' "fileWithContent"
  createRegularFile' "fileWithoutContent"
  createSymlink' "inputFileSymL" "fileWithContent"
  createDir' "alreadyExistsD"
  createRegularFile' "noPerms"
  noPerms "noPerms"
  createDir' "noPermsD"
  createRegularFile' "noPermsD/inputFile"
  noPerms "noPermsD"
  writeFile' "fileWithContent" "Blahfaselgagaga"


cleanupFiles :: IO ()
cleanupFiles = do
  deleteFile' "fileWithContent"
  deleteFile' "fileWithoutContent"
  deleteFile' "inputFileSymL"
  deleteDir' "alreadyExistsD"
  normalFilePerms "noPerms"
  deleteFile' "noPerms"
  normalDirPerms "noPermsD"
  deleteFile' "noPermsD/inputFile"
  deleteDir' "noPermsD"


spec :: Spec
spec = beforeAll_ (upTmpDir >> setupFiles) $ afterAll_ cleanupFiles $
  describe "HPath.IO.readFileEOF" $ do

    -- successes --
    it "readFileEOF (Strict) file with content, everything clear" $ do
      out <- readFileEOF' "fileWithContent"
      out `shouldBe` "Blahfaselgagaga"

    it "readFileEOF (Strict) symlink, everything clear" $ do
      out <- readFileEOF' "inputFileSymL"
      out `shouldBe` "Blahfaselgagaga"

    it "readFileEOF (Strict) empty file, everything clear" $ do
      out <- readFileEOF' "fileWithoutContent"
      out `shouldBe` ""


    -- posix failures --
    it "readFileEOF (Strict) directory, wrong file type" $ do
      readFileEOF' "alreadyExistsD"
        `shouldThrow` (\e -> ioeGetErrorType e == InappropriateType)

    it "readFileEOF (Strict) file, no permissions" $ do
      readFileEOF' "noPerms"
        `shouldThrow` (\e -> ioeGetErrorType e == PermissionDenied)

    it "readFileEOF (Strict) file, no permissions on dir" $ do
      readFileEOF' "noPermsD/inputFile"
        `shouldThrow` (\e -> ioeGetErrorType e == PermissionDenied)

    it "readFileEOF (Strict) file, no such file" $ do
      readFileEOF' "lalala"
        `shouldThrow` (\e -> ioeGetErrorType e == NoSuchThing)