From: =?UTF-8?q?Samo=20Poga=C4=8Dnik?= <samo_pogacnik@t-2.net>
Date: Sat, 19 Oct 2024 19:07:43 +0200
Forwarded: https://github.com/ingydotnet/git-subrepo/pull/639
Subject: Define project-wide GIT setup for all tests

Local git config 'autocrlf' has been removed.
Run tests with fixed locale.
Disabling system/global git configs and setting default project-wide GIT
configuration for the duration of running tests. Temporary GIT
configuration is set inside git-subrepo project-top-dir.
Also, initialize own git repo, if project isn't a git clone and ensure
that the project is not in a GIT detached HEAD state.
---
 Makefile             |  2 +-
 test/00-git-config.t | 55 ++++++++++++++++++++++++++++++++++++++++++++
 test/setup           | 17 +++++++-------
 3 files changed, 65 insertions(+), 9 deletions(-)
 create mode 100644 test/00-git-config.t

diff --git a/Makefile b/Makefile
index 8dd567f..7521bc6 100644
--- a/Makefile
+++ b/Makefile
@@ -110,7 +110,7 @@ compgen: force
 	    $(SHARE)/zsh-completion/_git-subrepo
 
 clean:
-	rm -fr tmp test/tmp
+	rm -fr tmp test/tmp .gitconfig
 
 define docker-make-test
 	docker run --rm \
diff --git a/test/00-git-config.t b/test/00-git-config.t
new file mode 100644
index 0000000..b121c26
--- /dev/null
+++ b/test/00-git-config.t
@@ -0,0 +1,55 @@
+#!/usr/bin/env bash
+
+set -e
+
+source test/setup
+
+use Test::More
+
+note "Define project-wide GIT setup for all tests"
+
+# Get git-subrepo project top directory
+PROJ_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )
+
+if [ -z "${PROJ_DIR}" ] || [ "${HOME}" != "${PROJ_DIR}" ]; then
+  is "${HOME}" "${PROJ_DIR}" \
+  "To define project-wide GIT setup for all tests: HOME '${HOME}' should equal PROJ_DIR '${PROJ_DIR}'"
+else
+
+  # Real GIT configuration for tests is set here:
+  rm -f "${PROJ_DIR}/.gitconfig"
+  git config --global user.email "you@example.com"
+  git config --global user.name "Your Name"
+  git config --global init.defaultBranch "master"
+  git config --global --add safe.directory "${PROJ_DIR}"
+  git config --global --add safe.directory "${PROJ_DIR}/.git"
+  git config --list
+
+  test-exists "${PROJ_DIR}/.gitconfig"
+
+  # Running tests depends on the whole project being git initialized.
+  # So, git initialize the project, if necessary.
+  if [ ! -d "${PROJ_DIR}/.git" ]; then
+    cd "${PROJ_DIR}"
+    git init .
+    git add .
+    git commit -a -m"Initial commit"
+    cd -
+  fi
+
+  test-exists "${PROJ_DIR}/.git/"
+
+  # Running tests depends on the whole project not being in a GIT detached HEAD state.
+  if ! git symbolic-ref --short --quiet HEAD &> /dev/null; then
+    git checkout -b test
+  fi
+
+  ok "$(
+    git symbolic-ref --short --quiet HEAD &> /dev/null
+  )" "Whole project is not in a GIT detached HEAD state"
+
+fi
+
+done_testing
+
+teardown
diff --git a/test/setup b/test/setup
index a05f7ff..ab945f3 100644
--- a/test/setup
+++ b/test/setup
@@ -1,18 +1,21 @@
 #!/usr/bin/env bash
 
-# Set this locally for Windows:
-git config core.autocrlf input
-
 set -e
 
-# Set the GIT_SUBREPO_ROOT for testing.
-source "$PWD"/.rc
+export LC_ALL=C.UTF-8
 
 # Get the location of this script
 SCRIPT_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )
 
+# Disable any GIT configuration set outside this 'git-subrepo' project.
+# Real GIT configuration for tests is set through the first test
+# (00-git-config.t).
+export XDG_CONFIG_HOME=$PWD
+export HOME=$PWD
+export GIT_CONFIG_NOSYSTEM=1
+
 BASHLIB=$(
-  find "$PWD"/ -type d -name bin -o -type d -name lib | tr '\n' ':'
+  find "$PWD"/ -type d -name bin -o -type d -name lib | grep -v "\/\.pc\/" | tr '\n' ':'
 )
 export BASHLIB
 
@@ -46,7 +49,6 @@ clone-foo-and-bar() {
     git clone "$UPSTREAM/foo" "$OWNER/foo"
     (
         cd "$OWNER/foo"
-        git config core.autocrlf input
         git config user.name "FooUser"
         git config user.email "foo@foo"
     )
@@ -54,7 +56,6 @@ clone-foo-and-bar() {
     git clone "$UPSTREAM/bar" "$OWNER/bar"
     (
         cd "$OWNER/bar"
-        git config core.autocrlf input
         git config user.name "BarUser"
         git config user.email "bar@bar"
     )
