Skip to content
This repository has been archived by the owner on Apr 24, 2020. It is now read-only.

[Bugfix] Don't unset global git user config after git tests #1221

Merged
merged 2 commits into from
Apr 1, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 20 additions & 37 deletions segments/vcs/vcs_git.spec
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,21 @@
setopt shwordsplit
SHUNIT_PARENT=$0

function oneTimeSetUp() {
# must be done before any setup
ORIGINAL_GIT_AUTHOR_NAME="$(git config --global user.name || true)"
ORIGINAL_GIT_AUTHOR_EMAIL="$(git config --global user.email || true)"
}

function oneTimeTearDown() {
# must be done after last tear down to check if tests messed with user config
assertEquals "$(git config --global user.name || true)" "$ORIGINAL_GIT_AUTHOR_NAME"
assertEquals "$(git config --global user.email || true)" "$ORIGINAL_GIT_AUTHOR_EMAIL"
echo testIfUserConfigWasMessedWith
unset ORIGINAL_GIT_AUTHOR_NAME
unset ORIGINAL_GIT_AUTHOR_EMAIL
}

function setUp() {
export TERM="xterm-256color"
local -a P9K_RIGHT_PROMPT_ELEMENTS
Expand All @@ -27,48 +42,16 @@ function setUp() {
GIT_CONFIG_NOSYSTEM=true

# Set username and email
OLD_GIT_AUTHOR_NAME=$GIT_AUTHOR_NAME
GIT_AUTHOR_NAME="Testing Tester"
OLD_GIT_AUTHOR_EMAIL=$GIT_AUTHOR_EMAIL
GIT_AUTHOR_EMAIL="[email protected]"

# Set default username if not already set!
if [[ -z $(git config user.name) ]]; then
GIT_AUTHOR_NAME_SET_BY_TEST=true
git config --global user.name "${GIT_AUTHOR_NAME}"
fi
# Set default email if not already set!
if [[ -z $(git config user.email) ]]; then
GIT_AUTHOR_EMAIL_SET_BY_TEST=true
git config --global user.email "${GIT_AUTHOR_EMAIL}"
fi

cat << EOF > "$HOME/.gitconfig"
[user]
name = Testing Tester
email = [email protected]
EOF
# Initialize FOLDER as git repository
git init 1>/dev/null
}

function tearDown() {
if [[ -n "${OLD_GIT_AUTHOR_NAME}" ]]; then
GIT_AUTHOR_NAME=$OLD_GIT_AUTHOR
unset OLD_GIT_AUTHOR_NAME
else
unset GIT_AUTHOR_NAME
fi

if [[ -n "${OLD_GIT_AUTHOR_EMAIL}" ]]; then
GIT_AUTHOR_EMAIL=$OLD_GIT_AUTHOR_EMAIL
unset OLD_GIT_AUTHOR_EMAIL
else
unset GIT_AUTHOR_EMAIL
fi

if [[ "${GIT_AUTHOR_NAME_SET_BY_TEST}" == "true" ]]; then
git config --global --unset user.name
fi
if [[ "${GIT_AUTHOR_EMAIL_SET_BY_TEST}" == "true" ]]; then
git config --global --unset user.email
fi

# Back to original home and use
HOME="$OLD_HOME"
unset OLD_HOME
Expand Down