Skip to content

Commit 346e230

Browse files
committed
Define project-wide GIT setup for all tests
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.
1 parent 44c8058 commit 346e230

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ compgen: force
110110
$(SHARE)/zsh-completion/_git-subrepo
111111

112112
clean:
113-
rm -fr tmp test/tmp test/repo
113+
rm -fr tmp test/tmp test/repo .gitconfig
114114

115115
define docker-make-test
116116
docker run --rm \

test/00-git-config.t

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#!/usr/bin/env bash
2+
3+
set -e
4+
5+
source test/setup
6+
7+
use Test::More
8+
9+
note "Define project-wide GIT setup for all tests"
10+
11+
# Get git-subrepo project top directory
12+
PROJ_DIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )/.." && pwd )
13+
14+
if [ -z "${PROJ_DIR}" ] || [ "${HOME}" != "${PROJ_DIR}" ]; then
15+
is "${HOME}" "${PROJ_DIR}" \
16+
"To define project-wide GIT setup for all tests: HOME '${HOME}' should equal PROJ_DIR '${PROJ_DIR}'"
17+
else
18+
19+
# Real GIT configuration for tests is set here:
20+
rm -f "${PROJ_DIR}/.gitconfig"
21+
git config --global user.email "[email protected]"
22+
git config --global user.name "Your Name"
23+
git config --global init.defaultBranch "master"
24+
git config --global --add safe.directory "${PROJ_DIR}"
25+
git config --global --add safe.directory "${PROJ_DIR}/.git"
26+
git config --list
27+
28+
test-exists "${PROJ_DIR}/.gitconfig"
29+
30+
# Running tests depends on the whole project being git initialized.
31+
# So, git initialize the project, if necessary.
32+
if [ ! -d "${PROJ_DIR}/.git" ]; then
33+
cd "${PROJ_DIR}"
34+
git init .
35+
git add .
36+
git commit -a -m"Initial commit"
37+
cd -
38+
fi
39+
40+
test-exists "${PROJ_DIR}/.git/"
41+
42+
# Running tests depends on the whole project not being in a GIT detached HEAD state.
43+
if ! git symbolic-ref --short --quiet HEAD &> /dev/null; then
44+
git checkout -b test
45+
fi
46+
47+
ok "$(
48+
git symbolic-ref --short --quiet HEAD &> /dev/null
49+
)" "Whole project is not in a GIT detached HEAD state"
50+
51+
fi
52+
53+
done_testing
54+
55+
teardown

test/setup

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,15 @@ if [ ! -d "${SCRIPT_DIR}/../.git" ]; then
1515
git config user.name "YouUser"
1616
git add .
1717
git commit -a -m"Initial commit"
18-
git config --list
1918
fi
2019

20+
# Disable any GIT configuration set outside this 'git-subrepo' project.
21+
# Real GIT configuration for tests is set through the first test
22+
# (00-git-config.t).
23+
export XDG_CONFIG_HOME=$PWD
24+
export HOME=$PWD
25+
export GIT_CONFIG_NOSYSTEM=1
26+
2127
# Generate additional testing git repos, if not already present.
2228
mkdir -p "${SCRIPT_DIR}/repo"
2329
if [ ! -d "${SCRIPT_DIR}/repo/bar" ]; then

0 commit comments

Comments
 (0)