# Copyright (C) Stichting Deltares 2021. All rights reserved. # # This file is part of the Dam Python Interface. # # The Dam Python Interface is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see . # # All names, logos, and references to "Deltares" are registered trademarks of # Stichting Deltares and remain full property of Stichting Deltares at all times. # All rights reserved. import os import sys from pathlib import Path from typing import List import pytest from teamcity import is_running_under_teamcity try: from pip import main as pipmain except: from pip._internal import main as pipmain only_teamcity = pytest.mark.skipif( not (is_running_under_teamcity() or "FORCE_TEAMCITY" in os.environ), reason="Console test only installed on TC.", ) 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_test_files_from_local_test_dir( dir_name: str, glob_filter: str ) -> List[Path]: """Returns all the files that need to be used as test input parameters from a given directory. Args: dir_name (str): Name of the local test data directory. glob_filter (str): Filter that will be applied with the glob function. Returns: List[Path]: List of files matching the above criteria. """ return [ input_file for input_file in Path(TestUtils.get_local_test_data_dir(dir_name)).glob( glob_filter ) if input_file.is_file() ] @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 os.path.exists(directory): os.makedirs(directory) return directory @staticmethod def get_local_test_data_dir(dir_name: str): """ 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): """ Returns the desired directory relative to the test external data. Avoiding extra code on the tests. """ test_dir = os.path.dirname(__file__) try: dir_path = os.path.join(test_data_name, dir_name) test_dir = os.path.join(test_dir, dir_path) except: print("An error occurred trying to find {}".format(dir_name)) return test_dir @staticmethod def get_test_dir(dir_name: str): """Returns the desired directory inside the Tests folder Arguments: dir_name {str} -- Target directory. Returns: {str} -- Path to the target directory. """ test_dir = os.path.dirname(__file__) dir_path = os.path.join(test_dir, dir_name) return dir_path