# HG changeset patch
# User Georges Racinet <georges.racinet@octobus.net>
# Date 1684061537 -7200
#      Sun May 14 12:52:17 2023 +0200
# Branch stable
# Node ID 4250ce078c4ddcf022651aede2ab6c1f79adc7ba
# Parent  0c1ed47dcf2a40aa6d7f9d0fb230611be03ab269
Gitaly Comparison test harness: fixing Gitaly bin_dir

In case the `gitaly` executable is to be found in a subdirectory
of `GITALY_INSTALL_DIR`, then that same subdirectory has to be
provided as the `bin_dir` configuration item, so that Gitaly can
find all its non-ruby bundled executables, notably the bundled Git.

It was working until now because Gitaly falls back to a system-wide
Git if the specified one is not found, whose version is acceptable
in CI because the `heptapod-gitaly` base image is itself based on the
`heptapod-base` image, which provides the Git version generally
accepted by the target GitLab version.

Since there is no doubt as to where the `gitaly` executable is supposed
to sit, we're only testing one possibility (minimal change to keep the diff
understable, to be refactored).

diff --git a/tests_with_gitaly/__init__.py b/tests_with_gitaly/__init__.py
--- a/tests_with_gitaly/__init__.py
+++ b/tests_with_gitaly/__init__.py
@@ -26,8 +26,6 @@
 
 logger = logging.getLogger(__name__)
 
-GITALY_BIN_REL_PATH = '_build/bin/gitaly'
-
 
 def gitaly_home():  # pragma no cover
     """Find where Gitaly sits on the file system
@@ -42,17 +40,17 @@
     """
     from_env = os.environ.get("GITALY_INSTALL_DIR")
     if from_env is not None:
-        install_dir = Path(from_env).resolve()
+        bin_dir = install_dir = Path(from_env).resolve()
     else:
         hgitaly_clone = Path(hgitaly.__file__).resolve().parent.parent
         hdk = hgitaly_clone.parent
         install_dir = hdk / 'gitaly'
+        bin_dir = install_dir / '_build/bin'
 
-    if install_dir.is_dir():
-        for bin_path in ('_build/bin/gitaly',
-                         'gitaly',  # Gitaly < 14.5 and Heptapod CI
+    if bin_dir.is_dir():
+        for bin_path in ('gitaly',
                          ):
-            gitaly = install_dir / bin_path
+            gitaly = bin_dir / bin_path
             if gitaly.is_file():
                 # startup costs about 0.15s. That's a lot, but it will
                 # make sure our skip is correct and it is useful to log the
@@ -62,13 +60,12 @@
                 assert 'Gitaly' in version
                 logger.info("Found %s at %s. Will lauch comparison tests "
                             "of HGitaly with Gitaly", version, gitaly)
-                return install_dir, bin_path
-        return install_dir, None
+                return install_dir, bin_dir
 
     return None, None
 
 
-GITALY_INSTALL_DIR, GITALY_BIN_REL_PATH = gitaly_home()
+GITALY_INSTALL_DIR, GITALY_BIN_DIR = gitaly_home()
 
 
 def gitaly_not_installed():
diff --git a/tests_with_gitaly/gitaly.py b/tests_with_gitaly/gitaly.py
--- a/tests_with_gitaly/gitaly.py
+++ b/tests_with_gitaly/gitaly.py
@@ -13,7 +13,7 @@
 from hgitaly.testing.grpc import wait_server_accepts_connection
 
 from . import (
-    GITALY_BIN_REL_PATH,
+    GITALY_BIN_DIR,
     GITALY_INSTALL_DIR,
 )
 
@@ -38,7 +38,7 @@
             'socket_path = "%s"' % self.gitaly_socket,
             # Gitaly compilation outputs its binaries at the root
             # of the checkout
-            'bin_dir = "%s"' % GITALY_INSTALL_DIR,
+            'bin_dir = "%s"' % GITALY_BIN_DIR,
             '[gitlab-shell]',
             'dir = "%s"' % gitlab_shell_dir,
             '[gitaly-ruby]',
@@ -68,7 +68,7 @@
         # it write to a file, let's redirect ourselves.
         with open(self.home_dir / 'gitaly.log', 'w') as logf:
             gitaly = subprocess.Popen(
-                [GITALY_INSTALL_DIR / GITALY_BIN_REL_PATH, self.gitaly_conf],
+                [GITALY_BIN_DIR / 'gitaly', self.gitaly_conf],
                 stdout=logf, stderr=logf,
                 env=env)