blob: 036fd48ebf1b3a9d469f87f64a8435de62693248 [file] [log] [blame]
Divya Kotharia0f56be2014-06-26 07:25:20 -05001#!/bin/bash
2
Rob Landley9b93dd32016-03-30 03:20:13 -05003# TODO: needs root to mount tmpfs to test moving across filesystems.
4# check handling of chattr +i immutable bit
5# "touch two; chmod -w two; mv one two" shouldn't prompt to delete two if
6# one doesn't exist.
7
Divya Kotharia0f56be2014-06-26 07:25:20 -05008# Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
9# Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>
10
11[ -f testing.sh ] && . testing.sh
12
13#testing "name" "command" "result" "infile" "stdin"
14
15touch file
Andy Chu7a0186c2016-03-19 00:34:42 -070016testing "file to file" \
17 "mv file file1 && [ ! -e file -a -f file1 ] && echo yes" \
18 "yes\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -050019rm -f file*
20
21touch file
22mkdir dir
Andy Chu7a0186c2016-03-19 00:34:42 -070023testing "file to dir" \
24 "mv file dir && [ ! -e file -a -f dir/file ] && echo yes" \
25 "yes\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -050026rm -rf file* dir*
27
28mkdir dir
Andy Chu7a0186c2016-03-19 00:34:42 -070029testing "dir to dir" \
30 "mv dir dir1 && [ ! -e dir -a -d dir1 ] && echo yes" \
31 "yes\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -050032rm -rf dir*
33
34mkdir dir1 dir2
35touch file1 file2 dir1/file3
36ln -s file1 link1
Andy Chu7a0186c2016-03-19 00:34:42 -070037testing "multiple files/dirs to a dir" \
38 "mv file1 file2 link1 dir1 dir2 &&
39 [ ! -e file1 -a ! -e file2 -a ! -e link1 -a ! -e dir1 ] &&
40 [ -f dir2/file1 -a -f dir2/file2 -a -L dir2/link1 -a -d dir2/dir1 ] &&
41 [ -f dir2/dir1/file3 ] && readlink dir2/link1" \
42 "file1\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -050043rm -rf file* link* dir*
44
Divya Kotharia0f56be2014-06-26 07:25:20 -050045dd if=/dev/zero of=file1 seek=10k count=1 >/dev/null 2>&1
Andy Chu7a0186c2016-03-19 00:34:42 -070046testing "random file to new file" \
47 "mv file1 file2 && [ ! -e file1 -a -f file2 ] && stat -c %s file2" \
48 "5243392\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -050049rm -f file*
50
51touch file1
52ln -s file1 link1
Andy Chu7a0186c2016-03-19 00:34:42 -070053testing "symlink to new symlink" \
54 "mv link1 link2 && [ ! -e link1 -a -L link2 ] && readlink link2" \
55 "file1\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -050056unlink tLink2 &>/dev/null
57rm -f file* link*
58
59touch file1
60ln file1 link1
Andy Chu7a0186c2016-03-19 00:34:42 -070061testing "hard link to new hardlink" \
62 "mv link1 link2 && [ ! -e link1 -a -f link2 -a file1 -ef link2 ] && echo yes" \
63 "yes\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -050064unlink link2 &>/dev/null
65rm -f file* link*
66
67touch file1
68chmod a-r file1
Andy Chu7a0186c2016-03-19 00:34:42 -070069testing "file to unreadable file" \
70 "mv file1 file2 && [ ! -e file1 -a -f file2 ] && echo yes" \
71 "yes\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -050072rm -f file*
73
74touch file1
75ln file1 link1
76mkdir dir1
Andy Chu7a0186c2016-03-19 00:34:42 -070077testing "file hardlink dir" \
78 "mv file1 link1 dir1 &&
79 [ ! -e file1 -a ! -e link1 -a -f dir1/file1 -a -f dir1/link1 ] &&
80 [ dir1/file1 -ef dir1/link1 ] && echo yes" \
81 "yes\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -050082rm -rf file* link* dir*
83
84mkdir -p dir1/dir2 dir3
85touch dir1/dir2/file1 dir1/dir2/file2
Andy Chu7a0186c2016-03-19 00:34:42 -070086testing "dir to new dir" \
87 "mv dir1/dir2 dir3/new &&
88 [ ! -e dir1/dir2 -a -d dir3/new -a -f dir3/new/file1 ] &&
89 [ -f dir3/new/file2 ] && echo yes" \
90 "yes\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -050091rm -rf file* dir*
92
93mkdir dir1 dir2
Andy Chu7a0186c2016-03-19 00:34:42 -070094testing "dir to existing dir" \
95 "mv dir1 dir2 && [ ! -e dir1 -a -d dir2/dir1 ] && echo yes" \
96 "yes\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -050097rm -rf dir*
98
99touch file1 file2
Andy Chu7a0186c2016-03-19 00:34:42 -0700100chmod 400 file1 file2
101testing "force over unwritable" \
102 "mv -f file1 file2 && [ ! -e file1 -a -e file2 ] && echo yes" \
103 "yes\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -0500104rm -f file*
105
106touch file1 file2
Andy Chu7a0186c2016-03-19 00:34:42 -0700107testing "no clobber (dest exists)" \
108 "mv -n file1 file2 && [ -e file1 -a -e file2 ] && echo yes"\
109 "yes\n" "" ""
110rm -f file*
111
112touch file1
113testing "no clobber (dest doesn't exist)" \
114 "mv -n file1 new-dest && [ ! -e file1 -a -e new-dest ] && echo yes"\
115 "yes\n" "" ""
116rm -f file*
117
118# If there is stdin, it prompts. If no stdin, it moves anyway and file2 won't
119# exist.
120touch file1 file2
121chmod 400 file1 file2
122testing "mv over unwritable file: no stdin" \
123 "mv file2 file1 && [ -e file1 -a ! -e file2 ] && echo yes" \
124 "yes\n" "" ""
Divya Kotharia0f56be2014-06-26 07:25:20 -0500125rm -f file*
Elliott Hughesf1f20b92015-08-30 06:35:30 -0500126
127touch file1 file2
128chmod 400 file1 file2
Andy Chu7a0186c2016-03-19 00:34:42 -0700129testing "mv over unwritable file: answered YES" \
130 "mv file2 file1 && [ -e file1 -a ! -e file2 ] && echo yes" \
131 "yes\n" "" "y\n"
132rm -f file*
133
134touch file1 file2
135chmod 400 file1 file2
136testing "mv over unwritable file: answered NO" \
137 "mv file2 file1 && [ -e file1 -a -e file2 ] && echo yes" \
138 "yes\n" "" "n\n"
139rm -f file*
140
141touch file1 file2
142testing "interactive: no stdin" \
143 "mv -i file2 file1 && [ -e file1 -a ! -e file2 ] && echo yes" \
144 "yes\n" "" ""
145rm -f file*
146
147touch file1 file2
148testing "interactive: answered YES" \
149 "mv -i file2 file1 && [ -e file1 -a ! -e file2 ] && echo yes" \
150 "yes\n" "" "y\n"
151rm -f file*
152
153touch file1 file2
154testing "interactive: answered NO" \
155 "mv -i file2 file1 && [ -e file1 -a -e file2 ] && echo yes" \
156 "yes\n" "" "n\n"
Elliott Hughesf1f20b92015-08-30 06:35:30 -0500157rm -f file*