Temporary files utilities#

Temporary files helpers.

saltfactories.utils.tempfiles.temp_directory(name=None, basepath=None)[source]#

This helper creates a temporary directory.

It should be used as a context manager which returns the temporary directory path, and, once out of context, deletes it.

Parameters:
  • name (basepath) – The name of the directory to create

  • name – The base path of where to create the directory. Defaults to gettempdir()

Return type:

pathlib.Path

Can be directly imported and used:

from saltfactories.utils.tempfiles import temp_directory


def test_func():
    with temp_directory() as temp_path:
        assert temp_path.is_dir()

    assert not temp_path.is_dir() is False

Or, it can be used as a pytest helper function:

import pytest


def test_blah():
    with pytest.helpers.temp_directory() as temp_path:
        assert temp_path.is_dir()

    assert not temp_path.is_dir() is False
saltfactories.utils.tempfiles.temp_file(name=None, contents=None, directory=None, strip_first_newline=True)[source]#

Create a temporary file as a context manager.

This helper creates a temporary file. It should be used as a context manager which returns the temporary file path, and, once out of context, deletes it.

Parameters:
  • name (str) – The temporary file name

  • contents (str) – The contents of the temporary file

  • directory (str,pathlib.Path) – The directory where to create the temporary file. Defaults to the value of gettempdir()

  • strip_first_newline (bool) – Either strip the initial first new line char or not.

Return type:

pathlib.Path

Can be directly imported and used:

from saltfactories.utils.tempfiles import temp_file


def test_func():
    with temp_file(name="blah.txt") as temp_path:
        assert temp_path.is_file()

    assert temp_path.is_file() is False

Or, it can be used as a pytest helper function:

import pytest


def test_blah():
    with pytest.helpers.temp_file("blah.txt") as temp_path:
        assert temp_path.is_file()

    assert temp_path.is_file() is False

To create files under a sub-directory, one has two choices:

import pytest


def test_relative_subdirectory():
    with pytest.helpers.temp_file("foo/blah.txt") as temp_path:
        assert temp_path.is_file()
        assert temp_path.parent.is_dir()
        assert temp_path.parent.name == "foo"

    assert not temp_path.is_file() is False
    assert not temp_path.parent.is_dir() is False
import os
import pytest
import tempfile

ROOT_DIR = tempfile.gettempdir()


def test_absolute_subdirectory_1():
    destpath = os.path.join(ROOT_DIR, "foo")
    with pytest.helpers.temp_file("blah.txt", directory=destpath) as temp_path:
        assert temp_path.is_file()
        assert temp_path.parent.is_dir()
        assert temp_path.parent.name == "foo"

    assert not temp_path.is_file() is False
    assert not temp_path.parent.is_dir() is False


def test_absolute_subdirectory_2():
    destpath = os.path.join(ROOT_DIR, "foo", "blah.txt")
    with pytest.helpers.temp_file(destpath) as temp_path:
        assert temp_path.is_file()
        assert temp_path.parent.is_dir()
        assert temp_path.parent.name == "foo"

    assert temp_path.is_file() is False
    assert temp_path.parent.is_dir() is False
class saltfactories.utils.tempfiles.SaltEnv(*, name, paths=_Nothing.NOTHING)[source]#

Bases: object

This helper class represent a Salt Environment, either for states or pillar.

It’s base purpose it to handle temporary file creation/deletion during testing.

Parameters:
  • name (str) – The salt environment name, commonly, ‘base’ or ‘prod’

  • paths (list) –

    The salt environment list of paths.

    Note

    The first entry in this list, is the path that will get used to create temporary files in, ie, the return value of the saltfactories.utils.tempfiles.SaltEnv.write_path attribute.

property write_path#

The path where temporary files are created.

temp_file(name, contents=None, strip_first_newline=True)[source]#

Create a temporary file within this saltenv.

Please check saltfactories.utils.tempfiles.temp_file() for documentation.

Note

The directory keyword is not supported(since the directory used will be the value of saltfactories.utils.tempfiles.SaltEnv.write_path. To place a file within a sub-directory, give path with directory for file, for example: “mydir/myfile”.

as_dict()[source]#

Returns a dictionary of the right types to update the salt configuration.

Return dict:

class saltfactories.utils.tempfiles.SaltEnvs(*, envs)[source]#

Bases: object

This class serves as a container for multiple salt environments for states or pillar.

Parameters:

envs (dict) – The envs dictionary should be a mapping of a string as key, the saltenv, commonly ‘base’ or ‘prod’, and the value an instance of SaltEnv or a list of strings(paths). In the case where a list of strings(paths) is passed, it is converted to an instance of SaltEnv

To provide a better user experience, the salt environments can be accessed as attributes of this class.

envs = SaltEnvs(
    {
        "base": [
            "/path/to/base/env",
        ],
        "prod": [
            "/path/to/prod/env",
        ],
    }
)
with envs.base.temp_file("foo.txt", "foo contents") as base_foo_path:
    ...
with envs.prod.temp_file("foo.txt", "foo contents") as prod_foo_path:
    ...
as_dict()[source]#

Returns a dictionary of the right types to update the salt configuration.

Return dict:

class saltfactories.utils.tempfiles.SaltStateTree(*, envs)[source]#

Bases: SaltEnvs

Helper class which handles temporary file creation within the state tree.

Parameters:

envs (dict) –

A mapping of a saltenv to a list of paths.

envs = {
    "base": [
        "/path/to/base/env",
        "/another/path/to/base/env",
    ],
    "prod": [
        "/path/to/prod/env",
        "/another/path/to/prod/env",
    ],
}

The state tree environments can be accessed by attribute:

# See example of envs definition above
state_tree = SaltStateTree(envs=envs)

# To access the base saltenv
base = state_tree.envs["base"]

# Alternatively, in a simpler form
base = state_tree.base

When setting up the Salt configuration to use an instance of SaltStateTree, the following pseudo code can be followed.

# Using the state_tree defined above:
salt_config = {
    # ... other salt config entries ...
    "file_roots": state_tree.as_dict()
    # ... other salt config entries ...
}

Attention

The temporary files created by the temp_file() are written to the first path passed when instantiating the SaltStateTree, ie, the return value of the saltfactories.utils.tempfiles.SaltStateTree.write_path attribute.

# Given the example mapping shown above ...

with state_tree.base.temp_file("foo.sls") as path:
    assert str(path) == "/path/to/base/env/foo.sls"
as_dict()#

Returns a dictionary of the right types to update the salt configuration.

Return dict:

class saltfactories.utils.tempfiles.SaltPillarTree(*, envs)[source]#

Bases: SaltEnvs

Helper class which handles temporary file creation within the pillar tree.

Parameters:

envs (dict) –

A mapping of a saltenv to a list of paths.

envs = {
    "base": [
        "/path/to/base/env",
        "/another/path/to/base/env",
    ],
    "prod": [
        "/path/to/prod/env",
        "/another/path/to/prod/env",
    ],
}

The pillar tree environments can be accessed by attribute:

# See example of envs definition above
pillar_tree = SaltPillarTree(envs=envs)

# To access the base saltenv
base = pillar_tree.envs["base"]

# Alternatively, in a simpler form
base = pillar_tree.base

When setting up the Salt configuration to use an instance of SaltPillarTree, the following pseudo code can be followed.

# Using the pillar_tree defined above:
salt_config = {
    # ... other salt config entries ...
    "pillar_roots": pillar_tree.as_dict()
    # ... other salt config entries ...
}

Attention

The temporary files created by the temp_file() are written to the first path passed when instantiating the SaltPillarTree, ie, the return value of the saltfactories.utils.tempfiles.SaltPillarTree.write_path attribute.

# Given the example mapping shown above ...

with state_tree.base.temp_file("foo.sls") as path:
    assert str(path) == "/path/to/base/env/foo.sls"
as_dict()#

Returns a dictionary of the right types to update the salt configuration.

Return dict: