@matheusbsbs / matheus-bagliano / commits / 3ea5a1b33d
Fork 0

Add more tests for git hash-object

Signed-off-by: Adam Roben <aroben@apple.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Adam Roben committed May 23, 2008 at 16:19 UTC 3ea5a1b33d99b6c4f5e745c0dd017307c58cff31
1 file changed +93 -27
t/t1007-hash-object.sh
+93 -27
@@ -4,32 +4,98 @@ test_description=git-hash-object
4
5 . ./test-lib.sh
6
7 -test_expect_success \
8 - 'git hash-object -w --stdin saves the object' \
9 - 'obname=$(echo foo | git hash-object -w --stdin) &&
10 - obpath=$(echo $obname | sed -e "s/\(..\)/\1\//") &&
11 - test -r .git/objects/"$obpath" &&
12 - rm -f .git/objects/"$obpath"'
13 -
14 -test_expect_success \
15 - 'git hash-object --stdin -w saves the object' \
16 - 'obname=$(echo foo | git hash-object --stdin -w) &&
17 - obpath=$(echo $obname | sed -e "s/\(..\)/\1\//") &&
18 - test -r .git/objects/"$obpath" &&
19 - rm -f .git/objects/"$obpath"'
20 -
21 -test_expect_success \
22 - 'git hash-object --stdin file1 <file0 first operates on file0, then file1' \
23 - 'echo foo > file1 &&
24 - obname0=$(echo bar | git hash-object --stdin) &&
25 - obname1=$(git hash-object file1) &&
26 - obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&
27 - obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&
28 - test "$obname0" = "$obname0new" &&
29 - test "$obname1" = "$obname1new"'
30 -
31 -test_expect_success \
32 - 'git hash-object refuses multiple --stdin arguments' \
33 - '! git hash-object --stdin --stdin < file1'
7 +echo_without_newline() {
8 + printf '%s' "$*"
9 +}
10 +
11 +test_blob_does_not_exist() {
12 + test_expect_success 'blob does not exist in database' "
13 + test_must_fail git cat-file blob $1
14 + "
15 +}
16 +
17 +test_blob_exists() {
18 + test_expect_success 'blob exists in database' "
19 + git cat-file blob $1
20 + "
21 +}
22 +
23 +hello_content="Hello World"
24 +hello_sha1=5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689
25 +
26 +example_content="This is an example"
27 +example_sha1=ddd3f836d3e3fbb7ae289aa9ae83536f76956399
28 +
29 +setup_repo() {
30 + echo_without_newline "$hello_content" > hello
31 + echo_without_newline "$example_content" > example
32 +}
33 +
34 +test_repo=test
35 +push_repo() {
36 + test_create_repo $test_repo
37 + cd $test_repo
38 +
39 + setup_repo
40 +}
41 +
42 +pop_repo() {
43 + cd ..
44 + rm -rf $test_repo
45 +}
46 +
47 +setup_repo
48 +
49 +# Argument checking
50 +
51 +test_expect_success "multiple '--stdin's are rejected" '
52 + test_must_fail git hash-object --stdin --stdin < example
53 +'
54 +
55 +# Behavior
56 +
57 +push_repo
58 +
59 +test_expect_success 'hash a file' '
60 + test $hello_sha1 = $(git hash-object hello)
61 +'
62 +
63 +test_blob_does_not_exist $hello_sha1
64 +
65 +test_expect_success 'hash from stdin' '
66 + test $example_sha1 = $(git hash-object --stdin < example)
67 +'
68 +
69 +test_blob_does_not_exist $example_sha1
70 +
71 +test_expect_success 'hash a file and write to database' '
72 + test $hello_sha1 = $(git hash-object -w hello)
73 +'
74 +
75 +test_blob_exists $hello_sha1
76 +
77 +test_expect_success 'git hash-object --stdin file1 <file0 first operates on file0, then file1' '
78 + echo foo > file1 &&
79 + obname0=$(echo bar | git hash-object --stdin) &&
80 + obname1=$(git hash-object file1) &&
81 + obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) &&
82 + obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) &&
83 + test "$obname0" = "$obname0new" &&
84 + test "$obname1" = "$obname1new"
85 +'
86 +
87 +pop_repo
88 +
89 +for args in "-w --stdin" "--stdin -w"; do
90 + push_repo
91 +
92 + test_expect_success "hash from stdin and write to database ($args)" '
93 + test $example_sha1 = $(git hash-object $args < example)
94 + '
95 +
96 + test_blob_exists $example_sha1
97 +
98 + pop_repo
99 +done
100
101 test_done