FakeFileSystem

class FakeFileSystem(clock: Clock) : FileSystem

A fully in-memory file system useful for testing. It includes features to support writing better tests.

Use openPaths to see which paths have been opened for read or write, but not yet closed. Tests should call checkNoOpenFiles in tearDown() to confirm that no file streams were leaked.

Strict By Default

-----------------

These actions are not allowed and throw an IOException if attempted:

  • Moving a file that is currently open for reading or writing.

  • Deleting a file that is currently open for reading or writing.

  • Moving a file to a path that currently resolves to an empty directory.

  • Reading and writing the same file at the same time.

  • Opening a file for writing that is already open for writing.

Programs that do not attempt any of the above operations should work fine on both UNIX and Windows systems. Relax these constraints individually or call emulateWindows or emulateUnix; to apply the constraints of a particular operating system.

Constructors

FakeFileSystem
Link copied to clipboard
fun FakeFileSystem(clock: Clock = Clock.System)

Functions

appendingSink
Link copied to clipboard
open override fun appendingSink(file: Path, mustExist: Boolean): Sink
atomicMove
Link copied to clipboard
open override fun atomicMove(source: Path, target: Path)
canonicalize
Link copied to clipboard
open override fun canonicalize(path: Path): Path
checkNoOpenFiles
Link copied to clipboard
fun checkNoOpenFiles()

Confirm that all files that have been opened on this file system (with source, sink, and appendingSink) have since been closed. Call this in your test's tearDown() function to confirm that your program hasn't leaked any open files.

copy
Link copied to clipboard
open fun copy(source: Path, target: Path)
createDirectories
Link copied to clipboard
fun createDirectories(dir: Path, mustCreate: Boolean)
createDirectory
Link copied to clipboard
open override fun createDirectory(dir: Path, mustCreate: Boolean)
createSymlink
Link copied to clipboard
open override fun createSymlink(source: Path, target: Path)
delete
Link copied to clipboard
open override fun delete(path: Path, mustExist: Boolean)
deleteRecursively
Link copied to clipboard
open fun deleteRecursively(fileOrDirectory: Path, mustExist: Boolean)
emulateUnix
Link copied to clipboard
fun emulateUnix()

Configure this file system to use a UNIX-like working directory (/, unless the working directory is already UNIX-like) and to follow a UNIX-like policy on what operations are permitted.

emulateWindows
Link copied to clipboard
fun emulateWindows()

Configure this file system to use a Windows-like working directory (F:\, unless the working directory is already Windows-like) and to follow a Windows-like policy on what operations are permitted.

exists
Link copied to clipboard
fun exists(path: Path): Boolean
list
Link copied to clipboard
open override fun list(dir: Path): List<Path>
listOrNull
Link copied to clipboard
open override fun listOrNull(dir: Path): List<Path>?
listRecursively
Link copied to clipboard
open fun listRecursively(dir: Path, followSymlinks: Boolean): Sequence<Path>
metadata
Link copied to clipboard
fun metadata(path: Path): FileMetadata
metadataOrNull
Link copied to clipboard
open override fun metadataOrNull(path: Path): FileMetadata?
openReadOnly
Link copied to clipboard
open override fun openReadOnly(file: Path): FileHandle
openReadWrite
Link copied to clipboard
open override fun openReadWrite(file: Path, mustCreate: Boolean, mustExist: Boolean): FileHandle
read
Link copied to clipboard
inline fun <T> read(file: Path, readerAction: BufferedSource.() -> T): T
setExtra
Link copied to clipboard
fun <T : Any> setExtra(path: Path, type: KClass<out T>, value: T?)

Sets the metadata of type type on path to value. If value is null this clears that metadata.

sink
Link copied to clipboard
open override fun sink(file: Path, mustCreate: Boolean): Sink
source
Link copied to clipboard
open override fun source(file: Path): Source
toString
Link copied to clipboard
open override fun toString(): String
write
Link copied to clipboard
inline fun <T> write(file: Path, mustCreate: Boolean, writerAction: BufferedSink.() -> T): T

Properties

allowClobberingEmptyDirectories
Link copied to clipboard
var allowClobberingEmptyDirectories: Boolean = false

True to allow the target of an atomicMove operation to be an empty directory. Windows file systems typically allow files to replace empty directories; UNIX file systems do not.

allowDeletingOpenFiles
Link copied to clipboard
var allowDeletingOpenFiles: Boolean = false

True to allow files to be deleted even if they're currently open for read or write. UNIX file systems typically allow open files to be deleted; Windows file systems do not.

allowMovingOpenFiles
Link copied to clipboard
var allowMovingOpenFiles: Boolean = false

True to allow files to be moved even if they're currently open for read or write. UNIX file systems typically allow open files to be moved; Windows file systems do not.

allowReadsWhileWriting
Link copied to clipboard
var allowReadsWhileWriting: Boolean = false

True to permit a file to have a source and sink open at the same time. Both Windows and UNIX file systems permit this but the result may be undefined.

allowSymlinks
Link copied to clipboard
var allowSymlinks: Boolean = false

True to allow symlinks to be created. UNIX file systems typically allow symlinks; Windows file systems do not. Setting this to false after creating a symlink does not prevent that symlink from being returned or used.

allowWritesWhileWriting
Link copied to clipboard
var allowWritesWhileWriting: Boolean = false

True to permit a file to have multiple sinks open at the same time. Both Windows and UNIX file systems permit this but the result may be undefined.

allPaths
Link copied to clipboard
@get:JvmName(name = "allPaths")
val allPaths: Set<Path>

Canonical paths for every file and directory in this file system. This omits file system roots like C:\ and /.

clock
Link copied to clipboard
val clock: Clock
openPaths
Link copied to clipboard
@get:JvmName(name = "openPaths")
val openPaths: List<Path>

Canonical paths currently opened for reading or writing in the order they were opened. This may contain duplicates if a single path is open by multiple readers.

workingDirectory
Link copied to clipboard
var workingDirectory: Path

An absolute path with this file system's current working directory. Relative paths will be resolved against this directory when they are used.