touch (5730B)
- #!/usr/bin/env atf-sh
- # SPDX-FileCopyrightText: 2023 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
- # SPDX-License-Identifier: MPL-2.0
- atf_test_case noargs
- noargs_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
- atf_check ../cmd/touch ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- }
- atf_test_case ref_noargs
- ref_noargs_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
- atf_check ../cmd/touch -r ../cmd/touch ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- atf_check -o "inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
- atf_check -o "inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
- }
- atf_test_case mtime
- mtime_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
- atf_check ../cmd/touch -m ./foo
- atf_check -o "inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- }
- atf_test_case ref_mtime
- ref_mtime_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
- atf_check ../cmd/touch -m -r ../cmd/touch ./foo
- atf_check -o "inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- atf_check -o "not-inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
- atf_check -o "inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
- }
- atf_test_case atime
- atime_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
- atf_check ../cmd/touch -a ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "inline:${mtime}\n" ./stat_mtime ./foo
- }
- atf_test_case ref_atime
- ref_atime_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
- atf_check ../cmd/touch -a -r ../cmd/touch ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "inline:${mtime}\n" ./stat_mtime ./foo
- atf_check -o "inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
- atf_check -o "not-inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
- }
- atf_test_case amtime
- amtime_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
- atf_check ../cmd/touch -a -m ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- }
- atf_test_case ref_amtime
- ref_amtime_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
- atf_check ../cmd/touch -a -m -r ../cmd/touch ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- atf_check -o "inline:$(./stat_atime ../cmd/touch)\n" ./stat_atime ./foo
- atf_check -o "inline:$(./stat_mtime ../cmd/touch)\n" ./stat_mtime ./foo
- }
- atf_test_case dir
- dir_body() {
- mkdir -p ./foo.d
- atime="$(./stat_atime ./foo.d)"
- mtime="$(./stat_mtime ./foo.d)"
- maybe_sleep
- atf_check ../cmd/touch ./foo.d
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo.d
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo.d
- }
- atf_test_case optd
- optd_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
- unset TZ
- atf_check ../cmd/touch -d 2003-06-02T13:37:42Z ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42(\.0+)? ?(Z|[\+\-]00:?00)$' ./stat_atime ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42(\.0+)? ?(Z|[\+\-]00:?00)$' ./stat_mtime ./foo
- }
- atf_test_case optd_frac
- optd_frac_body() {
- atf_check touch -a ./foo
- maybe_sleep
- atf_check touch -m ./foo
- maybe_sleep
- atime="$(./stat_atime ./foo)"
- mtime="$(./stat_mtime ./foo)"
- unset TZ
- atf_check ../cmd/touch -d 2003-06-02T13:37:42.713Z ./foo
- atf_check -o "not-inline:${atime}\n" ./stat_atime ./foo
- atf_check -o "not-inline:${mtime}\n" ./stat_mtime ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42.7130+ ?(Z|[\+\-]00:?00)$' ./stat_atime ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42.7130+ ?(Z|[\+\-]00:?00)$' ./stat_mtime ./foo
- atf_check ../cmd/touch -d 2003-06-02T13:37:42.123456789Z ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42.123456789 ?(Z|[\+\-]00:?00)$' ./stat_atime ./foo
- atf_check -o 'match:^2003-06-02[T ]13:37:42.123456789 ?(Z|[\+\-]00:?00)$' ./stat_mtime ./foo
- atf_check -s 'exit:1' ../cmd/touch -d 2003-06-02T13:37:42.1234567890Z ./foo
- }
- atf_init_test_cases() {
- cd "$(atf_get_srcdir)" || exit 1
- atf_add_test_case noargs
- atf_add_test_case atime
- atf_add_test_case mtime
- atf_add_test_case amtime
- atf_add_test_case ref_noargs
- atf_add_test_case ref_atime
- atf_add_test_case ref_mtime
- atf_add_test_case ref_amtime
- atf_add_test_case dir
- atf_add_test_case optd
- # No support for displaying fractional seconds on FreeBSD stat(1)
- if uname -s | grep -iq FreeBSD; then
- # shellcheck disable=SC2317
- maybe_sleep() { sleep 1; }
- else
- atf_add_test_case optd_frac
- # shellcheck disable=SC2317
- maybe_sleep() { sleep .1; }
- fi
- }