logo

utils

~/.local/bin tools and git-hooks git clone https://hacktivis.me/git/utils.git

touch (5730B)


  1. #!/usr/bin/env atf-sh
  2. # SPDX-FileCopyrightText: 2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. atf_test_case noargs
  5. noargs_body() {
  6. atf_check touch -a ./foo
  7. maybe_sleep
  8. atf_check touch -m ./foo
  9. maybe_sleep
  10. atime="$(./stat_atime ./foo)"
  11. mtime="$(./stat_mtime ./foo)"
  12. atf_check ../cmd/touch ./foo
  13. atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
  14. atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
  15. }
  16. atf_test_case ref_noargs
  17. ref_noargs_body() {
  18. atf_check touch -a ./foo
  19. maybe_sleep
  20. atf_check touch -m ./foo
  21. maybe_sleep
  22. atime="$(./stat_atime ./foo)"
  23. mtime="$(./stat_mtime ./foo)"
  24. atf_check ../cmd/touch -r ../cmd/touch ./foo
  25. atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
  26. atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
  27. atf_check -o "inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
  28. atf_check -o "inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
  29. }
  30. atf_test_case mtime
  31. mtime_body() {
  32. atf_check touch -a ./foo
  33. maybe_sleep
  34. atf_check touch -m ./foo
  35. maybe_sleep
  36. atime="$(./stat_atime ./foo)"
  37. mtime="$(./stat_mtime ./foo)"
  38. atf_check ../cmd/touch -m ./foo
  39. atf_check -o "inline:${atime}\n" ./stat_atime ./foo
  40. atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
  41. }
  42. atf_test_case ref_mtime
  43. ref_mtime_body() {
  44. atf_check touch -a ./foo
  45. maybe_sleep
  46. atf_check touch -m ./foo
  47. maybe_sleep
  48. atime="$(./stat_atime ./foo)"
  49. mtime="$(./stat_mtime ./foo)"
  50. atf_check ../cmd/touch -m -r ../cmd/touch ./foo
  51. atf_check -o "inline:${atime}\n" ./stat_atime ./foo
  52. atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
  53. atf_check -o "not-inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
  54. atf_check -o "inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
  55. }
  56. atf_test_case atime
  57. atime_body() {
  58. atf_check touch -a ./foo
  59. maybe_sleep
  60. atf_check touch -m ./foo
  61. maybe_sleep
  62. atime="$(./stat_atime ./foo)"
  63. mtime="$(./stat_mtime ./foo)"
  64. atf_check ../cmd/touch -a ./foo
  65. atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
  66. atf_check -o "inline:${mtime}\n" ./stat_mtime ./foo
  67. }
  68. atf_test_case ref_atime
  69. ref_atime_body() {
  70. atf_check touch -a ./foo
  71. maybe_sleep
  72. atf_check touch -m ./foo
  73. maybe_sleep
  74. atime="$(./stat_atime ./foo)"
  75. mtime="$(./stat_mtime ./foo)"
  76. atf_check ../cmd/touch -a -r ../cmd/touch ./foo
  77. atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
  78. atf_check -o "inline:${mtime}\n" ./stat_mtime ./foo
  79. atf_check -o "inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
  80. atf_check -o "not-inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
  81. }
  82. atf_test_case amtime
  83. amtime_body() {
  84. atf_check touch -a ./foo
  85. maybe_sleep
  86. atf_check touch -m ./foo
  87. maybe_sleep
  88. atime="$(./stat_atime ./foo)"
  89. mtime="$(./stat_mtime ./foo)"
  90. atf_check ../cmd/touch -a -m ./foo
  91. atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
  92. atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
  93. }
  94. atf_test_case ref_amtime
  95. ref_amtime_body() {
  96. atf_check touch -a ./foo
  97. maybe_sleep
  98. atf_check touch -m ./foo
  99. maybe_sleep
  100. atime="$(./stat_atime ./foo)"
  101. mtime="$(./stat_mtime ./foo)"
  102. atf_check ../cmd/touch -a -m -r ../cmd/touch ./foo
  103. atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
  104. atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
  105. atf_check -o "inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
  106. atf_check -o "inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
  107. }
  108. atf_test_case dir
  109. dir_body() {
  110. mkdir -p ./foo.d
  111. atime="$(./stat_atime ./foo.d)"
  112. mtime="$(./stat_mtime ./foo.d)"
  113. maybe_sleep
  114. atf_check ../cmd/touch ./foo.d
  115. atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo.d
  116. atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo.d
  117. }
  118. atf_test_case optd
  119. optd_body() {
  120. atf_check touch -a ./foo
  121. maybe_sleep
  122. atf_check touch -m ./foo
  123. maybe_sleep
  124. atime="$(./stat_atime ./foo)"
  125. mtime="$(./stat_mtime ./foo)"
  126. unset TZ
  127. atf_check ../cmd/touch -d 2003-06-02T13:37:42Z ./foo
  128. atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
  129. atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
  130. atf_check -o 'match:^2003-06-02[T ]13:37:42(\.0+)? ?(Z|[\+\-]00:?00)$' ./stat_atime ./foo
  131. atf_check -o 'match:^2003-06-02[T ]13:37:42(\.0+)? ?(Z|[\+\-]00:?00)$' ./stat_mtime ./foo
  132. }
  133. atf_test_case optd_frac
  134. optd_frac_body() {
  135. atf_check touch -a ./foo
  136. maybe_sleep
  137. atf_check touch -m ./foo
  138. maybe_sleep
  139. atime="$(./stat_atime ./foo)"
  140. mtime="$(./stat_mtime ./foo)"
  141. unset TZ
  142. atf_check ../cmd/touch -d 2003-06-02T13:37:42.713Z ./foo
  143. atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
  144. atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
  145. atf_check -o 'match:^2003-06-02[T ]13:37:42.7130+ ?(Z|[\+\-]00:?00)$' ./stat_atime ./foo
  146. atf_check -o 'match:^2003-06-02[T ]13:37:42.7130+ ?(Z|[\+\-]00:?00)$' ./stat_mtime ./foo
  147. atf_check ../cmd/touch -d 2003-06-02T13:37:42.123456789Z ./foo
  148. atf_check -o 'match:^2003-06-02[T ]13:37:42.123456789 ?(Z|[\+\-]00:?00)$' ./stat_atime ./foo
  149. atf_check -o 'match:^2003-06-02[T ]13:37:42.123456789 ?(Z|[\+\-]00:?00)$' ./stat_mtime ./foo
  150. atf_check -s 'exit:1' ../cmd/touch -d 2003-06-02T13:37:42.1234567890Z ./foo
  151. }
  152. atf_init_test_cases() {
  153. cd "$(atf_get_srcdir)" || exit 1
  154. atf_add_test_case noargs
  155. atf_add_test_case atime
  156. atf_add_test_case mtime
  157. atf_add_test_case amtime
  158. atf_add_test_case ref_noargs
  159. atf_add_test_case ref_atime
  160. atf_add_test_case ref_mtime
  161. atf_add_test_case ref_amtime
  162. atf_add_test_case dir
  163. atf_add_test_case optd
  164. # No support for displaying fractional seconds on FreeBSD stat(1)
  165. if uname -s | grep -iq FreeBSD; then
  166. # shellcheck disable=SC2317
  167. maybe_sleep() { sleep 1; }
  168. else
  169. atf_add_test_case optd_frac
  170. # shellcheck disable=SC2317
  171. maybe_sleep() { sleep .1; }
  172. fi
  173. }