xref: /freebsd/contrib/bmake/unit-tests/varmod-mtime.mk (revision 95eb4b873b6a8b527c5bd78d7191975dfca38998)
1# $NetBSD: varmod-mtime.mk,v 1.11 2024/07/04 17:47:54 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+2: while evaluating variable "no/such/file" with value "no/such/file": Invalid argument '123x' for modifier ':mtime'
46# expect+1: Malformed conditional (${no/such/file:L:mtime=123x})
47.if ${no/such/file:L:mtime=123x}
48.  error
49.else
50.  error
51.endif
52
53
54# The timestamp of a newly created file must be at least as great as the
55# timestamp when parsing of this makefile started.
56COOKIE=	${TMPDIR:U/tmp}/varmod-mtime.cookie
57_!=	touch ${COOKIE}
58.if ${COOKIE:mtime=0} < ${start}
59.   error ${COOKIE:mtime=0} < ${start}
60.endif
61_!=	rm -f ${COOKIE}
62
63
64# If the optional argument of the ':mtime' modifier is the word 'error', the
65# modifier fails with an error message, once for each affected file.
66#
67# expect+3: while evaluating variable "no/such/file1 no/such/file2" with value "no/such/file1 no/such/file2": Cannot determine mtime for 'no/such/file1': <ENOENT>
68# expect+2: while evaluating variable "no/such/file1 no/such/file2" with value "no/such/file1 no/such/file2": Cannot determine mtime for 'no/such/file2': <ENOENT>
69# expect+1: Malformed conditional (${no/such/file1 no/such/file2:L:mtime=error})
70.if ${no/such/file1 no/such/file2:L:mtime=error}
71.  error
72.else
73.  error
74.endif
75
76
77# Only the word 'error' is a special argument to the ':mtime' modifier, all
78# other words result in a parse error.
79# expect+2: while evaluating variable "MAKEFILE" with value "varmod-mtime.mk": Invalid argument 'errorhandler-no' for modifier ':mtime'
80# expect+1: Malformed conditional (${MAKEFILE:mtime=errorhandler-no} > 0)
81.if ${MAKEFILE:mtime=errorhandler-no} > 0
82.else
83.  error
84.endif
85
86
87# Only the word 'error' can be used as a fallback argument to the modifier.
88# expect+2: while evaluating variable "MAKEFILE" with value "varmod-mtime.mk": Invalid argument 'warn' for modifier ':mtime'
89# expect+1: Malformed conditional (${MAKEFILE:mtime=warn} > 0)
90.if ${MAKEFILE:mtime=warn} > 0
91.  error
92.else
93.  error
94.endif
95
96
97# Ensure that the fallback for a missing modification time is indeed the
98# current time, and not any later time.
99end:=	${%s:L:gmtime}
100.if ${not_found_mtime} > ${end}
101.  error
102.endif
103
104
105# If the expression is irrelevant, the ':mtime' modifier is only parsed, it
106# does not perform any filesystem operations.
107.if 0 && ${no/such/file:L:mtime=error}
108.  error
109.endif
110
111
112# If there is a typo in the modifier name, it does not match.
113# expect+2: while evaluating variable "anything" with value "anything": Unknown modifier "mtim"
114# expect+1: Malformed conditional (${anything:L:mtim})
115.if ${anything:L:mtim}
116.  error
117.else
118.  error
119.endif
120
121
122# An empty word list results in an empty mtime list.
123.if ${:U:mtime} != ""
124.  error
125.endif
126