Home | History | Annotate | Line # | Download | only in unit-tests
check-expect.lua revision 1.6
      1 #!  /usr/bin/lua
      2 -- $NetBSD: check-expect.lua,v 1.6 2023/06/01 20:56:35 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 
     24 
     25 local had_errors = false
     26 ---@param fmt string
     27 function print_error(fmt, ...)
     28   print(fmt:format(...))
     29   had_errors = true
     30 end
     31 
     32 
     33 ---@return nil | string[]
     34 local function load_lines(fname)
     35   local lines = {}
     36 
     37   local f = io.open(fname, "r")
     38   if f == nil then return nil end
     39 
     40   for line in f:lines() do
     41     table.insert(lines, line)
     42   end
     43   f:close()
     44 
     45   return lines
     46 end
     47 
     48 
     49 ---@param exp_lines string[]
     50 local function collect_lineno_diagnostics(exp_lines)
     51   ---@type table<string, string[]>
     52   local by_location = {}
     53 
     54   for _, line in ipairs(exp_lines) do
     55     ---@type string | nil, string, string
     56     local l_fname, l_lineno, l_msg =
     57       line:match('^make: "([^"]+)" line (%d+): (.*)')
     58     if l_fname ~= nil then
     59       local location = ("%s:%d"):format(l_fname, l_lineno)
     60       if by_location[location] == nil then
     61         by_location[location] = {}
     62       end
     63       table.insert(by_location[location], l_msg)
     64     end
     65   end
     66 
     67   return by_location
     68 end
     69 
     70 
     71 local function check_mk(mk_fname)
     72   local exp_fname = mk_fname:gsub("%.mk$", ".exp")
     73   local mk_lines = load_lines(mk_fname)
     74   local exp_lines = load_lines(exp_fname)
     75   if exp_lines == nil then return end
     76   local by_location = collect_lineno_diagnostics(exp_lines)
     77   local prev_expect_line = 0
     78 
     79   for mk_lineno, mk_line in ipairs(mk_lines) do
     80     for text in mk_line:gmatch("#%s*expect:%s*(.*)") do
     81       local i = prev_expect_line
     82       -- As of 2022-04-15, some lines in the .exp files contain trailing
     83       -- whitespace.  If possible, this should be avoided by rewriting the
     84       -- debug logging.  When done, the gsub can be removed.
     85       -- See deptgt-phony.exp lines 14 and 15.
     86       while i < #exp_lines and text ~= exp_lines[i + 1]:gsub("%s*$", "") do
     87         i = i + 1
     88       end
     89       if i < #exp_lines then
     90         prev_expect_line = i + 1
     91       else
     92         print_error("error: %s:%d: '%s:%d+' must contain '%s'",
     93           mk_fname, mk_lineno, exp_fname, prev_expect_line + 1, text)
     94       end
     95     end
     96     if mk_line:match("^#%s*expect%-reset$") then
     97       prev_expect_line = 0
     98     end
     99 
    100     ---@param text string
    101     for offset, text in mk_line:gmatch("#%s*expect([+%-]%d+):%s*(.*)") do
    102       local location = ("%s:%d"):format(mk_fname, mk_lineno + tonumber(offset))
    103 
    104       local found = false
    105       if by_location[location] ~= nil then
    106         for i, message in ipairs(by_location[location]) do
    107           if message ~= "" and message:find(text, 1, true) then
    108             by_location[location][i] = ""
    109             found = true
    110             break
    111           end
    112         end
    113       end
    114 
    115       if not found then
    116         print_error("error: %s:%d: %s must contain '%s'",
    117           mk_fname, mk_lineno, exp_fname, text)
    118       end
    119     end
    120   end
    121 
    122   -- XXX: The messages are not sorted in any meaningful way.
    123   for location, messages in pairs(by_location) do
    124     for _, message in ipairs(messages) do
    125       if message ~= "" and location:find(".mk:") then
    126         print_error("missing: %s: # expect+1: %s", location, message)
    127       end
    128     end
    129   end
    130 end
    131 
    132 for _, fname in ipairs(arg) do
    133   check_mk(fname)
    134 end
    135 os.exit(not had_errors)
    136