find.test 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #!/bin/bash
  2. [ -f testing.sh ] && . testing.sh
  3. mkdir dir
  4. cd dir
  5. touch file
  6. mkfifo fifo
  7. # fs timestamp granularity isn't always enough for -newer to tell, so wait
  8. sleep .1
  9. ln -s fifo link
  10. cd ..
  11. touch irrelevant
  12. mkdir perm
  13. touch perm/all-read-only
  14. chmod a=r perm/all-read-only
  15. #testing "name" "command" "result" "infile" "stdin"
  16. # Testing operators
  17. testing "-type l -a -type d -o -type p" \
  18. "find dir -type l -a -type d -o -type p" "dir/fifo\n" "" ""
  19. testing "-type l -type d -o -type p" "find dir -type l -type d -o -type p" \
  20. "dir/fifo\n" "" ""
  21. testing "-type l -o -type d -a -type p" \
  22. "find dir -type l -o -type d -a -type p" "dir/link\n" "" ""
  23. testing "-type l -o -type d -type p" "find dir -type l -o -type d -type p" \
  24. "dir/link\n" "" ""
  25. testing "-type l ( -type d -o -type l )" \
  26. "find dir -type l \( -type d -o -type l \)" "dir/link\n" "" ""
  27. testing "extra parentheses" \
  28. "find dir \( \( -type l \) \( -type d -o \( \( -type l \) \) \) \)" \
  29. "dir/link\n" "" ""
  30. testing "( -type p -o -type d ) -type p" \
  31. "find dir \( -type p -o -type d \) -type p" "dir/fifo\n" "" ""
  32. testing "-type l -o -type d -type p -o -type f" \
  33. "find dir -type l -o -type d -type p -o -type f | sort" \
  34. "dir/file\ndir/link\n" "" ""
  35. testing "-type l,f" \
  36. "find dir -type l,f | sort" "dir/file\ndir/link\n" "" ""
  37. # Testing short-circuit evaluations
  38. testing "-type f -a -print" \
  39. "find dir -type f -a -print" "dir/file\n" "" ""
  40. testing "-print -o -print" \
  41. "find dir -type f -a \( -print -o -print \)" "dir/file\n" "" ""
  42. # these were erroring or segfaulting:
  43. # find -type f -user nobody -exec : \;
  44. # find -type f -user nobody -exec : -exec : \;
  45. # Testing previous failures
  46. testing " " "cd perm; find" ".\n./all-read-only\n" "" ""
  47. testing "-type f -user -exec" \
  48. "find dir -type f -user $USER -exec ls {} \\;" "dir/file\n" "" ""
  49. testing "-type l -newer -exec" \
  50. "find dir -type l -newer dir/file -exec ls {} \\;" "dir/link\n" "" ""
  51. testing "-exec true \\; -print" \
  52. "find dir/file -exec true \\; -print" "dir/file\n" "" ""
  53. testing "-exec false \\; -print" \
  54. "find dir/file -exec false \\; -print" "" "" ""
  55. testing "-perm (exact success)" \
  56. "find perm -type f -perm 0444" "perm/all-read-only\n" "" ""
  57. testing "-perm (exact failure)" \
  58. "find perm -type f -perm 0400" "" "" ""
  59. testing "-perm (min success)" \
  60. "find perm -type f -perm -0400" "perm/all-read-only\n" "" ""
  61. testing "-perm (min failure)" \
  62. "find perm -type f -perm -0600" "" "" ""
  63. testing "-perm (any success)" \
  64. "find perm -type f -perm -0444" "perm/all-read-only\n" "" ""
  65. testing "-perm (any failure)" \
  66. "find perm -type f -perm -0222" "" "" ""
  67. testing "unterminated -exec {}" \
  68. "find dir -type f -exec ls {} 2>/dev/null || echo bad" "bad\n" "" ""
  69. testing "-exec {} +" \
  70. "find dir -type f -exec ls {} +" "dir/file\n" "" ""
  71. # `find . -iname` was segfaulting
  72. testing "-name file" "find dir -name file" "dir/file\n" "" ""
  73. testing "-name FILE" "find dir -name FILE" "" "" ""
  74. ln -s ../broken dir/link2
  75. testing "-iname file" "find dir -iname FILE" "dir/file\n" "" ""
  76. testing "-iname FILE" "find dir -iname FILE" "dir/file\n" "" ""
  77. testing "-name (no arguments)" \
  78. "find dir -name 2>&1 | grep -o '[-]name'" "-name\n" "" ""
  79. testing "-iname (no arguments)" \
  80. "find dir -iname 2>&1 | grep -o '[-]iname'" "-iname\n" "" ""
  81. testing "-lname" "find dir -lname '?./brok*'" "dir/link2\n" "" ""
  82. testing "-ilname" "find dir -ilname '*ROK*'" "dir/link2\n" "" ""
  83. testing "" "find dir \( -iname file -o -iname missing \) -exec echo {} \;" \
  84. "dir/file\n" "" ""
  85. testing "-path glob" "find dir -path 'dir*e'" "dir/file\n" "" ""
  86. testing "-wholename glob" "find dir -wholename 'dir*e'" "dir/file\n" "" ""
  87. testing "-ipath glob" "find dir -ipath 'dIr*E'" "dir/file\n" "" ""
  88. testing "-iwholename glob" "find dir -iwholename 'dIr*E'" "dir/file\n" "" ""
  89. testing "-printf" "find dir -name file -printf '%f %p %P %s'" \
  90. "file dir/file file 0" "" ""
  91. testing "-printf .N" "find dir -name file -printf %.2f" "fi" "" ""
  92. # findutils find supports C letter escapes and \0 octal, but not \x or \u.
  93. testing "-printf escapes" \
  94. "find dir -name file -printf '\0 \007 \t \079' | xxd -p" \
  95. "0020072009200739\n" "" ""
  96. # findutils find treats \c as "no more output from this -printf", not "no more
  97. # output from find".
  98. testing "-printf \\c escape" "find dir -name f* -printf 'x\cy'" "xx" "" ""
  99. # No error message for a dangling link.
  100. ln -s does-not-exist dir/dangler
  101. testing "-L dangling symlink silent" \
  102. "LANG=C find -L dir -name file 2>&1" "dir/file\n" "" ""
  103. rm -f dir/dangler
  104. # An error for a symlink loop.
  105. ln -s looper dir/looper
  106. testing "-L symlink loop noisy" \
  107. "LANG=C find -L dir -name file 2>err ; grep -q dir/looper err || echo missing error" \
  108. "dir/file\n" "" ""
  109. testing "-false" "find dir -false" "" "" ""
  110. testing "-true" "find dir/file -true" "dir/file\n" "" ""
  111. testing "missing root error" \
  112. "LANG=C find -L dir/missing-root 2>err ; grep -q dir/missing-root err || echo missing error" \
  113. "" "" ""
  114. rm -f dir/looper err
  115. testing "-path match root" "find dir/f* -path dir/file" "dir/file\n" "" ""
  116. testing "-name match root" "find dir/f* -name file" "dir/file\n" "" ""
  117. # https://github.com/landley/toybox/issues/69
  118. ln -s nowhere broken
  119. testing "-H broken" "find -H broken" "broken\n" "" ""
  120. testing "-L broken" "find -L broken" "broken\n" "" ""
  121. testing "one slash" 'find /etc/ -maxdepth 1 | grep /passwd\$' '/etc/passwd\n' \
  122. '' ''
  123. testing 'empty arg' 'find "" dir -name file 2>/dev/null' 'dir/file\n' '' ''
  124. testing 'quit' 'find dir perm -print -quit' 'dir\n' '' ''
  125. ln dir/file perm/hardlink
  126. testing 'samefile' 'find . -samefile dir/file | sort' \
  127. './dir/file\n./perm/hardlink\n' '' ''
  128. rm -rf dir broken perm irrelevant
  129. mkdir dir
  130. touch -d @12345 dir/one
  131. touch -d @12346 dir/two
  132. testing 'newerat' 'find dir -type f -newerat @12345' 'dir/two\n' '' ''
  133. testing 'newer nano' 'find dir -type f -newerat @12345.67890' 'dir/two\n' '' ''
  134. rm -rf dir