Safe Haskell | None |
---|---|
Language | Haskell2010 |
Test.Util.FS.Sim.MockFS
Description
Mock file system implementation
Intended for qualified import
import Test.Util.FS.Sim.MockFS (MockFS) import qualified Test.Util.FS.Sim.MockFS as Mock
Synopsis
- empty ∷ MockFS
- example ∷ MockFS
- handleIsOpen ∷ MockFS → HandleMock → Bool
- numOpenHandles ∷ MockFS → Int
- pretty ∷ MockFS → String
- dumpState ∷ CanSimFS m ⇒ m String
- hClose ∷ CanSimFS m ⇒ Handle' → m ()
- hGetSize ∷ CanSimFS m ⇒ Handle' → m Word64
- hGetSome ∷ CanSimFS m ⇒ Handle' → Word64 → m ByteString
- hGetSomeAt ∷ CanSimFS m ⇒ Handle' → Word64 → AbsOffset → m ByteString
- hIsOpen ∷ CanSimFS m ⇒ Handle' → m Bool
- hOpen ∷ CanSimFS m ⇒ FsPath → OpenMode → m Handle'
- hPutSome ∷ CanSimFS m ⇒ Handle' → ByteString → m Word64
- hSeek ∷ CanSimFS m ⇒ Handle' → SeekMode → Int64 → m ()
- hTruncate ∷ CanSimFS m ⇒ Handle' → Word64 → m ()
- createDirectory ∷ CanSimFS m ⇒ FsPath → m ()
- createDirectoryIfMissing ∷ CanSimFS m ⇒ Bool → FsPath → m ()
- doesDirectoryExist ∷ CanSimFS m ⇒ FsPath → m Bool
- doesFileExist ∷ CanSimFS m ⇒ FsPath → m Bool
- listDirectory ∷ CanSimFS m ⇒ FsPath → m (Set String)
- removeFile ∷ CanSimFS m ⇒ FsPath → m ()
- renameFile ∷ CanSimFS m ⇒ FsPath → FsPath → m ()
- type Files = FsTree ByteString
- mockFiles ∷ MockFS → Files
- data ClosedHandleState
- data FilePtr
- data HandleState
- data OpenHandleState
- data HandleMock
- data MockFS
Documentation
handleIsOpen ∷ MockFS → HandleMock → Bool Source #
Return True
iff the handle is open.
Throws an exception if the handle is unknown.
numOpenHandles ∷ MockFS → Int Source #
Number of open handles
pretty ∷ MockFS → String Source #
Renders the MockFS
in a human-readable fashion.
TODO: Right now does this not show the state of the handles.
Debugging
Operations on files
hGetSize ∷ CanSimFS m ⇒ Handle' → m Word64 Source #
Get file size
NOTE: In the mock implementation this is thread safe, because there can be only one writer, so concurrent threads cannot change the size of the file.
hGetSome ∷ CanSimFS m ⇒ Handle' → Word64 → m ByteString Source #
Get bytes from handle
NOTE: Unlike real I/O, we disallow hGetSome
on a handle in append mode.
hGetSomeAt ∷ CanSimFS m ⇒ Handle' → Word64 → AbsOffset → m ByteString Source #
Thread safe version of hGetSome
, which doesn't modify or read the file
offset.
hOpen ∷ CanSimFS m ⇒ FsPath → OpenMode → m Handle' Source #
Mock implementation of hOpen
.
NOTE: Differences from Posix:
- We do not support opening directories.
- We do not support more than one concurrent writer (we do however allow a writer and multiple concurrent readers)
- We do not support create file on ReadMode.
hPutSome ∷ CanSimFS m ⇒ Handle' → ByteString → m Word64 Source #
hSeek ∷ CanSimFS m ⇒ Handle' → SeekMode → Int64 → m () Source #
Mock implementation of hSeek
NOTE: This is more restricted than the IO version, because seek has some odd properties:
- We do not allow seeking at all on files in append mode
- We do not allow seeking past the end of the file
(this means that when using
SeekFromEnd
, the only valid offset is 0) - We do not return the new file offset
hTruncate ∷ CanSimFS m ⇒ Handle' → Word64 → m () Source #
Truncate a file
NOTE: Differences from Posix:
- Although this corresponds to Posix
ftruncate
, this can only be used to make files smaller, not larger. - We only support this in append mode. The reason is that Posix
ftruncate
does not modify the file offset, and adds padding with zeroes on subsequent writes. This is however not behaviour we want to emulate. In append mode however the Posix file offset is not used (and we don't even record it at all), appends always happen at the end of the file.
Operations on directories
createDirectory ∷ CanSimFS m ⇒ FsPath → m () Source #
createDirectoryIfMissing ∷ CanSimFS m ⇒ Bool → FsPath → m () Source #
doesDirectoryExist ∷ CanSimFS m ⇒ FsPath → m Bool Source #
Check if directory exists
It seems real I/O maps what would be "inapproriate device" errors to False.
doesFileExist ∷ CanSimFS m ⇒ FsPath → m Bool Source #
Check if file exists
See comments for doesDirectoryExist
.
removeFile ∷ CanSimFS m ⇒ FsPath → m () Source #
Remove a file
The behaviour of unlink
is to remove the file after all open file handles
that refer to it are closed. The open file handles referring to the file
can still be used to write/read to/from, while at the same time, the file
is invisible for all other operations.
We do not implement this behaviour and consider this a limitation of the mock file system, and throw an error when removing a file that still has open file handles to it.
In the state machine tests, removing the root directory may cause the IO
implementation to throw an FsInsufficientPermissions
error, depending on
the permissions of the temporary directory used to run the tests in. In
theory it should throw a FsResourceInappropriateType
error. To avoid this
mismatch during testing, we also consider removing the root folder a
limitation of the mock file system.
renameFile ∷ CanSimFS m ⇒ FsPath → FsPath → m () Source #
Exported for the benefit of tests only
opaque
data ClosedHandleState Source #
Instances
Show ClosedHandleState Source # | |
Defined in Test.Util.FS.Sim.MockFS | |
Generic ClosedHandleState Source # | |
Defined in Test.Util.FS.Sim.MockFS Methods | |
NoThunks ClosedHandleState Source # | |
Defined in Test.Util.FS.Sim.MockFS Methods noThunks ∷ Context → ClosedHandleState → IO (Maybe ThunkInfo) # wNoThunks ∷ Context → ClosedHandleState → IO (Maybe ThunkInfo) # | |
type Rep ClosedHandleState Source # | |
Defined in Test.Util.FS.Sim.MockFS type Rep ClosedHandleState = D1 ('MetaData "ClosedHandleState" "Test.Util.FS.Sim.MockFS" "ouroboros-consensus-test-0.1.0.0-inplace" 'False) (C1 ('MetaCons "ClosedHandle" 'PrefixI 'True) (S1 ('MetaSel ('Just "closedFilePath") 'NoSourceUnpackedness 'NoSourceStrictness 'DecidedLazy) (Rec0 FsPath))) |
File pointer
This is purely an internal abstraction.
Instances
Show FilePtr Source # | |
Generic FilePtr Source # | |
NoThunks FilePtr Source # | |
type Rep FilePtr Source # | |
Defined in Test.Util.FS.Sim.MockFS type Rep FilePtr = D1 ('MetaData "FilePtr" "Test.Util.FS.Sim.MockFS" "ouroboros-consensus-test-0.1.0.0-inplace" 'False) (C1 ('MetaCons "RW" 'PrefixI 'False) (S1 ('MetaSel ('Nothing ∷ Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool) :*: (S1 ('MetaSel ('Nothing ∷ Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Bool) :*: S1 ('MetaSel ('Nothing ∷ Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Word64))) :+: C1 ('MetaCons "Append" 'PrefixI 'False) (U1 ∷ Type → Type)) |
data HandleState Source #
Mock handle internal state
Instances
Show HandleState Source # | |
Defined in Test.Util.FS.Sim.MockFS | |
Generic HandleState Source # | |
Defined in Test.Util.FS.Sim.MockFS | |
NoThunks HandleState Source # | |
Defined in Test.Util.FS.Sim.MockFS Methods noThunks ∷ Context → HandleState → IO (Maybe ThunkInfo) # wNoThunks ∷ Context → HandleState → IO (Maybe ThunkInfo) # | |
type Rep HandleState Source # | |
Defined in Test.Util.FS.Sim.MockFS type Rep HandleState = D1 ('MetaData "HandleState" "Test.Util.FS.Sim.MockFS" "ouroboros-consensus-test-0.1.0.0-inplace" 'False) (C1 ('MetaCons "HandleOpen" 'PrefixI 'False) (S1 ('MetaSel ('Nothing ∷ Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 OpenHandleState)) :+: C1 ('MetaCons "HandleClosed" 'PrefixI 'False) (S1 ('MetaSel ('Nothing ∷ Maybe Symbol) 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 ClosedHandleState))) |
data OpenHandleState Source #
Instances
Show OpenHandleState Source # | |
Defined in Test.Util.FS.Sim.MockFS | |
Generic OpenHandleState Source # | |
Defined in Test.Util.FS.Sim.MockFS Methods from ∷ OpenHandleState → Rep OpenHandleState x Source # to ∷ Rep OpenHandleState x → OpenHandleState Source # | |
NoThunks OpenHandleState Source # | |
Defined in Test.Util.FS.Sim.MockFS Methods noThunks ∷ Context → OpenHandleState → IO (Maybe ThunkInfo) # wNoThunks ∷ Context → OpenHandleState → IO (Maybe ThunkInfo) # | |
type Rep OpenHandleState Source # | |
Defined in Test.Util.FS.Sim.MockFS type Rep OpenHandleState = D1 ('MetaData "OpenHandleState" "Test.Util.FS.Sim.MockFS" "ouroboros-consensus-test-0.1.0.0-inplace" 'False) (C1 ('MetaCons "OpenHandle" 'PrefixI 'True) (S1 ('MetaSel ('Just "openFilePath") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 FsPath) :*: S1 ('MetaSel ('Just "openPtr") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 FilePtr))) |
opaque
data HandleMock Source #
A mock handle to a file on disk.
This is only meaningful when interpreted against a MockFS
.
Instances
Instances
Show MockFS Source # | |
Generic MockFS Source # | |
NoThunks MockFS Source # | |
MonadState MockFS PureSimFS Source # | |
type Rep MockFS Source # | |
Defined in Test.Util.FS.Sim.MockFS type Rep MockFS = D1 ('MetaData "MockFS" "Test.Util.FS.Sim.MockFS" "ouroboros-consensus-test-0.1.0.0-inplace" 'False) (C1 ('MetaCons "MockFS" 'PrefixI 'True) (S1 ('MetaSel ('Just "mockFiles") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 Files) :*: (S1 ('MetaSel ('Just "mockHandles") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 (Map HandleMock HandleState)) :*: S1 ('MetaSel ('Just "mockNextHandle") 'NoSourceUnpackedness 'SourceStrict 'DecidedStrict) (Rec0 HandleMock)))) |