aboutsummaryrefslogtreecommitdiff
path: root/tests/mv.test
blob: f1f4a4f94b24f110c2e20beeb570b6e9ffb59e50 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#!/bin/bash

# TODO: needs root to mount tmpfs to test moving across filesystems.
# check handling of chattr +i immutable bit
# "touch two; chmod -w two; mv one two" shouldn't prompt to delete two if
#   one doesn't exist.

# Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
# Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>

[ -f testing.sh ] && . testing.sh

#testing "name" "command" "result" "infile" "stdin"

touch file
testing "file to file" \
  "mv file file1 && [ ! -e file -a -f file1 ] && echo yes" \
  "yes\n" "" ""
rm -f file*

touch file
mkdir dir
testing "file to dir" \
  "mv file dir && [ ! -e file -a -f dir/file ] && echo yes" \
  "yes\n" "" ""
rm -rf file* dir*

mkdir dir
testing "dir to dir" \
  "mv dir dir1 && [ ! -e dir -a -d dir1 ] && echo yes" \
  "yes\n" "" ""
rm -rf dir*

mkdir dir1 dir2
touch file1 file2 dir1/file3
ln -s file1 link1
testing "multiple files/dirs to a dir" \
  "mv file1 file2 link1 dir1 dir2 &&
  [ ! -e file1 -a ! -e file2 -a ! -e link1 -a ! -e dir1 ] &&
  [ -f dir2/file1 -a -f dir2/file2 -a -L dir2/link1 -a -d dir2/dir1 ] &&
  [ -f dir2/dir1/file3 ] && readlink dir2/link1" \
  "file1\n" "" ""
rm -rf file* link* dir*

dd if=/dev/zero of=file1 seek=10k count=1 >/dev/null 2>&1
testing "random file to new file" \
  "mv file1 file2 && [ ! -e file1 -a -f file2 ] && stat -c %s file2" \
  "5243392\n" "" ""
rm -f file*

touch file1
ln -s file1 link1
testing "symlink to new symlink" \
  "mv link1 link2 && [ ! -e link1 -a -L link2 ] && readlink link2" \
  "file1\n" "" ""
unlink tLink2 &>/dev/null
rm -f file* link*

touch file1
ln file1 link1
testing "hard link to new hardlink" \
  "mv link1 link2 && [ ! -e link1 -a -f link2 -a file1 -ef link2 ] && echo yes" \
  "yes\n" "" ""
unlink link2 &>/dev/null
rm -f file* link*

touch file1
chmod a-r file1
testing "file to unreadable file" \
  "mv file1 file2 && [ ! -e file1 -a -f file2 ] && echo yes" \
  "yes\n" "" ""
rm -f file*

touch file1
ln file1 link1
mkdir dir1
testing "file hardlink dir" \
  "mv file1 link1 dir1 &&
  [ ! -e file1 -a ! -e link1 -a -f dir1/file1 -a -f dir1/link1 ] &&
  [ dir1/file1 -ef dir1/link1 ] && echo yes" \
  "yes\n" "" ""
rm -rf file* link* dir*

mkdir -p dir1/dir2 dir3
touch dir1/dir2/file1 dir1/dir2/file2
testing "dir to new dir" \
  "mv dir1/dir2 dir3/new &&
  [ ! -e dir1/dir2 -a -d dir3/new -a -f dir3/new/file1 ] &&
  [ -f dir3/new/file2 ] && echo yes" \
  "yes\n" "" ""
rm -rf file* dir*

mkdir dir1 dir2
testing "dir to existing dir" \
  "mv dir1 dir2 && [ ! -e dir1 -a -d dir2/dir1 ] && echo yes" \
  "yes\n" "" ""
rm -rf dir*

touch file1 file2
chmod 400 file1 file2
testing "force over unwritable" \
  "mv -f file1 file2 && [ ! -e file1 -a -e file2 ] && echo yes" \
  "yes\n" "" ""
rm -f file*

touch file1 file2
testing "no clobber (dest exists)" \
  "mv -n file1 file2 && [ -e file1 -a -e file2 ] && echo yes"\
  "yes\n" "" ""
rm -f file*

touch file1
testing "no clobber (dest doesn't exist)" \
  "mv -n file1 new-dest && [ ! -e file1 -a -e new-dest ] && echo yes"\
  "yes\n" "" ""
rm -f file*

# If there is stdin, it prompts.  If no stdin, it moves anyway and file2 won't
# exist.
touch file1 file2
chmod 400 file1 file2
testing "mv over unwritable file: no stdin" \
  "mv file2 file1 2>/dev/null && [ -e file1 -a ! -e file2 ] && echo yes" \
  "yes\n" "" ""
rm -f file*

touch file1 file2
chmod 400 file1 file2
testing "mv over unwritable file: answered YES" \
  "mv file2 file1 2>/dev/null && [ -e file1 -a ! -e file2 ] && echo yes" \
  "yes\n" "" "y\n"
rm -f file*

touch file1 file2
chmod 400 file1 file2
testing "mv over unwritable file: answered NO" \
  "mv file2 file1 2>/dev/null && [ -e file1 -a -e file2 ] && echo yes" \
  "yes\n" "" "n\n"
rm -f file*

touch file1 file2
testing "interactive: no stdin" \
  "mv -i file2 file1 2>/dev/null && [ -e file1 -a ! -e file2 ] && echo yes" \
  "yes\n" "" ""
rm -f file*

touch file1 file2
testing "interactive: answered YES" \
  "mv -i file2 file1 2>/dev/null && [ -e file1 -a ! -e file2 ] && echo yes" \
  "yes\n" "" "y\n"
rm -f file*

touch file1 file2
testing "interactive: answered NO" \
  "mv -i file2 file1 2>/dev/null && [ -e file1 -a -e file2 ] && echo yes" \
  "yes\n" "" "n\n"
rm -f file*