logo

utils-std

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

touch (6781B)


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