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

Commit

Permalink
Merge pull request #1221 from Syphdias/fix-git-test
Browse files Browse the repository at this point in the history
[Bugfix] Don't unset global git user config after git tests
  • Loading branch information
dritter authored Apr 1, 2019
2 parents 6bfc17b + 14f648f commit 67b4474
Showing 1 changed file with 20 additions and 37 deletions.
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="test@powerlevel9k.theme"

# 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 = test@powerlevel9k.theme
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

0 comments on commit 67b4474

Please sign in to comment.