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 <luis.marsano@gmail.com> Acked-by: Ted Zlatanov <tzz@lifelogs.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Luis Marsano committed May 12, 2018 at 05:17 UTC f07eeed123b8880b1723b1ea9d6d6f41cfb34532
3 files changed +77 -30
contrib/credential/netrc/Makefile
+2 -2
@@ -1,5 +1,5 @@
1 test:
2 - ./test.pl
2 + ./t-git-credential-netrc.sh
3
4 testverbose:
5 - ./test.pl -d -v
5 + ./t-git-credential-netrc.sh -d -v
contrib/credential/netrc/t-git-credential-netrc.sh new
+31
@@ -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
+44 -28
@@ -1,83 +1,99 @@
1 #!/usr/bin/perl
2 +use lib (split(/:/, $ENV{GITPERLLIB}));
3
4 use warnings;
5 use strict;
5 -use Test;
6 +use Test::More qw(no_plan);
7 +use File::Basename;
8 +use File::Spec::Functions qw(:DEFAULT rel2abs);
9 use IPC::Open2;
10
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 +}
16
17 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";
28 chmod 0644, $netrc;
29 my $cred = run_credential(['-f', $netrc, 'get'],
30 { host => 'github.com' });
31
17 -ok(scalar keys %$cred, 0, "Got 0 keys from insecure file");
32 +ok(scalar keys %$cred == 0, "Got 0 keys from insecure file");
33
19 -print "# Testing missing file, nothing should be found\n";
34 +diag "Testing missing file, nothing should be found\n";
35 chmod 0644, $netrc;
36 $cred = run_credential(['-f', '///nosuchfile///', 'get'],
37 { host => 'github.com' });
38
24 -ok(scalar keys %$cred, 0, "Got 0 keys from missing file");
39 +ok(scalar keys %$cred == 0, "Got 0 keys from missing file");
40
41 chmod 0600, $netrc;
42
28 -print "# Testing with invalid data\n";
43 +diag "Testing with invalid data\n";
44 $cred = run_credential(['-f', $netrc, 'get'],
45 "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");
47
33 -print "# Testing netrc file for a missing corovamilkbar entry\n";
48 +diag "Testing netrc file for a missing corovamilkbar entry\n";
49 $cred = run_credential(['-f', $netrc, 'get'],
50 { host => 'corovamilkbar' });
51
37 -ok(scalar keys %$cred, 0, "Got no corovamilkbar keys");
52 +ok(scalar keys %$cred == 0, "Got no corovamilkbar keys");
53
39 -print "# Testing netrc file for a github.com entry\n";
54 +diag "Testing netrc file for a github.com entry\n";
55 $cred = run_credential(['-f', $netrc, 'get'],
56 { host => 'github.com' });
57
43 -ok(scalar keys %$cred, 2, "Got 2 Github keys");
58 +ok(scalar keys %$cred == 2, "Got 2 Github keys");
59
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");
62
48 -print "# Testing netrc file for a username-specific entry\n";
63 +diag "Testing netrc file for a username-specific entry\n";
64 $cred = run_credential(['-f', $netrc, 'get'],
65 { host => 'imap', username => 'bob' });
66
52 -ok(scalar keys %$cred, 2, "Got 2 username-specific keys");
67 +ok(scalar keys %$cred == 2, "Got 2 username-specific keys");
68
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");
71
57 -print "# Testing netrc file for a host:port-specific entry\n";
72 +diag "Testing netrc file for a host:port-specific entry\n";
73 $cred = run_credential(['-f', $netrc, 'get'],
74 { host => 'imap2:1099' });
75
61 -ok(scalar keys %$cred, 2, "Got 2 host:port-specific keys");
76 +ok(scalar keys %$cred == 2, "Got 2 host:port-specific keys");
77
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");
80
66 -print "# Testing netrc file that 'host:port kills host' entry\n";
81 +diag "Testing netrc file that 'host:port kills host' entry\n";
82 $cred = run_credential(['-f', $netrc, 'get'],
83 { host => 'imap2' });
84
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");
89
72 -ok($cred->{password}, 'bobwillknow', "Got correct 'host:port kills host' password");
73 -ok($cred->{username}, 'bob', "Got correct 'host:port kills host' username");
90
91 sub run_credential
92 {
93 my $args = shift @_;
94 my $data = shift @_;
95 my $pid = open2(my $chld_out, my $chld_in,
80 - './git-credential-netrc', @global_credential_args,
96 + $gcNetrc, @global_credential_args,
97 @$args);
98
99 die "Couldn't open pipe to netrc credential helper: $!" unless $pid;