scripts/unit-test.py: Rework handling of directories in clone_pkg()

This makes things a bit neater and accounts for directories that might
already exist as a result of the docker build process.

Tested: Ran run-unit-test-docker.sh, confirmed expected functionality

Change-Id: Ieebd63da0383e4431ae9cdf972e8905aff96f175
Signed-off-by: Andrew Jeffery <andrew@aj.id.au>
diff --git a/scripts/unit-test.py b/scripts/unit-test.py
index adb5f08..a2ad824 100755
--- a/scripts/unit-test.py
+++ b/scripts/unit-test.py
@@ -232,10 +232,13 @@
     Parameter descriptions:
     pkg                 Name of the package to clone
     """
+    pkg_dir = os.path.join(WORKSPACE, pkg)
+    if os.path.exists(os.path.join(pkg_dir, '.git')):
+        return pkg_dir
     pkg_repo = urljoin('https://gerrit.openbmc-project.xyz/openbmc/', pkg)
-    os.mkdir(os.path.join(WORKSPACE, pkg))
-    printline(os.path.join(WORKSPACE, pkg), "> git clone", pkg_repo, "./")
-    return Repo.clone_from(pkg_repo, os.path.join(WORKSPACE, pkg))
+    os.mkdir(pkg_dir)
+    printline(pkg_dir, "> git clone", pkg_repo, "./")
+    return Repo.clone_from(pkg_repo, pkg_dir).working_dir
 
 
 def get_deps(configure_ac):
@@ -319,13 +322,12 @@
                 # Dependency package not added
                 new_child = dep_tree.AddChild(dep_pkg)
                 dep_added[dep_pkg] = False
-                dep_repo = clone_pkg(dep_pkg)
+                dep_pkgdir = clone_pkg(dep_pkg)
                 # Determine this dependency package's
                 # dependencies and add them before
                 # returning to add this package
-                dep_pkgdir = os.path.join(WORKSPACE, dep_pkg)
                 dep_added = build_dep_tree(dep_pkg,
-                                           dep_repo.working_dir,
+                                           dep_pkgdir,
                                            dep_added,
                                            head,
                                            new_child)