* [PATCH 1/2 v2] t/test-lib.sh: add test_external and test_external_without_stderr @ 2008-06-17 6:59 Lea Wiemann 2008-06-17 6:59 ` [PATCH 2/2 v4] Git.pm: add test suite Lea Wiemann 2008-06-18 20:24 ` [PATCH 1/2 v2] t/test-lib.sh: add test_external and test_external_without_stderr Olivier Marin 0 siblings, 2 replies; 13+ messages in thread From: Lea Wiemann @ 2008-06-17 6:59 UTC (permalink / raw) To: git; +Cc: Lea Wiemann This is for running external test scripts in other programming languages that provide continuous output about their tests. Using test_expect_success (like "test_expect_success 'description' 'perl test-script.pl'") doesn't suffice here because test_expect_success eats stdout in non-verbose mode, which is not fixable without major file descriptor trickery. Signed-off-by: Lea Wiemann <LeWiemann@gmail.com> --- Changed since v1 <http://article.gmane.org/gmane.comp.version-control.git/83415>: Fixed whitespace. Both functions have been tested with real perl tests and are working fine. These two patches apply on master. t/test-lib.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 58 insertions(+), 0 deletions(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index c861141..b46fe68 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -302,6 +302,64 @@ test_expect_code () { echo >&3 "" } +# test_external runs external test scripts that provide continuous +# test output about their progress, and succeeds/fails on +# zero/non-zero exit code. It outputs the test output on stdout even +# in non-verbose mode, and announces the external script with "* run +# <n>: ..." before running it. When providing relative paths, keep in +# mind that all scripts run in "trash directory". +# Usage: test_external description command arguments... +# Example: test_external 'Perl API' perl ../path/to/test.pl +test_external () { + test "$#" -eq 3 || + error >&5 "bug in the test script: not 3 parameters to test_external" + descr="$1" + shift + if ! test_skip "$descr" "$@" + then + # Announce the script to reduce confusion about the + # test output that follows. + say_color "" " run $(expr "$test_count" + 1): $descr ($*)" + # Run command; redirect its stderr to &4 as in + # test_run_, but keep its stdout on our stdout even in + # non-verbose mode. + "$@" 2>&4 + if [ "$?" = 0 ] + then + test_ok_ "$descr" + else + test_failure_ "$descr" "$@" + fi + fi +} + +# Like test_external, but in addition tests that the command generated +# no output on stderr. +test_external_without_stderr () { + # The temporary file has no (and must have no) security + # implications. + tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi + stderr="$tmp/git-external-stderr.$$.tmp" + test_external "$@" 4> "$stderr" + [ -f "$stderr" ] || eror "Internal error: $stderr disappeared." + descr="no stderr: $1" + shift + say >&3 "expecting no stderr from previous command" + if [ ! -s "$stderr" ]; then + rm "$stderr" + test_ok_ "$descr" + else + if [ "$verbose" = t ]; then + output=`echo; echo Stderr is:; cat "$stderr"` + else + output= + fi + # rm first in case test_failure exits. + rm "$stderr" + test_failure_ "$descr" "$@" "$output" + fi +} + # This is not among top-level (test_expect_success | test_expect_failure) # but is a prefix that can be used in the test script, like: # -- 1.5.6.rc3.7.ged9620 ^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 2/2 v4] Git.pm: add test suite 2008-06-17 6:59 [PATCH 1/2 v2] t/test-lib.sh: add test_external and test_external_without_stderr Lea Wiemann @ 2008-06-17 6:59 ` Lea Wiemann 2008-06-18 3:16 ` Junio C Hamano 2008-06-18 20:32 ` Olivier Marin 2008-06-18 20:24 ` [PATCH 1/2 v2] t/test-lib.sh: add test_external and test_external_without_stderr Olivier Marin 1 sibling, 2 replies; 13+ messages in thread From: Lea Wiemann @ 2008-06-17 6:59 UTC (permalink / raw) To: git; +Cc: Lea Wiemann Add a shell script (t/t9700-perl-git.sh) that sets up a git repository and a perl script (t/t9700/test.pl) that runs the actual tests. Signed-off-by: Lea Wiemann <LeWiemann@gmail.com> --- Changed since v3 <http://thread.gmane.org/gmane.comp.version-control.git/83425/focus=83480>: Only test the vanilla Git.pm, as I'm not intending to extend it right now. So no Git::get_hash (or Git::rev_parse) method there. I also simplified the test repository setup code a bit. t/t9700-perl-git.sh | 39 ++++++++++++++++++++ t/t9700/test.pl | 99 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 138 insertions(+), 0 deletions(-) create mode 100755 t/t9700-perl-git.sh create mode 100755 t/t9700/test.pl diff --git a/t/t9700-perl-git.sh b/t/t9700-perl-git.sh new file mode 100755 index 0000000..592d79a --- /dev/null +++ b/t/t9700-perl-git.sh @@ -0,0 +1,39 @@ +#!/bin/sh +# +# Copyright (c) 2008 Lea Wiemann +# + +test_description='perl interface (Git.pm)' +. ./test-lib.sh + +# set up test repository + +test_expect_success \ + 'set up test repository' \ + 'echo "test file 1" > file1 && + echo "test file 2" > file2 && + mkdir directory1 && + echo "in directory1" >> directory1/file && + mkdir directory2 && + echo "in directory2" >> directory2/file && + git add . && + git commit -m "first commit" && + + echo "changed file 1" > file1 && + git commit -a -m "second commit" && + + git-config --add color.test.slot1 green && + git-config --add test.string value && + git-config --add test.dupstring value1 && + git-config --add test.dupstring value2 & + git-config --add test.booltrue true && + git-config --add test.boolfalse no && + git-config --add test.boolother other && + git-config --add test.int 2k + ' + +test_external_without_stderr \ + 'Perl API' \ + perl ../t9700/test.pl + +test_done diff --git a/t/t9700/test.pl b/t/t9700/test.pl new file mode 100755 index 0000000..8318fec --- /dev/null +++ b/t/t9700/test.pl @@ -0,0 +1,99 @@ +#!/usr/bin/perl +use lib (split(/:/, $ENV{GITPERLLIB})); + +use warnings; +use strict; + +use Test::More qw(no_plan); + +use Cwd; +use File::Basename; +use File::Temp; +use IO::String; + +BEGIN { use_ok('Git') } + +# set up +our $repo_dir = "trash directory"; +our $abs_repo_dir = Cwd->cwd; +die "this must be run by calling the t/t97* shell script(s)\n" + if basename(Cwd->cwd) ne $repo_dir; +ok(our $r = Git->repository(Directory => "."), "open repository"); + +# config +is($r->config("test.string"), "value", "config scalar: string"); +is_deeply([$r->config("test.dupstring")], ["value1", "value2"], + "config array: string"); +is($r->config("test.nonexistent"), undef, "config scalar: nonexistent"); +is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent"); +is($r->config_int("test.int"), 2048, "config_int: integer"); +is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent"); +ok($r->config_bool("test.booltrue"), "config_bool: true"); +ok(!$r->config_bool("test.boolfalse"), "config_bool: false"); +our $ansi_green = "\x1b[32m"; +is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color"); +# Cannot test $r->get_colorbool("color.foo")) because we do not +# control whether our STDOUT is a terminal. + +# Failure cases for config: +# Save and restore STDERR; we will probably extract this into a +# "dies_ok" method and possibly move the STDERR handling to Git.pm. +open our $tmpstderr, ">&", STDERR or die "cannot save STDERR"; close STDERR; +eval { $r->config("test.dupstring") }; +ok($@, "config: duplicate entry in scalar context fails"); +eval { $r->config_bool("test.boolother") }; +ok($@, "config_bool: non-boolean values fail"); +open STDERR, ">&", $tmpstderr or die "cannot restore STDERR"; + +# ident +like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/, + "ident scalar: author (type)"); +like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/, + "ident scalar: committer (type)"); +is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)"); +my ($name, $email, $time_tz) = $r->ident('author'); +is_deeply([$name, $email], ["A U Thor", "author\@example.com"], + "ident array: author"); +like($time_tz, qr/[0-9]+ \+0000/, "ident array: author"); +is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"], + "ident array: ident string"); +is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string"); + +# ident_person +is($r->ident_person("aUthor"), "A U Thor <author\@example.com>", + "ident_person: author (type)"); +is($r->ident_person("Name <email> 123 +0000"), "Name <email>", + "ident_person: ident string"); +is($r->ident_person("Name", "email", "123 +0000"), "Name <email>", + "ident_person: array"); + +# objects and hashes +ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)"); +our $iostring = IO::String->new; +is($r->cat_blob($file1hash, $iostring), 15, "cat_blob: size"); +is(${$iostring->string_ref}, "changed file 1\n", "cat_blob: data"); +our $tmpfile = File::Temp->new(); +print $tmpfile ${$iostring->string_ref}; +is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip"); +$tmpfile = File::Temp->new(); +print $tmpfile my $test_text = "test blob, to be inserted\n"; +$tmpfile->close; +like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/, + "hash_and_insert_object: returns hash"); +$iostring = IO::String->new; +is($r->cat_blob($newhash, $iostring), length $test_text, "cat_blob: roundtrip size"); +is(${$iostring->string_ref}, $test_text, "cat_blob: roundtrip data"); + +# paths +is($r->repo_path, "./.git", "repo_path"); +is($r->wc_path, $abs_repo_dir . "/", "wc_path"); +is($r->wc_subdir, "", "wc_subdir initial"); +$r->wc_chdir("directory1"); +is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir"); +TODO: { + local $TODO = "commands do not work after wc_chdir"; + # Failure output is active even in non-verbose mode and thus + # annoying. Hence we skip these tests as long as they fail. + todo_skip 'config after wc_chdir', 1; + is($r->config("color.string"), "value", "config after wc_chdir"); +} -- 1.5.6.rc3.7.ged9620 ^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH 2/2 v4] Git.pm: add test suite 2008-06-17 6:59 ` [PATCH 2/2 v4] Git.pm: add test suite Lea Wiemann @ 2008-06-18 3:16 ` Junio C Hamano 2008-06-18 18:04 ` Lea Wiemann 2008-06-18 20:32 ` Olivier Marin 1 sibling, 1 reply; 13+ messages in thread From: Junio C Hamano @ 2008-06-18 3:16 UTC (permalink / raw) To: Lea Wiemann; +Cc: git Lea Wiemann <lewiemann@gmail.com> writes: > diff --git a/t/t9700/test.pl b/t/t9700/test.pl > new file mode 100755 > index 0000000..8318fec > --- /dev/null > +++ b/t/t9700/test.pl > @@ -0,0 +1,99 @@ > +#!/usr/bin/perl > +use lib (split(/:/, $ENV{GITPERLLIB})); > + > +use warnings; > +use strict; > + > +use Test::More qw(no_plan); > + > +use Cwd; > +use File::Basename; > +use File::Temp; > +use IO::String; The system I first tried this did not have IO::String installed. Is this something we would want to rely on being available? It also indicates a slight problem in test_external. Can it become a bit easier to tell such a basic problem apart from real test errors? I needed to look into /tmp/* to see where it is failing but it would be nice if we can somehow see the breakage with "sh t9700-*.sh -v". ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 2/2 v4] Git.pm: add test suite 2008-06-18 3:16 ` Junio C Hamano @ 2008-06-18 18:04 ` Lea Wiemann 2008-06-18 19:40 ` Junio C Hamano 0 siblings, 1 reply; 13+ messages in thread From: Lea Wiemann @ 2008-06-18 18:04 UTC (permalink / raw) To: Junio C Hamano; +Cc: git Junio C Hamano wrote: > The system I first tried this did not have IO::String installed. My bad, I should've checked this before sending it off. I guess we'll solve this in Michael's patch thread; don't expect a reply from me in the next 12h though, I'm way tired and utterly unproductive right now. > [test_external:] Can it become a bit easier to tell such a basic > problem apart from real test errors? I needed to look into > /tmp/* to see where it is failing Hm, is your test output somehow different from the following? It states the error quite clearly at the bottom. $ ./t9700-perl-git.sh -v * expecting success: echo "test file 1" > file1 && echo "test file 2" > file2 && [...] git-config --add test.int 2k * ok 1: set up test repository [...] * run 2: Perl API (perl ../t9700/test.pl) * FAIL 2: Perl API perl ../t9700/test.pl * expecting no stderr from previous command * FAIL 3: no stderr: Perl API perl ../t9700/test.pl Stderr is: Can't locate IO/String.pm in @INC (@INC contains: /home/lea/G/t/../perl/blib/lib /home/lea/G/t/../perl/blib/arch/auto/Git /home/lea/.perl/lib /etc/perl /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 /usr/local/lib/site_perl .) at ../t9700/test.pl line 12. BEGIN failed--compilation aborted at ../t9700/test.pl line 12. # Looks like your test died before it could output anything. * failed 2 among 3 test(s) -- Lea ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 2/2 v4] Git.pm: add test suite 2008-06-18 18:04 ` Lea Wiemann @ 2008-06-18 19:40 ` Junio C Hamano 0 siblings, 0 replies; 13+ messages in thread From: Junio C Hamano @ 2008-06-18 19:40 UTC (permalink / raw) To: Lea Wiemann; +Cc: git Lea Wiemann <lewiemann@gmail.com> writes: > Junio C Hamano wrote: >> The system I first tried this did not have IO::String installed. > > My bad, I should've checked this before sending it off. I guess we'll > solve this in Michael's patch thread; don't expect a reply from me in > the next 12h though, I'm way tired and utterly unproductive right now. > >> [test_external:] Can it become a bit easier to tell such a basic >> problem apart from real test errors? I needed to look into >> /tmp/* to see where it is failing > > Hm, is your test output somehow different from the following? It > states the error quite clearly at the bottom. > ... > * expecting no stderr from previous command > * FAIL 3: no stderr: Perl API > perl ../t9700/test.pl > Stderr is: > Can't locate IO/String.pm in @INC (@INC contains: > /home/lea/G/t/../perl/blib/lib > /home/lea/G/t/../perl/blib/arch/auto/Git /home/lea/.perl/lib /etc/perl > /usr/local/lib/perl/5.10.0 /usr/local/share/perl/5.10.0 /usr/lib/perl5 > /usr/share/perl5 /usr/lib/perl/5.10 /usr/share/perl/5.10 > /usr/local/lib/site_perl .) at ../t9700/test.pl line 12. > BEGIN failed--compilation aborted at ../t9700/test.pl line 12. > # Looks like your test died before it could output anything. > * failed 2 among 3 test(s) Yeah, I do see "Can't locate IO/String.pm". Thanks. ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 2/2 v4] Git.pm: add test suite 2008-06-17 6:59 ` [PATCH 2/2 v4] Git.pm: add test suite Lea Wiemann 2008-06-18 3:16 ` Junio C Hamano @ 2008-06-18 20:32 ` Olivier Marin 1 sibling, 0 replies; 13+ messages in thread From: Olivier Marin @ 2008-06-18 20:32 UTC (permalink / raw) To: Lea Wiemann; +Cc: git Lea Wiemann a écrit : > > diff --git a/t/t9700-perl-git.sh b/t/t9700-perl-git.sh > new file mode 100755 > index 0000000..592d79a > --- /dev/null > +++ b/t/t9700-perl-git.sh > @@ -0,0 +1,39 @@ > +#!/bin/sh > +# > +# Copyright (c) 2008 Lea Wiemann > +# > + > +test_description='perl interface (Git.pm)' > +. ./test-lib.sh > + > +# set up test repository > + > +test_expect_success \ > + 'set up test repository' \ > + 'echo "test file 1" > file1 && > + echo "test file 2" > file2 && > + mkdir directory1 && > + echo "in directory1" >> directory1/file && > + mkdir directory2 && > + echo "in directory2" >> directory2/file && > + git add . && > + git commit -m "first commit" && > + > + echo "changed file 1" > file1 && > + git commit -a -m "second commit" && > + > + git-config --add color.test.slot1 green && > + git-config --add test.string value && > + git-config --add test.dupstring value1 && > + git-config --add test.dupstring value2 & While I was running the whole test suite, your test cases randomly failed. I did not understand until I see the missing & here. ;-) Olivier. ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 1/2 v2] t/test-lib.sh: add test_external and test_external_without_stderr 2008-06-17 6:59 [PATCH 1/2 v2] t/test-lib.sh: add test_external and test_external_without_stderr Lea Wiemann 2008-06-17 6:59 ` [PATCH 2/2 v4] Git.pm: add test suite Lea Wiemann @ 2008-06-18 20:24 ` Olivier Marin 2008-06-19 18:18 ` [PATCH 1/2 v3] " Lea Wiemann 1 sibling, 1 reply; 13+ messages in thread From: Olivier Marin @ 2008-06-18 20:24 UTC (permalink / raw) To: Lea Wiemann; +Cc: git Hi, Lea Wiemann a écrit : > +# Like test_external, but in addition tests that the command generated > +# no output on stderr. > +test_external_without_stderr () { > + # The temporary file has no (and must have no) security > + # implications. > + tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi > + stderr="$tmp/git-external-stderr.$$.tmp" > + test_external "$@" 4> "$stderr" > + [ -f "$stderr" ] || eror "Internal error: $stderr disappeared." Just a typo here: s/eror/error/ Olivier. ^ permalink raw reply [flat|nested] 13+ messages in thread
* [PATCH 1/2 v3] t/test-lib.sh: add test_external and test_external_without_stderr 2008-06-18 20:24 ` [PATCH 1/2 v2] t/test-lib.sh: add test_external and test_external_without_stderr Olivier Marin @ 2008-06-19 18:18 ` Lea Wiemann 2008-06-19 18:18 ` [PATCH 2/2 v5] Git.pm: add test suite Lea Wiemann 0 siblings, 1 reply; 13+ messages in thread From: Lea Wiemann @ 2008-06-19 18:18 UTC (permalink / raw) To: git; +Cc: Lea Wiemann This is for running external test scripts in other programming languages that provide continuous output about their tests. Using test_expect_success (like "test_expect_success 'description' 'perl test-script.pl'") doesn't suffice here because test_expect_success eats stdout in non-verbose mode, which is not fixable without major file descriptor trickery. Signed-off-by: Lea Wiemann <LeWiemann@gmail.com> --- Olivier Marin wrote: > Just a typo here: s/eror/error/ Thanks for spotting this, and also the missing ampersand in the other patch! (This typo is the only change since v2.) t/test-lib.sh | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 58 insertions(+), 0 deletions(-) diff --git a/t/test-lib.sh b/t/test-lib.sh index 3ac8755..dc2736e 100644 --- a/t/test-lib.sh +++ b/t/test-lib.sh @@ -304,6 +304,64 @@ test_expect_code () { echo >&3 "" } +# test_external runs external test scripts that provide continuous +# test output about their progress, and succeeds/fails on +# zero/non-zero exit code. It outputs the test output on stdout even +# in non-verbose mode, and announces the external script with "* run +# <n>: ..." before running it. When providing relative paths, keep in +# mind that all scripts run in "trash directory". +# Usage: test_external description command arguments... +# Example: test_external 'Perl API' perl ../path/to/test.pl +test_external () { + test "$#" -eq 3 || + error >&5 "bug in the test script: not 3 parameters to test_external" + descr="$1" + shift + if ! test_skip "$descr" "$@" + then + # Announce the script to reduce confusion about the + # test output that follows. + say_color "" " run $(expr "$test_count" + 1): $descr ($*)" + # Run command; redirect its stderr to &4 as in + # test_run_, but keep its stdout on our stdout even in + # non-verbose mode. + "$@" 2>&4 + if [ "$?" = 0 ] + then + test_ok_ "$descr" + else + test_failure_ "$descr" "$@" + fi + fi +} + +# Like test_external, but in addition tests that the command generated +# no output on stderr. +test_external_without_stderr () { + # The temporary file has no (and must have no) security + # implications. + tmp="$TMPDIR"; if [ -z "$tmp" ]; then tmp=/tmp; fi + stderr="$tmp/git-external-stderr.$$.tmp" + test_external "$@" 4> "$stderr" + [ -f "$stderr" ] || error "Internal error: $stderr disappeared." + descr="no stderr: $1" + shift + say >&3 "expecting no stderr from previous command" + if [ ! -s "$stderr" ]; then + rm "$stderr" + test_ok_ "$descr" + else + if [ "$verbose" = t ]; then + output=`echo; echo Stderr is:; cat "$stderr"` + else + output= + fi + # rm first in case test_failure exits. + rm "$stderr" + test_failure_ "$descr" "$@" "$output" + fi +} + # This is not among top-level (test_expect_success | test_expect_failure) # but is a prefix that can be used in the test script, like: # -- 1.5.6.149.g06c04.dirty ^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 2/2 v5] Git.pm: add test suite 2008-06-19 18:18 ` [PATCH 1/2 v3] " Lea Wiemann @ 2008-06-19 18:18 ` Lea Wiemann 2008-06-19 20:32 ` Lea Wiemann 0 siblings, 1 reply; 13+ messages in thread From: Lea Wiemann @ 2008-06-19 18:18 UTC (permalink / raw) To: git; +Cc: Lea Wiemann Add a shell script (t/t9700-perl-git.sh) that sets up a git repository and a perl script (t/t9700/test.pl) that runs the actual tests. Signed-off-by: Lea Wiemann <LeWiemann@gmail.com> --- Changes since v4: - Added missing ampersand (thanks Olivier!). - Use 5.006002 (lowest possible version for Test::More). - Use File::Temp instead of the external IO::String. Tested with Perl 5.6, 5.8, 5.10. Diff against v4 of this patch: index 592d79a..b2fb9ec 100755 --- a/t/t9700-perl-git.sh +++ b/t/t9700-perl-git.sh @@ -25,7 +25,7 @@ test_expect_success \ git-config --add color.test.slot1 green && git-config --add test.string value && git-config --add test.dupstring value1 && - git-config --add test.dupstring value2 & + git-config --add test.dupstring value2 && git-config --add test.booltrue true && git-config --add test.boolfalse no && git-config --add test.boolother other && diff --git a/t/t9700/test.pl b/t/t9700/test.pl index 8318fec..4d23125 100755 --- a/t/t9700/test.pl +++ b/t/t9700/test.pl @@ -1,6 +1,7 @@ #!/usr/bin/perl use lib (split(/:/, $ENV{GITPERLLIB})); +use 5.006002; # Test::More was introduced in 5.6.2 use warnings; use strict; @@ -9,7 +10,6 @@ use Test::More qw(no_plan); use Cwd; use File::Basename; use File::Temp; -use IO::String; BEGIN { use_ok('Git') } @@ -69,20 +69,21 @@ is($r->ident_person("Name", "email", "123 +0000"), "Name <email>", # objects and hashes ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)"); -our $iostring = IO::String->new; -is($r->cat_blob($file1hash, $iostring), 15, "cat_blob: size"); -is(${$iostring->string_ref}, "changed file 1\n", "cat_blob: data"); -our $tmpfile = File::Temp->new(); -print $tmpfile ${$iostring->string_ref}; +our $tmpfile = File::Temp->new; +is($r->cat_blob($file1hash, $tmpfile), 15, "cat_blob: size"); +our $blobcontents; +{ local $/; seek $tmpfile, 0, 0; $blobcontents = <$tmpfile>; } +is($blobcontents, "changed file 1\n", "cat_blob: data"); +seek $tmpfile, 0, 0; is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip"); $tmpfile = File::Temp->new(); print $tmpfile my $test_text = "test blob, to be inserted\n"; -$tmpfile->close; like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/, "hash_and_insert_object: returns hash"); -$iostring = IO::String->new; -is($r->cat_blob($newhash, $iostring), length $test_text, "cat_blob: roundtrip size"); -is(${$iostring->string_ref}, $test_text, "cat_blob: roundtrip data"); +$tmpfile = File::Temp->new; +is($r->cat_blob($newhash, $tmpfile), length $test_text, "cat_blob: roundtrip size"); +{ local $/; seek $tmpfile, 0, 0; $blobcontents = <$tmpfile>; } +is($blobcontents, $test_text, "cat_blob: roundtrip data"); # paths is($r->repo_path, "./.git", "repo_path"); t/t9700-perl-git.sh | 39 ++++++++++++++++++++ t/t9700/test.pl | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 0 deletions(-) create mode 100755 t/t9700-perl-git.sh create mode 100755 t/t9700/test.pl diff --git a/t/t9700-perl-git.sh b/t/t9700-perl-git.sh new file mode 100755 index 0000000..b2fb9ec --- /dev/null +++ b/t/t9700-perl-git.sh @@ -0,0 +1,39 @@ +#!/bin/sh +# +# Copyright (c) 2008 Lea Wiemann +# + +test_description='perl interface (Git.pm)' +. ./test-lib.sh + +# set up test repository + +test_expect_success \ + 'set up test repository' \ + 'echo "test file 1" > file1 && + echo "test file 2" > file2 && + mkdir directory1 && + echo "in directory1" >> directory1/file && + mkdir directory2 && + echo "in directory2" >> directory2/file && + git add . && + git commit -m "first commit" && + + echo "changed file 1" > file1 && + git commit -a -m "second commit" && + + git-config --add color.test.slot1 green && + git-config --add test.string value && + git-config --add test.dupstring value1 && + git-config --add test.dupstring value2 && + git-config --add test.booltrue true && + git-config --add test.boolfalse no && + git-config --add test.boolother other && + git-config --add test.int 2k + ' + +test_external_without_stderr \ + 'Perl API' \ + perl ../t9700/test.pl + +test_done diff --git a/t/t9700/test.pl b/t/t9700/test.pl new file mode 100755 index 0000000..4d23125 --- /dev/null +++ b/t/t9700/test.pl @@ -0,0 +1,100 @@ +#!/usr/bin/perl +use lib (split(/:/, $ENV{GITPERLLIB})); + +use 5.006002; +use warnings; +use strict; + +use Test::More qw(no_plan); + +use Cwd; +use File::Basename; +use File::Temp; + +BEGIN { use_ok('Git') } + +# set up +our $repo_dir = "trash directory"; +our $abs_repo_dir = Cwd->cwd; +die "this must be run by calling the t/t97* shell script(s)\n" + if basename(Cwd->cwd) ne $repo_dir; +ok(our $r = Git->repository(Directory => "."), "open repository"); + +# config +is($r->config("test.string"), "value", "config scalar: string"); +is_deeply([$r->config("test.dupstring")], ["value1", "value2"], + "config array: string"); +is($r->config("test.nonexistent"), undef, "config scalar: nonexistent"); +is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent"); +is($r->config_int("test.int"), 2048, "config_int: integer"); +is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent"); +ok($r->config_bool("test.booltrue"), "config_bool: true"); +ok(!$r->config_bool("test.boolfalse"), "config_bool: false"); +our $ansi_green = "\x1b[32m"; +is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color"); +# Cannot test $r->get_colorbool("color.foo")) because we do not +# control whether our STDOUT is a terminal. + +# Failure cases for config: +# Save and restore STDERR; we will probably extract this into a +# "dies_ok" method and possibly move the STDERR handling to Git.pm. +open our $tmpstderr, ">&", STDERR or die "cannot save STDERR"; close STDERR; +eval { $r->config("test.dupstring") }; +ok($@, "config: duplicate entry in scalar context fails"); +eval { $r->config_bool("test.boolother") }; +ok($@, "config_bool: non-boolean values fail"); +open STDERR, ">&", $tmpstderr or die "cannot restore STDERR"; + +# ident +like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/, + "ident scalar: author (type)"); +like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/, + "ident scalar: committer (type)"); +is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)"); +my ($name, $email, $time_tz) = $r->ident('author'); +is_deeply([$name, $email], ["A U Thor", "author\@example.com"], + "ident array: author"); +like($time_tz, qr/[0-9]+ \+0000/, "ident array: author"); +is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"], + "ident array: ident string"); +is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string"); + +# ident_person +is($r->ident_person("aUthor"), "A U Thor <author\@example.com>", + "ident_person: author (type)"); +is($r->ident_person("Name <email> 123 +0000"), "Name <email>", + "ident_person: ident string"); +is($r->ident_person("Name", "email", "123 +0000"), "Name <email>", + "ident_person: array"); + +# objects and hashes +ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)"); +our $tmpfile = File::Temp->new; +is($r->cat_blob($file1hash, $tmpfile), 15, "cat_blob: size"); +our $blobcontents; +{ local $/; seek $tmpfile, 0, 0; $blobcontents = <$tmpfile>; } +is($blobcontents, "changed file 1\n", "cat_blob: data"); +seek $tmpfile, 0, 0; +is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip"); +$tmpfile = File::Temp->new(); +print $tmpfile my $test_text = "test blob, to be inserted\n"; +like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/, + "hash_and_insert_object: returns hash"); +$tmpfile = File::Temp->new; +is($r->cat_blob($newhash, $tmpfile), length $test_text, "cat_blob: roundtrip size"); +{ local $/; seek $tmpfile, 0, 0; $blobcontents = <$tmpfile>; } +is($blobcontents, $test_text, "cat_blob: roundtrip data"); + +# paths +is($r->repo_path, "./.git", "repo_path"); +is($r->wc_path, $abs_repo_dir . "/", "wc_path"); +is($r->wc_subdir, "", "wc_subdir initial"); +$r->wc_chdir("directory1"); +is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir"); +TODO: { + local $TODO = "commands do not work after wc_chdir"; + # Failure output is active even in non-verbose mode and thus + # annoying. Hence we skip these tests as long as they fail. + todo_skip 'config after wc_chdir', 1; + is($r->config("color.string"), "value", "config after wc_chdir"); +} -- 1.5.6.149.g06c04.dirty ^ permalink raw reply related [flat|nested] 13+ messages in thread
* [PATCH 2/2 v5] Git.pm: add test suite 2008-06-19 18:18 ` [PATCH 2/2 v5] Git.pm: add test suite Lea Wiemann @ 2008-06-19 20:32 ` Lea Wiemann 2008-06-19 20:53 ` Jakub Narebski 2008-06-19 23:55 ` Junio C Hamano 0 siblings, 2 replies; 13+ messages in thread From: Lea Wiemann @ 2008-06-19 20:32 UTC (permalink / raw) To: git; +Cc: Lea Wiemann Add a shell script (t/t9700-perl-git.sh) that sets up a git repository and a perl script (t/t9700/test.pl) that runs the actual tests. Signed-off-by: Lea Wiemann <LeWiemann@gmail.com> --- [Resent with fixed diff to v4 so git-am doesn't get confused. ;-)] Changes since v4: - Added missing ampersand (thanks Olivier!). - Use 5.006002 (lowest possible version for Test::More). - Use File::Temp instead of the external IO::String. Tested with Perl 5.6, 5.8, 5.10. Diff against v4 of this patch: index 592d79a..b2fb9ec 100755 --- a/t/t9700-perl-git.sh +++ b/t/t9700-perl-git.sh @@ -25,7 +25,7 @@ test_expect_success \ git-config --add color.test.slot1 green && git-config --add test.string value && git-config --add test.dupstring value1 && - git-config --add test.dupstring value2 & + git-config --add test.dupstring value2 && git-config --add test.booltrue true && git-config --add test.boolfalse no && git-config --add test.boolother other && diff --git a/t/t9700/test.pl b/t/t9700/test.pl index 8318fec..4d23125 100755 --- a/t/t9700/test.pl +++ b/t/t9700/test.pl @@ -1,6 +1,7 @@ #!/usr/bin/perl use lib (split(/:/, $ENV{GITPERLLIB})); +use 5.006002; # Test::More was introduced in 5.6.2 use warnings; use strict; @@ -9,7 +10,6 @@ use Test::More qw(no_plan); use Cwd; use File::Basename; use File::Temp; -use IO::String; BEGIN { use_ok('Git') } @@ -69,20 +69,21 @@ is($r->ident_person("Name", "email", "123 +0000"), "Name <email>", # objects and hashes ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)"); -our $iostring = IO::String->new; -is($r->cat_blob($file1hash, $iostring), 15, "cat_blob: size"); -is(${$iostring->string_ref}, "changed file 1\n", "cat_blob: data"); -our $tmpfile = File::Temp->new(); -print $tmpfile ${$iostring->string_ref}; +our $tmpfile = File::Temp->new; +is($r->cat_blob($file1hash, $tmpfile), 15, "cat_blob: size"); +our $blobcontents; +{ local $/; seek $tmpfile, 0, 0; $blobcontents = <$tmpfile>; } +is($blobcontents, "changed file 1\n", "cat_blob: data"); +seek $tmpfile, 0, 0; is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip"); $tmpfile = File::Temp->new(); print $tmpfile my $test_text = "test blob, to be inserted\n"; -$tmpfile->close; like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/, "hash_and_insert_object: returns hash"); -$iostring = IO::String->new; -is($r->cat_blob($newhash, $iostring), length $test_text, "cat_blob: roundtrip size"); -is(${$iostring->string_ref}, $test_text, "cat_blob: roundtrip data"); +$tmpfile = File::Temp->new; +is($r->cat_blob($newhash, $tmpfile), length $test_text, "cat_blob: roundtrip size"); +{ local $/; seek $tmpfile, 0, 0; $blobcontents = <$tmpfile>; } +is($blobcontents, $test_text, "cat_blob: roundtrip data"); # paths is($r->repo_path, "./.git", "repo_path"); t/t9700-perl-git.sh | 39 ++++++++++++++++++++ t/t9700/test.pl | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 139 insertions(+), 0 deletions(-) create mode 100755 t/t9700-perl-git.sh create mode 100755 t/t9700/test.pl diff --git a/t/t9700-perl-git.sh b/t/t9700-perl-git.sh new file mode 100755 index 0000000..b2fb9ec --- /dev/null +++ b/t/t9700-perl-git.sh @@ -0,0 +1,39 @@ +#!/bin/sh +# +# Copyright (c) 2008 Lea Wiemann +# + +test_description='perl interface (Git.pm)' +. ./test-lib.sh + +# set up test repository + +test_expect_success \ + 'set up test repository' \ + 'echo "test file 1" > file1 && + echo "test file 2" > file2 && + mkdir directory1 && + echo "in directory1" >> directory1/file && + mkdir directory2 && + echo "in directory2" >> directory2/file && + git add . && + git commit -m "first commit" && + + echo "changed file 1" > file1 && + git commit -a -m "second commit" && + + git-config --add color.test.slot1 green && + git-config --add test.string value && + git-config --add test.dupstring value1 && + git-config --add test.dupstring value2 && + git-config --add test.booltrue true && + git-config --add test.boolfalse no && + git-config --add test.boolother other && + git-config --add test.int 2k + ' + +test_external_without_stderr \ + 'Perl API' \ + perl ../t9700/test.pl + +test_done diff --git a/t/t9700/test.pl b/t/t9700/test.pl new file mode 100755 index 0000000..4d23125 --- /dev/null +++ b/t/t9700/test.pl @@ -0,0 +1,100 @@ +#!/usr/bin/perl +use lib (split(/:/, $ENV{GITPERLLIB})); + +use 5.006002; +use warnings; +use strict; + +use Test::More qw(no_plan); + +use Cwd; +use File::Basename; +use File::Temp; + +BEGIN { use_ok('Git') } + +# set up +our $repo_dir = "trash directory"; +our $abs_repo_dir = Cwd->cwd; +die "this must be run by calling the t/t97* shell script(s)\n" + if basename(Cwd->cwd) ne $repo_dir; +ok(our $r = Git->repository(Directory => "."), "open repository"); + +# config +is($r->config("test.string"), "value", "config scalar: string"); +is_deeply([$r->config("test.dupstring")], ["value1", "value2"], + "config array: string"); +is($r->config("test.nonexistent"), undef, "config scalar: nonexistent"); +is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent"); +is($r->config_int("test.int"), 2048, "config_int: integer"); +is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent"); +ok($r->config_bool("test.booltrue"), "config_bool: true"); +ok(!$r->config_bool("test.boolfalse"), "config_bool: false"); +our $ansi_green = "\x1b[32m"; +is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color"); +# Cannot test $r->get_colorbool("color.foo")) because we do not +# control whether our STDOUT is a terminal. + +# Failure cases for config: +# Save and restore STDERR; we will probably extract this into a +# "dies_ok" method and possibly move the STDERR handling to Git.pm. +open our $tmpstderr, ">&", STDERR or die "cannot save STDERR"; close STDERR; +eval { $r->config("test.dupstring") }; +ok($@, "config: duplicate entry in scalar context fails"); +eval { $r->config_bool("test.boolother") }; +ok($@, "config_bool: non-boolean values fail"); +open STDERR, ">&", $tmpstderr or die "cannot restore STDERR"; + +# ident +like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/, + "ident scalar: author (type)"); +like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/, + "ident scalar: committer (type)"); +is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)"); +my ($name, $email, $time_tz) = $r->ident('author'); +is_deeply([$name, $email], ["A U Thor", "author\@example.com"], + "ident array: author"); +like($time_tz, qr/[0-9]+ \+0000/, "ident array: author"); +is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"], + "ident array: ident string"); +is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string"); + +# ident_person +is($r->ident_person("aUthor"), "A U Thor <author\@example.com>", + "ident_person: author (type)"); +is($r->ident_person("Name <email> 123 +0000"), "Name <email>", + "ident_person: ident string"); +is($r->ident_person("Name", "email", "123 +0000"), "Name <email>", + "ident_person: array"); + +# objects and hashes +ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)"); +our $tmpfile = File::Temp->new; +is($r->cat_blob($file1hash, $tmpfile), 15, "cat_blob: size"); +our $blobcontents; +{ local $/; seek $tmpfile, 0, 0; $blobcontents = <$tmpfile>; } +is($blobcontents, "changed file 1\n", "cat_blob: data"); +seek $tmpfile, 0, 0; +is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip"); +$tmpfile = File::Temp->new(); +print $tmpfile my $test_text = "test blob, to be inserted\n"; +like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/, + "hash_and_insert_object: returns hash"); +$tmpfile = File::Temp->new; +is($r->cat_blob($newhash, $tmpfile), length $test_text, "cat_blob: roundtrip size"); +{ local $/; seek $tmpfile, 0, 0; $blobcontents = <$tmpfile>; } +is($blobcontents, $test_text, "cat_blob: roundtrip data"); + +# paths +is($r->repo_path, "./.git", "repo_path"); +is($r->wc_path, $abs_repo_dir . "/", "wc_path"); +is($r->wc_subdir, "", "wc_subdir initial"); +$r->wc_chdir("directory1"); +is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir"); +TODO: { + local $TODO = "commands do not work after wc_chdir"; + # Failure output is active even in non-verbose mode and thus + # annoying. Hence we skip these tests as long as they fail. + todo_skip 'config after wc_chdir', 1; + is($r->config("color.string"), "value", "config after wc_chdir"); +} -- 1.5.6.149.g06c04.dirty ^ permalink raw reply related [flat|nested] 13+ messages in thread
* Re: [PATCH 2/2 v5] Git.pm: add test suite 2008-06-19 20:32 ` Lea Wiemann @ 2008-06-19 20:53 ` Jakub Narebski 2008-06-19 20:55 ` Lea Wiemann 2008-06-19 23:55 ` Junio C Hamano 1 sibling, 1 reply; 13+ messages in thread From: Jakub Narebski @ 2008-06-19 20:53 UTC (permalink / raw) To: Lea Wiemann; +Cc: git, Lea Wiemann Lea Wiemann <lewiemann@gmail.com> writes: > Changes since v4: > > - Use 5.006002 (lowest possible version for Test::More). [...] > #!/usr/bin/perl > use lib (split(/:/, $ENV{GITPERLLIB})); > > +use 5.006002; # Test::More was introduced in 5.6.2 Isn't "use Test::More" enough, so this line is not strictly necessary? -- Jakub Narebski Poland ShadeHawk on #git ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 2/2 v5] Git.pm: add test suite 2008-06-19 20:53 ` Jakub Narebski @ 2008-06-19 20:55 ` Lea Wiemann 0 siblings, 0 replies; 13+ messages in thread From: Lea Wiemann @ 2008-06-19 20:55 UTC (permalink / raw) To: Jakub Narebski; +Cc: git Jakub Narebski wrote: > Lea Wiemann <lewiemann@gmail.com> writes: >> +use 5.006002; # Test::More was introduced in 5.6.2 > > Isn't "use Test::More" enough, so this line is not strictly > necessary? I'd prefer to have a 'wrong Perl version' error instead of 'module not found'. Also it's good to document what we support, to remind me that I have to run it with perl5.6. -- Lea ^ permalink raw reply [flat|nested] 13+ messages in thread
* Re: [PATCH 2/2 v5] Git.pm: add test suite 2008-06-19 20:32 ` Lea Wiemann 2008-06-19 20:53 ` Jakub Narebski @ 2008-06-19 23:55 ` Junio C Hamano 1 sibling, 0 replies; 13+ messages in thread From: Junio C Hamano @ 2008-06-19 23:55 UTC (permalink / raw) To: Lea Wiemann; +Cc: git, Jakub Narebski Thanks. These two Perl-ish test patches will be part of 'next' tonight. When sending further improvements, if necessary, please make them relative to the copy there. ^ permalink raw reply [flat|nested] 13+ messages in thread
end of thread, other threads:[~2008-06-19 23:56 UTC | newest] Thread overview: 13+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2008-06-17 6:59 [PATCH 1/2 v2] t/test-lib.sh: add test_external and test_external_without_stderr Lea Wiemann 2008-06-17 6:59 ` [PATCH 2/2 v4] Git.pm: add test suite Lea Wiemann 2008-06-18 3:16 ` Junio C Hamano 2008-06-18 18:04 ` Lea Wiemann 2008-06-18 19:40 ` Junio C Hamano 2008-06-18 20:32 ` Olivier Marin 2008-06-18 20:24 ` [PATCH 1/2 v2] t/test-lib.sh: add test_external and test_external_without_stderr Olivier Marin 2008-06-19 18:18 ` [PATCH 1/2 v3] " Lea Wiemann 2008-06-19 18:18 ` [PATCH 2/2 v5] Git.pm: add test suite Lea Wiemann 2008-06-19 20:32 ` Lea Wiemann 2008-06-19 20:53 ` Jakub Narebski 2008-06-19 20:55 ` Lea Wiemann 2008-06-19 23:55 ` Junio C Hamano
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for NNTP newsgroup(s).