From b335d3f121536733fff3b71d4270c74a9d6e9e91 Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Wed, 23 Apr 2008 15:17:43 -0400 Subject: Add tests for git cat-file Signed-off-by: Adam Roben Signed-off-by: Junio C Hamano --- t/t1006-cat-file.sh | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 121 insertions(+) create mode 100755 t/t1006-cat-file.sh (limited to 't') diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh new file mode 100755 index 0000000000..0f7cc0371d --- /dev/null +++ b/t/t1006-cat-file.sh @@ -0,0 +1,121 @@ +#!/bin/sh + +test_description='git cat-file' + +. ./test-lib.sh + +echo_without_newline () { + printf '%s' "$*" +} + +strlen () { + echo_without_newline "$1" | wc -c | sed -e 's/^ *//' +} + +maybe_remove_timestamp () { + if test -z "$2"; then + echo_without_newline "$1" + else + echo_without_newline "$(printf '%s\n' "$1" | sed -e 's/ [0-9][0-9]* [-+][0-9][0-9][0-9][0-9]$//')" + fi +} + +run_tests () { + type=$1 + sha1=$2 + size=$3 + content=$4 + pretty_content=$5 + no_ts=$6 + + test_expect_success "$type exists" ' + git cat-file -e $sha1 + ' + + test_expect_success "Type of $type is correct" ' + test $type = "$(git cat-file -t $sha1)" + ' + + test_expect_success "Size of $type is correct" ' + test $size = "$(git cat-file -s $sha1)" + ' + + test -z "$content" || + test_expect_success "Content of $type is correct" ' + expect="$(maybe_remove_timestamp "$content" $no_ts)" + actual="$(maybe_remove_timestamp "$(git cat-file $type $sha1)" $no_ts)" + + if test "z$expect" = "z$actual" + then + : happy + else + echo "Oops: expected $expect" + echo "but got $actual" + false + fi + ' + + test_expect_success "Pretty content of $type is correct" ' + expect="$(maybe_remove_timestamp "$pretty_content" $no_ts)" + actual="$(maybe_remove_timestamp "$(git cat-file -p $sha1)" $no_ts)" + if test "z$expect" = "z$actual" + then + : happy + else + echo "Oops: expected $expect" + echo "but got $actual" + false + fi + ' +} + +hello_content="Hello World" +hello_size=$(strlen "$hello_content") +hello_sha1=$(echo_without_newline "$hello_content" | git hash-object --stdin) + +test_expect_success "setup" ' + echo_without_newline "$hello_content" > hello && + git update-index --add hello +' + +run_tests 'blob' $hello_sha1 $hello_size "$hello_content" "$hello_content" + +tree_sha1=$(git write-tree) +tree_size=33 +tree_pretty_content="100644 blob $hello_sha1 hello" + +run_tests 'tree' $tree_sha1 $tree_size "" "$tree_pretty_content" + +commit_message="Intial commit" +commit_sha1=$(echo_without_newline "$commit_message" | git commit-tree $tree_sha1) +commit_size=176 +commit_content="tree $tree_sha1 +author $GIT_AUTHOR_NAME <$GIT_AUTHOR_EMAIL> 0000000000 +0000 +committer $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL> 0000000000 +0000 + +$commit_message" + +run_tests 'commit' $commit_sha1 $commit_size "$commit_content" "$commit_content" 1 + +tag_header_without_timestamp="object $hello_sha1 +type blob +tag hellotag +tagger $GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>" +tag_description="This is a tag" +tag_content="$tag_header_without_timestamp 0000000000 +0000 + +$tag_description" +tag_pretty_content="$tag_header_without_timestamp Thu Jan 1 00:00:00 1970 +0000 + +$tag_description" + +tag_sha1=$(echo_without_newline "$tag_content" | git mktag) +tag_size=$(strlen "$tag_content") + +run_tests 'tag' $tag_sha1 $tag_size "$tag_content" "$tag_pretty_content" 1 + +test_expect_success \ + "Reach a blob from a tag pointing to it" \ + "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\"" + +test_done -- cgit v1.2.3 From 05d5667fec9650b049f47edd8cca23a43b135365 Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Wed, 23 Apr 2008 15:17:46 -0400 Subject: git-cat-file: Add --batch-check option This new option allows multiple objects to be specified on stdin. For each object specified, a line of the following form is printed: SP SP LF If the object does not exist in the repository, a line of the following form is printed: SP missing LF Signed-off-by: Adam Roben Signed-off-by: Junio C Hamano --- t/t1006-cat-file.sh | 65 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) (limited to 't') diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index 0f7cc0371d..decba02740 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -67,6 +67,19 @@ run_tests () { false fi ' + + test_expect_success "--batch-check output of $type is correct" ' + expect="$sha1 $type $size" + actual="$(echo_without_newline $sha1 | git cat-file --batch-check)" + if test "z$expect" = "z$actual" + then + : happy + else + echo "Oops: expected $expect" + echo "but got $actual" + false + fi + ' } hello_content="Hello World" @@ -118,4 +131,56 @@ test_expect_success \ "Reach a blob from a tag pointing to it" \ "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\"" +for opt in t s e p +do + test_expect_success "Passing -$opt with --batch-check fails" ' + test_must_fail git cat-file --batch-check -$opt $hello_sha1 + ' + + test_expect_success "Passing --batch-check with -$opt fails" ' + test_must_fail git cat-file -$opt --batch-check $hello_sha1 + ' +done + +test_expect_success "Passing with --batch-check fails" ' + test_must_fail git cat-file --batch-check blob $hello_sha1 +' + +test_expect_success "Passing --batch-check with fails" ' + test_must_fail git cat-file blob --batch-check $hello_sha1 +' + +test_expect_success "Passing sha1 with --batch-check fails" ' + test_must_fail git cat-file --batch-check $hello_sha1 +' + +test_expect_success "--batch-check for a non-existent object" ' + test "deadbeef missing" = \ + "$(echo_without_newline deadbeef | git cat-file --batch-check)" +' + +test_expect_success "--batch-check for an emtpy line" ' + test " missing" = "$(echo | git cat-file --batch-check)" +' + +batch_check_input="$hello_sha1 +$tree_sha1 +$commit_sha1 +$tag_sha1 +deadbeef + +" + +batch_check_output="$hello_sha1 blob $hello_size +$tree_sha1 tree $tree_size +$commit_sha1 commit $commit_size +$tag_sha1 tag $tag_size +deadbeef missing + missing" + +test_expect_success "--batch-check with multiple sha1s gives correct format" ' + test "$batch_check_output" = \ + "$(echo_without_newline "$batch_check_input" | git cat-file --batch-check)" +' + test_done -- cgit v1.2.3 From a8128ed62858063e29edc066b14b8b0fa6257cc2 Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Wed, 23 Apr 2008 15:17:47 -0400 Subject: git-cat-file: Add --batch option --batch is similar to --batch-check, except that the contents of each object is also printed. The output's form is: SP SP LF LF Signed-off-by: Adam Roben Signed-off-by: Junio C Hamano --- t/t1006-cat-file.sh | 74 +++++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 57 insertions(+), 17 deletions(-) (limited to 't') diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index decba02740..d5696765b1 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -28,6 +28,9 @@ run_tests () { pretty_content=$5 no_ts=$6 + batch_output="$sha1 $type $size +$content" + test_expect_success "$type exists" ' git cat-file -e $sha1 ' @@ -68,6 +71,20 @@ run_tests () { fi ' + test -z "$content" || + test_expect_success "--batch output of $type is correct" ' + expect="$(maybe_remove_timestamp "$batch_output" $no_ts)" + actual="$(maybe_remove_timestamp "$(echo $sha1 | git cat-file --batch)" no_ts)" + if test "z$expect" = "z$actual" + then + : happy + else + echo "Oops: expected $expect" + echo "but got $actual" + false + fi + ' + test_expect_success "--batch-check output of $type is correct" ' expect="$sha1 $type $size" actual="$(echo_without_newline $sha1 | git cat-file --batch-check)" @@ -131,28 +148,31 @@ test_expect_success \ "Reach a blob from a tag pointing to it" \ "test '$hello_content' = \"\$(git cat-file blob $tag_sha1)\"" -for opt in t s e p +for batch in batch batch-check do - test_expect_success "Passing -$opt with --batch-check fails" ' - test_must_fail git cat-file --batch-check -$opt $hello_sha1 + for opt in t s e p + do + test_expect_success "Passing -$opt with --$batch fails" ' + test_must_fail git cat-file --$batch -$opt $hello_sha1 + ' + + test_expect_success "Passing --$batch with -$opt fails" ' + test_must_fail git cat-file -$opt --$batch $hello_sha1 + ' + done + + test_expect_success "Passing with --$batch fails" ' + test_must_fail git cat-file --$batch blob $hello_sha1 ' - test_expect_success "Passing --batch-check with -$opt fails" ' - test_must_fail git cat-file -$opt --batch-check $hello_sha1 + test_expect_success "Passing --$batch with fails" ' + test_must_fail git cat-file blob --$batch $hello_sha1 ' -done - -test_expect_success "Passing with --batch-check fails" ' - test_must_fail git cat-file --batch-check blob $hello_sha1 -' - -test_expect_success "Passing --batch-check with fails" ' - test_must_fail git cat-file blob --batch-check $hello_sha1 -' -test_expect_success "Passing sha1 with --batch-check fails" ' - test_must_fail git cat-file --batch-check $hello_sha1 -' + test_expect_success "Passing sha1 with --$batch fails" ' + test_must_fail git cat-file --$batch $hello_sha1 + ' +done test_expect_success "--batch-check for a non-existent object" ' test "deadbeef missing" = \ @@ -163,6 +183,26 @@ test_expect_success "--batch-check for an emtpy line" ' test " missing" = "$(echo | git cat-file --batch-check)" ' +batch_input="$hello_sha1 +$commit_sha1 +$tag_sha1 +deadbeef + +" + +batch_output="$hello_sha1 blob $hello_size +$hello_content +$commit_sha1 commit $commit_size +$commit_content +$tag_sha1 tag $tag_size +$tag_content +deadbeef missing + missing" + +test_expect_success \ + "--batch with multiple sha1s gives correct format" \ + "test \"\$(maybe_remove_timestamp \"$batch_output\" 1)\" = \"\$(maybe_remove_timestamp \"\$(echo_without_newline \"$batch_input\" | git cat-file --batch)\" 1)\"" + batch_check_input="$hello_sha1 $tree_sha1 $commit_sha1 -- cgit v1.2.3 From 97e435adad0e0bf30e588b2ad7f206a20c734c06 Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Fri, 23 May 2008 16:19:36 +0200 Subject: Move git-hash-object tests from t5303 to t1007 This is a more appropriate location according to t/README. Signed-off-by: Adam Roben Signed-off-by: Junio C Hamano --- t/t1007-hash-object.sh | 35 +++++++++++++++++++++++++++++++++++ t/t5303-hash-object.sh | 35 ----------------------------------- 2 files changed, 35 insertions(+), 35 deletions(-) create mode 100755 t/t1007-hash-object.sh delete mode 100755 t/t5303-hash-object.sh (limited to 't') diff --git a/t/t1007-hash-object.sh b/t/t1007-hash-object.sh new file mode 100755 index 0000000000..543c0784bd --- /dev/null +++ b/t/t1007-hash-object.sh @@ -0,0 +1,35 @@ +#!/bin/sh + +test_description=git-hash-object + +. ./test-lib.sh + +test_expect_success \ + 'git hash-object -w --stdin saves the object' \ + 'obname=$(echo foo | git hash-object -w --stdin) && + obpath=$(echo $obname | sed -e "s/\(..\)/\1\//") && + test -r .git/objects/"$obpath" && + rm -f .git/objects/"$obpath"' + +test_expect_success \ + 'git hash-object --stdin -w saves the object' \ + 'obname=$(echo foo | git hash-object --stdin -w) && + obpath=$(echo $obname | sed -e "s/\(..\)/\1\//") && + test -r .git/objects/"$obpath" && + rm -f .git/objects/"$obpath"' + +test_expect_success \ + 'git hash-object --stdin file1 file1 && + obname0=$(echo bar | git hash-object --stdin) && + obname1=$(git hash-object file1) && + obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) && + obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) && + test "$obname0" = "$obname0new" && + test "$obname1" = "$obname1new"' + +test_expect_success \ + 'git hash-object refuses multiple --stdin arguments' \ + '! git hash-object --stdin --stdin < file1' + +test_done diff --git a/t/t5303-hash-object.sh b/t/t5303-hash-object.sh deleted file mode 100755 index 543c0784bd..0000000000 --- a/t/t5303-hash-object.sh +++ /dev/null @@ -1,35 +0,0 @@ -#!/bin/sh - -test_description=git-hash-object - -. ./test-lib.sh - -test_expect_success \ - 'git hash-object -w --stdin saves the object' \ - 'obname=$(echo foo | git hash-object -w --stdin) && - obpath=$(echo $obname | sed -e "s/\(..\)/\1\//") && - test -r .git/objects/"$obpath" && - rm -f .git/objects/"$obpath"' - -test_expect_success \ - 'git hash-object --stdin -w saves the object' \ - 'obname=$(echo foo | git hash-object --stdin -w) && - obpath=$(echo $obname | sed -e "s/\(..\)/\1\//") && - test -r .git/objects/"$obpath" && - rm -f .git/objects/"$obpath"' - -test_expect_success \ - 'git hash-object --stdin file1 file1 && - obname0=$(echo bar | git hash-object --stdin) && - obname1=$(git hash-object file1) && - obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) && - obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) && - test "$obname0" = "$obname0new" && - test "$obname1" = "$obname1new"' - -test_expect_success \ - 'git hash-object refuses multiple --stdin arguments' \ - '! git hash-object --stdin --stdin < file1' - -test_done -- cgit v1.2.3 From 3ea5a1b33d99b6c4f5e745c0dd017307c58cff31 Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Fri, 23 May 2008 16:19:37 +0200 Subject: Add more tests for git hash-object Signed-off-by: Adam Roben Signed-off-by: Junio C Hamano --- t/t1007-hash-object.sh | 120 ++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 93 insertions(+), 27 deletions(-) (limited to 't') diff --git a/t/t1007-hash-object.sh b/t/t1007-hash-object.sh index 543c0784bd..2019ea7891 100755 --- a/t/t1007-hash-object.sh +++ b/t/t1007-hash-object.sh @@ -4,32 +4,98 @@ test_description=git-hash-object . ./test-lib.sh -test_expect_success \ - 'git hash-object -w --stdin saves the object' \ - 'obname=$(echo foo | git hash-object -w --stdin) && - obpath=$(echo $obname | sed -e "s/\(..\)/\1\//") && - test -r .git/objects/"$obpath" && - rm -f .git/objects/"$obpath"' - -test_expect_success \ - 'git hash-object --stdin -w saves the object' \ - 'obname=$(echo foo | git hash-object --stdin -w) && - obpath=$(echo $obname | sed -e "s/\(..\)/\1\//") && - test -r .git/objects/"$obpath" && - rm -f .git/objects/"$obpath"' - -test_expect_success \ - 'git hash-object --stdin file1 file1 && - obname0=$(echo bar | git hash-object --stdin) && - obname1=$(git hash-object file1) && - obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) && - obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) && - test "$obname0" = "$obname0new" && - test "$obname1" = "$obname1new"' - -test_expect_success \ - 'git hash-object refuses multiple --stdin arguments' \ - '! git hash-object --stdin --stdin < file1' +echo_without_newline() { + printf '%s' "$*" +} + +test_blob_does_not_exist() { + test_expect_success 'blob does not exist in database' " + test_must_fail git cat-file blob $1 + " +} + +test_blob_exists() { + test_expect_success 'blob exists in database' " + git cat-file blob $1 + " +} + +hello_content="Hello World" +hello_sha1=5e1c309dae7f45e0f39b1bf3ac3cd9db12e7d689 + +example_content="This is an example" +example_sha1=ddd3f836d3e3fbb7ae289aa9ae83536f76956399 + +setup_repo() { + echo_without_newline "$hello_content" > hello + echo_without_newline "$example_content" > example +} + +test_repo=test +push_repo() { + test_create_repo $test_repo + cd $test_repo + + setup_repo +} + +pop_repo() { + cd .. + rm -rf $test_repo +} + +setup_repo + +# Argument checking + +test_expect_success "multiple '--stdin's are rejected" ' + test_must_fail git hash-object --stdin --stdin < example +' + +# Behavior + +push_repo + +test_expect_success 'hash a file' ' + test $hello_sha1 = $(git hash-object hello) +' + +test_blob_does_not_exist $hello_sha1 + +test_expect_success 'hash from stdin' ' + test $example_sha1 = $(git hash-object --stdin < example) +' + +test_blob_does_not_exist $example_sha1 + +test_expect_success 'hash a file and write to database' ' + test $hello_sha1 = $(git hash-object -w hello) +' + +test_blob_exists $hello_sha1 + +test_expect_success 'git hash-object --stdin file1 file1 && + obname0=$(echo bar | git hash-object --stdin) && + obname1=$(git hash-object file1) && + obname0new=$(echo bar | git hash-object --stdin file1 | sed -n -e 1p) && + obname1new=$(echo bar | git hash-object --stdin file1 | sed -n -e 2p) && + test "$obname0" = "$obname0new" && + test "$obname1" = "$obname1new" +' + +pop_repo + +for args in "-w --stdin" "--stdin -w"; do + push_repo + + test_expect_success "hash from stdin and write to database ($args)" ' + test $example_sha1 = $(git hash-object $args < example) + ' + + test_blob_exists $example_sha1 + + pop_repo +done test_done -- cgit v1.2.3 From d8ee4832509dd2d7448a49920f5cba2fc979283d Mon Sep 17 00:00:00 2001 From: Adam Roben Date: Fri, 23 May 2008 16:19:38 +0200 Subject: git-hash-object: Add --stdin-paths option This allows multiple paths to be specified on stdin. Signed-off-by: Adam Roben Signed-off-by: Junio C Hamano --- t/t1007-hash-object.sh | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 't') diff --git a/t/t1007-hash-object.sh b/t/t1007-hash-object.sh index 2019ea7891..05262954ab 100755 --- a/t/t1007-hash-object.sh +++ b/t/t1007-hash-object.sh @@ -52,6 +52,15 @@ test_expect_success "multiple '--stdin's are rejected" ' test_must_fail git hash-object --stdin --stdin < example ' +test_expect_success "Can't use --stdin and --stdin-paths together" ' + test_must_fail git hash-object --stdin --stdin-paths && + test_must_fail git hash-object --stdin-paths --stdin +' + +test_expect_success "Can't pass filenames as arguments with --stdin-paths" ' + test_must_fail git hash-object --stdin-paths hello < example +' + # Behavior push_repo @@ -98,4 +107,27 @@ for args in "-w --stdin" "--stdin -w"; do pop_repo done +filenames="hello +example" + +sha1s="$hello_sha1 +$example_sha1" + +test_expect_success "hash two files with names on stdin" ' + test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object --stdin-paths)" +' + +for args in "-w --stdin-paths" "--stdin-paths -w"; do + push_repo + + test_expect_success "hash two files with names on stdin and write to database ($args)" ' + test "$sha1s" = "$(echo_without_newline "$filenames" | git hash-object $args)" + ' + + test_blob_exists $hello_sha1 + test_blob_exists $example_sha1 + + pop_repo +done + test_done -- cgit v1.2.3 From 6c41e21d48c369f398ee2e24085e618b55ed916d Mon Sep 17 00:00:00 2001 From: Michele Ballabio Date: Fri, 23 May 2008 16:19:43 +0200 Subject: change quoting in test t1006-cat-file.sh Signed-off-by: Michele Ballabio Signed-off-by: Junio C Hamano --- t/t1006-cat-file.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 't') diff --git a/t/t1006-cat-file.sh b/t/t1006-cat-file.sh index d5696765b1..cb1fbe5820 100755 --- a/t/t1006-cat-file.sh +++ b/t/t1006-cat-file.sh @@ -199,9 +199,9 @@ $tag_content deadbeef missing missing" -test_expect_success \ - "--batch with multiple sha1s gives correct format" \ - "test \"\$(maybe_remove_timestamp \"$batch_output\" 1)\" = \"\$(maybe_remove_timestamp \"\$(echo_without_newline \"$batch_input\" | git cat-file --batch)\" 1)\"" +test_expect_success '--batch with multiple sha1s gives correct format' ' + test "$(maybe_remove_timestamp "$batch_output" 1)" = "$(maybe_remove_timestamp "$(echo_without_newline "$batch_input" | git cat-file --batch)" 1)" +' batch_check_input="$hello_sha1 $tree_sha1 -- cgit v1.2.3