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