Skip to content

Commit f07eeed

Browse files
lmmarsanogitster
authored andcommitted
git-credential-netrc: adapt to test framework for git
git-credential-netrc tests did not run in a test repository. Reuse the main test framework to stage a temporary repository. To imitate Perl tests under t/ - switch to Test::More module - use File::Basename & File::Spec::Functions Signed-off-by: Luis Marsano <[email protected]> Acked-by: Ted Zlatanov <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 468165c commit f07eeed

File tree

3 files changed

+77
-30
lines changed

3 files changed

+77
-30
lines changed

contrib/credential/netrc/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
test:
2-
./test.pl
2+
./t-git-credential-netrc.sh
33

44
testverbose:
5-
./test.pl -d -v
5+
./t-git-credential-netrc.sh -d -v
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#!/bin/sh
2+
(
3+
cd ../../../t
4+
test_description='git-credential-netrc'
5+
. ./test-lib.sh
6+
7+
if ! test_have_prereq PERL; then
8+
skip_all='skipping perl interface tests, perl not available'
9+
test_done
10+
fi
11+
12+
perl -MTest::More -e 0 2>/dev/null || {
13+
skip_all="Perl Test::More unavailable, skipping test"
14+
test_done
15+
}
16+
17+
# set up test repository
18+
19+
test_expect_success \
20+
'set up test repository' \
21+
:
22+
23+
# The external test will outputs its own plan
24+
test_external_has_tap=1
25+
26+
test_external \
27+
'git-credential-netrc' \
28+
perl "$TEST_DIRECTORY"/../contrib/credential/netrc/test.pl
29+
30+
test_done
31+
)

contrib/credential/netrc/test.pl

Lines changed: 44 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,83 +1,99 @@
11
#!/usr/bin/perl
2+
use lib (split(/:/, $ENV{GITPERLLIB}));
23

34
use warnings;
45
use strict;
5-
use Test;
6+
use Test::More qw(no_plan);
7+
use File::Basename;
8+
use File::Spec::Functions qw(:DEFAULT rel2abs);
69
use IPC::Open2;
710

8-
BEGIN { plan tests => 15 }
11+
BEGIN {
12+
# t-git-credential-netrc.sh kicks off our testing, so we have to go from there.
13+
Test::More->builder->current_test(1);
14+
Test::More->builder->no_ending(1);
15+
}
916

1017
my @global_credential_args = @ARGV;
11-
my $netrc = './test.netrc';
12-
print "# Testing insecure file, nothing should be found\n";
18+
my $scriptDir = dirname rel2abs $0;
19+
my $netrc = catfile $scriptDir, 'test.netrc';
20+
my $gcNetrc = catfile $scriptDir, 'git-credential-netrc';
21+
local $ENV{PATH} = join ':'
22+
, $scriptDir
23+
, $ENV{PATH}
24+
? $ENV{PATH}
25+
: ();
26+
27+
diag "Testing insecure file, nothing should be found\n";
1328
chmod 0644, $netrc;
1429
my $cred = run_credential(['-f', $netrc, 'get'],
1530
{ host => 'github.com' });
1631

17-
ok(scalar keys %$cred, 0, "Got 0 keys from insecure file");
32+
ok(scalar keys %$cred == 0, "Got 0 keys from insecure file");
1833

19-
print "# Testing missing file, nothing should be found\n";
34+
diag "Testing missing file, nothing should be found\n";
2035
chmod 0644, $netrc;
2136
$cred = run_credential(['-f', '///nosuchfile///', 'get'],
2237
{ host => 'github.com' });
2338

24-
ok(scalar keys %$cred, 0, "Got 0 keys from missing file");
39+
ok(scalar keys %$cred == 0, "Got 0 keys from missing file");
2540

2641
chmod 0600, $netrc;
2742

28-
print "# Testing with invalid data\n";
43+
diag "Testing with invalid data\n";
2944
$cred = run_credential(['-f', $netrc, 'get'],
3045
"bad data");
31-
ok(scalar keys %$cred, 4, "Got first found keys with bad data");
46+
ok(scalar keys %$cred == 4, "Got first found keys with bad data");
3247

33-
print "# Testing netrc file for a missing corovamilkbar entry\n";
48+
diag "Testing netrc file for a missing corovamilkbar entry\n";
3449
$cred = run_credential(['-f', $netrc, 'get'],
3550
{ host => 'corovamilkbar' });
3651

37-
ok(scalar keys %$cred, 0, "Got no corovamilkbar keys");
52+
ok(scalar keys %$cred == 0, "Got no corovamilkbar keys");
3853

39-
print "# Testing netrc file for a github.com entry\n";
54+
diag "Testing netrc file for a github.com entry\n";
4055
$cred = run_credential(['-f', $netrc, 'get'],
4156
{ host => 'github.com' });
4257

43-
ok(scalar keys %$cred, 2, "Got 2 Github keys");
58+
ok(scalar keys %$cred == 2, "Got 2 Github keys");
4459

45-
ok($cred->{password}, 'carolknows', "Got correct Github password");
46-
ok($cred->{username}, 'carol', "Got correct Github username");
60+
is($cred->{password}, 'carolknows', "Got correct Github password");
61+
is($cred->{username}, 'carol', "Got correct Github username");
4762

48-
print "# Testing netrc file for a username-specific entry\n";
63+
diag "Testing netrc file for a username-specific entry\n";
4964
$cred = run_credential(['-f', $netrc, 'get'],
5065
{ host => 'imap', username => 'bob' });
5166

52-
ok(scalar keys %$cred, 2, "Got 2 username-specific keys");
67+
ok(scalar keys %$cred == 2, "Got 2 username-specific keys");
5368

54-
ok($cred->{password}, 'bobwillknow', "Got correct user-specific password");
55-
ok($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
69+
is($cred->{password}, 'bobwillknow', "Got correct user-specific password");
70+
is($cred->{protocol}, 'imaps', "Got correct user-specific protocol");
5671

57-
print "# Testing netrc file for a host:port-specific entry\n";
72+
diag "Testing netrc file for a host:port-specific entry\n";
5873
$cred = run_credential(['-f', $netrc, 'get'],
5974
{ host => 'imap2:1099' });
6075

61-
ok(scalar keys %$cred, 2, "Got 2 host:port-specific keys");
76+
ok(scalar keys %$cred == 2, "Got 2 host:port-specific keys");
6277

63-
ok($cred->{password}, 'tzzknow', "Got correct host:port-specific password");
64-
ok($cred->{username}, 'tzz', "Got correct host:port-specific username");
78+
is($cred->{password}, 'tzzknow', "Got correct host:port-specific password");
79+
is($cred->{username}, 'tzz', "Got correct host:port-specific username");
6580

66-
print "# Testing netrc file that 'host:port kills host' entry\n";
81+
diag "Testing netrc file that 'host:port kills host' entry\n";
6782
$cred = run_credential(['-f', $netrc, 'get'],
6883
{ host => 'imap2' });
6984

70-
ok(scalar keys %$cred, 2, "Got 2 'host:port kills host' keys");
85+
ok(scalar keys %$cred == 2, "Got 2 'host:port kills host' keys");
86+
87+
is($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password");
88+
is($cred->{username}, 'bob', "Got correct 'host:port kills host' username");
7189

72-
ok($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password");
73-
ok($cred->{username}, 'bob', "Got correct 'host:port kills host' username");
7490

7591
sub run_credential
7692
{
7793
my $args = shift @_;
7894
my $data = shift @_;
7995
my $pid = open2(my $chld_out, my $chld_in,
80-
'./git-credential-netrc', @global_credential_args,
96+
$gcNetrc, @global_credential_args,
8197
@$args);
8298

8399
die "Couldn't open pipe to netrc credential helper: $!" unless $pid;

0 commit comments

Comments
 (0)