logo

utils-std

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

touch.sh (2321B)


  1. #!/bin/sh
  2. # SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
  3. # SPDX-License-Identifier: MPL-2.0
  4. if ! command -v stat >/dev/null 2>/dev/null; then
  5. echo '1..0 # SKIP: missing command: stat'
  6. exit 0
  7. fi
  8. WD=$(dirname "$0")
  9. plans=33
  10. target="${WD}/../cmd/touch"
  11. . "${WD}/tap.sh"
  12. tempfile="$(mktemp)"
  13. tempdir="$(mktemp -d)"
  14. t noargs
  15. t file "$tempfile"
  16. t ref_file:setup "-d 2007-11-12T10:15:30Z $tempfile"
  17. t ref_file "-r $target $tempfile"
  18. t_cmd ref_file:atime "$("${WD}/stat_atime" "$target")
  19. " "${WD}/stat_atime" "$tempfile"
  20. t_cmd ref_file:mtime "$("${WD}/stat_mtime" "$target")
  21. " "${WD}/stat_mtime" "$tempfile"
  22. t mtime:setup "-d 2007-11-12T10:15:30Z $tempfile"
  23. atime=$("${WD}/stat_atime" "$tempfile")
  24. t mtime "-m $tempfile"
  25. t_cmd mtime:atime "$atime
  26. " "${WD}/stat_atime" "$tempfile"
  27. t ref_mtime:setup "-d 2007-11-12T10:15:30Z $tempfile"
  28. atime=$("${WD}/stat_atime" "$tempfile")
  29. t ref_mtime "-m -r $target $tempfile"
  30. t_cmd ref_mtime:atime "$atime
  31. " "${WD}/stat_atime" "$tempfile"
  32. t_cmd ref_mtime:mtime "$("${WD}/stat_mtime" "$target")
  33. " "${WD}/stat_mtime" "$tempfile"
  34. t atime:setup "-d 2007-11-12T10:15:30Z $tempfile"
  35. mtime=$("${WD}/stat_mtime" "$tempfile")
  36. t atime "-a $tempfile"
  37. t_cmd atime:mtime "$mtime
  38. " "${WD}/stat_mtime" "$tempfile"
  39. t ref_atime:setup "-d 2007-11-12T10:15:30Z $tempfile"
  40. mtime=$("${WD}/stat_mtime" "$tempfile")
  41. t ref_atime "-a -r $target $tempfile"
  42. t_cmd ref_atime:mtime "$mtime
  43. " "${WD}/stat_mtime" "$tempfile"
  44. t_cmd ref_atime:atime "$("${WD}/stat_atime" "$target")
  45. " "${WD}/stat_atime" "$tempfile"
  46. t amtime "-a -m $tempfile"
  47. t ref_amtime:setup "-d 2007-11-12T10:15:30Z $tempfile"
  48. t ref_amtime "-a -m -r $target $tempfile"
  49. t_cmd ref_amtime:atime "$("${WD}/stat_atime" "$target")
  50. " "${WD}/stat_atime" "$tempfile"
  51. t_cmd ref_amtime:mtime "$("${WD}/stat_mtime" "$target")
  52. " "${WD}/stat_mtime" "$tempfile"
  53. t dir "${tempdir}"
  54. t dir_optd "-d 2003-06-02T13:37:42Z $tempdir"
  55. TZ=UTC t dir_optd_tz:utc "-d 2003-06-02T13:37:42Z $tempdir"
  56. TZ=UTC t dir_optd_tz:0666 "-d 2003-06-02T13:37:42+0666 $tempdir"
  57. t optt "-t 200306021337.42 $tempfile"
  58. touch_empty_str() { "$target" ''; }
  59. t_cmd --exit=1 empty_str "touch: error: Failed opening file '': No such file or directory
  60. " touch_empty_str
  61. t_cmd rm_tempfile '' rm "${tempfile}"
  62. t_cmd rm_tempdir '' rm -r "${tempdir}"