FakeFileSystem

class FakeFileSystem(val clock: Clock = Clock.System) : 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

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

Properties

Link copied to clipboard

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.

Link copied to clipboard

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.

Link copied to clipboard

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.

Link copied to clipboard

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.

Link copied to clipboard

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.

Link copied to clipboard

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.

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 /.

Link copied to clipboard
val clock: Clock
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.

Link copied to clipboard

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

Functions

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

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.

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

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.

Link copied to clipboard

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.

Link copied to clipboard
expect fun exists(path: Path): Boolean
Link copied to clipboard
open override fun list(dir: Path): List<Path>
Link copied to clipboard
open override fun listOrNull(dir: Path): List<Path>?
Link copied to clipboard
expect open fun listRecursively(dir: Path, followSymlinks: Boolean): Sequence<Path>
Link copied to clipboard
expect fun metadata(path: Path): FileMetadata
Link copied to clipboard
open override fun metadataOrNull(path: Path): FileMetadata?
Link copied to clipboard
open override fun openReadOnly(file: Path): FileHandle
Link copied to clipboard
open override fun openReadWrite(file: Path, mustCreate: Boolean, mustExist: Boolean): FileHandle
Link copied to clipboard
expect inline fun <T> read(file: Path, readerAction: BufferedSource.() -> T): T
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.

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