From 8b0a5d00fbc33c6c2596167f152e1a5cc216439a Mon Sep 17 00:00:00 2001 From: Steve Vermeulen Date: Mon, 18 Sep 2023 09:49:07 +0800 Subject: [PATCH 1/2] Fixed issue where json fails to encode and then no errors are output --- busted/core.lua | 2 +- busted/outputHandlers/json.lua | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/busted/core.lua b/busted/core.lua index a00912db..6250e108 100644 --- a/busted/core.lua +++ b/busted/core.lua @@ -79,7 +79,7 @@ return function() end info.traceback = debug.traceback('', level) - info.message = msg + info.message = tostring(msg) local file = busted.getFile(element) return file and file.getTrace(file.name, info) or trimTrace(info) diff --git a/busted/outputHandlers/json.lua b/busted/outputHandlers/json.lua index 880e639d..f7aa8668 100644 --- a/busted/outputHandlers/json.lua +++ b/busted/outputHandlers/json.lua @@ -7,13 +7,23 @@ return function(options) local handler = require 'busted.outputHandlers.base'() handler.suiteEnd = function() - io_write(json.encode({ + local error_info = { pendings = handler.pendings, successes = handler.successes, failures = handler.failures, errors = handler.errors, duration = handler.getDuration() - })) + } + local ok, result = pcall(function() + return json.encode(error_info) + end) + + if ok then + io_write(result) + else + io_write("Failed to encode test results to json: " .. result) + end + io_write("\n") io_flush() From 0453e157879734eb0594e2cf537d7bcee067f63e Mon Sep 17 00:00:00 2001 From: Steve Vermeulen Date: Mon, 18 Sep 2023 18:18:54 +0800 Subject: [PATCH 2/2] Response to code review to use pcall correctly --- busted/outputHandlers/json.lua | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/busted/outputHandlers/json.lua b/busted/outputHandlers/json.lua index f7aa8668..97a07f37 100644 --- a/busted/outputHandlers/json.lua +++ b/busted/outputHandlers/json.lua @@ -14,9 +14,7 @@ return function(options) errors = handler.errors, duration = handler.getDuration() } - local ok, result = pcall(function() - return json.encode(error_info) - end) + local ok, result = pcall(json.encode, error_info) if ok then io_write(result)