mv.test 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #!/bin/bash
  2. # TODO: needs root to mount tmpfs to test moving across filesystems.
  3. # check handling of chattr +i immutable bit
  4. # "touch two; chmod -w two; mv one two" shouldn't prompt to delete two if
  5. # one doesn't exist.
  6. # Copyright 2013 Robin Mittal <robinmittal.it@gmail.com>
  7. # Copyright 2013 Divya Kothari <divya.s.kothari@gmail.com>
  8. [ -f testing.sh ] && . testing.sh
  9. #testing "name" "command" "result" "infile" "stdin"
  10. touch file
  11. testing "file to file" \
  12. "mv file file1 && [ ! -e file -a -f file1 ] && echo yes" \
  13. "yes\n" "" ""
  14. rm -f file*
  15. touch file
  16. mkdir dir
  17. testing "file to dir" \
  18. "mv file dir && [ ! -e file -a -f dir/file ] && echo yes" \
  19. "yes\n" "" ""
  20. rm -rf file* dir*
  21. mkdir dir
  22. testing "dir to dir" \
  23. "mv dir dir1 && [ ! -e dir -a -d dir1 ] && echo yes" \
  24. "yes\n" "" ""
  25. rm -rf dir*
  26. mkdir dir1 dir2
  27. touch file1 file2 dir1/file3
  28. ln -s file1 link1
  29. testing "multiple files/dirs to a dir" \
  30. "mv file1 file2 link1 dir1 dir2 &&
  31. [ ! -e file1 -a ! -e file2 -a ! -e link1 -a ! -e dir1 ] &&
  32. [ -f dir2/file1 -a -f dir2/file2 -a -L dir2/link1 -a -d dir2/dir1 ] &&
  33. [ -f dir2/dir1/file3 ] && readlink dir2/link1" \
  34. "file1\n" "" ""
  35. rm -rf file* link* dir*
  36. dd if=/dev/zero of=file1 seek=10k count=1 >/dev/null 2>&1
  37. testing "random file to new file" \
  38. "mv file1 file2 && [ ! -e file1 -a -f file2 ] && stat -c %s file2" \
  39. "5243392\n" "" ""
  40. rm -f file*
  41. touch file1
  42. ln -s file1 link1
  43. testing "symlink to new symlink" \
  44. "mv link1 link2 && [ ! -e link1 -a -L link2 ] && readlink link2" \
  45. "file1\n" "" ""
  46. unlink tLink2 &>/dev/null
  47. rm -f file* link*
  48. touch file1
  49. ln file1 link1
  50. testing "hard link to new hardlink" \
  51. "mv link1 link2 && [ ! -e link1 -a -f link2 -a file1 -ef link2 ] && echo yes" \
  52. "yes\n" "" ""
  53. unlink link2 &>/dev/null
  54. rm -f file* link*
  55. touch file1
  56. chmod a-r file1
  57. testing "file to unreadable file" \
  58. "mv file1 file2 && [ ! -e file1 -a -f file2 ] && echo yes" \
  59. "yes\n" "" ""
  60. rm -f file*
  61. touch file1
  62. ln file1 link1
  63. mkdir dir1
  64. testing "file hardlink dir" \
  65. "mv file1 link1 dir1 &&
  66. [ ! -e file1 -a ! -e link1 -a -f dir1/file1 -a -f dir1/link1 ] &&
  67. [ dir1/file1 -ef dir1/link1 ] && echo yes" \
  68. "yes\n" "" ""
  69. rm -rf file* link* dir*
  70. mkdir -p dir1/dir2 dir3
  71. touch dir1/dir2/file1 dir1/dir2/file2
  72. testing "dir to new dir" \
  73. "mv dir1/dir2 dir3/new &&
  74. [ ! -e dir1/dir2 -a -d dir3/new -a -f dir3/new/file1 ] &&
  75. [ -f dir3/new/file2 ] && echo yes" \
  76. "yes\n" "" ""
  77. rm -rf file* dir*
  78. mkdir dir1 dir2
  79. testing "dir to existing dir" \
  80. "mv dir1 dir2 && [ ! -e dir1 -a -d dir2/dir1 ] && echo yes" \
  81. "yes\n" "" ""
  82. rm -rf dir*
  83. # Trailing slash was once throwing: bad 'dir1/dir2/': Resource busy
  84. mkdir -p dir1/dir2
  85. testing "dir1/dir2 to ." \
  86. "mv dir1/dir2 . && [ ! -e dir1/dir2 -a -d dir2 ] && echo yes" \
  87. "yes\n" "" ""
  88. rm -rf dir*
  89. mkdir -p dir1/dir2
  90. testing "dir1/dir2/ to ." \
  91. "mv dir1/dir2/ . && [ ! -e dir1/dir2 -a -d dir2 ] && echo yes" \
  92. "yes\n" "" ""
  93. rm -rf dir*
  94. mkdir -p dir1/dir2
  95. testing "dir1/dir2/ to ./" \
  96. "mv dir1/dir2/ ./ && [ ! -e dir1/dir2 -a -d dir2 ] && echo yes" \
  97. "yes\n" "" ""
  98. rm -rf dir*
  99. testing "not/exists/ to ./" \
  100. "mv not/exists/ ./ 2>&1 | grep -o not/exists/" \
  101. "not/exists/\n" "" ""
  102. touch file1 file2
  103. chmod 400 file1 file2
  104. testing "force over unwritable" \
  105. "mv -f file1 file2 && [ ! -e file1 -a -e file2 ] && echo yes" \
  106. "yes\n" "" ""
  107. rm -f file*
  108. touch file1 file2
  109. testing "no clobber (dest exists)" \
  110. "mv -n file1 file2 && [ -e file1 -a -e file2 ] && echo yes"\
  111. "yes\n" "" ""
  112. rm -f file*
  113. touch file1
  114. testing "no clobber (dest doesn't exist)" \
  115. "mv -n file1 new-dest && [ ! -e file1 -a -e new-dest ] && echo yes"\
  116. "yes\n" "" ""
  117. rm -f file*
  118. touch file1 file2
  119. chmod 400 file1 file2
  120. testing "over unwritable file only prompts when stdin is a terminal" \
  121. "mv file2 file1 2>/dev/null && [ -e file1 -a ! -e file2 ] && echo yes" \
  122. "yes\n" "" ""
  123. rm -f file*
  124. touch file1 file2
  125. testing "interactive: no stdin" \
  126. "mv -i file2 file1 2>/dev/null && [ -e file1 -a -e file2 ] && echo yes" \
  127. "yes\n" "" ""
  128. rm -f file*
  129. touch file1 file2
  130. testing "interactive: answered YES" \
  131. "mv -i file2 file1 2>/dev/null && [ -e file1 -a ! -e file2 ] && echo yes" \
  132. "yes\n" "" "y\n"
  133. rm -f file*
  134. touch file1 file2
  135. testing "interactive: answered NO" \
  136. "mv -i file2 file1 2>/dev/null && [ -e file1 -a -e file2 ] && echo yes" \
  137. "yes\n" "" "n\n"
  138. rm -f file*