touch.test 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. #!/bin/bash
  2. [ -f testing.sh ] && . testing.sh
  3. #testing "name" "command" "result" "infile" "stdin"
  4. testing "touch" "touch walrus && [ -e walrus ] && echo yes" "yes\n" "" ""
  5. testing "1 2 3" "touch one two three && rm one two three && echo yes" "yes\n" \
  6. "" ""
  7. testing "-c" "touch -c walrus && [ -e walrus ] && echo yes" "yes\n" "" ""
  8. testing "-c missing" "touch -c warrus && [ ! -e warrus ] && echo yes" \
  9. "yes\n" "" ""
  10. testing "-t" \
  11. "touch -t 201201231234 walrus && date -r walrus +%Y%m%d-%H%M%S.%N" \
  12. "20120123-123400.000000000\n" "" ""
  13. # Yes, the year could roll over while you're running this test. I do not care.
  14. testing "-t MMDDhhmm" \
  15. "touch -t 01231234 input && date +%Y-%m-%d:%H-%M-%S -r input" \
  16. "$(date +%Y)-01-23:12-34-00\n" "" ""
  17. testing "-t YYMMDDhhmm" \
  18. "touch -t 2101231234 input && date +%Y-%m-%d:%H-%M-%S -r input" \
  19. "$(date +%C)21-01-23:12-34-00\n" "" ""
  20. testing "-t CCYYMMDDhhmm" \
  21. "touch -t 201201231234 walrus && date -r walrus +%Y%m%d-%H%M%S.%N" \
  22. "20120123-123400.000000000\n" "" ""
  23. testing "-t seconds" \
  24. "touch -t 201201231234.56 walrus && date -r walrus +%Y%m%d-%H%M%S.%N" \
  25. "20120123-123456.000000000\n" "" ""
  26. testing "-t -" "TZ=utc touch -t 200109090146.40 - > walrus && TZ=utc date -r walrus +%s" \
  27. "1000000000\n" "" ""
  28. toyonly testing "-t nanoseconds" \
  29. "touch -t 201201231234.56123456789 walrus && date -r walrus +%Y%m%d-%H%M%S.%N" \
  30. "20120123-123456.123456789\n" "" ""
  31. testing "-d" \
  32. "touch -d 2009-02-13T23:31:30Z walrus && date -r walrus +%s" \
  33. "1234567890\n" "" ""
  34. testing "-d with space" \
  35. "touch -d '2009-02-13 23:31:30Z' walrus && date -r walrus +%s" \
  36. "1234567890\n" "" ""
  37. testing "-d nanoseconds" \
  38. "touch -d 2009-02-13T23:31:30.123456789Z walrus && date -r walrus +%s.%N" \
  39. "1234567890.123456789\n" "" ""
  40. testing "-r" \
  41. "touch -r walrus walrus2 && date -r walrus2 +%s.%N" \
  42. "1234567890.123456789\n" "" ""
  43. # Yes, the year could roll over while you're running this test. I do not care.
  44. testing "-t MMDDhhmm" \
  45. "touch -t 01231234 input && date +%Y-%m-%d:%H-%M-%S -r input" \
  46. "$(date +%Y)-01-23:12-34-00\n" "" ""
  47. testing "-t CCMMDDhhmm" \
  48. "touch -t 2101231234 input && date +%Y-%m-%d:%H-%M-%S -r input" \
  49. "$(date +%C)21-01-23:12-34-00\n" "" ""
  50. testing "-a" "TZ=utc touch -t 197101020304 walrus &&
  51. TZ=utc touch -t 197203040506 -a walrus && TZ=utc stat -c '%X %Y' walrus" \
  52. "68533560 31633440\n" "" ""
  53. testing "-m" "TZ=utc touch -t 197101020304 walrus &&
  54. TZ=utc touch -t 197203040506 -m walrus && TZ=utc stat -c '%X %Y' walrus" \
  55. "31633440 68533560\n" "" ""
  56. testing "-am" "TZ=utc touch -t 197101020304 walrus &&
  57. TZ=utc touch -t 197203040506 -am walrus && TZ=utc stat -c '%X %Y' walrus" \
  58. "68533560 68533560\n" "" ""
  59. rm walrus walrus2