1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
|
use v6;
=begin pod
=head1 NAME
Test::META - Test that a Raku project has a good and proper META file
=head1 SYNOPSIS
This is the actual *t/030-my-meta.t* from this distribution
=begin code
#!raku
use v6.c;
use Test;
use Test::META;
plan 1;
# That's it
meta-ok();
done-testing;
=end code
=head1 DESCRIPTION
This provides a simple mechanism for module authors to have some
confidence that they have a working distribution META description
file (as described in L<http://design.raku.org/S22.html#META6.json>.)
It exports one subroutine *meta-ok* that runs a single sub-test that
checks that:
=item The META file (either META6.json or META.info) exists
=item The META file can be parsed as valid JSON
=item The attributes marked as "mandatory" are present
=item The files mention in the "provides" section are present.
=item The authors field is used instead of author
=item The name attribute doesn't have a hyphen rather than '::'
=item The version exists and it isn't '*'
=item If the META6 file specifies a meta-version version greater than 0 that the version strings do not contain a 'v' prefix
The C<meta-ok> takes one optional adverb C<:relaxed-name> that can stop
the name check being a fail if it is intended to be like that.
There are mechanisms (used internally for testing) to over-ride the
location or name of the META file and these can be seen in the test-suite,
though they won't typically be needed.
=end pod
module Test::META:ver<0.0.17>:auth<github:jonathanstowe> {
use Test;
use META6:ver(v0.0.24+);
use License::SPDX;
use URI;
our $TESTING = False;
sub my-diag(Str() $mess) {
diag $mess unless $TESTING;
}
sub meta-ok(:$relaxed-name) is export(:DEFAULT) {
subtest {
my $meta-file = get-meta();
if $meta-file.defined and $meta-file.e {
pass "have a META file";
my $meta;
my Int $seen-vee = 0;
lives-ok {
CONTROL {
when CX::Warn {
if $_.message ~~ /'prefix "v" seen in version string'/ {
$seen-vee++;
$_.resume;
}
}
};
$meta = META6.new(file => $meta-file);
}, "META parses okay";
if $meta.defined {
ok check-mandatory($meta), "have all required entries";
ok check-provides($meta), "'provides' looks sane";
ok check-authors($meta), "Optional 'authors' and not 'author'";
ok check-license($meta), "License is correct";
ok check-name($meta, :$relaxed-name), "name has a '::' rather than a hyphen (if this is intentional please pass :relaxed-name to meta-ok)";
# this is transitional as the method changed in META6
ok ($meta.?meta6 | $meta.?meta-version ) ~~ Version.new(0) ?? True !! $seen-vee == 0, "no 'v' in version strings (meta-version greater than 0)";
ok check-version($meta), "version is present and doesn't have an asterisk";
ok check-sources($meta), "have usable source";
}
}
else {
flunk "don't have META file";
}
}, "Project META file is good";
}
our sub get-meta() {
$*META-FILE // do {
my $meta;
for meta-candidates().map({ dist-dir.add($_) }) -> $m {
if $m.e {
$meta = $m;
last;
}
}
$meta;
}
}
our sub check-mandatory(META6:D $meta --> Bool) {
my Bool $rc = True;
for $meta.^attributes -> $attr {
if $attr.does(META6::MetaAttribute) {
if $attr.optionality ~~ META6::Mandatory {
if not $attr.get_value($meta).defined {
my $name = $attr.name.substr(2);
$rc = False;
my-diag "required attribute '$name' is not defined";
}
}
}
}
$rc;
}
our sub check-provides(META6:D $meta --> Bool) {
my Bool $rc = True;
for $meta.provides.kv -> $name, $path {
if not dist-dir().add($path).e {
$rc = False;
my-diag "file for '$name' '$path' does not exist";
}
elsif $path.IO.is-absolute {
$rc = False;
my-diag "file for '$name' '$path' is absolute, it should be relative to the dist directory";
}
}
$rc;
}
our sub check-authors(META6:D $meta --> Bool) {
my Bool $rc = True;
if $meta.author.defined {
if $meta.authors.elems == 0 {
$rc = False;
my-diag "there is an 'author' field rather than the specified 'authors'";
}
}
$rc;
}
our sub check-license(META6:D $meta --> Bool) {
my Bool $rc = True;
if $meta.license.defined {
my $licence-list = License::SPDX.new;
my $licence = $licence-list.get-license($meta.license );
if !$licence.defined {
if $meta.license eq any('NOASSERTION', 'NONE') {
my-diag "NOTICE! License is $meta.license(). This is valid, but licenses are prefered.";
$rc = True;
}
elsif $meta.support.license {
my-diag "notice license is “$meta.support.license()’, which isn't a SPDX standardized identifier, but license URL was supplied";
$rc = True;
}
else {
my-diag qq:to/END/;
license ‘$meta.license()’ is not one of the standardized SPDX license identifiers.
please use use one of the identifiers from https://spdx.org/licenses/
for the license field or if your license is not on the list,
include a URL to the license text as one of the 'support' keys
in addition to listing its name.
END
$rc = False;
}
}
elsif $licence.is-deprecated-license {
my-diag qq:to/END/;
the licence ‘$meta.license()’ is valid but deprecated, you may want to use another license.
END
}
}
$rc;
}
our sub check-name(META6:D $meta, :$relaxed-name --> Bool) {
my Bool $rc = True;
if $meta.name.defined {
if not $relaxed-name {
my Str $name = $meta.name;
if so $name ~~ /\-/ && $name !~~ /\:\:/ {
$rc = False;
}
}
else {
$rc = True;
}
}
else {
$rc = False;
}
$rc;
}
our sub check-version(META6:D $meta --> Bool ) {
$meta.version.defined && not any($meta.version.parts) eq "*"
}
our sub check-sources(META6:D $meta --> Bool ) {
my $src-count = 0;
for ( $meta.source-url, $meta.support.source ).grep(*.defined) -> $source {
if try URI.new($source) -> $uri {
if $uri.host eq 'github.com' {
if $uri.path ~~ /\.git$/ | /'/archive/' .+? [ '.tar.gz' | '.zip' ] $/ {
$src-count++;
}
else {
my-diag "github source $source needs to end in .git or be a proper release.";
}
}
else {
$src-count++;
}
}
else {
my-diag "source $source is not a valid URI";
}
}
?$src-count;
}
sub meta-candidates() {
@*META-CANDIDATES // <META6.json META.info>;
}
sub dist-dir() {
$*DIST-DIR // test-dir().parent;
}
sub test-dir() {
$*TEST-DIR // $*PROGRAM.parent;
}
}
# vim: expandtab shiftwidth=4 ft=raku
|