xref: /freebsd/contrib/bmake/unit-tests/varmod-mtime.mk (revision 6a7405f5a6b639682cacf01e35d561411ff556aa)
1# $NetBSD: varmod-mtime.mk,v 1.14 2025/01/11 20:54:46 rillig Exp $
2#
3# Tests for the ':mtime' variable modifier, which maps each word of the
4# expression to that file's modification time.
5
6# Note: strftime() uses mktime() for %s and mktime() assumes localtime
7# so this should match time()
8start:=	${%s:L:localtime}	# see varmod-gmtime.mk, keyword '%s'
9
10
11# Ensure that this makefile exists and has a modification time.  If the file
12# didn't exist, the ':mtime' modifier would return the current time.
13.if ${MAKEFILE:mtime} >= ${start}
14.  error
15.endif
16
17
18# For a file that doesn't exist, the ':mtime' modifier returns the current
19# time, without an error or warning message.  The returned timestamp differs
20# from the 'now' that is used when updating the timestamps in archives or for
21# touching files using the '-t' option, which is taken once when make is
22# started.
23not_found_mtime:=	${no/such/file:L:mtime}
24.if ${not_found_mtime} < ${start}
25.  error
26.endif
27
28
29# The ':mtime' modifier accepts a timestamp in seconds as an optional
30# argument.  This timestamp is used as a fallback in case the file's time
31# cannot be determined, without any error or warning message.
32.if ${no/such/file:L:mtime=0} != "0"
33.  error
34.endif
35
36
37# The fallback timestamp must start with a digit, and it is interpreted as a
38# decimal integer.
39.if ${no/such/file:L:mtime=00042} != "42"
40.  error
41.endif
42
43
44# The fallback timestamp must only be an integer, without trailing characters.
45# expect+1: Invalid argument '123x' for modifier ':mtime'
46.if ${no/such/file:L:mtime=123x}
47.  error
48.else
49.  error
50.endif
51
52
53# The timestamp of a newly created file must be at least as great as the
54# timestamp when parsing of this makefile started.
55COOKIE=	${TMPDIR:U/tmp}/varmod-mtime.cookie
56_!=	touch ${COOKIE}
57.if ${COOKIE:mtime=0} < ${start}
58.   error ${COOKIE:mtime=0} < ${start}
59.endif
60_!=	rm -f ${COOKIE}
61
62
63# If the optional argument of the ':mtime' modifier is the word 'error', the
64# modifier fails with an error message, once for each affected file.
65#
66# expect+2: Cannot determine mtime for 'no/such/file1': <ENOENT>
67# expect+1: Cannot determine mtime for 'no/such/file2': <ENOENT>
68.if ${no/such/file1 no/such/file2:L:mtime=error}
69.  error
70.else
71.  error
72.endif
73
74
75# Only the word 'error' is a special argument to the ':mtime' modifier, all
76# other words result in a parse error.
77# expect+1: Invalid argument 'errorhandler-no' for modifier ':mtime'
78.if ${MAKEFILE:mtime=errorhandler-no} > 0
79.else
80.  error
81.endif
82
83
84# Only the word 'error' can be used as a fallback argument to the modifier.
85# expect+1: Invalid argument 'warn' for modifier ':mtime'
86.if ${MAKEFILE:mtime=warn} > 0
87.  error
88.else
89.  error
90.endif
91
92
93# Ensure that the fallback for a missing modification time is indeed the
94# current time, and not any later time.
95end:=	${%s:L:gmtime}
96.if ${not_found_mtime} > ${end}
97.  error
98.endif
99
100
101# If the expression is irrelevant, the ':mtime' modifier is only parsed, it
102# does not perform any filesystem operations.
103.if 0 && ${no/such/file:L:mtime=error}
104.  error
105.endif
106
107
108# If there is a typo in the modifier name, it does not match.
109# expect+1: Unknown modifier "mtim"
110.if ${anything:L:mtim}
111.  error
112.else
113.  error
114.endif
115
116
117# An empty word list results in an empty mtime list.
118.if ${:U:mtime} != ""
119.  error
120.endif
121