Skip to content

Commit

Permalink
Add QA objects and first couple of META6 checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyil committed Apr 26, 2018
1 parent 7ef38be commit d943b71
Show file tree
Hide file tree
Showing 6 changed files with 297 additions and 0 deletions.
5 changes: 5 additions & 0 deletions META6.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"File::Which:ver<1.0.0+>",
"JSON::Fast:ver<0.9.11+>",
"MIME::Base64:ver<1.2.1+>",
"PostCocoon::Url",
"SemVer:ver<0.1.3+>",
"Template::Mustache:ver<1.0.1+>"
],
Expand All @@ -22,6 +23,10 @@
"Dist::Helper::Clean": "lib/Dist/Helper/Clean.pm6",
"Dist::Helper::Meta": "lib/Dist/Helper/Meta.pm6",
"Dist::Helper::Path": "lib/Dist/Helper/Path.pm6",
"Dist::Helper::QA": "lib/Dist/Helper/QA.pm6",
"Dist::Helper::QA::Check": "lib/Dist/Helper/QA/Check.pm6",
"Dist::Helper::QA::Checks::Meta": "lib/Dist/Helper/QA/Checks/Meta.pm6",
"Dist::Helper::QA::Result": "lib/Dist/Helper/QA/Result.pm6",
"Dist::Helper::Template": "lib/Dist/Helper/Template.pm6"
},
"resources": [
Expand Down
52 changes: 52 additions & 0 deletions lib/Dist/Helper/QA.pm6
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#! /usr/bin/env false

use v6.c;

use Dist::Helper::QA::Checks::Meta;

unit module Dist::Helper::QA;

multi sub dist-qa (
IO::Path:D $path,
*%checks,
Bool:D :$skip-rest = False,
--> Hash
) is export {
sub include-check(
Str:D $name
) {
if (%checks{$name}:!exists) {
return !$skip-rest;
}

return ?%checks{$name};
}

my IO::Path $cwd = $*CWD;
chdir $path;

my %todo;

if (include-check("meta")) { %todo<meta> = Dist::Helper::QA::Checks::Meta }

my %results;

for %todo.kv -> $scope, $check {
%results{$scope} = %todo{$scope}.check;
}

chdir $cwd;

%results;
}

multi sub dist-qa (
Str:D $path = ".",
*%checks,
Bool:D :$skip-rest = False,
--> Hash
) is export {
dist-qa($path.IO, |%checks, :$skip-rest);
}

# vim: ft=perl6 noet
14 changes: 14 additions & 0 deletions lib/Dist/Helper/QA/Check.pm6
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#! /usr/bin/env false

use v6.c;

class Dist::Helper::QA::Check
{
method check (
--> Iterable
) {
*
}
}

# vim: ft=perl6 noet
156 changes: 156 additions & 0 deletions lib/Dist/Helper/QA/Checks/Meta.pm6
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
#! /usr/bin/env false

use v6.c;

use Dist::Helper::Meta;
use Dist::Helper::QA::Check;
use Dist::Helper::QA::Result;
use PostCocoon::Url;

class Dist::Helper::QA::Checks::Meta is Dist::Helper::QA::Check
{
method check (
--> Iterable
) {
try {
CATCH {
default {
return [
Dist::Helper::QA::Result.new(
description => "Distribution contains a valid META6.json"
).failure: "Failed parsing META6.json";
];
}
}

my %meta = get-meta;

[
self!required-fields(%meta),
self!recommended-fields(%meta),
self!correct-value-types(%meta),
self!semantic-version(%meta),
self!unused-deps(%meta),
self!dependency-version-adverbs(%meta),
]
}
}

method !correct-value-types (
%meta,
--> Dist::Helper::QA::Result
) {
my Dist::Helper::QA::Result $result .= new(
description => "META6.json uses the correct types for all used fields",
);

my %types =
api => Str,
auth => Str,
authors => Array,
build-depends => Hash,
depends => Hash,
description => Str,
license => Str, # TODO: Check for valid license
name => Str,
perl => Str,
provides => Hash,
resources => Hash,
source-url => "Url",
tags => Array,
test-depends => Hash,
version => Str,
;

for %types.kv -> $field, $type {
next unless %meta{$field}:exists;

given $type {
when "Url" {
if (!is-valid-url(%meta{$field})) {
$result.failure: "$field is of the wrong type, should be $type";
}
}
default {
if (%meta{$field} !~~ $type) {
$result.failure: "$field is of the wrong type, should be $type";
}
}
}
}
}

method !dependency-version-adverbs (
--> Dist::Helper::QA::Result
) {
}

method !recommended-fields (
%meta,
--> Dist::Helper::QA::Result
) {
my @fields = <
api
auth
authors
resources
source-url
tags
test-depends
>;

my Dist::Helper::QA::Result $result .= new(
description => "META6.json contains all recommended fields",
);

for @fields -> $field {
if (%meta{$field}:!exists) {
$result.failure: "Missing recommended field $field";
}
}

$result;
}

method !required-fields (
%meta,
--> Dist::Helper::QA::Result
) {
my @fields = <
depends
description
license
name
perl
provides
version
>;

my Dist::Helper::QA::Result $result .= new(
description => "META6.json contains all required fields",
);

for @fields -> $field {
if (%meta{$field}:!exists) {
$result.failure: "Missing required field $field";
}
}

$result;
}

method !semantic-version (
--> Dist::Helper::QA::Result
) {
}

method !unused-deps (
--> Dist::Helper::QA::Result
) {
}
}

# vim: ft=perl6 noet
32 changes: 32 additions & 0 deletions lib/Dist/Helper/QA/Result.pm6
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#! /usr/bin/env false

use v6.c;

class Dist::Helper::QA::Result
{
has Bool $.passed = True;
has Str $.description;
has Str @.reasons;

method failure (
Str:D $reason,
--> Dist::Helper::QA::Result
) {
$!passed = False;
@!reasons.push: $reason;

self;
}

method Str
{
my Str $check-box = $.passed
?? "[x]"
!! "[ ]"
;

"$check-box $.message";
}
}

# vim: ft=perl6 noet
38 changes: 38 additions & 0 deletions t/01-qa-meta.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#! /usr/bin/env perl6

use v6.c;

use Test;
use Dist::Helper::QA;
use Dist::Helper::QA::Result;

plan 2;

subtest "Check empty directory" => {
plan 5;

my %qa = dist-qa("t/repos/empty");

ok %qa<meta>:exists, "meta key exists";
is %qa<meta>.elems, 1, "meta contains 1 element";
nok %qa<meta>[0].passed, "meta[0] did not pass";
is %qa<meta>[0].reasons.elems, 1, "meta[0] has 1 failure reason";

subtest "Types" => {
plan 3;

isa-ok %qa<meta>[0], Dist::Helper::QA::Result, "meta[0] is a Dist::Helper::QA::Result";
isa-ok %qa<meta>[0].description, Str, "meta[0].description is a Str";
isa-ok %qa<meta>[0].reasons[0], Str, "meta[0].reasons[0] is a Str";
}
}

subtest "Ignore checks" => {
plan 1;

my %qa = dist-qa("t/repos/empty", :!meta);

ok %qa<meta>:!exists, "meta key does not exist";
}

# vim: ft=perl6 noet

0 comments on commit d943b71

Please sign in to comment.