import sys import os from pathlib import Path try: from pip import main as pipmain except Exception: from pip._internal import main as pipmain class TestUtils: _name_external = "external_test_data" _name_local = "test_data" _name_output = "test_output" @staticmethod def install_package(package: str): """Installs a package that is normally only used by a test configuration. Arguments: package {str} -- Name of the PIP package. """ pipmain(["install", package]) @staticmethod def get_output_test_data_dir(dir_name: str): """ Returns the full path of a directory containing generated data from the tests. If it does not exist it creates it. """ directory = TestUtils.get_test_data_dir(dir_name, TestUtils._name_output) if not directory.is_dir(): # Create missing directories Path.mkdir(directory, parents=True) return directory @staticmethod def get_local_test_data_dir(dir_name: str) -> Path: """ Returns the desired directory relative to the test data. Avoiding extra code on the tests. """ directory = TestUtils.get_test_data_dir(dir_name, TestUtils._name_local) return directory @staticmethod def get_external_test_data_dir(dir_name: str): """ Returns the desired directory relative to the test external data. Avoiding extra code on the tests. """ directory = TestUtils.get_test_data_dir(dir_name, TestUtils._name_external) return directory @staticmethod def get_test_data_dir(dir_name: str, test_data_name: str) -> Path: """ Returns the desired directory relative to the test external data. Avoiding extra code on the tests. """ current_dir = Path(__file__).parent return current_dir / test_data_name / dir_name @staticmethod def get_test_dir(dir_name: str) -> Path: """Returns the desired directory inside the Tests folder Arguments: dir_name {str} -- Target directory. Returns: {Path} -- Path to the target directory. """ test_dir = Path(__file__).parent return test_dir / dir_name