@matheusbsbs / matheus-bagliano / commits / b4780d725c
Fork 0

Git.pm: add test suite

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> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Lea Wiemann committed Jun 19, 2008 at 22:32 UTC b4780d725c673db26692e11c56539381d60ad17c
2 files changed +139
t/t9700-perl-git.sh new
+39
@@ -0,0 +1,39 @@
1 +#!/bin/sh
2 +#
3 +# Copyright (c) 2008 Lea Wiemann
4 +#
5 +
6 +test_description='perl interface (Git.pm)'
7 +. ./test-lib.sh
8 +
9 +# set up test repository
10 +
11 +test_expect_success \
12 + 'set up test repository' \
13 + 'echo "test file 1" > file1 &&
14 + echo "test file 2" > file2 &&
15 + mkdir directory1 &&
16 + echo "in directory1" >> directory1/file &&
17 + mkdir directory2 &&
18 + echo "in directory2" >> directory2/file &&
19 + git add . &&
20 + git commit -m "first commit" &&
21 +
22 + echo "changed file 1" > file1 &&
23 + git commit -a -m "second commit" &&
24 +
25 + git-config --add color.test.slot1 green &&
26 + git-config --add test.string value &&
27 + git-config --add test.dupstring value1 &&
28 + git-config --add test.dupstring value2 &&
29 + git-config --add test.booltrue true &&
30 + git-config --add test.boolfalse no &&
31 + git-config --add test.boolother other &&
32 + git-config --add test.int 2k
33 + '
34 +
35 +test_external_without_stderr \
36 + 'Perl API' \
37 + perl ../t9700/test.pl
38 +
39 +test_done
t/t9700/test.pl new
+100
@@ -0,0 +1,100 @@
1 +#!/usr/bin/perl
2 +use lib (split(/:/, $ENV{GITPERLLIB}));
3 +
4 +use 5.006002;
5 +use warnings;
6 +use strict;
7 +
8 +use Test::More qw(no_plan);
9 +
10 +use Cwd;
11 +use File::Basename;
12 +use File::Temp;
13 +
14 +BEGIN { use_ok('Git') }
15 +
16 +# set up
17 +our $repo_dir = "trash directory";
18 +our $abs_repo_dir = Cwd->cwd;
19 +die "this must be run by calling the t/t97* shell script(s)\n"
20 + if basename(Cwd->cwd) ne $repo_dir;
21 +ok(our $r = Git->repository(Directory => "."), "open repository");
22 +
23 +# config
24 +is($r->config("test.string"), "value", "config scalar: string");
25 +is_deeply([$r->config("test.dupstring")], ["value1", "value2"],
26 + "config array: string");
27 +is($r->config("test.nonexistent"), undef, "config scalar: nonexistent");
28 +is_deeply([$r->config("test.nonexistent")], [], "config array: nonexistent");
29 +is($r->config_int("test.int"), 2048, "config_int: integer");
30 +is($r->config_int("test.nonexistent"), undef, "config_int: nonexistent");
31 +ok($r->config_bool("test.booltrue"), "config_bool: true");
32 +ok(!$r->config_bool("test.boolfalse"), "config_bool: false");
33 +our $ansi_green = "\x1b[32m";
34 +is($r->get_color("color.test.slot1", "red"), $ansi_green, "get_color");
35 +# Cannot test $r->get_colorbool("color.foo")) because we do not
36 +# control whether our STDOUT is a terminal.
37 +
38 +# Failure cases for config:
39 +# Save and restore STDERR; we will probably extract this into a
40 +# "dies_ok" method and possibly move the STDERR handling to Git.pm.
41 +open our $tmpstderr, ">&", STDERR or die "cannot save STDERR"; close STDERR;
42 +eval { $r->config("test.dupstring") };
43 +ok($@, "config: duplicate entry in scalar context fails");
44 +eval { $r->config_bool("test.boolother") };
45 +ok($@, "config_bool: non-boolean values fail");
46 +open STDERR, ">&", $tmpstderr or die "cannot restore STDERR";
47 +
48 +# ident
49 +like($r->ident("aUthor"), qr/^A U Thor <author\@example.com> [0-9]+ \+0000$/,
50 + "ident scalar: author (type)");
51 +like($r->ident("cOmmitter"), qr/^C O Mitter <committer\@example.com> [0-9]+ \+0000$/,
52 + "ident scalar: committer (type)");
53 +is($r->ident("invalid"), "invalid", "ident scalar: invalid ident string (no parsing)");
54 +my ($name, $email, $time_tz) = $r->ident('author');
55 +is_deeply([$name, $email], ["A U Thor", "author\@example.com"],
56 + "ident array: author");
57 +like($time_tz, qr/[0-9]+ \+0000/, "ident array: author");
58 +is_deeply([$r->ident("Name <email> 123 +0000")], ["Name", "email", "123 +0000"],
59 + "ident array: ident string");
60 +is_deeply([$r->ident("invalid")], [], "ident array: invalid ident string");
61 +
62 +# ident_person
63 +is($r->ident_person("aUthor"), "A U Thor <author\@example.com>",
64 + "ident_person: author (type)");
65 +is($r->ident_person("Name <email> 123 +0000"), "Name <email>",
66 + "ident_person: ident string");
67 +is($r->ident_person("Name", "email", "123 +0000"), "Name <email>",
68 + "ident_person: array");
69 +
70 +# objects and hashes
71 +ok(our $file1hash = $r->command_oneline('rev-parse', "HEAD:file1"), "(get file hash)");
72 +our $tmpfile = File::Temp->new;
73 +is($r->cat_blob($file1hash, $tmpfile), 15, "cat_blob: size");
74 +our $blobcontents;
75 +{ local $/; seek $tmpfile, 0, 0; $blobcontents = <$tmpfile>; }
76 +is($blobcontents, "changed file 1\n", "cat_blob: data");
77 +seek $tmpfile, 0, 0;
78 +is(Git::hash_object("blob", $tmpfile), $file1hash, "hash_object: roundtrip");
79 +$tmpfile = File::Temp->new();
80 +print $tmpfile my $test_text = "test blob, to be inserted\n";
81 +like(our $newhash = $r->hash_and_insert_object($tmpfile), qr/[0-9a-fA-F]{40}/,
82 + "hash_and_insert_object: returns hash");
83 +$tmpfile = File::Temp->new;
84 +is($r->cat_blob($newhash, $tmpfile), length $test_text, "cat_blob: roundtrip size");
85 +{ local $/; seek $tmpfile, 0, 0; $blobcontents = <$tmpfile>; }
86 +is($blobcontents, $test_text, "cat_blob: roundtrip data");
87 +
88 +# paths
89 +is($r->repo_path, "./.git", "repo_path");
90 +is($r->wc_path, $abs_repo_dir . "/", "wc_path");
91 +is($r->wc_subdir, "", "wc_subdir initial");
92 +$r->wc_chdir("directory1");
93 +is($r->wc_subdir, "directory1", "wc_subdir after wc_chdir");
94 +TODO: {
95 + local $TODO = "commands do not work after wc_chdir";
96 + # Failure output is active even in non-verbose mode and thus
97 + # annoying. Hence we skip these tests as long as they fail.
98 + todo_skip 'config after wc_chdir', 1;
99 + is($r->config("color.string"), "value", "config after wc_chdir");
100 +}