1
0
Fork 0
mirror of https://github.com/pnx/neotest-phpunit synced 2026-06-16 03:54:55 +02:00

Bunch of config reworks, better sail configurability

This commit is contained in:
V13Axel 2024-02-16 22:35:36 -05:00
parent 4a1cde9121
commit a8263451f5
2 changed files with 116 additions and 44 deletions

View file

@ -1,47 +1,118 @@
local logger = require('neotest.logging')
local ok, async = pcall(require, "nio")
if not ok then
async = require("neotest.async")
end
local is_callable = function(obj)
return type(obj) == "function" or (type(obj) == "table" and obj.__call)
end
local M = {
opts = {},
env = {
root_ignore_files = {},
root_files = { "tests/Pest.php" },
filter_dirs = { "vendor" },
test_file_suffix = { "Test.php" },
autostart_sail = false,
sail_executable = "vendor/bin/sail",
},
_sail_error = false,
_sail_running = false,
}
function M.get(key)
if M.opts[key] then
if is_callable(M.opts[key]) then
return M.opts[key]()
end
return M.opts[key]
if is_callable(M.env[key] or nil) then
return M.env[key]()
end
return M[key]()
return M.env[key] or {}
end
function M.enable_sail()
if vim.fn.filereadable("vendor/bin/sail") ~= 1 then
function M.env.sail_enabled()
if M.sail_available() == false then
logger.error("Sail executable not found")
return false
end
logger.debug("Attempting to check if sail is running")
local sail_ps_output = vim.fn.system("vendor/bin/sail ps | wc -l")
logger.debug("Sail ps output:", sail_ps_output)
if sail_ps_output > 1 then
logger.debug("Sail is running")
if M.sail_running() then
logger.info("Sail is already running")
return true
end
logger.debug("Sail is not running")
if M.get('autostart_sail') then
return M.start_sail();
end
vim.api.nvim_echo({ {
"Sail not running, and not configured to autostart! add 'autostart_sail = true' to your config or start it manually",
"None",
} }, false, {})
return false
end
function M.env.results_path()
if M.get('sail_enabled') then
return "storage/app/" .. os.date("junit-%Y%m%d-%H%M%S")
end
return async.fn.tempname()
end
function M.sail_error()
return M._sail_error
end
function M.sail_available()
return vim.fn.filereadable(M.get('sail_executable')) == 1
end
function M.sail_running()
if (M._sail_running) then
return true
end
logger.info("Attempting to check if sail is running")
local sail_ps_output = vim.fn.system("vendor/bin/sail ps | wc -l")
logger.info("Sail ps output:", sail_ps_output)
if tonumber(sail_ps_output) > 1 then
logger.info("Sail is running")
M._sail_running = true
return true
end
logger.info("Sail is not running")
return false
end
function M.pest_cmd()
function M.start_sail()
logger.info("Attempting to start sail")
vim.api.nvim_echo({ { "Sail not running! Attempting to start it...", "None" } }, false, {})
local sail_up_output = vim.fn.system("vendor/bin/sail up -d") or ""
logger.info("Sail up output:", sail_up_output)
if vim.v.shell_error == 0 then
logger.info("Sail started successfully")
vim.api.nvim_echo({ { "Sail started!", "None" } }, false, {})
M._sail_running = true
return true
end
logger.error("Failed to start sail")
logger.error("Sail up output:", sail_up_output)
logger.error("Sail up error:", vim.v.shell_error)
vim.api.nvim_echo({ { "Failed to start sail!", "ErrorMsg" } }, false, {})
M._sail_error = true
return false
end
function M.env.pest_cmd()
local binary = "pest"
if vim.fn.filereadable("vendor/bin/pest") == 1 then
@ -51,20 +122,10 @@ function M.pest_cmd()
return binary
end
function M.env()
return {}
end
function M.root_ignore_files()
return {}
end
function M.root_files()
return { "tests/Pest.php" }
end
function M.filter_dirs()
return { "tests" }
function M.merge(env)
for key, value in pairs(env) do
M.env[key] = value
end
end
return M

View file

@ -53,18 +53,21 @@ end
---@return boolean True when matching
function NeotestAdapter.filter_dir(name, rel_path, root)
for _, filter_dir in ipairs(config.get("filter_dirs")) do
if vim.startswith(rel_path, filter_dir) then return true end
if name == filter_dir then return false end
end
return false
return true
end
---@async
---@param file_path string
---@return boolean
function NeotestAdapter.is_test_file(file_path)
logger.info("Checking file" .. file_path)
return vim.endswith(file_path, "Test.php")
for _, suffix in ipairs(config.get("test_file_suffix")) do
if vim.endswith(file_path, suffix) then return true end
end
return false
end
function NeotestAdapter.discover_positions(path)
@ -91,26 +94,32 @@ end
---@return neotest.RunSpec | nil
function NeotestAdapter.build_spec(args)
local position = args.tree:data()
local results_path = "storage/app/" .. os.date("junit-%Y%m%d-%H%M%S")
local results_path = config.get('results_path')
local binary = config.get('pest_cmd')
local binary = config.get_pest_cmd()
logger.info("Building spec for:", position)
logger.info("Results path:", results_path)
local command = {}
if config.enable_sail() then
if config.get('sail_enabled') then
logger.info("Sail enabled")
command = vim.tbl_flatten({
"vendor/bin/sail", "bin", "pest",
position.name ~= "tests" and ("/var/www/html" .. string.sub(position.path, string.len(vim.loop.cwd()) + 1)),
"--log-junit=" .. results_path,
position.name ~= "tests" and ("/var/www/html" .. string.sub(position.path, string.len(vim.loop.cwd() or "") + 1)),
})
else
logger.info("Sail not enabled")
command = vim.tbl_flatten({
binary,
position.name ~= "tests" and position.path,
"--log-junit=" .. results_path,
})
end
command = vim.tbl_flatten({
command,
"--log-junit=" .. results_path,
})
if position.type == "test" then
local script_args = vim.tbl_flatten({
@ -124,6 +133,8 @@ function NeotestAdapter.build_spec(args)
})
end
logger.info("Command:", command)
return {
command = command,
context = {
@ -165,7 +176,7 @@ setmetatable(NeotestAdapter, {
__call = function(_, opts)
logger.info("Initializing opts")
config.opts = opts or {}
config.merge(opts or {})
return NeotestAdapter
end,