dpkg (1.18.25) stretch; urgency=medium
[dpkg] / scripts / t / Dpkg_Path.t
CommitLineData
1479465f
GJ
1#!/usr/bin/perl
2#
3# This program is free software; you can redistribute it and/or modify
4# it under the terms of the GNU General Public License as published by
5# the Free Software Foundation; either version 2 of the License, or
6# (at your option) any later version.
7#
8# This program is distributed in the hope that it will be useful,
9# but WITHOUT ANY WARRANTY; without even the implied warranty of
10# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11# GNU General Public License for more details.
12#
13# You should have received a copy of the GNU General Public License
14# along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16use strict;
17use warnings;
18
19use Test::More tests => 16;
20
21use_ok('Dpkg::Path', 'canonpath', 'resolve_symlink',
22 'check_files_are_the_same', 'get_pkg_root_dir',
23 'guess_pkg_root_dir', 'relative_to_pkg_root');
24
25my $tmpdir = 't.tmp/Dpkg_Path';
26
27mkdir $tmpdir;
28mkdir "$tmpdir/a";
29mkdir "$tmpdir/a/b";
30mkdir "$tmpdir/a/b/c";
31mkdir "$tmpdir/a/DEBIAN";
32mkdir "$tmpdir/debian";
33mkdir "$tmpdir/debian/a";
34mkdir "$tmpdir/debian/a/b";
35mkdir "$tmpdir/debian/a/b/c";
36symlink 'a/b/c', "$tmpdir/cbis";
37symlink '/this/does/not/exist', "$tmpdir/tmp";
38symlink '.', "$tmpdir/here";
39
40is(canonpath("$tmpdir/./a///b/c"), "$tmpdir/a/b/c", 'canonpath basic test');
41is(canonpath("$tmpdir/a/b/../../a/b/c"), "$tmpdir/a/b/c", 'canonpath and ..');
42is(canonpath("$tmpdir/a/b/c/../../"), "$tmpdir/a", 'canonpath .. at end');
43is(canonpath("$tmpdir/cbis/../"), "$tmpdir/cbis/..", 'canonpath .. after symlink');
44
45is(resolve_symlink("$tmpdir/here/cbis"), "$tmpdir/here/a/b/c", 'resolve_symlink');
46is(resolve_symlink("$tmpdir/tmp"), '/this/does/not/exist', 'resolve_symlink absolute');
47is(resolve_symlink("$tmpdir/here"), $tmpdir, 'resolve_symlink .');
48
49ok(!check_files_are_the_same("$tmpdir/here", $tmpdir), 'Symlink is not the same!');
50ok(check_files_are_the_same("$tmpdir/here/a", "$tmpdir/a"), 'Same directory');
51
52is(get_pkg_root_dir("$tmpdir/a/b/c"), "$tmpdir/a", 'get_pkg_root_dir');
53is(guess_pkg_root_dir("$tmpdir/a/b/c"), "$tmpdir/a", 'guess_pkg_root_dir');
54is(relative_to_pkg_root("$tmpdir/a/b/c"), 'b/c', 'relative_to_pkg_root');
55
56chdir($tmpdir);
57
58is(get_pkg_root_dir('debian/a/b/c'), undef, 'get_pkg_root_dir undef');
59is(relative_to_pkg_root('debian/a/b/c'), undef, 'relative_to_pkg_root undef');
60is(guess_pkg_root_dir('debian/a/b/c'), 'debian/a', 'guess_pkg_root_dir fallback');