From 582bdc4001c82242a1ab84d21a54e1b61eaab710 Mon Sep 17 00:00:00 2001 From: baileythegreen Date: Fri, 29 Jul 2022 15:20:52 +0100 Subject: [PATCH] Make changes to tests as discussed in Issue #405 --- pyani/versiondb.py | 9 ++++++++- tests/test_versiondb.py | 35 +++++++++++++++++++++++------------ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/pyani/versiondb.py b/pyani/versiondb.py index 3deef4e0..91e0ce23 100644 --- a/pyani/versiondb.py +++ b/pyani/versiondb.py @@ -31,13 +31,20 @@ def get_version(alembic_exe: Path = pyani_config.ALEMBIC_DEFAULT) -> str: The following circumstances are explicitly reported as strings: + - a value of None given for the executable - no executable at passed path - non-executable file at passed path (this includes cases where the user doesn't have execute permissions on the file) - no version info returned """ try: - alembic_path = Path(shutil.which(alembic_exe)) # type:ignore + # Returns a TypeError if `alembic_exe` is None + try: + alembic_path = shutil.which(alembic_exe) # type:ignore + except TypeError: + return f"expected path to alembic executable; received {alembic_exe}" + # Returns a TypeError if `alembic_path` is not on the PATH + alembic_path = Path(alembic_path) except TypeError: return f"{alembic_exe} is not found in $PATH" diff --git a/tests/test_versiondb.py b/tests/test_versiondb.py index ca10dd51..d0e643e1 100644 --- a/tests/test_versiondb.py +++ b/tests/test_versiondb.py @@ -41,33 +41,44 @@ def test_get_version_nonetype(): """Test behaviour when no location for the executable is given.""" test_file_0 = None - assert versiondb.get_version(test_file_0) == f"{test_file_0} is not found in $PATH" + assert ( + versiondb.get_version(test_file_0) + == f"expected path to alembic executable; received {test_file_0}" + ) + + +# Test case 1: no such file exists +def test_get_version_random_string(): + """Test behaviour when the given 'file' is not one.""" + test_file_1 = "string" + assert versiondb.get_version(test_file_1) == f"{test_file_1} is not found in $PATH" -# Test case 1: there is no executable + +# Test case 2: there is no executable def test_get_version_no_exe(executable_missing, monkeypatch): """Test behaviour when there is no file at the specified executable location.""" - test_file_1 = Path("/non/existent/alembic") - assert versiondb.get_version(test_file_1) == f"No alembic at {test_file_1}" + test_file_2 = Path("/non/existent/alembic") + assert versiondb.get_version(test_file_2) == f"No alembic at {test_file_2}" -# Test case 2: there is a file, but it is not executable +# Test case 3: there is a file, but it is not executable def test_get_version_exe_not_executable(executable_not_executable, monkeypatch): """Test behaviour when the file at the executable location is not executable.""" - test_file_2 = Path("/non/executable/alembic") + test_file_3 = Path("/non/executable/alembic") assert ( - versiondb.get_version(test_file_2) - == f"alembic exists at {test_file_2} but not executable" + versiondb.get_version(test_file_3) + == f"alembic exists at {test_file_3} but not executable" ) -# Test case 3: there is an executable file, but the version can't be retrieved +# Test case 4: there is an executable file, but the version can't be retrieved def test_get_version_exe_no_version(executable_without_version, monkeypatch): """Test behaviour when the version for the executable can not be retrieved.""" - test_file_3 = Path("/missing/version/alembic") + test_file_4 = Path("/missing/version/alembic") assert ( - versiondb.get_version(test_file_3) - == f"alembic exists at {test_file_3} but could not retrieve version" + versiondb.get_version(test_file_4) + == f"alembic exists at {test_file_4} but could not retrieve version" )