From 97054b4fccd8415237c86eb4bf697a0512acaba1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?V=C3=A1clav=20Val=C3=AD=C4=8Dek?= Date: Sun, 26 Jun 2022 00:46:56 +0200 Subject: [PATCH] ConfigFileNotFound exception + run-test script MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Václav Valíček --- old/checker/run-checker | 2 +- .../lib/config_file_not_found_error.py | 20 ++++++++++++ run-test | 19 ++++++++++++ tests/lib/test_config_file_not_found.py | 31 +++++++++++++++++++ 4 files changed, 71 insertions(+), 1 deletion(-) create mode 100644 repo_cloner/lib/config_file_not_found_error.py create mode 100755 run-test create mode 100644 tests/lib/test_config_file_not_found.py diff --git a/old/checker/run-checker b/old/checker/run-checker index dc236dd..cb41281 100755 --- a/old/checker/run-checker +++ b/old/checker/run-checker @@ -5,7 +5,7 @@ IFS=$'\n\t' # source libs mydir=$(dirname $(realpath $0)) source $mydir/cloner-lib-general -source $mydir/cloner-lib-auth + source $mydir/cloner-lib-cfg source $mydir/detector-lib-cfg diff --git a/repo_cloner/lib/config_file_not_found_error.py b/repo_cloner/lib/config_file_not_found_error.py new file mode 100644 index 0000000..f0bbb8d --- /dev/null +++ b/repo_cloner/lib/config_file_not_found_error.py @@ -0,0 +1,20 @@ +from pathlib import PosixPath +import os + + +class ConfigFileNotFoundError(OSError): + + def __str__(self): + if len(self.args) == 2: + path: str = "" + filename: str = "" + if isinstance(self.args[0], PosixPath): + path = str(PosixPath(self.args[0])) + else: + path = str(self.args[0]) + + filename = str(self.args[1]) + path = os.path.join(path, filename) + return f"Config file does not exist: {path}" + else: + return super.__str__(self) diff --git a/run-test b/run-test new file mode 100755 index 0000000..39e10d8 --- /dev/null +++ b/run-test @@ -0,0 +1,19 @@ +#!/bin/bash +set -euo pipefail + +if [ $# -eq 0 ] +then + python3 -m pytest . \ + -v \ + -n auto \ + --ignore=tests/_support_data \ + --color=yes \ + --cov . \ + --cov-config .coveragerc \ + --cov-report term-missing \ + --cov-report html \ + --capture=no +else + python3 -m pytest --capture=no $1 +fi + diff --git a/tests/lib/test_config_file_not_found.py b/tests/lib/test_config_file_not_found.py new file mode 100644 index 0000000..fab505b --- /dev/null +++ b/tests/lib/test_config_file_not_found.py @@ -0,0 +1,31 @@ +from repo_cloner.lib.config_file_not_found_error import ConfigFileNotFoundError +from pathlib import PosixPath + + +def test_type(): + x = ConfigFileNotFoundError() + assert isinstance(x, ConfigFileNotFoundError) + + +def test_return(): + x = ConfigFileNotFoundError("/tmp", "cloner.cfg") + s = x.__str__() + assert s == "Config file does not exist: /tmp/cloner.cfg" + + +def test_str_single_arg(): + x = ConfigFileNotFoundError("/tmp") + s = x.__str__() + assert s == "ConfigFileNotFoundError('/tmp')" + + +def test_return_empty(): + x = ConfigFileNotFoundError() + s = x.__str__() + assert s == "ConfigFileNotFoundError()" + + +def test_posix_path(): + d = PosixPath("/nonexistent") + x = ConfigFileNotFoundError(d, "config.cfg") + assert x.__str__() == "Config file does not exist: /nonexistent/config.cfg"