File specification

File hierarchies are specified using dictionaries, where the keys specify file paths and the values specify file contents.

The paths (keys) must always be strings. The paths may contain directories and subdirectories that don’t exist yet; any necessary directories will be created automatically.

The contents (values) can be either strings or dictionaries. Strings are used for simple text files; the given text will be written to the file verbatim. This is by far the most common way to specify file contents. Dictionaries are used when more control is required, e.g. to create symlinks, to use non-default file permissions, etc. The fields expected in these dictionaries depends on the type of file being created, as documented below:

Text files

Text files can be specified either as strings, or as dictionaries with the following keys:

  • "type" (optional): "text"

  • "content" (optional): The text to write to the file. If not specified, the file will be empty.

  • "mode" (optional): An octal number (e.g. 644) describing which permission bits to set for the file. This value is passed on to pathlib.Path.chmod(), so refer to that documentation for the precise meaning of these bits.

  • "atime" (optional): The date and time when the file was most recently accessed, as an ISO-formatted string. By default, this will be the current time.

  • "mtime" (optional): The date and time when the file was most recently modified, as an ISO-formatted string. By default, this will be the current time.

Examples:

A file named spam.txt containing the string eggs (specified several equivalent ways):

{'spam.txt': 'eggs'}
{'spam.txt': {'content': 'eggs'}}
{'spam.txt': {'type': 'text', 'content': 'eggs'}}

A user-executable file:

{'spam.sh': {'mode': '744', 'content': '#!/usr/bin/env sh\necho eggs'}}

A file that was last modified on a specific date:

{'spam.txt': {'mtime': '2022-03-11', 'content': 'eggs'}}

Binary files

Binary files can be specified as dictionaries with the following keys:

  • "type" (required): "bin"

  • "base64" (optional): The base-64 encoded contents of the file.text to write to the file. If not specified, the file will be empty.

  • "mode" (optional): See Text files.

  • "atime" (optional): See Text files.

  • "mtime" (optional): See Text files.

Example:

A PNG file:

{'emoji.png': {'type': 'bin', 'base64': 'iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAApgAAAKYB3X3/OAAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAANCSURBVEiJtZZPbBtFFMZ/M7ubXdtdb1xSFyeilBapySVU8h8OoFaooFSqiihIVIpQBKci6KE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Os can be specified as dictionaries with the following keys:

Directories

Directories can be specified as dictionaries with the following keys:

  • "type" (required): "dir"

  • "contents" (optional): A dictionary with the same format as the top-level dictionary (i.e. the one specifying the whole file hierarchy), except that the paths (keys) are now relative to this directory. These directory dictionaries can be nested any number of times.

  • "mode" (optional): See Text files.

  • "atime" (optional): See Text files.

  • "mtime" (optional): See Text files.

Note that it’s only necessary to specify a directory like this if you want an empty directory. Otherwise, it’s easier just the specify the path the file you want, and all of the directories along that path will be created automatically.

Example:

An empty directory:

{'empty': {'type': 'dir'}}

A directory containing a text file. Note that the directory does not need to be specified separately from the file itself:

{'dir/spam.txt': 'eggs'}

Need something else?

If you need some kind of file that isn’t currently supported, please feel free to open a bug report describing your situation. I’d like this library to support as many use-cases as possible! Alternatively, you can add use the tmp_file_type decorator to define your own custom file types.