Initial Commit - moved out of ubuntu_config.

This commit is contained in:
Nathan Broadbent
2011-10-18 00:18:17 +08:00
commit e634d0c4b5
24 changed files with 2592 additions and 0 deletions

View File

@@ -0,0 +1,185 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Git Breeze - Streamline your git workflow.
# Copyright 2011 Nathan Broadbent (http://madebynathan.com). All Rights Reserved.
# Released under the LGPL (GNU Lesser General Public License)
# ------------------------------------------------------------------------------
#
# Unit tests for git shell scripts
thisDir="$( cd -P "$( dirname "$0" )" && pwd )"
# Zsh compatibility
if [ -n "${ZSH_VERSION:-}" ]; then shell="zsh"; SHUNIT_PARENT=$0; setopt shwordsplit; fi
# Load test helpers
. "$thisDir/../../support/test_helper"
# Load functions to test
. "$thisDir/../../../lib/_shared.sh"
. "$thisDir/../../../lib/git/repo_management.sh"
# Setup and tear down
#-----------------------------------------------------------------------------
oneTimeSetUp() {
GIT_REPO_DIR=$(mktemp -d)
GIT_REPOS="/tmp/test_repo_1:/tmp/test_repo_11"
git_status_command="git status"
git_index_file="$GIT_REPO_DIR/.git_index"
silentGitCommands
cd $GIT_REPO_DIR
# Setup test repos in temp repo dir
for repo in github bitbucket source_forge; do
mkdir $repo; cd $repo; git init; cd - > /dev/null
done
# Add some nested dirs for testing resursive tab completion
mkdir -p github/videos/octocat/live_action
# Add hidden dir to test that '.git' is filtered, but other hidden dirs are available.
mkdir -p github/.im_hidden
# Setup a test repo with some submodules
# (just a dummy '.gitmodules' file and some nested .git directories)
mkdir submodules_everywhere
cd submodules_everywhere
git init
cat > .gitmodules <<EOF
[submodule "very/nested/directory/red_submodule"]
[submodule "very/nested/directory/green_submodule"]
[submodule "very/nested/directory/blue_submodule"]
EOF
mkdir -p "very/nested/directory"
cd "very/nested/directory"
for repo in red_submodule green_submodule blue_submodule; do
mkdir $repo; cd $repo; git init; cd - > /dev/null
done
# Setup some custom repos outside the main repo dir
local IFS=":"
for dir in $GIT_REPOS; do
mkdir -p $dir; cd $dir; git init;
done
unset IFS
verboseGitCommands
cd "$orig_cwd"
}
oneTimeTearDown() {
rm -rf "${GIT_REPO_DIR}"
local IFS=":"
for dir in $GIT_REPOS; do rm -rf $dir; done
}
ensureIndex() {
_check_git_repo_index
}
index_no_newlines() {
cat $git_index_file | tr "\\n" " "
}
#-----------------------------------------------------------------------------
# Unit tests
#-----------------------------------------------------------------------------
test_repo_index_command() {
git_repo --rebuild-index > /dev/null
# Test that all repos are detected, and sorted alphabetically
assertIncludes "$(index_no_newlines)" "bitbucket.*\
blue_submodule.*\
github.*\
green_submodule.*\
red_submodule.*\
source_forge.*\
submodules_everywhere.*\
test_repo_11.*\
test_repo_1"
}
test_check_git_repo_index() {
ensureIndex
echo "should not be regenerated" >> $git_index_file
_check_git_repo_index
# Test that index is not rebuilt unless empty
assertIncludes "$(index_no_newlines)" "should not be regenerated"
rm $git_index_file
# Test the index is rebuilt
_check_git_repo_index
assertTrue "[ -f $git_index_file ]"
}
test_git_repo_count() {
assertEquals "9" "$(_git_repo_count)"
}
test_repo_list() {
ensureIndex
list=$(git_repo --list)
assertIncludes "$list" "bitbucket" || return
assertIncludes "$list" "blue_submodule" || return
assertIncludes "$list" "test_repo_11"
}
# Test matching rules for changing directory
test_git_repo_changing_directory() {
ensureIndex
git_repo "github"; assertEquals "$GIT_REPO_DIR/github" "$PWD"
git_repo "github/"; assertEquals "$GIT_REPO_DIR/github" "$PWD"
git_repo "bucket"; assertEquals "$GIT_REPO_DIR/bitbucket" "$PWD"
git_repo "green_sub"; assertEquals "$GIT_REPO_DIR/submodules_everywhere/very/nested/directory/green_submodule" "$PWD"
git_repo "_submod"; assertEquals "$GIT_REPO_DIR/submodules_everywhere/very/nested/directory/blue_submodule" "$PWD"
git_repo "test_repo_1"; assertEquals "/tmp/test_repo_1" "$PWD"
git_repo "test_repo_11"; assertEquals "/tmp/test_repo_11" "$PWD"
git_repo "test_repo_"; assertEquals "/tmp/test_repo_11" "$PWD"
git_repo "github/videos/octocat/live_action"; assertEquals "$GIT_REPO_DIR/github/videos/octocat/live_action" "$PWD"
}
test_git_repo_tab_completion() {
# Only run tab completion test for bash
if [[ "$0" == *bash ]]; then
ensureIndex
COMP_CWORD=0
# Test that '--' commands have tab completion
COMP_WORDS="--"
_git_repo_tab_completion
assertEquals "Incorrect number of tab-completed '--' commands" "5" "$(tab_completions | wc -w)"
COMP_WORDS="gith"
_git_repo_tab_completion
assertIncludes "$(tab_completions)" "github/"
# Test completion for project sub-directories when project ends with '/'
COMP_WORDS="github/"
_git_repo_tab_completion
assertIncludes "$(tab_completions)" "github/videos/"
# Check that '.git/' is filtered from completion, but other hidden dirs are available
assertNotIncludes "$(tab_completions)" "github/.git/"
assertIncludes "$(tab_completions)" "github/.im_hidden/"
COMP_WORDS="github/videos/"
_git_repo_tab_completion
assertIncludes "$(tab_completions)" "github/videos/octocat/"
# Test that completion checks for other matching projects even if one matches perfectly
COMP_WORDS="test_repo_1"
_git_repo_tab_completion
assertIncludes "$(tab_completions)" "test_repo_1/ test_repo_11/"
fi
}
# load and run shUnit2
# Call this function to run tests
. "$thisDir/../../support/shunit2"

View File

@@ -0,0 +1,255 @@
#!/bin/bash
# ------------------------------------------------------------------------------
# Git Breeze - Streamline your git workflow.
# Copyright 2011 Nathan Broadbent (http://madebynathan.com). All Rights Reserved.
# Released under the LGPL (GNU Lesser General Public License)
# ------------------------------------------------------------------------------
#
# Unit tests for git shell scripts
thisDir="$( cd -P "$( dirname "$0" )" && pwd )"
# Zsh compatibility
if [ -n "${ZSH_VERSION:-}" ]; then shell="zsh"; SHUNIT_PARENT=$0; setopt shwordsplit; fi
# Load test helpers
. "$thisDir/../../support/test_helper"
# Load functions to test
. "$thisDir/../../../lib/_shared.sh"
. "$thisDir/../../../lib/git/status_shortcuts.sh"
# Setup and tear down
#-----------------------------------------------------------------------------
oneTimeSetUp() {
# Test Config
git_env_char="e"
gs_max_changes="20"
ga_auto_remove="yes"
testRepo=$(mktemp -d)
}
oneTimeTearDown() {
rm -rf "${testRepo}"
}
setupTestRepo() {
rm -rf "${testRepo}"
mkdir -p "$testRepo"
cd "$testRepo"
git init > /dev/null
}
#-----------------------------------------------------------------------------
# Unit tests
#-----------------------------------------------------------------------------
test_git_expand_args() {
local e1="one"; local e2="two"; local e3="three"; local e4="four"; local e5="five"; local e6="six"; local e7="seven"
local error="Args not expanded correctly"
assertEquals "$error" "one three seven" "$(git_expand_args 1 3 7)"
assertEquals "$error" "one two three six" "$(git_expand_args 1..3 6)"
assertEquals "$error" "seven two three four five one" "$(git_expand_args seven 2..5 1)"
}
test_git_status_shortcuts() {
setupTestRepo
silentGitCommands
# Set up some modifications
touch deleted_file
git add deleted_file
git commit -m "Test commit"
touch new_file
touch untracked_file
git add new_file
echo "changed" > new_file
rm deleted_file
verboseGitCommands
# Test that groups can be filtered by passing a parameter
git_status1=$(git_status_shortcuts 1)
git_status3=$(git_status_shortcuts 3)
git_status4=$(git_status_shortcuts 4)
# Test for presence of expected groups
assertIncludes "$git_status1" "Changes to be committed"
assertIncludes "$git_status3" "Changes not staged for commit"
assertIncludes "$git_status4" "Untracked files"
assertNotIncludes "$git_status3" "Changes to be committed"
assertNotIncludes "$git_status4" "Changes not staged for commit"
assertNotIncludes "$git_status1" "Untracked files"
assertNotIncludes "$git_status4" "Changes to be committed"
assertNotIncludes "$git_status1" "Changes not staged for commit"
assertNotIncludes "$git_status3" "Untracked files"
# Run command in shell, load output from temp file into variable
temp_file=$(mktemp)
git_status_shortcuts > $temp_file
git_status=$(cat $temp_file | strip_colors)
assertIncludes "$git_status" "new file: *\[1\] *new_file" || return
assertIncludes "$git_status" "deleted: *\[2\] *deleted_file" || return
assertIncludes "$git_status" "modified: *\[3\] *new_file" || return
assertIncludes "$git_status" "untracked: *\[4\] *untracked_file" || return
# Test that shortcut env variables are set with full path
local error="Env variable was not set"
assertEquals "$error" "$testRepo/new_file" "$e1" || return
assertEquals "$error" "$testRepo/deleted_file" "$e2" || return
assertEquals "$error" "$testRepo/new_file" "$e3" || return
assertEquals "$error" "$testRepo/untracked_file" "$e4" || return
}
test_git_status_produces_relative_paths() {
setupTestRepo
mkdir -p dir1/sub1/subsub1
mkdir -p dir1/sub2
mkdir -p dir2
touch dir1/sub1/subsub1/testfile
touch dir1/sub2/testfile
touch dir2/testfile
git add .
git_status=$(git_status_shortcuts | strip_colors)
assertIncludes "$git_status" "dir1/sub1/subsub1/testfile" || return
cd $testRepo/dir1
git_status=$(git_status_shortcuts | strip_colors)
assertIncludes "$git_status" " sub1/subsub1/testfile" || return
assertIncludes "$git_status" " sub2/testfile" || return
assertIncludes "$git_status" "../dir2/testfile" || return
cd $testRepo/dir1/sub1
git_status=$(git_status_shortcuts | strip_colors)
assertIncludes "$git_status" " subsub1/testfile" || return
assertIncludes "$git_status" " ../sub2/testfile" || return
assertIncludes "$git_status" "../../dir2/testfile" || return
cd $testRepo/dir1/sub1/subsub1
git_status=$(git_status_shortcuts | strip_colors)
assertIncludes "$git_status" " testfile" || return
assertIncludes "$git_status" " ../../sub2/testfile" || return
assertIncludes "$git_status" "../../../dir2/testfile" || return
}
test_git_status_shortcuts_merge_conflicts() {
setupTestRepo
silentGitCommands
# Set up every possible merge conflict
touch both_modified both_deleted deleted_by_them deleted_by_us
echo "renamed file needs some content" > renamed_file
git add both_modified both_deleted renamed_file deleted_by_them deleted_by_us
git commit -m "First commit"
git checkout -b conflict_branch
echo "added by branch" > both_added
echo "branch line" > both_modified
echo "deleted by us" > deleted_by_us
git rm deleted_by_them both_deleted
git mv renamed_file renamed_file_on_branch
git add both_added both_modified deleted_by_us
git commit -m "Branch commit"
git checkout master
echo "added by master" > both_added
echo "master line" > both_modified
echo "deleted by them" > deleted_by_them
git rm deleted_by_us both_deleted
git mv renamed_file renamed_file_on_master
git add both_added both_modified deleted_by_them
git commit -m "Master commit"
git merge conflict_branch
verboseGitCommands
# Test output without stripped color codes
git_status=$(git_status_shortcuts | strip_colors)
assertIncludes "$git_status" "both added: *\[[0-9]*\] *both_added" || return
assertIncludes "$git_status" "both modified: *\[[0-9]*\] *both_modified" || return
assertIncludes "$git_status" "deleted by them: *\[[0-9]*\] *deleted_by_them" || return
assertIncludes "$git_status" "deleted by us: *\[[0-9]*\] *deleted_by_us" || return
assertIncludes "$git_status" "both deleted: *\[[0-9]*\] *renamed_file" || return
assertIncludes "$git_status" "added by them: *\[[0-9]*\] *renamed_file_on_branch" || return
assertIncludes "$git_status" "added by us: *\[[0-9]*\] *renamed_file_on_master" || return
}
test_git_status_shortcuts_max_changes() {
setupTestRepo
export gs_max_changes="5"
# Add 5 untracked files
touch a b c d e
git_status=$(git_status_shortcuts | strip_colors)
for i in $(seq 1 5); do
assertIncludes "$git_status" "\[$i\]" || return
done
# 6 untracked files is more than $gs_max_changes
touch f
git_status=$(git_status_shortcuts | strip_colors)
assertNotIncludes "$git_status" "\[[0-9]*\]" || return
export gs_max_changes="20"
}
test_git_add_shorcuts() {
setupTestRepo
touch a b c d e f g h i j
# Show git status, which sets up env variables
git_status_shortcuts > /dev/null
git_add_shorcuts 2..4 7 8 > /dev/null
git_status=$(git_status_shortcuts 1 | strip_colors)
for c in b c d g h; do
assertIncludes "$git_status" "\[[0-9]*\] $c" || return
done
}
test_git_commit_prompt() {
setupTestRepo
commit_msg="\"Nathan's git commit prompt function!\""
dbl_escaped_msg="\\\\\"Nathan's git commit prompt function\"'"'!'"'\"\\\\\""
# Create temporary history file
HISTFILE=$(mktemp)
HISTFILESIZE=1000
HISTSIZE=1000
touch a b c d
git add . > /dev/null
# Lightly test the git commit prompt, by piping a commit message
# instead of user input.
echo "$commit_msg" | git_commit_prompt > /dev/null
git_show_output=$(git show --oneline --name-only)
assertIncludes "$git_show_output" "$commit_msg"
# Test that history was appended correctly.
if [[ $shell != "zsh" ]]; then history -n; fi # Reload bash history
test_history="$(history)"
assertIncludes "$test_history" "$commit_msg"
assertIncludes "$test_history" "git commit -m \"$dbl_escaped_msg\""
}
# load and run shUnit2
. "$thisDir/../../support/shunit2"

1048
test/support/shunit2 Executable file

File diff suppressed because it is too large Load Diff

38
test/support/test_helper Normal file
View File

@@ -0,0 +1,38 @@
orig_cwd="$PWD"
#
# Test helpers
#-----------------------------------------------------------------------------
# Strip color codes from a string
strip_colors() {
sed -r "s/\x1B\[([0-9]{1,2}(;[0-9]{1,2})?)?[m|K]//g"
}
# Print space separated tab completion options
tab_completions(){ echo "${COMPREPLY[@]}"; }
# Silence git commands
silentGitCommands() {
git() { /usr/bin/env git "$@" > /dev/null 2>&1; }
}
# Cancel silent git commands
verboseGitCommands() {
unset -f git
}
# Asserts
#-----------------------------------------------------------------------------
# assert $1 contains $2
assertIncludes() {
result=1; if echo "$1" | grep -Pq "$2"; then result=0; fi
assertTrue "'$1' should have contained '$2'" $result
}
# assert $1 does not contain $2
assertNotIncludes() {
result=1; if echo "$1" | grep -Pq "$2"; then result=0; fi
assertFalse "'$1' should not have contained '$2'" $result
}