From cd227857256b2e6bbc98969956fd290b505eff71 Mon Sep 17 00:00:00 2001 From: Rich FitzJohn Date: Mon, 7 Aug 2023 15:57:35 +0100 Subject: [PATCH] Add utility to test actions --- src/outpack/util.py | 14 ++++++++++++++ tests/test_util.py | 8 ++++++++ 2 files changed, 22 insertions(+) create mode 100644 src/outpack/util.py create mode 100644 tests/test_util.py diff --git a/src/outpack/util.py b/src/outpack/util.py new file mode 100644 index 0000000..735e9fe --- /dev/null +++ b/src/outpack/util.py @@ -0,0 +1,14 @@ +from pathlib import Path + + +def find_file_descend(filename, path): + path = Path(path) + root = Path(path.root) + + while path != root: + attempt = path / filename + if attempt.exists(): + return str(attempt.parent) + path = path.parent + + return None diff --git a/tests/test_util.py b/tests/test_util.py new file mode 100644 index 0000000..6f7c6f1 --- /dev/null +++ b/tests/test_util.py @@ -0,0 +1,8 @@ +from outpack.util import find_file_descend + + +def test_find_descend(tmp_path): + (tmp_path / "a" / "b" / "c" / "d").mkdir(parents=True) + (tmp_path / "a" / "b" / ".foo").mkdir(parents=True) + assert find_file_descend(".foo", tmp_path / "a/b/c/d") == str(tmp_path / "a/b") + assert find_file_descend(".foo", tmp_path / "a") is None