Skip to content

Commit

Permalink
fix(test_harness): floating results window (#520)
Browse files Browse the repository at this point in the history
* Resolved invalid escape sequence error caused by Windows backslash in filepaths.

* PlenaryBustedDirectory now works with no path issues, and the PlenaryTestFile keymap now properly tests only the file.

* Refactored test running and floating window logic out into _test_paths and made test_directory only responsible for passing a list of test files to the new _test_paths function. Finally added a test_file function which calls _test_paths with only one path: the current file.

* Update fix to be unique to Windows machines. For some reason, Path:absolute behaves differently on Linux.
  • Loading branch information
linguini1 authored Sep 12, 2023
1 parent 4cd4c29 commit 9ce85b0
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
26 changes: 20 additions & 6 deletions lua/plenary/test_harness.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,7 @@ function harness.test_directory_command(command)
return harness.test_directory(directory, opts)
end

function harness.test_directory(directory, opts)
print "Starting..."
directory = directory:gsub("\\", "/")
local function test_paths(paths, opts)
local minimal = not opts or not opts.init or opts.minimal or opts.minimal_init

opts = vim.tbl_deep_extend("force", {
Expand Down Expand Up @@ -78,10 +76,7 @@ function harness.test_directory(directory, opts)

local outputter = headless and print_output or get_nvim_output(res.job_id)

local paths = harness._find_files_to_run(directory)

local path_len = #paths

local failure = false

local jobs = vim.tbl_map(function(p)
Expand Down Expand Up @@ -183,6 +178,25 @@ function harness.test_directory(directory, opts)
end
end

function harness.test_directory(directory, opts)
print "Starting..."
directory = directory:gsub("\\", "/")
local paths = harness._find_files_to_run(directory)

-- Paths work strangely on Windows, so lets have abs paths
if vim.fn.has "win32" == 1 or vim.fn.has "win64" == 1 then
paths = vim.tbl_map(function(p)
return Path:new(directory, p.filename)
end, paths)
end

test_paths(paths, opts)
end

function harness.test_file(filepath)
test_paths { Path:new(filepath) }
end

function harness._find_files_to_run(directory)
local finder
if vim.fn.has "win32" == 1 or vim.fn.has "win64" == 1 then
Expand Down
4 changes: 2 additions & 2 deletions plugin/plenary.vim
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@

" Create command for running busted
command! -nargs=1 -complete=file PlenaryBustedFile
\ lua require('plenary.busted').run(vim.fn.expand([[<args>]]))
\ lua require('plenary.test_harness').test_file([[<args>]])

command! -nargs=+ -complete=file PlenaryBustedDirectory
\ lua require('plenary.test_harness').test_directory_command([[<args>]])

nnoremap <Plug>PlenaryTestFile :lua require('plenary.test_harness').test_directory(vim.fn.expand("%:p"))<CR>
nnoremap <Plug>PlenaryTestFile :lua require('plenary.test_harness').test_file(vim.fn.expand("%:p"))<CR>

0 comments on commit 9ce85b0

Please sign in to comment.