Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
lucc committed Oct 7, 2023
1 parent 52bc400 commit 3a85d3b
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
2 changes: 1 addition & 1 deletion makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 17 additions & 3 deletions test/helpers.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand All @@ -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)
Expand Down
6 changes: 6 additions & 0 deletions test/meta_test.lua
Original file line number Diff line number Diff line change
@@ -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")
24 changes: 18 additions & 6 deletions test/native_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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("'", [['\'']]) .. "' " ..
Expand All @@ -46,6 +55,7 @@ local function run_test_file(filename, extra_args)
-- force quit nvimpager
"-c 'quitall!' " ..
extra_args
--)
)
end)

Expand All @@ -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

Expand All @@ -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)
Expand Down

0 comments on commit 3a85d3b

Please sign in to comment.