1#!/usr/bin/perl
2#
3# This file is part of GNU Stow.
4#
5# GNU Stow is free software: you can redistribute it and/or modify it
6# under the terms of the GNU General Public License as published by
7# the Free Software Foundation, either version 3 of the License, or
8# (at your option) any later version.
9#
10# GNU Stow is distributed in the hope that it will be useful, but
11# WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13# General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program. If not, see https://www.gnu.org/licenses/.
17
18#
19# Testing find_stowed_path()
20#
21
22use strict;
23use warnings;
24
25use Test::More tests => 18;
26
27use testutil;
28use Stow::Util qw(set_debug_level);
29
30init_test_dirs();
31
32my $stow = new_Stow(dir => "$TEST_DIR/stow");
33#set_debug_level(4);
34
35my ($path, $stow_path, $package) =
36    $stow->find_stowed_path("$TEST_DIR/target/a/b/c", "../../../stow/a/b/c");
37is($path, "$TEST_DIR/stow/a/b/c", "path");
38is($stow_path, "$TEST_DIR/stow", "stow path");
39is($package, "a", "package");
40
41cd("$TEST_DIR/target");
42$stow->set_stow_dir("../stow");
43($path, $stow_path, $package) =
44    $stow->find_stowed_path("a/b/c", "../../../stow/a/b/c");
45is($path, "../stow/a/b/c", "path from target directory");
46is($stow_path, "../stow", "stow path from target directory");
47is($package, "a", "from target directory");
48
49make_path("stow");
50cd("../..");
51$stow->set_stow_dir("$TEST_DIR/target/stow");
52
53($path, $stow_path, $package) =
54    $stow->find_stowed_path("$TEST_DIR/target/a/b/c", "../../stow/a/b/c");
55is($path, "$TEST_DIR/target/stow/a/b/c", "path");
56is($stow_path, "$TEST_DIR/target/stow", "stow path");
57is($package, "a", "stow is subdir of target directory");
58
59($path, $stow_path, $package) =
60    $stow->find_stowed_path("$TEST_DIR/target/a/b/c", "../../empty");
61is($path, "", "empty path");
62is($stow_path, "", "empty stow path");
63is($package, "", "target is not stowed");
64
65make_path("$TEST_DIR/target/stow2");
66make_file("$TEST_DIR/target/stow2/.stow");
67
68($path, $stow_path, $package) =
69    $stow->find_stowed_path("$TEST_DIR/target/a/b/c","../../stow2/a/b/c");
70is($path, "$TEST_DIR/target/stow2/a/b/c", "path");
71is($stow_path, "$TEST_DIR/target/stow2", "stow path");
72is($package, "a", "detect alternate stow directory");
73
74# Possible corner case with rogue symlink pointing to ancestor of
75# stow dir.
76($path, $stow_path, $package) =
77    $stow->find_stowed_path("$TEST_DIR/target/a/b/c","../../..");
78is($path, "", "path");
79is($stow_path, "", "stow path");
80is($package, "", "corner case - link points to ancestor of stow dir");
81