1#!/bin/sh
2#
3# Copyright (c) 2007 Lars Hjemli
4#
56
test_description='Basic porcelain support for submodules
78
This test tries to verify basic sanity of the init, update and status
9subcommands of git-submodule.
10'
1112
. ./test-lib.sh
1314
#
15# Test setup:
16# -create a repository in directory lib
17# -add a couple of files
18# -add directory lib to 'superproject', this creates a DIRLINK entry
19# -add a couple of regular files to enable testing of submodule filtering
20# -mv lib subrepo
21# -add an entry to .gitmodules for path 'lib'
22#
23test_expect_success 'Prepare submodule testing' '
24mkdir lib &&
25cd lib &&
26git-init &&
27echo a >a &&
28git-add a &&
29git-commit -m "submodule commit 1" &&
30git-tag -a -m "rev-1" rev-1 &&
31rev1=$(git-rev-parse HEAD) &&
32if test -z "$rev1"
33then
34echo "[OOPS] submodule git-rev-parse returned nothing"
35false
36fi &&
37cd .. &&
38echo a >a &&
39echo z >z &&
40git-add a lib z &&
41git-commit -m "super commit 1" &&
42mv lib .subrepo &&
43GIT_CONFIG=.gitmodules git-config module.lib.url ./.subrepo
44'
4546
test_expect_success 'status should only print one line' '
47lines=$(git-submodule status | wc -l) &&
48test $lines = 1
49'
5051
test_expect_success 'status should initially be "missing"' '
52git-submodule status | grep "^-$rev1"
53'
5455
test_expect_success 'init should fail when path is used by a file' '
56echo "hello" >lib &&
57if git-submodule init
58then
59echo "[OOPS] init should have failed"
60false
61elif test -f lib && test "$(cat lib)" != "hello"
62then
63echo "[OOPS] init failed but lib file was molested"
64false
65else
66rm lib
67fi
68'
6970
test_expect_success 'init should fail when path is used by a nonempty directory' '
71mkdir lib &&
72echo "hello" >lib/a &&
73if git-submodule init
74then
75echo "[OOPS] init should have failed"
76false
77elif test "$(cat lib/a)" != "hello"
78then
79echo "[OOPS] init failed but lib/a was molested"
80false
81else
82rm lib/a
83fi
84'
8586
test_expect_success 'init should work when path is an empty dir' '
87rm -rf lib &&
88mkdir lib &&
89git-submodule init &&
90head=$(cd lib && git-rev-parse HEAD) &&
91if test -z "$head"
92then
93echo "[OOPS] Failed to obtain submodule head"
94false
95elif test "$head" != "$rev1"
96then
97echo "[OOPS] Submodule head is $head but should have been $rev1"
98false
99fi
100'
101102
test_expect_success 'status should be "up-to-date" after init' '
103git-submodule status | grep "^ $rev1"
104'
105106
test_expect_success 'status should be "modified" after submodule commit' '
107cd lib &&
108echo b >b &&
109git-add b &&
110git-commit -m "submodule commit 2" &&
111rev2=$(git-rev-parse HEAD) &&
112cd .. &&
113if test -z "$rev2"
114then
115echo "[OOPS] submodule git-rev-parse returned nothing"
116false
117fi &&
118git-submodule status | grep "^+$rev2"
119'
120121
test_expect_success 'the --cached sha1 should be rev1' '
122git-submodule --cached status | grep "^+$rev1"
123'
124125
test_expect_success 'update should checkout rev1' '
126git-submodule update &&
127head=$(cd lib && git-rev-parse HEAD) &&
128if test -z "$head"
129then
130echo "[OOPS] submodule git-rev-parse returned nothing"
131false
132elif test "$head" != "$rev1"
133then
134echo "[OOPS] init did not checkout correct head"
135false
136fi
137'
138139
test_expect_success 'status should be "up-to-date" after update' '
140git-submodule status | grep "^ $rev1"
141'
142143
test_done