INSTALL.md: move clean-up to a separate section
[gnu-stow.git] / t / find_stowed_path.t
blob8ae4fca9c6c984be6cfb1fa6a7fe6d080ef5b1f3
1 #!/usr/bin/perl
3 # This file is part of GNU Stow.
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.
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.
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/.
19 # Testing Stow:: find_stowed_path()
22 use strict;
23 use warnings;
25 use Test::More tests => 10;
27 use testutil;
28 use Stow::Util qw(set_debug_level);
30 init_test_dirs();
32 subtest("find link to a stowed path with relative target" => sub {
33 plan tests => 3;
35 # This is a relative path, unlike $ABS_TEST_DIR below.
36 my $target = "$TEST_DIR/target";
38 my $stow = new_Stow(dir => "$TEST_DIR/stow", target => $target);
39 my ($path, $stow_path, $package) =
40 $stow->find_stowed_path("a/b/c", "../../../stow/a/b/c");
41 is($path, "../stow/a/b/c", "path");
42 is($stow_path, "../stow", "stow path");
43 is($package, "a", "package");
44 });
46 my $stow = new_Stow(dir => "$ABS_TEST_DIR/stow", target => "$ABS_TEST_DIR/target");
48 # Required by creation of stow2 and stow2/.stow below
49 cd("$ABS_TEST_DIR/target");
51 subtest("find link to a stowed path" => sub {
52 plan tests => 3;
53 my ($path, $stow_path, $package) =
54 $stow->find_stowed_path("a/b/c", "../../../stow/a/b/c");
55 is($path, "../stow/a/b/c", "path from target directory");
56 is($stow_path, "../stow", "stow path from target directory");
57 is($package, "a", "from target directory");
58 });
60 subtest("find link to alien path not owned by Stow" => sub {
61 plan tests => 3;
62 my ($path, $stow_path, $package) =
63 $stow->find_stowed_path("a/b/c", "../../alien");
64 is($path, "", "alien is not stowed, so path is empty");
65 is($stow_path, "", "alien, so stow path is empty");
66 is($package, "", "alien is not stowed in any package");
67 });
69 # Make a second stow directory within the target directory, so that we
70 # can check that links to package files within that stow directory are
71 # detected correctly.
72 make_path("stow2");
74 # However this second stow directory is still "alien" to stow until we
75 # put a .stow file in it. So first test a symlink pointing to a path
76 # within this second stow directory
77 subtest("second stow dir still alien without .stow" => sub {
78 plan tests => 3;
79 my ($path, $stow_path, $package) =
80 $stow->find_stowed_path("a/b/c", "../../stow2/a/b/c");
81 is($path, "", "stow2 not a stow dir yet, so path is empty");
82 is($stow_path, "", "stow2 not a stow dir yet so stow path is empty");
83 is($package, "", "not stowed in any recognised package yet");
84 });
86 # Now make stow2 a secondary stow directory and test that
87 make_file("stow2/.stow");
89 subtest(".stow makes second stow dir owned by Stow" => sub {
90 plan tests => 3;
91 my ($path, $stow_path, $package) =
92 $stow->find_stowed_path("a/b/c", "../../stow2/a/b/c");
93 is($path, "stow2/a/b/c", "path");
94 is($stow_path, "stow2", "stow path");
95 is($package, "a", "detect alternate stow directory");
96 });
98 subtest("relative symlink pointing to target dir" => sub {
99 plan tests => 3;
100 my ($path, $stow_path, $package) =
101 $stow->find_stowed_path("a/b/c", "../../..");
102 # Technically the target dir is not owned by Stow, since
103 # Stow won't touch the target dir itself, only its contents.
104 is($path, "", "path");
105 is($stow_path, "", "stow path");
106 is($package, "", "corner case - link points to target dir");
109 subtest("relative symlink pointing to parent of target dir" => sub {
110 plan tests => 3;
111 my ($path, $stow_path, $package) =
112 $stow->find_stowed_path("a/b/c", "../../../..");
113 is($path, "", "path");
114 is($stow_path, "", "stow path");
115 is($package, "", "corner case - link points to parent of target dir");
118 subtest("unowned symlink pointing to absolute path inside target" => sub {
119 plan tests => 3;
120 my ($path, $stow_path, $package) =
121 $stow->find_stowed_path("a/b/c", "$ABS_TEST_DIR/target/d");
122 is($path, "", "path");
123 is($stow_path, "", "stow path");
124 is($package, "", "symlink unowned by Stow points to absolute path outside target directory");
127 subtest("unowned symlink pointing to absolute path outside target" => sub {
128 plan tests => 3;
129 my ($path, $stow_path, $package) =
130 $stow->find_stowed_path("a/b/c", "/dev/null");
131 is($path, "", "path");
132 is($stow_path, "", "stow path");
133 is($package, "", "symlink unowned by Stow points to absolute path outside target directory");
136 # Now make stow2 the primary stow directory and test that it still
137 # works when the stow directory is under the target directory
138 $stow->set_stow_dir("$ABS_TEST_DIR/target/stow2");
140 subtest("stow2 becomes the primary stow directory" => sub {
141 plan tests => 3;
143 my ($path, $stow_path, $package) =
144 $stow->find_stowed_path("a/b/c", "../../stow2/a/b/c");
145 is($path, "stow2/a/b/c", "path in stow2");
146 is($stow_path, "stow2", "stow path for stow2");
147 is($package, "a", "stow2 is subdir of target directory");