File: Lib.pm

package info (click to toggle)
libtest-lib-perl 0.003-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 132 kB
  • sloc: perl: 32; makefile: 2
file content (72 lines) | stat: -rw-r--r-- 1,685 bytes parent folder | download
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
package Test::Lib;
use strict;
use warnings;

our $VERSION = '0.003';
$VERSION =~ tr/_/d/;

use File::Spec;
use Cwd ();
use lib ();

sub import {
    my $class = shift;
    my $dir = shift;
    if (! defined $dir) {
        my $file = File::Spec->rel2abs((caller)[1]);
        $dir = File::Spec->catpath((File::Spec->splitpath($file))[0,1], '');
    }
    for my $i (0..5) {
        my $tdir = File::Spec->catdir($dir, (File::Spec->updir) x $i);
        my $abs_path = Cwd::abs_path($tdir);
        my $dirname = (File::Spec->splitdir($abs_path))[-1];

        if ($dirname eq 't') {
            my $tlib = File::Spec->catdir($tdir, 'lib');
            if (-d $tlib) {
                lib->import($tlib);
                return;
            }
        }
    }
    require Carp;
    Carp::croak("unable to find t/lib directory in $dir");
}

1;

__END__

=head1 NAME

Test::Lib - Use libraries from a t/lib directory

=head1 SYNOPSIS

    use Test::Lib;
    use Test::More;
    use Private::Testing::Module;

    ok 1, 'passing test';
    my_test 'test from private module';

=head1 DESCRIPTION

Searches upward from the calling module for a directory F<t> with
a F<lib> directory inside it, and adds it to the module search path.
Looks upward up to 5 directories.  This is intended to be used in
test modules either directly in F<t> or in a subdirectory to find
their included testing libraries located in F<t/lib>.

=head1 AUTHOR

Graham Knop <haarg@haarg.org>

=head1 COPYRIGHT AND LICENSE

This software is Copyright (c) 2012 by Graham Knop.

This is free software; you can redistribute it and/or modify it under the same
terms as the Perl 5 programming language system itself.

=cut