check-expect.lua revision 1.14 1 1.1 rillig #! /usr/bin/lua
2 1.14 rillig -- $NetBSD: check-expect.lua,v 1.14 2025/06/29 17:10:04 rillig Exp $
3 1.1 rillig
4 1.1 rillig --[[
5 1.1 rillig
6 1.1 rillig usage: lua ./check-expect.lua *.mk
7 1.1 rillig
8 1.4 rillig Check that the various 'expect' comments in the .mk files produce the
9 1.4 rillig expected text in the corresponding .exp file.
10 1.1 rillig
11 1.4 rillig # expect: <line>
12 1.4 rillig All of these lines must occur in the .exp file, in the same order as
13 1.4 rillig in the .mk file.
14 1.4 rillig
15 1.4 rillig # expect-reset
16 1.4 rillig Search the following 'expect:' comments from the top of the .exp
17 1.4 rillig file again.
18 1.4 rillig
19 1.4 rillig # expect[+-]offset: <message>
20 1.4 rillig Each message must occur in the .exp file and refer back to the
21 1.4 rillig source line in the .mk file.
22 1.9 rillig
23 1.9 rillig # expect-not: <substring>
24 1.9 rillig The substring must not occur as part of any line of the .exp file.
25 1.9 rillig
26 1.11 rillig # expect-not-matches: <pattern>
27 1.11 rillig The pattern (see https://lua.org/manual/5.4/manual.html#6.4.1)
28 1.11 rillig must not occur as part of any line of the .exp file.
29 1.1 rillig ]]
30 1.1 rillig
31 1.1 rillig
32 1.1 rillig local had_errors = false
33 1.1 rillig ---@param fmt string
34 1.1 rillig function print_error(fmt, ...)
35 1.1 rillig print(fmt:format(...))
36 1.1 rillig had_errors = true
37 1.1 rillig end
38 1.1 rillig
39 1.1 rillig
40 1.1 rillig ---@return nil | string[]
41 1.1 rillig local function load_lines(fname)
42 1.1 rillig local lines = {}
43 1.1 rillig
44 1.1 rillig local f = io.open(fname, "r")
45 1.1 rillig if f == nil then return nil end
46 1.1 rillig
47 1.1 rillig for line in f:lines() do
48 1.1 rillig table.insert(lines, line)
49 1.1 rillig end
50 1.1 rillig f:close()
51 1.1 rillig
52 1.1 rillig return lines
53 1.1 rillig end
54 1.1 rillig
55 1.1 rillig
56 1.1 rillig ---@param exp_lines string[]
57 1.1 rillig local function collect_lineno_diagnostics(exp_lines)
58 1.1 rillig ---@type table<string, string[]>
59 1.1 rillig local by_location = {}
60 1.1 rillig
61 1.1 rillig for _, line in ipairs(exp_lines) do
62 1.1 rillig ---@type string | nil, string, string
63 1.1 rillig local l_fname, l_lineno, l_msg =
64 1.12 rillig line:match('^make: ([^:]+):(%d+): (.*)')
65 1.1 rillig if l_fname ~= nil then
66 1.1 rillig local location = ("%s:%d"):format(l_fname, l_lineno)
67 1.1 rillig if by_location[location] == nil then
68 1.1 rillig by_location[location] = {}
69 1.1 rillig end
70 1.1 rillig table.insert(by_location[location], l_msg)
71 1.1 rillig end
72 1.1 rillig end
73 1.1 rillig
74 1.1 rillig return by_location
75 1.1 rillig end
76 1.1 rillig
77 1.1 rillig
78 1.7 rillig local function missing(by_location)
79 1.14 rillig ---@type {filename: string, lineno: number, location: string}[]
80 1.14 rillig local locations = {}
81 1.7 rillig
82 1.14 rillig for location in pairs(by_location) do
83 1.14 rillig local filename, lineno = location:match("^(%S+%.mk):(%d+)$")
84 1.14 rillig if filename then
85 1.14 rillig table.insert(locations, {
86 1.14 rillig filename = filename,
87 1.14 rillig lineno = tonumber(lineno),
88 1.14 rillig location = location
89 1.14 rillig })
90 1.7 rillig end
91 1.7 rillig end
92 1.14 rillig table.sort(locations, function(a, b)
93 1.7 rillig if a.filename ~= b.filename then
94 1.7 rillig return a.filename < b.filename
95 1.7 rillig end
96 1.7 rillig return a.lineno < b.lineno
97 1.7 rillig end)
98 1.14 rillig
99 1.14 rillig ---@type {location: string, message: string}[]
100 1.14 rillig local missing_expectations = {}
101 1.14 rillig for _, location in ipairs(locations) do
102 1.14 rillig for _, message in ipairs(by_location[location.location]) do
103 1.14 rillig if message ~= "" then
104 1.14 rillig table.insert(missing_expectations, {
105 1.14 rillig location = location.location,
106 1.14 rillig message = message
107 1.14 rillig })
108 1.14 rillig end
109 1.14 rillig end
110 1.14 rillig end
111 1.14 rillig
112 1.7 rillig return missing_expectations
113 1.7 rillig end
114 1.7 rillig
115 1.7 rillig
116 1.1 rillig local function check_mk(mk_fname)
117 1.1 rillig local exp_fname = mk_fname:gsub("%.mk$", ".exp")
118 1.1 rillig local mk_lines = load_lines(mk_fname)
119 1.1 rillig local exp_lines = load_lines(exp_fname)
120 1.1 rillig if exp_lines == nil then return end
121 1.1 rillig local by_location = collect_lineno_diagnostics(exp_lines)
122 1.1 rillig local prev_expect_line = 0
123 1.1 rillig
124 1.1 rillig for mk_lineno, mk_line in ipairs(mk_lines) do
125 1.9 rillig
126 1.9 rillig for text in mk_line:gmatch("#%s*expect%-not:%s*(.*)") do
127 1.9 rillig local i = 1
128 1.9 rillig while i <= #exp_lines and not exp_lines[i]:find(text, 1, true) do
129 1.9 rillig i = i + 1
130 1.9 rillig end
131 1.9 rillig if i <= #exp_lines then
132 1.9 rillig print_error("error: %s:%d: %s must not contain '%s'",
133 1.9 rillig mk_fname, mk_lineno, exp_fname, text)
134 1.9 rillig end
135 1.9 rillig end
136 1.9 rillig
137 1.11 rillig for text in mk_line:gmatch("#%s*expect%-not%-matches:%s*(.*)") do
138 1.11 rillig local i = 1
139 1.13 rillig while i <= #exp_lines and not exp_lines[i]:find(text) do
140 1.11 rillig i = i + 1
141 1.11 rillig end
142 1.11 rillig if i <= #exp_lines then
143 1.11 rillig print_error("error: %s:%d: %s must not match '%s'",
144 1.11 rillig mk_fname, mk_lineno, exp_fname, text)
145 1.11 rillig end
146 1.11 rillig end
147 1.11 rillig
148 1.1 rillig for text in mk_line:gmatch("#%s*expect:%s*(.*)") do
149 1.1 rillig local i = prev_expect_line
150 1.3 rillig -- As of 2022-04-15, some lines in the .exp files contain trailing
151 1.3 rillig -- whitespace. If possible, this should be avoided by rewriting the
152 1.11 rillig -- debug logging. When done, the trailing gsub can be removed.
153 1.3 rillig -- See deptgt-phony.exp lines 14 and 15.
154 1.11 rillig while i < #exp_lines and text ~= exp_lines[i + 1]:gsub("^%s*", ""):gsub("%s*$", "") do
155 1.1 rillig i = i + 1
156 1.1 rillig end
157 1.1 rillig if i < #exp_lines then
158 1.1 rillig prev_expect_line = i + 1
159 1.1 rillig else
160 1.1 rillig print_error("error: %s:%d: '%s:%d+' must contain '%s'",
161 1.1 rillig mk_fname, mk_lineno, exp_fname, prev_expect_line + 1, text)
162 1.1 rillig end
163 1.1 rillig end
164 1.2 rillig if mk_line:match("^#%s*expect%-reset$") then
165 1.2 rillig prev_expect_line = 0
166 1.2 rillig end
167 1.1 rillig
168 1.1 rillig ---@param text string
169 1.1 rillig for offset, text in mk_line:gmatch("#%s*expect([+%-]%d+):%s*(.*)") do
170 1.1 rillig local location = ("%s:%d"):format(mk_fname, mk_lineno + tonumber(offset))
171 1.1 rillig
172 1.1 rillig local found = false
173 1.1 rillig if by_location[location] ~= nil then
174 1.1 rillig for i, message in ipairs(by_location[location]) do
175 1.8 rillig if message == text then
176 1.1 rillig by_location[location][i] = ""
177 1.1 rillig found = true
178 1.1 rillig break
179 1.10 rillig elseif message ~= "" then
180 1.10 rillig print_error("error: %s:%d: out-of-order '%s'",
181 1.10 rillig mk_fname, mk_lineno, message)
182 1.1 rillig end
183 1.1 rillig end
184 1.1 rillig end
185 1.1 rillig
186 1.1 rillig if not found then
187 1.1 rillig print_error("error: %s:%d: %s must contain '%s'",
188 1.1 rillig mk_fname, mk_lineno, exp_fname, text)
189 1.1 rillig end
190 1.1 rillig end
191 1.4 rillig end
192 1.4 rillig
193 1.7 rillig for _, m in ipairs(missing(by_location)) do
194 1.7 rillig print_error("missing: %s: # expect+1: %s", m.location, m.message)
195 1.1 rillig end
196 1.1 rillig end
197 1.1 rillig
198 1.1 rillig for _, fname in ipairs(arg) do
199 1.1 rillig check_mk(fname)
200 1.1 rillig end
201 1.1 rillig os.exit(not had_errors)
202