tested.local_files

Utils for testing with local files.

Things like:

Making folder paths (and ensuring things about them):

>>> import os
>>> f = temp_dirpath()
>>> assert os.path.isdir(f)  # the directory exists (by default)
>>> assert dir_is_empty(f)  # ... and is empty (by default)

Making file paths (and ensuring things about them):

>>> filepath = temp_filepath()
>>> containing_dirpath = os.path.dirname(filepath)
>>> assert os.path.isdir(containing_dirpath)
tested.local_files.dir_is_empty(dirpath)[source]
Parameters

path

Returns

>>> dirpath = temp_dirpath(ensure_is_empty=True)
>>> dir_is_empty(dirpath)
True
tested.local_files.empty_dir(dirpath)[source]

Empty a directory of it’s contents (and recreated the directory) Note: This means that the directory creation date will be now.

tested.local_files.non_empty_tail(path)[source]

The tail of the path, disregarding trailing slash if present.

If / is the separator, this means:

```

This/is/a/path -> path This/is/a/path/ -> path

```

>>> path = os.path.join('This', 'is', 'a', 'path')
>>> non_empty_tail(path)
'path'
>>> non_empty_tail(path + os.path.sep)
'path'
tested.local_files.temp_dirpath(subpath: str = 'tempdir', ensure_exists: Optional[bool] = True, ensure_is_empty: Optional[bool] = True)[source]

Get a fresh temporary folder path with assurances about existence and emptiness.

Parameters
  • subpath – The (relative) name of the folder.

  • ensure_exists

  • ensure_is_empty

Returns

A path (string) of a directory in a temporary

>>> import os
>>> from tested.local_files import temp_dirpath, non_empty_tail, dir_is_empty
>>>
>>> f = temp_dirpath()
>>> assert non_empty_tail(f) == 'tempdir'
>>> f = temp_dirpath('your_choice_of_a_dirname')
>>> assert non_empty_tail(f) == 'your_choice_of_a_dirname'  # the directory name is indeed what you asked for
>>> assert os.path.isdir(f)  # the directory exists!
>>> assert dir_is_empty(f)  # ... and is empty
>>> assert os.listdir(f) == []  # see!

Let’s write stuff in it:

>>> import pathlib
>>> p = pathlib.Path(f)
>>> contents = 'hello world!'
>>> assert p.joinpath('temp_file.txt').write_text(contents) == len(contents)
>>> assert p.joinpath('temp_file.txt').read_text() == contents
>>> assert os.listdir(f) == ['temp_file.txt']

By default ensure_is_empty=True, so you got an empty directory. But if you say False…

>>> ff = temp_dirpath('your_choice_of_a_dirname', ensure_is_empty=False)
>>> assert ff == f  # same path as before, but...
>>> assert not dir_is_empty(f)
>>> assert os.listdir(f) == ['temp_file.txt']
>>>
>>>
>>> ff = temp_dirpath('your_choice_of_a_dirname', ensure_is_empty=True)
>>> assert ff == f  # same path as before, but...
>>> assert os.listdir(f) == []

By default ensure_exists=True, but the value could be: - None; meaning don’t even check - False; meaning check, and if it exists, remove it

>>> ff = temp_dirpath('your_choice_of_a_dirname', ensure_exists=None)
>>> assert ff == f  # same path as before
>>> assert os.path.isdir(ff)
>>>
>>> f = temp_dirpath('your_choice_of_a_dirname', ensure_exists=False)
>>> assert not os.path.isdir(f)
tested.local_files.temp_filepath(filename='temp_file', subdir='temp_filepaths/', ensure_containing_dirs_exist=True, ensure_file_does_not_exist=False)[source]

Make a temp filepath, ensuring (by default) that the containing directories exist, and (optionally) that the file doesn’t exist either.

>>> filepath = temp_filepath()
>>> containing_dirpath = os.path.dirname(filepath)
>>> assert os.path.isdir(containing_dirpath)
>>> filepath = temp_filepath('my_own_name.txt')
>>> assert os.path.basename(filepath) == 'my_own_name.txt'

Let’s write something in that file.

>>> _ = Path(filepath).write_text('hello file!')  # but we can write in it
>>> assert os.path.isfile(filepath)  # and now it exists
>>> assert Path(filepath).read_text() == 'hello file!'  # here's what we wrote

If you ask for that filepath again (a short time later), you’ll get the same filepath, and the file will already exist. If you want to check if the file exists and delete it if it does, use ensure_file_does_not_exist==True:

>>> assert os.path.isfile(filepath)  # before
>>> filepath2 = temp_filepath('my_own_name.txt', ensure_file_does_not_exist=True)
>>> assert filepath2 == filepath
>>> assert not os.path.isfile(filepath)  # after (it doesn't exist!)