1#!/usr/bin/perl -w
2
3BEGIN {
4    unshift @INC, 't/lib';
5}
6
7use strict;
8use warnings;
9use TAP::Harness;
10use Test::More;
11use File::Path qw/mkpath rmtree/;
12use File::Spec::Functions qw/catdir catfile rel2abs/;
13
14for my $path (@INC) {
15       $path = rel2abs($path);
16}
17
18if ( eval { require CPAN::Meta::YAML; 1 } ) {
19    plan tests => 4;
20}
21else {
22    plan skip_all => "requires CPAN::Meta::YAML";
23}
24
25# create temp directories long-hand
26# XXX should we add File::Temp as a prereq to do this?
27my $initial_dir = rel2abs(".");
28my $work_dir = catdir($initial_dir, "tmp" . int(rand(2**31)));
29my $t_dir = catdir($work_dir, 't');
30mkpath($t_dir) or die "Could not create $t_dir: $!";
31chdir $work_dir;
32
33# clean up at the end, but only if we didn't skip
34END { if ($initial_dir) {chdir $initial_dir; rmtree($work_dir) } }
35
36# Create test rules in t
37{
38    open my $fh, ">", catfile($t_dir, "testrules.yml");
39    print {$fh} <<'HERE';
40---
41par: t/p*.t
42HERE
43    close $fh;
44}
45
46my $th = TAP::Harness->new;
47my $exp = {
48    par => 't/p*.t'
49};
50is_deeply( $th->rules, $exp, "rules set from t/testrules.yml" );
51
52# Create test rules in dist root
53{
54    open my $fh, ">", catfile($work_dir, "testrules.yml");
55    print {$fh} <<'HERE';
56---
57seq:
58- seq: t/p*.t
59- par: '**'
60HERE
61    close $fh;
62}
63
64$th = TAP::Harness->new;
65$exp = {
66    seq => [
67        { seq => 't/p*.t' },
68        { par => '**' },
69    ],
70};
71is_deeply( $th->rules, $exp, "root testrules.yml overrides t/testrules.yml" );
72
73# Create alternately named file
74my $altrules = catfile($work_dir, "myrules.yml");
75{
76    open my $fh, ">", $altrules;
77    print {$fh} <<'HERE';
78---
79seq: **
80HERE
81    close $fh;
82}
83
84{
85    local $ENV{HARNESS_RULESFILE} = $altrules;
86    $th = TAP::Harness->new;
87    $exp = {
88        seq => '**'
89    };
90    is_deeply( $th->rules, $exp, "HARNESS_RULESFILE overrides testrules.yml" );
91}
92
93$th = TAP::Harness->new( { rulesfile => $altrules} );
94$exp = {
95    seq => '**'
96};
97is_deeply( $th->rules, $exp, "rulesfile param overrides testrules.yml" );
98