xref: /openbsd-src/gnu/usr.bin/perl/ext/File-Find/t/lib/Testing.pm (revision ff0e7be1ebbcc809ea8ad2b6dafe215824da9e46)
1package Testing;
2use 5.006_001;
3use strict;
4use warnings;
5use Exporter 'import';
6our @EXPORT_OK = qw(
7    create_file_ok
8    mkdir_ok
9    symlink_ok
10    dir_path
11    file_path
12);
13
14# Wrappers around Test::More::ok() for creation of files, directories and
15# symlinks used in testing of File-Find
16
17*ok = \&Test::More::ok;
18
19sub create_file_ok($;$) {
20    my $file = $_[0];
21    my $msg = $_[2] || "able to create file: $file";
22    ok( open(my $T,'>',$file), $msg )
23        or die("Unable to create file: $file");
24}
25
26sub mkdir_ok($$;$) {
27    my ($dir, $mask) = @_[0..1];
28    my $msg = $_[2] || "able to mkdir: $dir";
29    ok( mkdir($dir, $mask), $msg )
30        or die("Unable to mkdir $!: $dir");
31}
32
33sub symlink_ok($$;$) {
34    my ($oldfile, $newfile) = @_[0..1];
35    my $msg = $_[2] || "able to symlink from $oldfile to $newfile";
36    ok( symlink( $oldfile, $newfile ), $msg)
37      or die("Unable to symlink from $oldfile to $newfile");
38}
39
40# Use dir_path() to specify a directory path that is expected for
41# $File::Find::dir (%Expect_Dir). Also use it in file operations like
42# chdir, rmdir etc.
43#
44# dir_path() concatenates directory names to form a *relative*
45# directory path, independent from the platform it is run on, although
46# there are limitations. Do not try to create an absolute path,
47# because that may fail on operating systems that have the concept of
48# volume names (e.g. Mac OS). As a special case, you can pass it a "."
49# as first argument, to create a directory path like "./fa/dir". If there is
50# no second argument, this function will return "./"
51
52sub dir_path {
53    my $first_arg = shift @_;
54
55    if ($first_arg eq '.') {
56	    return './' unless @_;
57	    my $path = File::Spec->catdir(@_);
58	    # add leading "./"
59	    $path = "./$path";
60	    return $path;
61    }
62    else { # $first_arg ne '.'
63        return $first_arg unless @_; # return plain filename
64	    my $fname = File::Spec->catdir($first_arg, @_); # relative path
65	    $fname = VMS::Filespec::unixpath($fname) if $^O eq 'VMS';
66        return $fname;
67    }
68}
69
70# Use file_path() to specify a file path that is expected for $_
71# (%Expect_File). Also suitable for file operations like unlink etc.
72#
73# file_path() concatenates directory names (if any) and a filename to
74# form a *relative* file path (the last argument is assumed to be a
75# file). It is independent from the platform it is run on, although
76# there are limitations. As a special case, you can pass it a "." as
77# first argument, to create a file path like "./fa/file" on operating
78# systems. If there is no second argument, this function will return the
79# string "./"
80
81sub file_path {
82    my $first_arg = shift @_;
83
84    if ($first_arg eq '.') {
85	    return './' unless @_;
86	    my $path = File::Spec->catfile(@_);
87	    # add leading "./"
88	    $path = "./$path";
89	    return $path;
90    }
91    else { # $first_arg ne '.'
92        return $first_arg unless @_; # return plain filename
93	    my $fname = File::Spec->catfile($first_arg, @_); # relative path
94	    $fname = VMS::Filespec::unixify($fname) if $^O eq 'VMS';
95        return $fname;
96    }
97}
98
991;
100