logo

utils-std

Collection of commonly available Unix tools git clone https://anongit.hacktivis.me/git/utils-std.git/

rm.t (4683B)


  1. #!/usr/bin/env cram
  2. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. $ export PATH="$TESTDIR/../cmd:$PATH"
  5. $ test "$(command -v rm)" = "$TESTDIR/../cmd/rm"
  6. $ touch file
  7. $ rm file
  8. $ test ! -e file
  9. POSIX rm(1p) step 1a, no -f option
  10. $ test ! -f enoent
  11. $ touch exists
  12. $ test -f exists
  13. $ rm enoent exists
  14. rm: error: Failed getting status for 'enoent': No such file or directory
  15. [1]
  16. $ test ! -e exists
  17. POSIX rm(1p) step 1a, -f option
  18. Note: Still printing an error message, even if POSIX doesn't requires it
  19. $ touch exists_f
  20. $ test -f exists_f
  21. $ test ! -f enoent
  22. $ rm -f enoent exists_f
  23. $ test ! -e exists_f
  24. POSIX rm(1p) step 2a:
  25. $ mkdir no_rR.d
  26. $ test -d no_rR.d
  27. $ touch no_rR.f
  28. $ test -f no_rR.f
  29. $ rm no_rR.d no_rR.f
  30. rm: error: Is a directory, pass -r or -d to remove: no_rR.d
  31. [1]
  32. $ test -d no_rR.d
  33. $ test ! -e no_rR.f
  34. $ rm -r no_rR.d
  35. $ test ! -e no_rR.d
  36. POSIX rm(1p) step 2b, empty directory, no -i
  37. $ mkdir 2b-empty-noi
  38. $ test -d 2b-empty-noi
  39. $ rm -r 2b-empty-noi
  40. $ test ! -e 2b-empty-noi
  41. POSIX rm(1p) step 2b, empty directory, -i
  42. Note: Ignoring if standard input is a terminal
  43. $ mkdir 2b-empty-no
  44. $ test -d 2b-empty-no
  45. $ printf '%s\n' | rm -ri 2b-empty-no
  46. rm: Recurse into '2b-empty-no' ? [y/N]
  47. rm: Got empty response, considering it false
  48. $ test -e 2b-empty-no
  49. $ printf '%s\n' y n | rm -ri 2b-empty-no
  50. rm: Recurse into '2b-empty-no' ? [y/N] y
  51. rm: Remove '2b-empty-no' ? [y/N] n
  52. $ test -e 2b-empty-no
  53. $ printf '%s\n' y y | rm -ri 2b-empty-no
  54. rm: Recurse into '2b-empty-no' ? [y/N] y
  55. rm: Remove '2b-empty-no' ? [y/N] y
  56. $ test ! -e 2b-empty-no
  57. POSIX rm(1p) step 2c, don't follow symlinks
  58. $ mkdir 2c-origin
  59. $ mkdir 2c-symlinks
  60. $ ln -s 2c-origin 2c-symlinks/dir
  61. $ touch 2c-origin/file
  62. $ ln -s 2c-origin/file 2c-symlinks/file
  63. $ test -L 2c-symlinks/file
  64. $ test -L 2c-symlinks/dir
  65. $ rm -r 2c-symlinks
  66. $ test ! -e 2c-symlinks
  67. $ test -d 2c-origin
  68. $ test -f 2c-origin/file
  69. $ rm -r 2c-origin
  70. POSIX rm(1p) step 3, no write
  71. Extra check from rm(1), unrelated to the EPERM that unlink gets on a non-writable directory
  72. $ touch no_write
  73. $ chmod -- -w no_write
  74. $ echo | rm no_write
  75. rm: Remove non-writable 'no_write' ? [y/N]
  76. rm: Got empty response, considering it false
  77. $ test -f no_write
  78. $ echo n | rm no_write
  79. rm: Remove non-writable 'no_write' ? [y/N] n
  80. $ test -f no_write
  81. $ echo y | rm no_write
  82. rm: Remove non-writable 'no_write' ? [y/N] y
  83. $ test ! -f no_write
  84. $ touch file_i
  85. $ echo | rm -i file_i
  86. rm: Remove 'file_i' ? [y/N]
  87. rm: Got empty response, considering it false
  88. $ test -e file_i
  89. $ rm file_i
  90. $ touch file_in
  91. $ echo n | rm -i file_in
  92. rm: Remove 'file_in' ? [y/N] n
  93. $ test -e file_in
  94. $ rm file_in
  95. $ touch file_iy
  96. $ echo y | rm -i file_iy
  97. rm: Remove 'file_iy' ? [y/N] y
  98. $ test ! -e file_iy
  99. Verbose
  100. $ touch file_v
  101. $ rm -v file_v
  102. rm: Removed: file_v
  103. POSIX, -f shouldn't return an error when non-existent files are passed
  104. $ rm -f /var/empty/e/no/ent
  105. $ rm /var/empty/e/no/ent
  106. rm: error: Failed getting status for '/var/empty/e/no/ent': No such file or directory
  107. [1]
  108. POSIX, -f shouldn't return an error when no operands are passed
  109. $ rm -f
  110. $ rm
  111. rm: error: missing operand
  112. Usage: rm [-firRv] [files ...]
  113. [1]
  114. POSIX 2024/D4.1, -d
  115. $ mkdir empty_dir
  116. $ rm -d empty_dir
  117. $ test ! -e empty_dir
  118. $ mkdir non_empty_dir
  119. $ touch non_empty_dir/.keep
  120. $ rm -d non_empty_dir
  121. rm: error: Couldn't remove 'non_empty_dir': Directory not empty
  122. [1]
  123. $ test -e non_empty_dir
  124. $ rm -fr non_empty_dir
  125. Don't follow symlinks
  126. $ touch bar
  127. $ ln bar foo
  128. $ rm foo
  129. $ test -e bar
  130. $ test ! -e foo
  131. $ rm bar
  132. Correct path when multiple files are given
  133. $ touch path_v_foo path_v_bar
  134. $ rm -v path_v_foo path_v_enoent path_v_bar
  135. rm: Removed: path_v_foo
  136. rm: error: Failed getting status for 'path_v_enoent': No such file or directory
  137. rm: Removed: path_v_bar
  138. [1]
  139. $ test ! -e path_v_foo
  140. $ test ! -e path_v_bar
  141. $ mkdir -p dir_v_foo/ dir_v_bar/ dir_v_dir/foo.d
  142. $ touch dir_v_foo/bar dir_v_foo/baz dir_v_bar/foo
  143. $ rm -rv dir_v_foo dir_v_foo/baz dir_v_bar/foo dir_v_bar dir_v_dir/foo.d dir_v_dir
  144. rm: Removed: dir_v_foo/ba[zr] (re)
  145. rm: Removed: dir_v_foo/ba[zr] (re)
  146. rm: Removed: dir_v_foo
  147. rm: error: Failed getting status for 'dir_v_foo/baz': No such file or directory
  148. rm: Removed: dir_v_bar/foo
  149. rm: Removed: dir_v_bar
  150. rm: Removed: dir_v_dir/foo.d
  151. rm: Removed: dir_v_dir
  152. [1]
  153. $ test ! -e dir_v_foo
  154. $ test ! -e dir_v_bar
  155. $ test ! -e dir_v_dir
  156. No files should be left
  157. $ find .
  158. .