manim/tests/assert_utils.py
Christopher Besch 3ce4c133dd
Introduced API for scene sections via :meth:.Scene.next_section (#2152)
* feat: added section class

* fix: fixed imports in scene.py

* feat: added section class

* fix: fixed imports in scene.py

* feat: scene file writer update; needs testing

* broken test

* fixed wrong partial movie files

* feat: added animation concatenation for sections

* fix: sections can contains None as partial movie file

* fix: correct section output file names

* fix: removed media folder

* feat: section output able to extract type

* feat: added guarantee_existence tests

* fix: touch file on windows

* fix: touch file on windows

* feat: automatically create control data with --set_test flag

* feat: added tests for sections output folder

* feat: added flag to enable sections

* feat: testing section metadata file

* feat: finished metadata tests; changed enums to be extendable

* fix: removed testing files

* fix: fixed section types

* fix: fixed outdated control data

* fix: fixed simple_scenes.py

* fix: fixed section type documentation example

* feat: added section video metadata to API

* fix: isort should stop destroying the import order for commands.py

* fix: fixed types of section metadata output

* fix: better comments

* ?

* feat: added elaborate test; switched name and type in next_section call

* feat: changed testing terminology: "config" got renamed to "movie metadata"

* fix: removed unicode lambda :<

* fix: fixed control data

* fix: removed test setter

* feat: added tutorial in the documentation

* fix: updated test control data

* fix: fixed double '.' in video tests

* fix: fixed some documentation

* fix: fixed missign JSON file in docs

* fix: implemented proposed changes

* fix: better --save_sections help

* fix: better docstrings

* fix: fixed last docstring...hopefully :>

* feat: custom section output supported

* fix: removed resolved todos

* fix: added debug for movie concatenation back

* fix: removed implemented TODO

* fix: removed doubled log

* fix: changed debug to info log

* fix: fixed log test data
2021-10-20 10:36:24 +02:00

127 lines
4 KiB
Python

import os
from pathlib import Path
from pprint import pformat
from typing import Dict, List, Union
def assert_file_exists(filepath: Union[str, os.PathLike]) -> None:
"""Assert that filepath points to an existing file. Print all the elements (files and dir) of the parent dir of the given filepath.
This is mostly to have better assert message than using a raw assert os.path.isfile(filepath).
Parameters
----------
filepath
Filepath to check.
Raises
------
AssertionError
If filepath does not point to a file (if the file does not exist or it's a dir).
"""
path = Path(filepath)
if not path.is_file():
elems = pformat([path.name for path in list(path.parent.iterdir())])
message = f"{path.absolute()} is not a file. Other elements in the parent directory are \n{elems}"
raise AssertionError(message)
def assert_dir_exists(dirpath: Union[str, os.PathLike]) -> None:
"""Assert that directory exists.
Parameters
----------
dirpath
Path to directory to check.
Raises
------
AssertionError
If dirpath does not point to a directory (if the file does exist or it's a file).
"""
path = Path(dirpath)
if not path.is_dir():
elems = pformat([path.name for path in list(path.parent.iterdir())])
message = f"{path.absolute()} is not a directory. Other elements in the parent directory are \n{elems}"
raise AssertionError(message)
def assert_dir_filled(dirpath: Union[str, os.PathLike]) -> None:
"""Assert that directory exists and contains at least one file or directory (or file like objects like symlinks on Linux).
Parameters
----------
dirpath
Path to directory to check.
Raises
------
AssertionError
If dirpath does not point to a directory (if the file does exist or it's a file) or the directory is empty.
"""
if len(os.listdir(dirpath)) == 0:
raise AssertionError(f"{dirpath} is an empty directory.")
def assert_file_not_exists(filepath: Union[str, os.PathLike]) -> None:
"""Assert that filepath does not point to an existing file. Print all the elements (files and dir) of the parent dir of the given filepath.
This is mostly to have better assert message than using a raw assert os.path.isfile(filepath).
Parameters
----------
filepath
Filepath to check.
Raises
------
AssertionError
If filepath does point to a file.
"""
path = Path(filepath)
if path.is_file():
elems = pformat([path.name for path in list(path.parent.iterdir())])
message = f"{path.absolute()} is a file. Other elements in the parent directory are \n{elems}"
raise AssertionError(message)
def assert_dir_not_exists(dirpath: Union[str, os.PathLike]) -> None:
"""Assert that directory does not exist.
Parameters
----------
dirpath
Path to directory to check.
Raises
------
AssertionError
If dirpath points to a directory.
"""
path = Path(dirpath)
if path.is_dir():
elems = pformat([path.name for path in list(path.parent.iterdir())])
message = f"{path.absolute()} is a directory. Other elements in the parent directory are \n{elems}"
raise AssertionError(message)
def assert_shallow_dict_compare(a: Dict, b: Dict, message_start: str) -> None:
"""Assert that Directories ``a`` and ``b`` are the same.
``b`` is treated as the expected values that ``a`` shall abide by.
Print helpful error with custom message start.
"""
mismatch: List[str] = []
for b_key, b_value in b.items():
if b_key not in a:
mismatch.append(f"Missing item {b_key}: {b_value}")
elif b_value != a[b_key]:
mismatch.append(f"For {b_key} got {a[b_key]}, expected {b_value}")
for a_key, a_value in a.items():
if a_key not in b:
mismatch.append(f"Extraneous item {a_key}: {a_value}")
mismatch_str = "\n".join(mismatch)
assert len(mismatch) == 0, f"{message_start}\n{mismatch_str}"