]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
pytest: source_tree_topdir() checks more thoroughly
authorDouglas Bagnall <douglas.bagnall@catalyst.net.nz>
Thu, 20 Mar 2025 03:43:41 +0000 (16:43 +1300)
committerRalph Boehme <slow@samba.org>
Sat, 29 Mar 2025 07:23:43 +0000 (07:23 +0000)
We are more certain of how many ../ steps we are from top than we are
that the existence of a thing called 'source4' is a definite sign
that this is a full source tree. So we check fewer depths and more
subdirectories.

This also modernises the return type to be a pathlib.Path, which works
without change for current callers.

Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
Reviewed-by: Ralph Boehme <slow@samba.org>
python/samba/tests/__init__.py

index 0374dfa65f60261a5ce3dd34fa1bc3cba5e79bef..a5b6cb658eb8ec891deda898b1596d2d84ea0326 100644 (file)
@@ -52,7 +52,7 @@ import samba.dcerpc.dcerpc
 import samba.dcerpc.epmapper
 
 from unittest import SkipTest
-
+from pathlib import Path
 
 BINDIR = os.path.abspath(os.path.join(os.path.dirname(__file__),
                                       "../../../../bin"))
@@ -66,13 +66,17 @@ RE_CAMELCASE = re.compile(r"([_\-])+")
 
 
 def source_tree_topdir():
-    """Return the top level source directory."""
-    paths = ["../../..", "../../../.."]
-    for p in paths:
-        topdir = os.path.normpath(os.path.join(os.path.dirname(__file__), p))
-        if os.path.exists(os.path.join(topdir, 'source4')):
-            return topdir
-    raise RuntimeError("unable to find top level source directory")
+    """Return the top level source directory if this seems to be a
+    full source tree. Otherwise raise FileNotFoundError."""
+    topdir = Path(__file__) / "../../../.."
+    topdir = topdir.resolve()
+
+    for dirpath in ('source4', 'docs-xml', 'python/samba/tests'):
+        d = topdir / dirpath
+        if not d.is_dir():
+            raise FileNotFoundError(f"missing or not a directory: {d}")
+
+    return topdir
 
 
 def ldb_err(v):