Skip to contentSkip to conversation

Add basic test-script for git-submodule

PATCH In review Lars Hjemli 3 participants 9 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Add basic test-script for git-submodule

This test verifies the basic operations in git-submodule, i.e. that it is able to clone and update a submodule repository, that its status output is sane, and that it errors out when the submodule path is occupied during init.

---

Finally the test-script emerges. Btw, thanks for fixing my broken submodule patch :)

 t/t7400-submodule-basic.sh |   82 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 82 insertions(+), 0 deletions(-)
 create mode 100755 t/t7400-submodule-basic.sh
t/t7400-submodule-basic.sh new file +82 -0
new file mode 100755
index 0000000..2212b29
@@ -0,0 +1,82 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Lars Hjemli
+#
+
+test_description='Basic submodule support in porcelain
+
+This test tries to run the init, update and status commands of git-submodule
+with a known good setup (and two known bad)
+'
+
+. ./test-lib.sh
+
+
+# create a submodule repository
+mkdir lib && cd lib
+git-init >/dev/null
+echo a >a && git-add a && git-commit -q -m "submodule commit 1"
+git-tag -a -m "rev-1" rev-1
+rev1=$(git-rev-parse HEAD)
+cd ..
+
+# add submodule and other files to super repo
+echo a >a && echo z >z
+git-add a lib z && git-commit -q -m "super commit 1"
+
+# move submodule to another location, register repo url in .gitmodules
+mv lib .subrepo
+GIT_CONFIG=.gitmodules git-config module.lib.url ./.subrepo
+
+test_expect_success 'status is "missing"' \
+	'git-submodule status | grep "^-$rev1"'
+
+# make sure 'init' will not overwrite a regular file
+touch lib
+test_expect_failure 'init fails when path is used by a file' \
+	'git-submodule init'
+
+# make sure 'init' will not overwrite a nonempty directory
+rm lib
+mkdir -p lib/foo
+test_expect_failure 'init fails when path is used by a nonempty directory' \
+	'git-submodule init'
+
+# turn lib into an empty directory, just like git-checkout would do
+rmdir lib/foo
+test_expect_success 'init works when path is an empty dir' \
+	'git-submodule init && test -d lib/.git && git-diff --exit-code'
+
+head=$(cd lib && git-rev-parse HEAD)
+test_expect_success 'submodule HEAD should match rev1' \
+	'test "$head" = "$rev1"'
+
+test_expect_success 'status is "up-to-date" after init' \
+	'git-submodule status | grep "^ $rev1"'
+
+# change the submodule HEAD
+cd lib
+echo b >b && git-add b && git-commit -q -m "submodule commit 2"
+rev2=$(git-rev-parse HEAD)
+cd ..
+
+test_expect_success 'status is "modified" after submodule commit' \
+	'git-submodule status | grep "^\+$rev2"'
+
+test_expect_success 'the --cached sha1 should be rev1' \
+	'git-submodule --cached status | grep "^\+$rev1"'
+
+test_expect_failure 'git-diff --exit-code reports local modifications' \
+	'git-diff --exit-code'
+
+test_expect_success 'update should checkout the correct commit' \
+	'git-submodule update && git-diff --exit-code'
+
+head=$(cd lib && git-rev-parse HEAD)
+test_expect_success 'submodule HEAD should match rev1' \
+	'test "$head" = "$rev1"'
+
+test_expect_success 'status is "up-to-date" after update' \
+	'git-submodule status | grep "^ $rev1"'
+
+test_done
Signature · 1 line
1.5.2.74.g6b2d
#2 lore raw
In reply to Junio C Hamano
Quoted 11 lines · Junio C Hamano
Quoted 9 lines · Lars Hjemli
t/t7400-submodule-basic.sh new file +9 -0
+# create a submodule repository
+mkdir lib && cd lib
+git-init >/dev/null
+echo a >a && git-add a && git-commit -q -m "submodule commit 1"
+git-tag -a -m "rev-1" rev-1
+rev1=$(git-rev-parse HEAD)
+cd ..
+
+# add submodule and other files to super repo
Quoted 8 lines

t/t7400-submodule-basic.sh line 29

Quoted 6 lines
t/t7400-submodule-basic.sh new file +6 -0
+echo a >a && echo z >z
+git-add a lib z && git-commit -q -m "super commit 1"
+
+# move submodule to another location, register repo url in .gitmodules
+mv lib .subrepo
+GIT_CONFIG=.gitmodules git-config module.lib.url ./.subrepo

We typically try to catch malfunction even while setting up the test case.

Ok

Quoted 12 lines

t/t7400-submodule-basic.sh line 37

Quoted 8 lines
t/t7400-submodule-basic.sh new file +7 -0
+test_expect_success 'status is "missing"' \
+     'git-submodule status | grep "^-$rev1"'
+
+# make sure 'init' will not overwrite a regular file
+touch lib
+test_expect_failure 'init fails when path is used by a file' \
+     'git-submodule init'

I am guilty for introducing "expect-failure", but it is usually a mistake to use it unless you are testing something very trivial.

Quoted 4 lines

For example, for this case, you would want to make sure the command "git-submodule init" exits with non-zero status, but also you would want to make sure that it does not disturb the existing file "lib".

Would you prefer a patch on top of this version of the test-script or on top of the later patch which uses .git/config to store submodule url?

--
larsh

#3 lore raw
Quoted 3 lines · Lars Hjemli

Would you prefer a patch on top of this version of the test-script or on top of the later patch which uses .git/config to store submodule url?

As you said you will be redoing submodule + .git/config stuff in your other message, I would expect a replacement (i.e. not on top of this) patch for tests to check the existing submodule features. If you want to finish the submodule + .git/config one first and then test script on top to test both existing featues and submodule + .git/config features, that is fine as well. Your choice.

One important point is to treat the test-script take #1 as "already rejected", and not basing further patches on it.

#4 lore raw

This test tries to verify basic sanity of git-submodule, i.e. that it is able to clone and update a submodule repository, that its status output is sane, and that it barfs when the submodule path is occupied during init.

---

Quoted 7 lines · Junio C Hamano

As you said you will be redoing submodule + .git/config stuff in your other message, I would expect a replacement (i.e. not on top of this) patch for tests to check the existing submodule features. If you want to finish the submodule + .git/config one first and then test script on top to test both existing featues and submodule + .git/config features, that is fine as well. Your choice.

My continuous quest for self-discipline left me no choice but to attempt the creation of a proper testscript for the existing features ;-)

  t/t7400-submodule-basic.sh |  143 ++++++++++++++++++++++++++++++++++++++++++++
  1 files changed, 143 insertions(+), 0 deletions(-)

create mode 100755 t/t7400-submodule-basic.sh

t/t7400-submodule-basic.sh new file +143 -0
new file mode 100755
index 0000000..a8beee2
@@ -0,0 +1,143 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Lars Hjemli
+#
+
+test_description='Basic porcelain support for submodules
+
+This test tries to verify basic sanity of the init, update and status
+subcommands of git-submodule.
+'
+
+. ./test-lib.sh
+
+#
+# Test setup:
+#  -create a repository in directory lib
+#  -add a couple of files
+#  -add directory lib to 'superproject', this creates a DIRLINK entry
+#  -add a couple of regular files to enable testing of submodule filtering
+#  -mv lib subrepo
+#  -add an entry to .gitmodules for path 'lib'
+#
+test_expect_success 'Prepare submodule testing' '
+	mkdir lib &&
+	cd lib &&
+	git-init &&
+	echo a >a &&
+	git-add a &&
+	git-commit -m "submodule commit 1" &&
+	git-tag -a -m "rev-1" rev-1 &&
+	rev1=$(git-rev-parse HEAD) &&
+	if test -z "$rev1"
+	then
+		echo "[OOPS] submodule git-rev-parse returned nothing"
+		false
+	fi &&
+	cd .. &&
+	echo a >a &&
+	echo z >z &&
+	git-add a lib z &&
+	git-commit -m "super commit 1" &&
+	mv lib .subrepo &&
+	GIT_CONFIG=.gitmodules git-config module.lib.url ./.subrepo
+'
+
+test_expect_success 'status should only print one line' '
+	lines=$(git-submodule status | wc -l)
+	test "$lines" = "1"
+'
+
+test_expect_success 'status should initially be "missing"' '
+	git-submodule status | grep "^-$rev1"
+'
+
+test_expect_success 'init should fail when path is used by a file' '
+	echo "hello" >lib &&
+	if git-submodule init
+	then
+		echo "[OOPS] init should have failed"
+		false
+	elif test -f lib && test "$(cat lib)" != "hello"
+	then
+		echo "[OOPS] init failed but lib file was molested"
+		false
+	else
+		rm lib
+	fi
+'
+
+test_expect_success 'init should fail when path is used by a nonempty directory' '
+	mkdir lib &&
+	echo "hello" >lib/a &&
+	if git-submodule init
+	then
+		echo "[OOPS] init should have failed"
+		false
+	elif test "$(cat lib/a)" != "hello"
+	then
+		echo "[OOPS] init failed but lib/a was molested"
+		false
+	else
+		rm lib/a
+	fi
+'
+
+test_expect_success 'init should work when path is an empty dir' '
+	rm -rf lib &&
+	mkdir lib &&
+	git-submodule init &&
+	head=$(cd lib && git-rev-parse HEAD) &&
+	if test -z "$head"
+	then
+		echo "[OOPS] Failed to obtain submodule head"
+		false
+	elif test "$head" != "$rev1"
+	then
+		echo "[OOPS] Submodule head is $head but should have been $rev1"
+		false
+	fi
+'
+
+test_expect_success 'status should be "up-to-date" after init' '
+	git-submodule status | grep "^ $rev1"
+'
+
+test_expect_success 'status should be "modified" after submodule commit' '
+	cd lib &&
+	echo b >b &&
+	git-add b &&
+	git-commit -m "submodule commit 2" &&
+	rev2=$(git-rev-parse HEAD) &&
+	cd .. &&
+	if test -z "$rev2"
+	then
+		echo "[OOPS] submodule git-rev-parse returned nothing"
+		false
+	fi &&
+	git-submodule status | grep "^\+$rev2"
+'
+
+test_expect_success 'the --cached sha1 should be rev1' '
+	git-submodule --cached status | grep "^\+$rev1"
+'
+
+test_expect_success 'update should checkout rev1' '
+	git-submodule update &&
+	head=$(cd lib && git-rev-parse HEAD) &&
+	if test -z "$head"
+	then
+		echo "[OOPS] submodule git-rev-parse returned nothing"
+		false
+	elif test "$head" != "$rev1"
+	then
+		echo "[OOPS] init did not checkout correct head"
+		false
+	fi
+'
+
+test_expect_success 'status should be "up-to-date" after update' '
+	git-submodule status | grep "^ $rev1"
+'
+
+test_done
Signature · 1 line
1.5.2.839.ga3b1-dirty
#5 lore raw

t/t7400-submodule-basic.sh line 47

Quoted 2 lines · Lars Hjemli
t/t7400-submodule-basic.sh new file +2 -0
+test_expect_success 'status should only print one line' '
+       lines=$(git-submodule status | wc -l)

&& is missing.

Quoted 1 line

+ test "$lines" = "1"

Please don't put output of wc in quotes because some versions of wc emit padding:

test $lines = 1

(without the quotes around 1 - like in other test scripts).

-- Hannes

#6 lore raw

This test tries to verify basic sanity of git-submodule, i.e. that it is able to clone and update a submodule repository, that its status output is sane, and that it barfs when the submodule path is occupied during init.

---

Quoted 6 lines · Johannes Sixt

Review

Quoted 2 lines · Lars Hjemli
(quoted diff) +2 -0
+test_expect_success 'status should only print one line' '
+       lines=$(git-submodule status | wc -l)

&& is missing.

Quoted 9 lines
Quoted 1 line

+ test "$lines" = "1"

Please don't put output of wc in quotes because some versions of wc emit padding:

test $lines = 1

(without the quotes around 1 - like in other test scripts).

Thanks, and sorry...

  t/t7400-submodule-basic.sh |  143 ++++++++++++++++++++++++++++++++++++++++++++
  1 files changed, 143 insertions(+), 0 deletions(-)

create mode 100755 t/t7400-submodule-basic.sh

t/t7400-submodule-basic.sh new file +143 -0
new file mode 100755
index 0000000..a985a6e
@@ -0,0 +1,143 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Lars Hjemli
+#
+
+test_description='Basic porcelain support for submodules
+
+This test tries to verify basic sanity of the init, update and status
+subcommands of git-submodule.
+'
+
+. ./test-lib.sh
+
+#
+# Test setup:
+#  -create a repository in directory lib
+#  -add a couple of files
+#  -add directory lib to 'superproject', this creates a DIRLINK entry
+#  -add a couple of regular files to enable testing of submodule filtering
+#  -mv lib subrepo
+#  -add an entry to .gitmodules for path 'lib'
+#
+test_expect_success 'Prepare submodule testing' '
+	mkdir lib &&
+	cd lib &&
+	git-init &&
+	echo a >a &&
+	git-add a &&
+	git-commit -m "submodule commit 1" &&
+	git-tag -a -m "rev-1" rev-1 &&
+	rev1=$(git-rev-parse HEAD) &&
+	if test -z "$rev1"
+	then
+		echo "[OOPS] submodule git-rev-parse returned nothing"
+		false
+	fi &&
+	cd .. &&
+	echo a >a &&
+	echo z >z &&
+	git-add a lib z &&
+	git-commit -m "super commit 1" &&
+	mv lib .subrepo &&
+	GIT_CONFIG=.gitmodules git-config module.lib.url ./.subrepo
+'
+
+test_expect_success 'status should only print one line' '
+	lines=$(git-submodule status | wc -l) &&
+	test $lines = 1
+'
+
+test_expect_success 'status should initially be "missing"' '
+	git-submodule status | grep "^-$rev1"
+'
+
+test_expect_success 'init should fail when path is used by a file' '
+	echo "hello" >lib &&
+	if git-submodule init
+	then
+		echo "[OOPS] init should have failed"
+		false
+	elif test -f lib && test "$(cat lib)" != "hello"
+	then
+		echo "[OOPS] init failed but lib file was molested"
+		false
+	else
+		rm lib
+	fi
+'
+
+test_expect_success 'init should fail when path is used by a nonempty directory' '
+	mkdir lib &&
+	echo "hello" >lib/a &&
+	if git-submodule init
+	then
+		echo "[OOPS] init should have failed"
+		false
+	elif test "$(cat lib/a)" != "hello"
+	then
+		echo "[OOPS] init failed but lib/a was molested"
+		false
+	else
+		rm lib/a
+	fi
+'
+
+test_expect_success 'init should work when path is an empty dir' '
+	rm -rf lib &&
+	mkdir lib &&
+	git-submodule init &&
+	head=$(cd lib && git-rev-parse HEAD) &&
+	if test -z "$head"
+	then
+		echo "[OOPS] Failed to obtain submodule head"
+		false
+	elif test "$head" != "$rev1"
+	then
+		echo "[OOPS] Submodule head is $head but should have been $rev1"
+		false
+	fi
+'
+
+test_expect_success 'status should be "up-to-date" after init' '
+	git-submodule status | grep "^ $rev1"
+'
+
+test_expect_success 'status should be "modified" after submodule commit' '
+	cd lib &&
+	echo b >b &&
+	git-add b &&
+	git-commit -m "submodule commit 2" &&
+	rev2=$(git-rev-parse HEAD) &&
+	cd .. &&
+	if test -z "$rev2"
+	then
+		echo "[OOPS] submodule git-rev-parse returned nothing"
+		false
+	fi &&
+	git-submodule status | grep "^\+$rev2"
+'
+
+test_expect_success 'the --cached sha1 should be rev1' '
+	git-submodule --cached status | grep "^\+$rev1"
+'
+
+test_expect_success 'update should checkout rev1' '
+	git-submodule update &&
+	head=$(cd lib && git-rev-parse HEAD) &&
+	if test -z "$head"
+	then
+		echo "[OOPS] submodule git-rev-parse returned nothing"
+		false
+	elif test "$head" != "$rev1"
+	then
+		echo "[OOPS] init did not checkout correct head"
+		false
+	fi
+'
+
+test_expect_success 'status should be "up-to-date" after update' '
+	git-submodule status | grep "^ $rev1"
+'
+
+test_done
Signature · 1 line
1.5.2.839.ga3b1-dirty
#7 lore raw
Quoted 5 lines · Lars Hjemli

This test tries to verify basic sanity of git-submodule, i.e. that it is able to clone and update a submodule repository, that its status output is sane, and that it barfs when the submodule path is occupied during init.

Micronit...

t/t7400-submodule-basic.sh line 123

Quoted 3 lines
t/t7400-submodule-basic.sh new file +3 -0
+test_expect_success 'the --cached sha1 should be rev1' '
+	git-submodule --cached status | grep "^\+$rev1"
+'

What's the purpose of that solitary backslash?

#8 lore raw

PATCH 1/1 Add basic test-script for git-submodule

This test tries to verify basic sanity of git-submodule, i.e. that it is able to clone and update a submodule repository, that its status output is sane, and that it barfs when the submodule path is occupied during init.

---

Quoted 6 lines · Junio C Hamano

Review

Quoted 3 lines · Lars Hjemli
(quoted diff) +3 -0
+test_expect_success 'the --cached sha1 should be rev1' '
+     git-submodule --cached status | grep "^\+$rev1"
+'

What's the purpose of that solitary backslash?

That's just a sign of me being sloppy, expecting the behaviour of 'grep -E' without bothering to study the man-page. Sorry.

 t/t7400-submodule-basic.sh |  143 ++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 143 insertions(+), 0 deletions(-)
 create mode 100755 t/t7400-submodule-basic.sh
t/t7400-submodule-basic.sh new file +143 -0
new file mode 100755
index 0000000..6274729
@@ -0,0 +1,143 @@
+#!/bin/sh
+#
+# Copyright (c) 2007 Lars Hjemli
+#
+
+test_description='Basic porcelain support for submodules
+
+This test tries to verify basic sanity of the init, update and status
+subcommands of git-submodule.
+'
+
+. ./test-lib.sh
+
+#
+# Test setup:
+#  -create a repository in directory lib
+#  -add a couple of files
+#  -add directory lib to 'superproject', this creates a DIRLINK entry
+#  -add a couple of regular files to enable testing of submodule filtering
+#  -mv lib subrepo
+#  -add an entry to .gitmodules for path 'lib'
+#
+test_expect_success 'Prepare submodule testing' '
+	mkdir lib &&
+	cd lib &&
+	git-init &&
+	echo a >a &&
+	git-add a &&
+	git-commit -m "submodule commit 1" &&
+	git-tag -a -m "rev-1" rev-1 &&
+	rev1=$(git-rev-parse HEAD) &&
+	if test -z "$rev1"
+	then
+		echo "[OOPS] submodule git-rev-parse returned nothing"
+		false
+	fi &&
+	cd .. &&
+	echo a >a &&
+	echo z >z &&
+	git-add a lib z &&
+	git-commit -m "super commit 1" &&
+	mv lib .subrepo &&
+	GIT_CONFIG=.gitmodules git-config module.lib.url ./.subrepo
+'
+
+test_expect_success 'status should only print one line' '
+	lines=$(git-submodule status | wc -l) &&
+	test $lines = 1
+'
+
+test_expect_success 'status should initially be "missing"' '
+	git-submodule status | grep "^-$rev1"
+'
+
+test_expect_success 'init should fail when path is used by a file' '
+	echo "hello" >lib &&
+	if git-submodule init
+	then
+		echo "[OOPS] init should have failed"
+		false
+	elif test -f lib && test "$(cat lib)" != "hello"
+	then
+		echo "[OOPS] init failed but lib file was molested"
+		false
+	else
+		rm lib
+	fi
+'
+
+test_expect_success 'init should fail when path is used by a nonempty directory' '
+	mkdir lib &&
+	echo "hello" >lib/a &&
+	if git-submodule init
+	then
+		echo "[OOPS] init should have failed"
+		false
+	elif test "$(cat lib/a)" != "hello"
+	then
+		echo "[OOPS] init failed but lib/a was molested"
+		false
+	else
+		rm lib/a
+	fi
+'
+
+test_expect_success 'init should work when path is an empty dir' '
+	rm -rf lib &&
+	mkdir lib &&
+	git-submodule init &&
+	head=$(cd lib && git-rev-parse HEAD) &&
+	if test -z "$head"
+	then
+		echo "[OOPS] Failed to obtain submodule head"
+		false
+	elif test "$head" != "$rev1"
+	then
+		echo "[OOPS] Submodule head is $head but should have been $rev1"
+		false
+	fi
+'
+
+test_expect_success 'status should be "up-to-date" after init' '
+	git-submodule status | grep "^ $rev1"
+'
+
+test_expect_success 'status should be "modified" after submodule commit' '
+	cd lib &&
+	echo b >b &&
+	git-add b &&
+	git-commit -m "submodule commit 2" &&
+	rev2=$(git-rev-parse HEAD) &&
+	cd .. &&
+	if test -z "$rev2"
+	then
+		echo "[OOPS] submodule git-rev-parse returned nothing"
+		false
+	fi &&
+	git-submodule status | grep "^+$rev2"
+'
+
+test_expect_success 'the --cached sha1 should be rev1' '
+	git-submodule --cached status | grep "^+$rev1"
+'
+
+test_expect_success 'update should checkout rev1' '
+	git-submodule update &&
+	head=$(cd lib && git-rev-parse HEAD) &&
+	if test -z "$head"
+	then
+		echo "[OOPS] submodule git-rev-parse returned nothing"
+		false
+	elif test "$head" != "$rev1"
+	then
+		echo "[OOPS] init did not checkout correct head"
+		false
+	fi
+'
+
+test_expect_success 'status should be "up-to-date" after update' '
+	git-submodule status | grep "^ $rev1"
+'
+
+test_done
Signature · 1 line
1.5.2.839.ga3b1-dirty
#1 lore raw
In reply to Lars Hjemli
Quoted 5 lines · Lars Hjemli
t/t7400-submodule-basic.sh new file +5 -0
+# create a submodule repository
+mkdir lib && cd lib
+git-init >/dev/null
+echo a >a && git-add a && git-commit -q -m "submodule commit 1"
+git-tag -a -m "rev-1" rev-1

t/t7400-submodule-basic.sh line 29

Quoted 10 lines
t/t7400-submodule-basic.sh new file +10 -0
+rev1=$(git-rev-parse HEAD)
+cd ..
+
+# add submodule and other files to super repo
+echo a >a && echo z >z
+git-add a lib z && git-commit -q -m "super commit 1"
+
+# move submodule to another location, register repo url in .gitmodules
+mv lib .subrepo
+GIT_CONFIG=.gitmodules git-config module.lib.url ./.subrepo

We typically try to catch malfunction even while setting up the test case.

t/t7400-submodule-basic.sh line 37

Quoted 7 lines
t/t7400-submodule-basic.sh new file +7 -0
+test_expect_success 'status is "missing"' \
+	'git-submodule status | grep "^-$rev1"'
+
+# make sure 'init' will not overwrite a regular file
+touch lib
+test_expect_failure 'init fails when path is used by a file' \
+	'git-submodule init'

I am guilty for introducing "expect-failure", but it is usually a mistake to use it unless you are testing something very trivial.

For example, for this case, you would want to make sure the command "git-submodule init" exits with non-zero status, but also you would want to make sure that it does not disturb the existing file "lib". You might also want to see if the command gives the right error message (or status code) as well, although typically the wording of error message is subject to change, so we tend to try not to be too strict about it.

In any case, I would probably write this part of the code like this:

test_expect_success 'init fails when path is used by a file' '

echo hello >lib &&
if git-submodule init
then
echo "Oops, should have failed" false
elif test -f lib && test "$(cat lib)" = hello then
: happy
else
echo "Oops, failed but lib file was molested" false
fi
'

Later when somebody tries to improve git-submodule and botches, he can run the test with "-i -v" and observe which "Oops" comes out to see why the test failed -- it would give him a clue on how he broke it.