summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYves Fischer <yvesf-git@xapek.org>2018-11-27 12:36:56 +0100
committerYves Fischer <yvesf-git@xapek.org>2018-11-27 12:36:56 +0100
commit248ea9bbb80111e4c8c1a70419bc963355853167 (patch)
tree03299b8ed7a3dd2838c1da3d79a115d8734967c5
parentf0cc28e3758524d74d20a8219b13966d01085fea (diff)
downloadgit-repo-248ea9bbb80111e4c8c1a70419bc963355853167.tar.gz
git-repo-248ea9bbb80111e4c8c1a70419bc963355853167.zip
add tests and fix errors found by tests
-rw-r--r--.gitignore6
-rw-r--r--Makefile3
-rwxr-xr-xgit-repo54
-rwxr-xr-xtest/run.sh272
4 files changed, 312 insertions, 23 deletions
diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..faff549
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,6 @@
+/test/bin
+/test/work
+/*.deb
+/target
+/work
+*~
diff --git a/Makefile b/Makefile
index 4f93756..a9011b3 100644
--- a/Makefile
+++ b/Makefile
@@ -14,3 +14,6 @@ work/data/$(PREFIX):
shellcheck:
shellcheck --shell=bash --color=never git-repo
+.PHONY: test
+test:
+ test/run.sh
diff --git a/git-repo b/git-repo
index ea047bf..6ce356f 100755
--- a/git-repo
+++ b/git-repo
@@ -62,7 +62,6 @@ do_list() {
}
_create_repo() {
- local label=$1
local shared=$2
local group=$3
local is_public=$4
@@ -82,12 +81,12 @@ _create_repo() {
read -r -p 'Set Description: '
local repo_desc=$REPLY
- read -r -p "Create $label in $repo_dir Ok? (y/N)"
+ read -r -p "Create $repo_name in $repo_dir Ok? (y/N)"
test "$REPLY" == "y" || exit 1
mkdir -p "$repo_dir"
git init -q --bare --shared="$shared" "$repo_dir"
- git config receive.denyNonFastforwards false
+ GIT_DIR="$repo_dir" git config receive.denyNonFastforwards false
chgrp -R "$group" "$repo_dir"
echo "$repo_desc" > "$repo_dir/description"
@@ -96,7 +95,7 @@ _create_repo() {
echo "created $repo_dir/PUBLIC to expose via cgit"
fi
- echo "done creating $label in $repo_dir"
+ echo "done creating $repo_name in $repo_dir"
echo "use 'git repo show $repo_dir' for details"
return 0
}
@@ -165,15 +164,16 @@ _do_make() {
local repo_dir_mode=$3
local repo_is_public=$4
test -f "$repo_path/HEAD" || { echo "$repo_path is not a git repository"; exit 1; }
- set -x
git init -q --bare --shared="$repo_file_mode" "$repo_path"
find "$repo_path" -type d -exec chown "$USER:share" \{\} \;
find "$repo_path" -type d -exec chmod "$repo_dir_mode" \{\} \;
find "$repo_path" -type f -exec chmod "$repo_file_mode" \{\} \;
if $repo_is_public; then
touch "$repo_path/PUBLIC"
+ else
+ rm -f "$repo_path/PUBLIC"
fi
- set +x
+ do_list "$repo_path"
}
do_make_public() {
@@ -188,23 +188,8 @@ do_make_private() {
_do_make "$1" 0600 0700 false
}
-case "$1" in
- list) do_list "$2" ;;
- create-public) do_create_public ;;
- create-shared) do_create_shared ;;
- create-private) do_create_private ;;
- make-public) do_make_public "$2" ;;
- make-shared) do_make_shared "$2" ;;
- make-private) do_make_private "$2" ;;
- show)
- shift
- do_show "$@"
- ;;
- mirror) do_mirror "$2" ;;
- *)
- cat <<HERE
-Unknown subcommand: '$1'
-
+do_help() {
+ cat <<HERE
Subcommands of git repo:
list [<dir>] List infos (optional: only below <dir>)
show <dir...> Show Commands for repo <dir>
@@ -230,5 +215,28 @@ Subcommands of git repo:
☛ For limited ssh-access with current user configure ~/.ssh/authorized_keys with:
command=\"git shell -c \\\"\$SSH_ORIGINAL_COMMAND\\\"\",no-port-forwarding,no-agent-forwarding,no-X11-forwarding,no-pty ssh-rsa AAAAB3.....
HERE
+}
+
+case "$1" in
+ list) do_list "$2" ;;
+ create-public) do_create_public ;;
+ create-shared) do_create_shared ;;
+ create-private) do_create_private ;;
+ make-public) do_make_public "$2" ;;
+ make-shared) do_make_shared "$2" ;;
+ make-private) do_make_private "$2" ;;
+ show)
+ shift
+ do_show "$@"
+ ;;
+ mirror) do_mirror "$2" ;;
+ help)
+ do_help
+ ;;
+ *)
+ cat <<HERE
+Unknown subcommand: '$1'
+HERE
+ do_help
;;
esac
diff --git a/test/run.sh b/test/run.sh
new file mode 100755
index 0000000..430041c
--- /dev/null
+++ b/test/run.sh
@@ -0,0 +1,272 @@
+#!/bin/bash
+# depends on: bwrap, expect
+
+cd $(dirname "$0")
+set -x
+
+fail() {
+ set +x
+ echo >&2
+ echo "Test : $testname failed" >&2
+ echo "Reason: $@" >&2
+ echo >&2
+ exit 2
+}
+
+clean() {
+ if [ -d "bin" ]; then
+ rm -r bin
+ fi
+ if [ -d "work" ]; then
+ rm -r work
+ fi
+ mkdir bin work
+ cp ../git-repo bin
+}
+
+run() {
+ bwrap \
+ --ro-bind /bin /bin \
+ --ro-bind /usr /usr \
+ --ro-bind /etc /etc \
+ --ro-bind /lib /lib \
+ --ro-bind /lib64 /lib64 \
+ --ro-bind /run /run \
+ --dev /dev \
+ --proc /proc \
+ --ro-bind bin /usr/local/bin \
+ --bind work /git \
+ $1
+}
+
+filemode() {
+ stat --format=%a "$@"
+}
+
+########################
+testname=help
+clean
+
+{
+ cat <<EOF
+spawn git repo help
+expect "Subcommands of git repo are"
+wait
+EOF
+} | run expect -f - || fail "Expect script failed"
+
+echo "done"
+########################
+testname=create-public
+clean
+
+{
+ cat <<EOF
+spawn git repo create-public
+
+expect "New repository name.git"
+send "test-repo.git\n"
+
+expect "Description"
+send "Test description\n"
+
+expect "Create test-repo.git in"
+send "y\n"
+wait
+EOF
+} | run expect -f - || fail "Expect script failed"
+
+test -d work/$USER/test-repo.git ||
+ fail "repo creation failed"
+grep "Test description" work/$USER/test-repo.git/description ||
+ fail "Setting description failed"
+test $(filemode work/$USER/test-repo.git) == 2775 ||
+ fail "Repodir created with wrong filemode"
+test $(filemode work/$USER/test-repo.git/description) == 664 ||
+ fail "Repodir created with wrong filemode"
+test -f work/$USER/test-repo.git/PUBLIC ||
+ fail "Test for PUBLIC file"
+
+########################
+testname=create-shared
+clean
+
+{
+ cat <<EOF
+spawn git repo create-shared
+
+expect "New repository name.git"
+send "test-repo.git\n"
+
+expect "Description"
+send "Test description\n"
+
+expect "Create test-repo.git in"
+send "y\n"
+wait
+EOF
+} | run expect -f - || fail "Expect script failed"
+
+test -d work/$USER/test-repo.git ||
+ fail "repo creation failed"
+grep "Test description" work/$USER/test-repo.git/description ||
+ fail "Setting description failed"
+test $(filemode work/$USER/test-repo.git) == 2770 ||
+ fail "Repodir created with wrong filemode"
+test $(filemode work/$USER/test-repo.git/description) == 660 ||
+ fail "Repodir created with wrong filemode"
+test \! -f work/$USER/test-repo.git/PUBLIC ||
+ fail "Test for PUBLIC file"
+
+########################
+testname=create-private
+clean
+
+{
+ cat <<EOF
+spawn git repo create-private
+
+expect "New repository name.git"
+send "test-repo.git\n"
+
+expect "Description"
+send "Test description\n"
+
+expect "Create test-repo.git in"
+send "y\n"
+wait
+EOF
+} | run expect -f - || fail "Expect script failed"
+
+test -d work/$USER/test-repo.git ||
+ fail "repo creation failed"
+grep "Test description" work/$USER/test-repo.git/description ||
+ fail "Setting description failed"
+test $(filemode work/$USER/test-repo.git) == 2700 ||
+ fail "Repodir created with wrong filemode"
+test $(filemode work/$USER/test-repo.git/description) == 600 ||
+ fail "Repodir created with wrong filemode"
+test \! -f work/$USER/test-repo.git/PUBLIC ||
+ fail "Test for PUBLIC file"
+
+########################
+testname=list
+clean
+
+{
+ cat <<EOF
+spawn git repo create-private
+expect "New repository name.git"
+send "test-repo1.git\n"
+expect "Description"
+send "Test description\n"
+expect "Create"
+send "y\n"
+wait
+
+spawn git repo create-private
+expect "New repository name.git"
+send "test-repo2.git\n"
+expect "Description"
+send "Test description\n"
+expect "Create"
+send "y\n"
+wait
+
+spawn git repo list
+expect -re "test-repo1\.git.*owner=$USER.*Test description"
+expect -re "test-repo2"
+EOF
+} | run expect -f - || fail "Expect script failed"
+
+
+########################
+testname=public-make-private
+clean
+
+{
+ cat <<EOF
+spawn git repo create-public
+expect "New repository name.git*"
+send "test-repo.git\n"
+expect "Description"
+send "Test description\n"
+expect "Create test-repo.git "
+send "y\n"
+expect eof
+wait
+
+spawn git repo make-private /git/$USER/test-repo.git
+expect "test-repo.git*(owner=$USER)"
+expect eof
+wait
+EOF
+} | run expect -f - || fail "Expect script failed"
+
+test $(filemode work/$USER/test-repo.git) == 2700 ||
+ fail "Repodir created with wrong filemode"
+test $(filemode work/$USER/test-repo.git/description) == 600 ||
+ fail "Repodir created with wrong filemode"
+test \! -f work/$USER/test-repo.git/PUBLIC ||
+ fail "Test for PUBLIC file"
+
+
+########################
+testname=private-make-public
+clean
+
+{
+ cat <<EOF
+spawn git repo create-private
+expect "New repository name.git*"
+send "test-repo.git\n"
+expect "Description"
+send "Test description\n"
+expect "Create test-repo.git "
+send "y\n"
+expect eof
+wait
+
+spawn git repo make-public /git/$USER/test-repo.git
+expect "test-repo.git*(owner=$USER group=* PUBLIC)"
+expect eof
+wait
+EOF
+} | run expect -f - || fail "Expect script failed"
+
+test $(filemode work/$USER/test-repo.git) == 2775 ||
+ fail "Repodir created with wrong filemode"
+test $(filemode work/$USER/test-repo.git/description) == 664 ||
+ fail "Repodir created with wrong filemode"
+test -f work/$USER/test-repo.git/PUBLIC ||
+ fail "Test for PUBLIC file"
+
+
+########################
+testname=show-public
+clean
+
+{
+ cat <<EOF
+spawn git repo create-public
+expect "New repository name.git*"
+send "test-repo.git\n"
+expect "Description"
+send "Test description\n"
+expect "Create test-repo.git "
+send "y\n"
+expect eof
+wait
+
+spawn git repo show /git/$USER/test-repo.git
+#expect "/git/$USER/test-repo.git"
+expect "Permissions mode 2775"
+expect "sharedrepository=0664"
+expect "SSH"
+expect "HTTP"
+expect "set-url origin"
+expect "update description"
+expect eof
+wait
+EOF
+} | run expect -f - || fail "Expect script failed"