Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/tests/usr.bin/xlint tests/lint: make error handling simpler



details:   https://anonhg.NetBSD.org/src/rev/111bd4d883ae
branches:  trunk
changeset: 1026786:111bd4d883ae
user:      rillig <rillig%NetBSD.org@localhost>
date:      Tue Dec 07 07:09:12 2021 +0000

description:
tests/lint: make error handling simpler

The previous form was better suited for integrated unit tests (as in
distrib/sets/fmt-list).  These small validation programs are easy enough
to be tested from the command line though.

diffstat:

 tests/usr.bin/xlint/check-expect.lua |  42 ++++++++++++++++++------------------
 1 files changed, 21 insertions(+), 21 deletions(-)

diffs (101 lines):

diff -r 113bdacc4c89 -r 111bd4d883ae tests/usr.bin/xlint/check-expect.lua
--- a/tests/usr.bin/xlint/check-expect.lua      Tue Dec 07 06:55:44 2021 +0000
+++ b/tests/usr.bin/xlint/check-expect.lua      Tue Dec 07 07:09:12 2021 +0000
@@ -1,5 +1,5 @@
 #!  /usr/bin/lua
--- $NetBSD: check-expect.lua,v 1.13 2021/09/05 19:16:37 rillig Exp $
+-- $NetBSD: check-expect.lua,v 1.14 2021/12/07 07:09:12 rillig Exp $
 
 --[[
 
@@ -11,6 +11,14 @@
 ]]
 
 
+local had_errors = false
+---@param fmt string
+function print_error(fmt, ...)
+  print(fmt:format(...))
+  had_errors = true
+end
+
+
 local function load_lines(fname)
   local lines = {}
 
@@ -26,7 +34,7 @@
 end
 
 
-local function load_expect_comments_from_c(fname, errors)
+local function load_expect_comments_from_c(fname)
 
   local lines = load_lines(fname)
   if lines == nil then return nil, nil end
@@ -61,7 +69,7 @@
     local ppl_lineno, ppl_fname = line:match("^#%s*(%d+)%s+\"([^\"]+)\"")
     if ppl_lineno ~= nil then
       if ppl_fname == fname and tonumber(ppl_lineno) ~= phys_lineno + 1 then
-        errors:add("error: %s:%d: preprocessor line number must be %d",
+        print_error("error: %s:%d: preprocessor line number must be %d",
           fname, phys_lineno, phys_lineno + 1)
       end
       pp_fname = ppl_fname
@@ -93,11 +101,11 @@
 end
 
 
-local function check_test(c_fname, errors)
+local function check_test(c_fname)
   local exp_fname = c_fname:gsub("%.c$", ".exp")
 
   local comment_locations, comments_by_location =
-    load_expect_comments_from_c(c_fname, errors)
+    load_expect_comments_from_c(c_fname)
   if comment_locations == nil then return end
 
   local messages = load_actual_messages_from_exp(exp_fname)
@@ -117,14 +125,15 @@
     end
 
     if not found then
-      errors:add("error: %s: missing /* expect+1: %s */", act.location, exp_comment)
+      print_error("error: %s: missing /* expect+1: %s */",
+        act.location, exp_comment)
     end
   end
 
   for _, location in ipairs(comment_locations) do
     for _, message in ipairs(comments_by_location[location]) do
       if message ~= "" then
-        errors:add(
+        print_error(
           "error: %s: declared message \"%s\" is not in the actual output",
           location, message)
       end
@@ -134,20 +143,11 @@
 
 
 local function main(args)
-  local errors = {}
-  errors.add = function(self, fmt, ...)
-    table.insert(self, string.format(fmt, ...))
+  for _, name in ipairs(args) do
+    check_test(name)
   end
-
-  for _, name in ipairs(args) do
-    check_test(name, errors)
-  end
-
-  for _, error in ipairs(errors) do
-    print(error)
-  end
-
-  return #errors == 0
 end
 
-os.exit(main(arg))
+
+main(arg)
+os.exit(not had_errors)



Home | Main Index | Thread Index | Old Index