From 1b0b6d14d449e19ea9050fcc0904f967a974591b Mon Sep 17 00:00:00 2001 From: Jon Michael Aanes Date: Fri, 26 Jan 2018 13:09:53 +0100 Subject: [PATCH] =?UTF-8?q?Added=20support=20for=20files=20loaded=20throug?= =?UTF-8?q?h=20L=C3=96VE,=20and=20added=20error-handling=20if=20no=20file?= =?UTF-8?q?=20can=20be=20found.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- assert-gooder.lua | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/assert-gooder.lua b/assert-gooder.lua index a996b85..d060d0e 100644 --- a/assert-gooder.lua +++ b/assert-gooder.lua @@ -148,19 +148,38 @@ end -------------------------------------------------------------------------------- +local function get_module_filetext (module_filepath) + assert(type(module_filepath) == 'string') + + -- Just attempt standard file open + local filehandle = io.open(module_filepath, 'r') + if filehandle then + filetext = filehandle:read '*all' + filehandle:close() + return filetext + end + + -- What about LÖVE? + local filetext = love and love.filesystem and love.filesystem.read(module_filepath) or nil + if filetext then return filetext end + + -- I give up... + return nil +end + local function get_assert_body_text (call_info) if call_info.what == 'Lua' or call_info.what == 'main' then -- Find filetext local filetext = nil if call_info.source:find '^@' then - local filehandle = io.open(call_info.short_src, 'r') - filetext = filehandle:read '*all' - filehandle:close() + filetext = get_module_filetext(call_info.short_src) elseif call_info.short_src:find '^%[string' then filetext = call_info.source else error 'Not implemented yet!' end + -- If cannot find + if not filetext then return nil end -- Get lines local filetext = filetext .. '\n' local lines_after, line_i = {}, 0 @@ -264,6 +283,9 @@ local function determine_error_message (call_info, msg, condition) -- Get assert body. local body_text = get_assert_body_text(call_info) + -- If we couldn't find the body text, we give up. + if not body_text then return end + -- Simplest formatting. -- No analysis of the assert-body, just report that it failed, -- along with it's body.