Add test for using Git at root of file system
This kind of test requires a throw-away root filesystem so that it can play on. If you have such a system, go ahead, "chmod 777 /" and run this test manually. Because this is a dangerous test, you are required to set an env variable, and not to use root to run it. Script prepare-root.sh may help you set up a chroot environment with Git test suite inside. You will need Linux, static linked busybox, rsync and root permission to use it. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Nguyễn Thái Ngọc Duy committed
Feb 14, 2010 at 22:44 UTC
3719b2fe554adc2f7a34a16b90f6894f299aab3c
3 files changed
+301
t/t1509-root-worktree.sh
new
+249
@@ -0,0 +1,249 @@
1
+#!/bin/sh
2
+
3
+test_description='Test Git when git repository is located at root
4
+
5
+This test requires write access in root. Do not bother if you do not
6
+have a throwaway chroot or VM.
7
+
8
+Script t1509/prepare-chroot.sh may help you setup chroot, then you
9
+can chroot in and execute this test from there.
10
+'
11
+
12
+. ./test-lib.sh
13
+
14
+test_cmp_val() {
15
+ echo "$1" > expected
16
+ echo "$2" > result
17
+ test_cmp expected result
18
+}
19
+
20
+test_vars() {
21
+ test_expect_success "$1: gitdir" '
22
+ test_cmp_val "'"$2"'" "$(git rev-parse --git-dir)"
23
+ '
24
+
25
+ test_expect_success "$1: worktree" '
26
+ test_cmp_val "'"$3"'" "$(git rev-parse --show-toplevel)"
27
+ '
28
+
29
+ test_expect_success "$1: prefix" '
30
+ test_cmp_val "'"$4"'" "$(git rev-parse --show-prefix)"
31
+ '
32
+}
33
+
34
+test_foobar_root() {
35
+ test_expect_success 'add relative' '
36
+ test -z "$(cd / && git ls-files)" &&
37
+ git add foo/foome &&
38
+ git add foo/bar/barme &&
39
+ git add me &&
40
+ ( cd / && git ls-files --stage ) > result &&
41
+ test_cmp /ls.expected result &&
42
+ rm "$(git rev-parse --git-dir)/index"
43
+ '
44
+
45
+ test_expect_success 'add absolute' '
46
+ test -z "$(cd / && git ls-files)" &&
47
+ git add /foo/foome &&
48
+ git add /foo/bar/barme &&
49
+ git add /me &&
50
+ ( cd / && git ls-files --stage ) > result &&
51
+ test_cmp /ls.expected result &&
52
+ rm "$(git rev-parse --git-dir)/index"
53
+ '
54
+
55
+}
56
+
57
+test_foobar_foo() {
58
+ test_expect_success 'add relative' '
59
+ test -z "$(cd / && git ls-files)" &&
60
+ git add foome &&
61
+ git add bar/barme &&
62
+ git add ../me &&
63
+ ( cd / && git ls-files --stage ) > result &&
64
+ test_cmp /ls.expected result &&
65
+ rm "$(git rev-parse --git-dir)/index"
66
+ '
67
+
68
+ test_expect_success 'add absolute' '
69
+ test -z "$(cd / && git ls-files)" &&
70
+ git add /foo/foome &&
71
+ git add /foo/bar/barme &&
72
+ git add /me &&
73
+ ( cd / && git ls-files --stage ) > result &&
74
+ test_cmp /ls.expected result &&
75
+ rm "$(git rev-parse --git-dir)/index"
76
+ '
77
+}
78
+
79
+test_foobar_foobar() {
80
+ test_expect_success 'add relative' '
81
+ test -z "$(cd / && git ls-files)" &&
82
+ git add ../foome &&
83
+ git add barme &&
84
+ git add ../../me &&
85
+ ( cd / && git ls-files --stage ) > result &&
86
+ test_cmp /ls.expected result &&
87
+ rm "$(git rev-parse --git-dir)/index"
88
+ '
89
+
90
+ test_expect_success 'add absolute' '
91
+ test -z "$(cd / && git ls-files)" &&
92
+ git add /foo/foome &&
93
+ git add /foo/bar/barme &&
94
+ git add /me &&
95
+ ( cd / && git ls-files --stage ) > result &&
96
+ test_cmp /ls.expected result &&
97
+ rm "$(git rev-parse --git-dir)/index"
98
+ '
99
+}
100
+
101
+if ! test_have_prereq POSIXPERM || ! [ -w / ]; then
102
+ say "Dangerous test skipped. Read this test if you want to execute it"
103
+ test_done
104
+fi
105
+
106
+if [ "$IKNOWWHATIAMDOING" != "YES" ]; then
107
+ say "You must set env var IKNOWWHATIAMDOING=YES in order to run this test"
108
+ test_done
109
+fi
110
+
111
+if [ "$UID" = 0 ]; then
112
+ say "No you can't run this with root"
113
+ test_done
114
+fi
115
+
116
+ONE_SHA1=d00491fd7e5bb6fa28c517a0bb32b8b506539d4d
117
+
118
+test_expect_success 'setup' '
119
+ rm -rf /foo
120
+ mkdir /foo &&
121
+ mkdir /foo/bar &&
122
+ echo 1 > /foo/foome &&
123
+ echo 1 > /foo/bar/barme &&
124
+ echo 1 > /me
125
+'
126
+
127
+say "GIT_DIR absolute, GIT_WORK_TREE set"
128
+
129
+test_expect_success 'go to /' 'cd /'
130
+
131
+cat >ls.expected <<EOF
132
+100644 $ONE_SHA1 0 foo/bar/barme
133
+100644 $ONE_SHA1 0 foo/foome
134
+100644 $ONE_SHA1 0 me
135
+EOF
136
+
137
+export GIT_DIR="$TRASH_DIRECTORY/.git"
138
+export GIT_WORK_TREE=/
139
+
140
+test_vars 'abs gitdir, root' "$GIT_DIR" "/" ""
141
+test_foobar_root
142
+
143
+test_expect_success 'go to /foo' 'cd /foo'
144
+
145
+test_vars 'abs gitdir, foo' "$GIT_DIR" "/" "foo/"
146
+test_foobar_foo
147
+
148
+test_expect_success 'go to /foo/bar' 'cd /foo/bar'
149
+
150
+test_vars 'abs gitdir, foo/bar' "$GIT_DIR" "/" "foo/bar/"
151
+test_foobar_foobar
152
+
153
+say "GIT_DIR relative, GIT_WORK_TREE set"
154
+
155
+test_expect_success 'go to /' 'cd /'
156
+
157
+export GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git"
158
+export GIT_WORK_TREE=/
159
+
160
+test_vars 'rel gitdir, root' "$GIT_DIR" "/" ""
161
+test_foobar_root
162
+
163
+test_expect_success 'go to /foo' 'cd /foo'
164
+
165
+export GIT_DIR="../$TRASH_DIRECTORY/.git"
166
+export GIT_WORK_TREE=/
167
+
168
+test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/"
169
+test_foobar_foo
170
+
171
+test_expect_success 'go to /foo/bar' 'cd /foo/bar'
172
+
173
+export GIT_DIR="../../$TRASH_DIRECTORY/.git"
174
+export GIT_WORK_TREE=/
175
+
176
+test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/"
177
+test_foobar_foobar
178
+
179
+say "GIT_DIR relative, GIT_WORK_TREE relative"
180
+
181
+test_expect_success 'go to /' 'cd /'
182
+
183
+export GIT_DIR="$(echo $TRASH_DIRECTORY|sed 's,^/,,')/.git"
184
+export GIT_WORK_TREE=.
185
+
186
+test_vars 'rel gitdir, root' "$GIT_DIR" "/" ""
187
+test_foobar_root
188
+
189
+test_expect_success 'go to /' 'cd /foo'
190
+
191
+export GIT_DIR="../$TRASH_DIRECTORY/.git"
192
+export GIT_WORK_TREE=..
193
+
194
+test_vars 'rel gitdir, foo' "$TRASH_DIRECTORY/.git" "/" "foo/"
195
+test_foobar_foo
196
+
197
+test_expect_success 'go to /foo/bar' 'cd /foo/bar'
198
+
199
+export GIT_DIR="../../$TRASH_DIRECTORY/.git"
200
+export GIT_WORK_TREE=../..
201
+
202
+test_vars 'rel gitdir, foo/bar' "$TRASH_DIRECTORY/.git" "/" "foo/bar/"
203
+test_foobar_foobar
204
+
205
+say ".git at root"
206
+
207
+unset GIT_DIR
208
+unset GIT_WORK_TREE
209
+
210
+test_expect_success 'go to /' 'cd /'
211
+test_expect_success 'setup' '
212
+ rm -rf /.git
213
+ echo "Initialized empty Git repository in /.git/" > expected &&
214
+ git init > result &&
215
+ test_cmp expected result
216
+'
217
+
218
+test_vars 'auto gitdir, root' ".git" "/" ""
219
+test_foobar_root
220
+
221
+test_expect_success 'go to /foo' 'cd /foo'
222
+test_vars 'auto gitdir, foo' "/.git" "/" "foo/"
223
+test_foobar_foo
224
+
225
+test_expect_success 'go to /foo/bar' 'cd /foo/bar'
226
+test_vars 'auto gitdir, foo/bar' "/.git" "/" "foo/bar/"
227
+test_foobar_foobar
228
+
229
+test_expect_success 'cleanup' 'rm -rf /.git'
230
+
231
+say "auto bare gitdir"
232
+
233
+# DESTROYYYYY!!!!!
234
+test_expect_success 'setup' '
235
+ rm -rf /refs /objects /info /hooks
236
+ rm /*
237
+ cd / &&
238
+ echo "Initialized empty Git repository in /" > expected &&
239
+ git init --bare > result &&
240
+ test_cmp expected result
241
+'
242
+
243
+test_vars 'auto gitdir, root' "." "" ""
244
+
245
+test_expect_success 'go to /foo' 'cd /foo'
246
+
247
+test_vars 'auto gitdir, root' "/" "" ""
248
+
249
+test_done
t/t1509/excludes
new
+14
@@ -0,0 +1,14 @@
1
+*.o
2
+*~
3
+*.bak
4
+*.c
5
+*.h
6
+.git
7
+contrib
8
+Documentation
9
+git-gui
10
+gitk-git
11
+gitweb
12
+t/t4013
13
+t/t5100
14
+t/t5515
t/t1509/prepare-chroot.sh
new
+38
@@ -0,0 +1,38 @@
1
+#!/bin/sh
2
+
3
+die() {
4
+ echo >&2 "$@"
5
+ exit 1
6
+}
7
+
8
+xmkdir() {
9
+ while [ -n "$1" ]; do
10
+ [ -d "$1" ] || mkdir "$1" || die "Unable to mkdir $1"
11
+ shift
12
+ done
13
+}
14
+
15
+R="$1"
16
+
17
+[ -n "$R" ] || die "Usage: prepare-chroot.sh <root>"
18
+[ -x git ] || die "This script needs to be executed at git source code's top directory"
19
+[ -x /bin/busybox ] || die "You need busybox"
20
+
21
+xmkdir "$R" "$R/bin" "$R/etc" "$R/lib" "$R/dev"
22
+[ -c "$R/dev/null" ] || die "/dev/null is missing. Do mknod $R/dev/null c 1 3 && chmod 666 $R/dev/null"
23
+echo "root:x:0:0:root:/:/bin/sh" > "$R/etc/passwd"
24
+echo "$(id -nu):x:$(id -u):$(id -g)::$(pwd)/t:/bin/sh" >> "$R/etc/passwd"
25
+echo "root::0:root" > "$R/etc/group"
26
+echo "$(id -ng)::$(id -g):$(id -nu)" >> "$R/etc/group"
27
+
28
+[ -x "$R/bin/busybox" ] || cp /bin/busybox "$R/bin/busybox"
29
+[ -x "$R/bin/sh" ] || ln -s /bin/busybox "$R/bin/sh"
30
+[ -x "$R/bin/su" ] || ln -s /bin/busybox "$R/bin/su"
31
+
32
+mkdir -p "$R$(pwd)"
33
+rsync --exclude-from t/t1509/excludes -Ha . "$R$(pwd)"
34
+ldd git | grep '/' | sed 's,.*\s\(/[^ ]*\).*,\1,' | while read i; do
35
+ mkdir -p "$R$(dirname $i)"
36
+ cp "$i" "$R/$i"
37
+done
38
+echo "Execute this in root: 'chroot $R /bin/su - $(id -nu)'"