From nobody Wed Mar 27 11:26:20 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4V4PWJ244cz5FHkw; Wed, 27 Mar 2024 11:26:20 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4V4PWJ1GRvz4bqb; Wed, 27 Mar 2024 11:26:20 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1711538780; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=HeyUfO39tS9GFDU8Z/MvlU8fRCGNluAk/iRsPRZGPWQ=; b=W+99MvNGrUZxWQ1LIS8zGRju3OZcCsM/ph49tBSLdn1J2/+xBz3BIl4C7qjoasmjLNPL+t q91muj7yVfuU34jepxq5RSSGDStD2FPPwEetAnLKptAo5pWLAcLMU5dTesSscZ9Gr1HfVA jL3AD8saw8+QxQ4wFkidBzXmQaC7udZPfOKXgpdF5709tTvBKMsDxvYK+Lsg7USr6E93DE AZn0/iaY+rErJDRBHmosMNM8r6GvPdEwJtwbnnel25UShLJPsfNUjq092MDEnqSuNFeutD Avg00kyMcNR6fkpIT7HvXbeaBQYh750VaY3DCNBJC1RSHQkSSJkjbzA70jOQMw== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1711538780; a=rsa-sha256; cv=none; b=coAkrbWanfS9bdnvLRrjmLQya90qNZP9wp6h6J909UnzQnVpic/If9DiT9ms7vF37yGst6 oG8UrwL4QJbfIG8vFSm5H+vIrKztfVFxQKW+Isa7vsleEDWkPSVRbXr7DJ/VoYcrVs/BNp BkAACCN7zUxW9A/TLU9PuIGy1D+7YGTmUYhq3rWlxPc/GNUipYiKe/yRAWrbvpPU54wNdR RcFeduei36daFi1bE/8nDeThnon5jpGDQkHN56kCs9WRdBfL/rEKmBENY02fxsPOY8+VVo /iBiCs1zrsuztwW2bjtQ+ODNDbWOR3YY0Vl9GuSJMK7Q9iUOJUCswWwmIfbUvQ== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1711538780; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=HeyUfO39tS9GFDU8Z/MvlU8fRCGNluAk/iRsPRZGPWQ=; b=L6Em7ZcQkFAZDuyqPgWrg52eVjEOJXbGNfvQ6WYLeOb71PHhsbq0gpo+Xh7TzQ7IYXTsf3 xL2NNJTro49oXe9KWiNP2HErDJFCjn6+kj+JviOTAs49Sk9BuTj/dsZETvrbt/+qKk9UUE xTvO90delCSncd51edrm9eiXp1ej1y4bvsk4j+RNwFj2/SMvucroOjkSTK4hRYIXeahBB1 IqO9m9fmLcUd+6J/7J0pFYAxRVi0aNHQtAgabsz861Flg/xU+IL0w1532Cb5qotZTcJOO9 DSGluGSQP/SJR4qSnRsDJ4lLw0QAgwqE/u6Vm0M0VUKcTmvxfA1Fvjao1nXnfg== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4V4PWJ0tcjzj2L; Wed, 27 Mar 2024 11:26:20 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 42RBQKVl003839; Wed, 27 Mar 2024 11:26:20 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 42RBQKir003836; Wed, 27 Mar 2024 11:26:20 GMT (envelope-from git) Date: Wed, 27 Mar 2024 11:26:20 GMT Message-Id: <202403271126.42RBQKir003836@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Dag-Erling =?utf-8?Q?Sm=C3=B8rgrav?= Subject: git: 74a4aa9b1517 - main - touch: Add unit tests. List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: Sender: owner-dev-commits-src-all@freebsd.org X-BeenThere: dev-commits-src-all@freebsd.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: des X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 74a4aa9b1517d92bfa85b0b1cd7d4c1262bb1ef9 Auto-Submitted: auto-generated The branch main has been updated by des: URL: https://cgit.FreeBSD.org/src/commit/?id=74a4aa9b1517d92bfa85b0b1cd7d4c1262bb1ef9 commit 74a4aa9b1517d92bfa85b0b1cd7d4c1262bb1ef9 Author: Dag-Erling Smørgrav AuthorDate: 2024-03-27 10:03:45 +0000 Commit: Dag-Erling Smørgrav CommitDate: 2024-03-27 10:03:45 +0000 touch: Add unit tests. MFC after: 1 week Reviewed by: bapt Differential Revision: https://reviews.freebsd.org/D44505 --- etc/mtree/BSD.tests.dist | 2 + usr.bin/touch/Makefile | 3 + usr.bin/touch/tests/Makefile | 4 + usr.bin/touch/tests/touch_test.sh | 157 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 166 insertions(+) diff --git a/etc/mtree/BSD.tests.dist b/etc/mtree/BSD.tests.dist index 978b8910a2f4..d0451081022b 100644 --- a/etc/mtree/BSD.tests.dist +++ b/etc/mtree/BSD.tests.dist @@ -1187,6 +1187,8 @@ .. tftp .. + touch + .. tr .. truncate diff --git a/usr.bin/touch/Makefile b/usr.bin/touch/Makefile index 421c88062c60..2e9253ddac4c 100644 --- a/usr.bin/touch/Makefile +++ b/usr.bin/touch/Makefile @@ -1,4 +1,7 @@ +.include PROG= touch +HAS_TESTS= +SUBDIR.${MK_TESTS}= tests .include diff --git a/usr.bin/touch/tests/Makefile b/usr.bin/touch/tests/Makefile new file mode 100644 index 000000000000..543b682b96a0 --- /dev/null +++ b/usr.bin/touch/tests/Makefile @@ -0,0 +1,4 @@ +PACKAGE= tests +ATF_TESTS_SH= touch_test + +.include diff --git a/usr.bin/touch/tests/touch_test.sh b/usr.bin/touch/tests/touch_test.sh new file mode 100644 index 000000000000..da39abef622e --- /dev/null +++ b/usr.bin/touch/tests/touch_test.sh @@ -0,0 +1,157 @@ +# +# Copyright (c) 2024 Dag-Erling Smørgrav +# +# SPDX-License-Identifier: BSD-2-Clause +# + +export TZ=UTC + +atf_check_mtime() +{ + local mtime=$1 filename=$2 + atf_check -o inline:"$((mtime))\n" stat -f%m "$filename" +} + +atf_test_case touch_none +touch_none_head() +{ + atf_set descr "No arguments" +} +touch_none_body() +{ + atf_check -s exit:1 -e match:"^usage" touch +} + +atf_test_case touch_one +touch_one_head() +{ + atf_set descr "One argument" +} +touch_one_body() +{ + atf_check touch foo + atf_check test -f foo +} + +atf_test_case touch_multiple +touch_multiple_head() +{ + atf_set descr "Multiple arguments" +} +touch_multiple_body() +{ + atf_check touch foo bar baz + atf_check test -f foo -a -f bar -a -f baz +} + +atf_test_case touch_absolute +touch_absolute_head() +{ + atf_set descr "Absolute date / time" +} +touch_absolute_body() +{ + atf_check touch -t 7001010101 foo + atf_check_mtime 3660 foo + atf_check rm foo + + atf_check touch -t 7001010101.01 foo + atf_check_mtime 3661 foo + atf_check rm foo + + atf_check touch -t 196912312359 foo + atf_check_mtime -60 foo + atf_check rm foo + + atf_check touch -t 196912312359.58 foo + atf_check_mtime -2 foo + atf_check rm foo + + atf_check touch -t 196912312359.59 foo + atf_expect_fail "VFS interprets -1 as “do not set”" + atf_check_mtime -1 foo + atf_check rm foo + + atf_check touch -d1969-12-31T23:59:58 foo + atf_check_mtime -2 foo + atf_check rm foo + + atf_check touch -d1969-12-31\ 23:59:58 foo + atf_check_mtime -2 foo + atf_check rm foo + + atf_check env TZ=CET touch -d1970-01-01T00:59:58 foo + atf_check_mtime -2 foo + atf_check rm foo + + atf_check env TZ=CET touch -d1970-01-01T00:59:58Z foo + atf_check_mtime 3598 foo + atf_check rm foo + + atf_check touch -d1969-12-31T23:59:59Z foo + atf_expect_fail "VFS interprets -1 as “do not set”" + atf_check_mtime -1 foo + atf_check rm foo +} + +atf_test_case touch_relative +touch_relative_head() +{ + atf_set descr "Relative date / time" +} +touch_relative_body() +{ + atf_check touch -t 202403241234.56 foo + atf_check_mtime 1711283696 foo + atf_check touch -A -36 foo + atf_check_mtime 1711283660 foo + atf_check touch -A -0100 foo + atf_check_mtime 1711283600 foo + atf_check touch -A -010000 foo + atf_check_mtime 1711280000 foo + atf_check touch -A 010136 foo + atf_check_mtime 1711283696 foo +} + +atf_test_case touch_copy +touch_copy_head() +{ + atf_set descr "Copy time from another file" +} +touch_copy_body() +{ + atf_check touch -t 202403241234.56 foo + atf_check_mtime 1711283696 foo + atf_check touch -t 7001010000 bar + atf_check_mtime 0 bar + atf_check touch -r foo bar + atf_check_mtime 1711283696 bar +} + +atf_test_case touch_nocreate +touch_nocreate_head() +{ + atf_set descr "Do not create file" +} +touch_nocreate_body() +{ + atf_check touch -t 202403241234.56 foo + atf_check_mtime 1711283696 foo + atf_check touch -c -t 7001010000 foo bar + atf_check_mtime 0 foo + atf_check -s exit:1 test -f bar + atf_check touch -c bar + atf_check -s exit:1 test -f bar +} + +atf_init_test_cases() +{ + atf_add_test_case touch_none + atf_add_test_case touch_one + atf_add_test_case touch_multiple + atf_add_test_case touch_absolute + atf_add_test_case touch_relative + atf_add_test_case touch_copy + atf_add_test_case touch_nocreate + # TODO: add test cases for -a, -h, -m +}