File: Util.pm

package info (click to toggle)
libperl-prereqscanner-notquitelite-perl 0.9917-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 944 kB
  • sloc: perl: 5,239; makefile: 2
file content (163 lines) | stat: -rw-r--r-- 4,228 bytes parent folder | download | duplicates (3)
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
package t::Util;

use strict;
use warnings;
use Test::More;
use Perl::PrereqScanner::NotQuiteLite;
use Perl::PrereqScanner::NotQuiteLite::App;
use Exporter qw/import/;
use File::Temp qw/tempdir/;
use File::Basename qw/dirname/;
use File::Path qw/mkpath rmtree/;
use if (-d ".git" and !$ENV{PERL_PSNQL_DEBUG}), "Test::FailWarnings";

our @EXPORT = qw/
  test todo_test used test_app test_file test_cpanfile test_bin
/;
our $EVAL;
our $PARSERS;

sub todo_test {
  SKIP: {
    local $TODO = "FIXME";
    test(@_);
  }
}

sub test {
  my ($description, $string, $expected_requires, $expected_suggests, $expected_recommends, $expected_noes) = @_;
  subtest $description => sub {
    my $scanner = Perl::PrereqScanner::NotQuiteLite->new(
      parsers => $PARSERS || [qw/:bundled/],
      suggests => $expected_suggests ? 1 : 0,
      perl_minimum_version => ($ENV{PERL_PSNQL_MINVER} || 0),
    );
    ok my $context = $scanner->scan_string($string);
    if ($expected_requires) {
      my $requires = $context->requires;
      my $requires_hash = $requires ? $requires->as_string_hash : {};
      is_deeply $requires_hash => $expected_requires, "requires ok";
      note explain $requires_hash;
    }
    if ($expected_suggests) {
      my $suggests = $context->suggests;
      my $suggests_hash = $suggests ? $suggests->as_string_hash : {};
      is_deeply $suggests_hash => $expected_suggests, "suggests ok";
      note explain $suggests_hash;
    }
    if ($expected_recommends) {
      my $recommends = $context->recommends;
      my $recommends_hash = $recommends ? $recommends->as_string_hash : {};
      is_deeply $recommends_hash => $expected_recommends, "recommends ok";
      note explain $recommends_hash;
    }
    if ($expected_noes) {
      my $noes = $context->noes;
      my $noes_hash = $noes ? $noes->as_string_hash : {};
      is_deeply $noes_hash => $expected_noes, "noes ok";
      note explain $noes_hash;
    }
    if ($EVAL) {
      eval "no strict; $string";
      ok !$@, "no eval error";
      note $@ if $@;
    }
    ok !@{$context->{errors} || []}, 'no errors' or note explain $context->{errors};
  };
}

sub used { return {map {$_ => 0} @_} }

sub test_app {
  my ($description, $setup, $args, $expected) = @_;
  note $description;

  my $tmpdir = tempdir(
    'PerlPrereqScannerNQLite_XXXX',
    CLEANUP => 1,
    TMPDIR => 1,
  );
  $setup->($tmpdir);

  my $prereqs = Perl::PrereqScanner::NotQuiteLite::App->new(
    parsers => [':bundled'],
    base_dir => $tmpdir,
    recommends => 1,
    suggests => 1,
    %{$args || {}},
  )->run->as_string_hash;

  for my $phase (sort keys %$expected) {
    for my $type (sort keys %{$expected->{$phase}}) {
      is_deeply $prereqs->{$phase}{$type} => $expected->{$phase}{$type}, "$phase $type ok";
    }
  }
  note explain $prereqs;

  rmtree($tmpdir);
}

sub test_file {
  my ($file, $body) = @_;
  my $dir = dirname($file);
  mkpath($dir) unless -d $dir;
  open my $fh, '>', $file or die "$file: $!";
  print $fh $body;
}

sub test_cpanfile {
  my ($description, $setup, $args, $expected) = @_;
  note $description;

  my $tmpdir = tempdir(
    'PerlPrereqScannerNQLite_XXXX',
    CLEANUP => 1,
    TMPDIR => 1,
  );
  $setup->($tmpdir);

  my $prereqs = Perl::PrereqScanner::NotQuiteLite::App->new(
    parsers => [':bundled'],
    base_dir => $tmpdir,
    recommends => 1,
    suggests => 1,
    save_cpanfile => 1,
    %{$args || {}},
  )->run;

  my $file = "$tmpdir/cpanfile";
  if (ok -f $file, "cpanfile exists") {
    my $got = do { open my $fh, '<', $file; local $/; <$fh> };
    is $got => $expected;
  }

  rmtree($tmpdir);
}

sub test_bin {
  my ($description, $setup, $args, $expected) = @_;
  note $description;

  my $tmpdir = tempdir(
    'PerlPrereqScannerNQLite_XXXX',
    CLEANUP => 1,
    TMPDIR => 1,
  );
  $setup->($tmpdir);

  my $opt = join ' ', @{$args || []};
  my $bin = "bin/scan-perl-prereqs-nqlite";
  SKIP: {
    skip "bin not found", 1 unless -e $bin;
    # note "$^X $bin --base-dir $tmpdir --cpanfile $opt";
    my $got = `$^X $bin --base-dir $tmpdir --cpanfile $opt`;
    $got =~ s/(?:\015\012|\015|\012)/\n/g;
    $got =~ s/\n+$//s;
    $expected =~ s/\n+$//s;
    is $got => $expected;
  }

  rmtree($tmpdir);
}

1;