Skip to content

Commit c810959

Browse files
committed
Added support for official tests
package-url/purl-spec#514
1 parent fd36323 commit c810959

File tree

12 files changed

+574
-13
lines changed

12 files changed

+574
-13
lines changed

MANIFEST

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ lib/URI/VersionRange/Constraint.pm
1414
lib/URI/VersionRange/Version.pm
1515
LICENSE
1616
Makefile.PL
17-
MANIFEST This list of files
17+
MANIFEST
1818
README.md
1919
t/00-load.t
2020
t/10-encode.t
@@ -24,8 +24,11 @@ t/40-cli.t
2424
t/50-version-range.t
2525
t/90-cpan-distname-info.t
2626
t/99-official-purl-test-suite.t
27+
t/99-official-purl-tests.t
2728
t/manifest.t
29+
t/official-tests/specification-test.json
2830
t/pod-coverage.t
2931
t/pod.t
3032
t/sync-purl-test-suite-data.sh
33+
t/sync-purl-tests.sh
3134
t/test-suite-data.json

lib/URI/PackageURL.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use constant DEBUG => $ENV{PURL_DEBUG};
1313

1414
use overload '""' => 'to_string', fallback => 1;
1515

16-
our $VERSION = '2.23';
16+
our $VERSION = '2.23_1';
1717
our @EXPORT = qw(encode_purl decode_purl);
1818

1919
my $PURL_REGEXP = qr{^pkg:[A-Za-z\\.\\-\\+][A-Za-z0-9\\.\\-\\+]*/.+};

lib/URI/PackageURL/App.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use Data::Dumper ();
1313

1414
use URI::PackageURL ();
1515

16-
our $VERSION = '2.23';
16+
our $VERSION = '2.23_1';
1717

1818
sub cli_error {
1919
my ($error) = @_;

lib/URI/PackageURL/Util.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use warnings;
77

88
use Exporter qw(import);
99

10-
our $VERSION = '2.23';
10+
our $VERSION = '2.23_1';
1111
our @EXPORT = qw(purl_to_urls purl_components_normalize);
1212

1313
sub purl_components_normalize {

lib/URI/VersionRange.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use constant FALSE => !!0;
1818

1919
use overload '""' => 'to_string', fallback => 1;
2020

21-
our $VERSION = '2.23';
21+
our $VERSION = '2.23_1';
2222
our @EXPORT = qw(encode_vers decode_vers);
2323

2424
my $VERS_REGEXP = qr{^vers:[a-z\\.\\-\\+][a-z0-9\\.\\-\\+]*/.+};

lib/URI/VersionRange/App.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use Data::Dumper ();
1313

1414
use URI::VersionRange ();
1515

16-
our $VERSION = '2.23';
16+
our $VERSION = '2.23_1';
1717

1818
sub cli_error {
1919
my ($error) = @_;

lib/URI/VersionRange/Constraint.pm

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use overload '""' => 'to_string', fallback => 1;
1212

1313
use URI::VersionRange::Version;
1414

15-
our $VERSION = '2.23';
15+
our $VERSION = '2.23_1';
1616

1717
our %COMPARATOR = (
1818
'=' => 'equal',

t/99-official-purl-tests.t

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
#!perl
2+
3+
use JSON::PP;
4+
use Test::More;
5+
use File::Spec;
6+
7+
require_ok('URI::PackageURL');
8+
9+
my $purl_tests_dir = File::Spec->catdir('t', 'official-tests');
10+
11+
BAIL_OUT('"official-tests" directory not found') if (!-d $purl_tests_dir);
12+
13+
opendir(my $dh, $purl_tests_dir) or Carp::croak "Can't open directory: $!";
14+
15+
while (my $file = readdir $dh) {
16+
17+
next if ($file eq '.' or $file eq '..');
18+
next unless ($file =~ /(specification|cpan)/);
19+
20+
my $test_file = File::Spec->catfile('t', 'official-tests', $file);
21+
22+
subtest $test_file => sub {
23+
execute_test($test_file);
24+
}
25+
26+
}
27+
28+
closedir $dh;
29+
30+
sub execute_test {
31+
32+
my $test_file = shift;
33+
34+
open my $fh, '<', $test_file or Carp::croak "Can't open file: $!";
35+
36+
my $test_content = do { local $/; <$fh> };
37+
my $test_data = JSON::PP::decode_json($test_content);
38+
39+
foreach my $test (@{$test_data->{tests}}) {
40+
41+
diag $test->{description};
42+
43+
TODO: {
44+
execute_parse_test($test) if $test->{test_type} eq 'parse';
45+
execute_build_test($test) if $test->{test_type} eq 'build';
46+
execute_roundtrip_test($test) if $test->{test_type} eq 'roundtrip';
47+
}
48+
49+
}
50+
51+
}
52+
53+
sub execute_build_test {
54+
55+
my $test = shift;
56+
57+
my $test_description = $test->{description};
58+
59+
my $purl = eval { URI::PackageURL->new(%{$test->{input}}); };
60+
61+
local $TODO = 'DUBIOUS MAVEN TEST' if $test_description =~ /invalid encoded colon : between scheme and type/i;
62+
local $TODO = 'DUBIOUS CONAN TEST' if $@ =~ /Conan 'channel' qualifier does not exist for namespace/i;
63+
64+
if ($test->{expected_failure}) {
65+
like($@, qr/Invalid Package URL/i, "ENCODE: $test_description");
66+
return;
67+
}
68+
69+
if (!$test->{expected_failure} && $@) {
70+
fail("DECODE: $test_description ($@)");
71+
return;
72+
}
73+
74+
is($purl->to_string, $test->{expected_output}, "ENCODE: $test_description");
75+
76+
}
77+
78+
sub execute_parse_test {
79+
80+
my $test = shift;
81+
82+
my $test_description = $test->{description};
83+
my $purl_string = $test->{input};
84+
85+
diag $purl_string;
86+
87+
my $purl = eval { URI::PackageURL->from_string($purl_string) };
88+
89+
local $TODO = 'DUBIOUS NPM TEST' if $purl_string =~ /pkg\:npm\/@/;
90+
local $TODO = 'DUBIOUS CONAN TEST' if $@ =~ /Conan 'channel' qualifier does not exist for namespace/i;
91+
92+
if ($test->{expected_failure}) {
93+
like($@, qr/(Invalid|Malformed) Package URL/i, "DECODE $purl_string: $test_description");
94+
return;
95+
}
96+
97+
if (!$test->{expected_failure} && $@) {
98+
fail("DECODE: $test_description ($@)");
99+
return;
100+
}
101+
102+
my @components = qw(type namespace name version subpath);
103+
104+
foreach my $component (@components) {
105+
is(
106+
$purl->$component,
107+
$test->{expected_output}->{$component},
108+
"DECODE: Compare '$test_description' $component component"
109+
);
110+
}
111+
112+
}
113+
114+
sub execute_roundtrip_test {
115+
116+
my $test = shift;
117+
118+
my $test_description = $test->{description};
119+
my $purl_string = $test->{input};
120+
121+
diag $purl_string;
122+
123+
my $purl = eval { URI::PackageURL->from_string($purl_string) };
124+
125+
if ($@) {
126+
fail("DECODE: $test_description ($@)");
127+
return;
128+
}
129+
130+
is($purl->to_string, $test->{expected_output}, "ENCODE: $test_description");
131+
132+
}
133+
134+
done_testing();

0 commit comments

Comments
 (0)