From 11829b2b83f1ce9a4f155fac4fd05d6f9309b28d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?LAURENS=20J=C3=A9r=C3=B4me?= Date: Wed, 3 Jul 2024 08:46:33 +0200 Subject: [PATCH] report open error properly --- build.lua | 14 ++++++++++---- l3build-check.lua | 18 +++++++++--------- l3build-ctan.lua | 2 +- l3build-manifest-setup.lua | 2 +- l3build-manifest.lua | 5 +++-- l3build-tagging.lua | 5 ++++- l3build-unpack.lua | 5 +++-- l3build-upload.lua | 19 +++++++++++-------- l3build-zip.lua | 2 +- l3build.dtx | 2 +- 10 files changed, 44 insertions(+), 30 deletions(-) diff --git a/build.lua b/build.lua index 453c393..d4b5ef8 100644 --- a/build.lua +++ b/build.lua @@ -103,8 +103,13 @@ function docinit_hook() local insert = table.insert local open = io.open - local f = open("README.md","rb") - local readme = f:read("*all") + ---@type file*? + local f = assert(open("README.md","rb")) + ---@cast f file* + local readme = f:read("a") + f:close() + f = nil + local date_start,date_end = find(readme,"%d%d%d%d%p%d%d%p%d%d") local man_t = {} @@ -121,9 +126,10 @@ function docinit_hook() insert(man_t,overview) local cmd = "texlua ./" .. module .. ".lua --help" - local f = assert(io.popen(cmd,"r")) - local help_text = assert(f:read("*a")) + f = assert(io.popen(cmd,"r")) + local help_text = assert(f:read("a")) f:close() + f = nil insert(man_t,(help_text:gsub("\nUsage.*names>]\n\n","") :gsub("Valid targets",".SH COMMANDS\nValid targets") diff --git a/l3build-check.lua b/l3build-check.lua index 11c8973..a0c1ba7 100644 --- a/l3build-check.lua +++ b/l3build-check.lua @@ -95,10 +95,10 @@ function checkinit_hook() return 0 end local function rewrite(source,result,processor,...) local file = assert(open(source,"rb")) - local content = gsub(file:read("*all") .. "\n","\r\n","\n") + local content = gsub(file:read("a") .. "\n","\r\n","\n") close(file) local new_content = processor(content,...) - local newfile = open(result,"w") + local newfile = assert(open(result,"w")) output(newfile) write(new_content) close(newfile) @@ -919,7 +919,7 @@ local function showsavecommands(failurelist) end end print(" To regenerate the test files, run\n") - local f = open(testdir .. "/.savecommands", "w") + local f = assert(open(testdir .. "/.savecommands", "w")) for _, cmds in pairs(savecmds) do print(" " .. cmds) f:write(cmds, "\n") @@ -1089,9 +1089,9 @@ function showfailedlog(name) for _,i in ipairs(ordered_filelist(testdir, name..".log")) do print(" - " .. testdir .. "/" .. i) print("") - local f = open(testdir .. "/" .. i,"r") - local content = f:read("*all") - close(f) + local f = assert(open(testdir .. "/" .. i,"r")) + local content = f:read("a") + f:close() print("-----------------------------------------------------------------------------------") print(content) print("-----------------------------------------------------------------------------------") @@ -1103,9 +1103,9 @@ function showfaileddiff() for _,i in ipairs(ordered_filelist(testdir, "*" .. os_diffext)) do print(" - " .. testdir .. "/" .. i) print("") - local f = open(testdir .. "/" .. i,"r") - local content = f:read("*all") - close(f) + local f = assert(open(testdir .. "/" .. i,"r")) + local content = f:read("a") + f:close() print("-----------------------------------------------------------------------------------") print(content) print("-----------------------------------------------------------------------------------") diff --git a/l3build-ctan.lua b/l3build-ctan.lua index c1feb25..167cd33 100644 --- a/l3build-ctan.lua +++ b/l3build-ctan.lua @@ -85,7 +85,7 @@ function ctan() options["engine"] = nil local function dirzip(dir, zipname) zipname = zipname .. ".zip" - local zip = newzip(dir .. '/' .. zipname) + local zip = assert(newzip(dir .. '/' .. zipname)) local function tab_to_check(table) local patterns = {} for n,i in ipairs(table) do diff --git a/l3build-manifest-setup.lua b/l3build-manifest-setup.lua index 8654b3d..55c5688 100644 --- a/l3build-manifest-setup.lua +++ b/l3build-manifest-setup.lua @@ -335,7 +335,7 @@ end -- From the first match of a pattern in a file: manifest_extract_filedesc = function(filehandle) - local all_file = filehandle:read("*all") + local all_file = filehandle:read("a") local matchstr = "\\section{(.-)}" filedesc = string.match(all_file,matchstr) diff --git a/l3build-manifest.lua b/l3build-manifest.lua index aa5a061..7f7fdf1 100644 --- a/l3build-manifest.lua +++ b/l3build-manifest.lua @@ -153,6 +153,7 @@ manifest_build_init = function(entry) end +local open = io.open manifest_build_file = function(entry,this_file) @@ -173,7 +174,7 @@ manifest_build_file = function(entry,this_file) if not(entry.skipfiledescription) then - local ff = assert(io.open(entry.dir .. "/" .. this_file, "r")) + local ff = assert(open(entry.dir .. "/" .. this_file, "r")) this_descr = manifest_extract_filedesc(ff,this_file) ff:close() @@ -197,7 +198,7 @@ end manifest_write = function(manifest_entries) - local f = assert(io.open(manifestfile, "w")) + local f = assert(open(manifestfile, "w")) manifest_write_opening(f) for ii,vv in ipairs(manifest_entries) do diff --git a/l3build-tagging.lua b/l3build-tagging.lua index 0753123..225ba23 100644 --- a/l3build-tagging.lua +++ b/l3build-tagging.lua @@ -39,9 +39,12 @@ end local function update_file_tag(file,tagname,tagdate) local filename = basename(file) print("Tagging ".. filename) + ---@type file*? local f = assert(open(file,"rb")) - local content = f:read("*all") + ---@cast f file* + local content = f:read("a") f:close() + f = nil -- Deal with Unix/Windows line endings content = gsub(content .. (match(content,"\n$") and "" or "\n"), "\r\n", "\n") diff --git a/l3build-unpack.lua b/l3build-unpack.lua index 3c73dc8..ba8a5b5 100644 --- a/l3build-unpack.lua +++ b/l3build-unpack.lua @@ -69,11 +69,12 @@ function bundleunpack(sourcedirs, sources) return errorlevel end end + local popen = io.popen for _,i in ipairs(unpackfiles) do for _,p in ipairs(tree(unpackdir, i)) do local path, name = splitpath(p.src) local localdir = abspath(localdir) - local success = io.popen( + local success = assert(popen( "cd " .. unpackdir .. "/" .. path .. os_concat .. os_setenv .. " TEXINPUTS=." .. os_pathsep .. localdir .. (unpacksearch and os_pathsep or "") .. @@ -84,7 +85,7 @@ function bundleunpack(sourcedirs, sources) unpackexe .. " " .. unpackopts .. " " .. name .. (options["quiet"] and (" > " .. os_null) or ""), "w" - ):write(string.rep("y\n", 300)):close() + ):write(string.rep("y\n", 300))):close() if not success then return 1 end diff --git a/l3build-upload.lua b/l3build-upload.lua index 87158d1..645445f 100644 --- a/l3build-upload.lua +++ b/l3build-upload.lua @@ -94,9 +94,9 @@ function upload(tagnames) -- Get data from command line if appropriate if options["file"] then - local f = open(options["file"],"r") - uploadconfig.announcement = assert(f:read('*a')) - close(f) + local f = assert(open(options["file"],"r")) + uploadconfig.announcement = assert(f:read('a')) + f:close() end uploadconfig.announcement = options["message"] or uploadconfig.announcement or file_contents(uploadconfig.announcement_file) uploadconfig.email = options["email"] or uploadconfig.email @@ -139,10 +139,13 @@ function upload(tagnames) -- curl file version local curloptfile = uploadconfig.curlopt_file or (ctanzip .. ".curlopt") - local curlopt=open(curloptfile,"w") + ---@type file*? + local curlopt=assert(open(curloptfile,"w")) + ---@cast curlopt file* output(curlopt) write(ctan_post) - close(curlopt) + curlopt:close() + curlopt = nil ctan_post=curlexe .. " --config " .. curloptfile @@ -376,12 +379,12 @@ end -- if filename is non nil and file readable return contents otherwise nil function file_contents (filename) if filename ~= nil then - local f= open(filename,"r") + local f= assert(open(filename,"r")) if f==nil then return nil else - local s = f:read("*all") - close(f) + local s = f:read("a") + f:close() return s end else diff --git a/l3build-zip.lua b/l3build-zip.lua index 5107415..0c326b7 100644 --- a/l3build-zip.lua +++ b/l3build-zip.lua @@ -132,7 +132,7 @@ local meta = {__index = { }} return function(filename) - local f, msg = open(filename, 'wb') + local f, msg = open(filename, 'wb') -- closed just above if not f then return f, msg end return setmetatable({ f = f, diff --git a/l3build.dtx b/l3build.dtx index 12d662e..e9f0b3d 100644 --- a/l3build.dtx +++ b/l3build.dtx @@ -2144,7 +2144,7 @@ % \begin{verbatim} % manifest_extract_filedesc = function(filehandle,filename) % -% local all_file = filehandle:read("*all") +% local all_file = filehandle:read("a") % local matchstr = "\\section{(.-)}" % % filedesc = string.match(all_file,matchstr)