tests: convert 'if test "$VERBOSE" = yes; then' to test ... &&
[coreutils/ericb.git] / tests / misc / pwd-long
blobc58207abe58d1dbd5391c6b760284bc789eaa0c0
1 #!/bin/sh
2 # -*- perl -*-
3 # Ensure that pwd works even when run from a very deep directory.
5 # Copyright (C) 2006-2010 Free Software Foundation, Inc.
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
20 : ${srcdir=.}
21 . $srcdir/require-perl
23 . $srcdir/test-lib.sh
24 test "$VERBOSE" = yes && env -- pwd --version
26 require_readable_root_
28 ARGV_0=$0
29 export ARGV_0
31 # Don't use CuTmpdir here, since File::Temp's use of rmtree can't
32 # remove the deep tree we create.
33 $PERL -Tw -- - <<\EOF
35 # Show that pwd works even when the length of the resulting
36 # directory name is longer than PATH_MAX.
37 use strict;
39 (my $ME = $ENV{ARGV_0}) =~ s|.*/||;
41 sub normalize_to_cwd_relative ($$$)
43 my ($dir, $dev, $ino) = @_;
44 my $slash = -1;
45 my $next_slash;
46 while (1)
48 $slash = index $dir, '/', $slash + 1;
49 $slash <= -1
50 and die "$ME: $dir does not contain old CWD\n";
51 my $dir_prefix = $slash ? substr ($dir, 0, $slash) : '/';
52 my ($d, $i) = (stat $dir_prefix)[0, 1];
53 $d eq $dev && $i eq $ino
54 and return substr $dir, $slash + 1;
58 # Set up a safe, well-known environment
59 delete @ENV{qw(BASH_ENV CDPATH ENV)};
60 $ENV{IFS} = '';
62 # Taint checking requires a sanitized $PATH. This script performs no $PATH
63 # search, so on most Unix-based systems, it is fine simply to clear $ENV{PATH}.
64 # However, on Cygwin, it's used to find cygwin1.dll, so set it.
65 $ENV{PATH} = '/bin:/usr/bin';
67 # Save CWD's device and inode numbers.
68 my ($dev, $ino) = (stat '.')[0, 1];
70 # Construct the expected "."-relative part of pwd's output.
71 my $z = 'z' x 31;
72 my $n = 256;
73 my $expected = "/$z" x $n;
74 # Remove the leading "/".
75 substr ($expected, 0, 1) = '';
77 my $i = 0;
80 if (!mkdir $z, 0700)
82 warn "$ME: skipping this test; cannot create long directory name "
83 . "at depth $i: $!\n";
84 exit 77;
86 chdir $z
88 until (++$i == $n);
90 my $abs_top_builddir = $ENV{abs_top_builddir};
91 $abs_top_builddir
92 or die "$ME: envvar abs_top_builddir not defined\n";
93 my $build_src_dir = "$abs_top_builddir/src";
94 if ($build_src_dir !~ m!^([-+.:/\w]+)$!)
96 warn "$ME: skipping this test; odd build source directory name:\n"
97 . "$build_src_dir\n";
98 exit 77;
100 $build_src_dir = $1;
102 my $pwd_binary = "$build_src_dir/pwd";
104 -x $pwd_binary
105 or die "$ME: $pwd_binary is not an executable file\n";
106 chomp (my $actual = `$pwd_binary`);
108 # Convert the absolute name from pwd into a $CWD-relative name.
109 # This is necessary in order to avoid a spurious failure when run
110 # from a directory in a bind-mounted partition. What happens is
111 # pwd reads a ".." that contains two or more entries with identical
112 # dev,ino that match the ones we're looking for, and it chooses a
113 # name that does not correspond to the one already recorded in $CWD.
114 $actual = normalize_to_cwd_relative $actual, $dev, $ino;
116 if ($expected ne $actual)
118 my $e_len = length $expected;
119 my $a_len = length $actual;
120 warn "expected len: $e_len\n";
121 warn "actual len: $a_len\n";
122 warn "expected: $expected\n";
123 warn "actual: $actual\n";
124 exit 1;
128 fail=$?
130 Exit $fail