fix(test_harness): floating results window (#520)

* 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.
This commit is contained in:
Matteo Golin 2023-09-12 09:47:13 -04:00 committed by GitHub
parent 4cd4c29e39
commit 9ce85b0f7d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 22 additions and 8 deletions

View file

@ -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", {
@ -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)
@ -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

View file

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