From 3a85d3bb1434da22142fba7695e6555d9f985c03 Mon Sep 17 00:00:00 2001 From: Lucas Hoffmann Date: Sat, 7 Oct 2023 20:37:55 +0200 Subject: [PATCH] debug --- makefile | 2 +- test/helpers.lua | 20 +++++++++++++++++--- test/meta_test.lua | 6 ++++++ test/native_spec.lua | 24 ++++++++++++++++++------ 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/makefile b/makefile index f92964c..5001a72 100644 --- a/makefile +++ b/makefile @@ -35,7 +35,7 @@ nvimpager.1: nvimpager.md LPATH = --lpath "lua/?.lua" --lpath "lua/?/init.lua" \ --lpath "?.lua" --lpath "?/init.lua" test: - @$(BUSTED) $(LPATH) test + $(BUSTED) $(LPATH) test luacov.stats.out: nvimpager lua/nvimpager/*.lua test/unit_spec.lua @$(BUSTED) $(LPATH) --coverage test/unit_spec.lua luacov.report.out: luacov.stats.out diff --git a/test/helpers.lua b/test/helpers.lua index 14ff7eb..7f1e50f 100644 --- a/test/helpers.lua +++ b/test/helpers.lua @@ -30,11 +30,25 @@ local function run(command) -- https://www.lua.org/manual/5.2/manual.html#pdf-os.execute -- https://stackoverflow.com/questions/7607384 command = string.format( - "export XDG_CONFIG_HOME=%s XDG_DATA_HOME=%s; (%s) 2>&1; echo $?", + "export XDG_CONFIG_HOME=%s XDG_DATA_HOME=%s; (%s) 2>&1; x=$?; if [ $x -eq 0 ];then echo alles ok; else echo Fehler passiert mit $x als code; fi; echo $x", confdir, datadir, command) local proc = io.popen(command) if proc == nil then error("Could not open pipe to child process") end local output = proc:read('*a') + local clean = output--:gsub("\27%[%?25h", "") -- show the cursor + --:gsub("\27%[%?25l", "") -- hide the cursor + --:gsub("\27%[%?1004h", "") -- enable focus reporting + --:gsub("\27%[%?1004l", "") -- disable focus reporting + --:gsub("\27%[%?1049h", "") -- enable alternative screen + --:gsub("\27%[%?1049l", "") -- disable alternative screen + --:gsub("\27%[%?2004l", "") -- enable bracketed paste + --:gsub("\27%[%?2004h", "") -- disable bracketed paste + + + + local printable = clean:gsub("%c", function(char) return "\n{" .. char:byte() .. "}" end) + print("PRINTING COMMAND OUTPUT IN HELPER FUNCTION\n"..printable) + local status = {proc:close()} -- This is *not* the return value of the command. assert.equal(true, status[1]) @@ -43,12 +57,12 @@ local function run(command) -- assert.equal(0, status[3]) -- For Lua 5.1 we have echoed the return status with the output. First we -- assert the last two bytes, which is easy: - assert.equal("0\n", output:sub(-2), "command failed") + assert.equal("0\n", output:sub(-2), "command failed foo") -- When the original command did not produce any output this is it. if #output ~= 2 then -- Otherwise we can only hope that the command did not produce a digit as -- it's last character of output. - assert.is_nil(tonumber(output:sub(-3, -3)), "command failed") + assert.is_nil(tonumber(output:sub(-3, -3)), "command failed bar") end -- If the assert succeeded we can remove two bytes from the end. return output:sub(1, -3) diff --git a/test/meta_test.lua b/test/meta_test.lua index d53d54f..ee28119 100644 --- a/test/meta_test.lua +++ b/test/meta_test.lua @@ -1 +1,7 @@ +-- ensure that we open a file +vim.cmd.edit("test/meta_test.lua") +-- generate an error +vim.fn.assert_report("this is an error") +vim.fn.assert_report("this is an error") +vim.fn.assert_report("this is an error") vim.fn.assert_report("this is an error") diff --git a/test/native_spec.lua b/test/native_spec.lua index 27d9ab5..df4ac2f 100644 --- a/test/native_spec.lua +++ b/test/native_spec.lua @@ -13,6 +13,14 @@ local helpers = require("test/helpers") -- returned by this function (as a single string) local function run_test_file(filename, extra_args) extra_args = extra_args or "" + + -- Check that the input file exists + local infile, err1 = io.open(filename) + if infile == nil then + error(err1) + end + infile:close() + -- create an output file to transport the errors from within neovim to -- busted (it is complicated to write to stdout or stderr from within neovim -- and catch that from the outside) @@ -22,18 +30,19 @@ local function run_test_file(filename, extra_args) -- We write a marker to the output file in order to detect if the -- nvimpager command below really writes to the file. If it does write to -- the file our marker should be overwritten. - local handle, err1 = io.open(outfile, "w") + local handle, err2 = io.open(outfile, "w") if handle == nil then - error(err1) + error(err2) end handle:write("This should be overwritten") handle:close() -- run the actual test in protected mode in order to clean up the temp file -- if anything fails - local status, err2 = pcall(function() + local status, err3 = pcall(function() -- Run the given test file in nvimpager and write all errors from the -- assert_* functions into outfile. + --print( helpers.run( -- define an environment variable to hold the output file name "OUTFILE='" .. outfile:gsub("'", [['\'']]) .. "' " .. @@ -46,6 +55,7 @@ local function run_test_file(filename, extra_args) -- force quit nvimpager "-c 'quitall!' " .. extra_args + --) ) end) @@ -60,13 +70,15 @@ local function run_test_file(filename, extra_args) if status then return output else - error(err2) + error(err3) end end local function test(title, filename, extra_args) it(title, function() - assert.equal("", run_test_file(filename, extra_args)) + local result = run_test_file(filename, extra_args) + print(result) + assert.equal("", result) end) end @@ -78,7 +90,7 @@ describe("native", function() local output = run_test_file("test/meta_test.lua") assert.not_nil(output:find("this is an error")) end) - it("detects aborted test scripts", function() + it("detects aborted test scripts #debug", function() local output = run_test_file("test/abort_test.lua") assert.equal("This should be overwritten", output) end)