From 0aaf5242fd9fcebfabb6e8183a2fa1c7dff53988 Mon Sep 17 00:00:00 2001 From: Fredrik Averpil Date: Fri, 5 Jul 2024 09:05:45 +0200 Subject: [PATCH] refactor: move shared logic into new path lib (#98) --- lua/neotest-golang/parse.lua | 3 +- lua/neotest-golang/path.lua | 39 +++++++++++++++++++++++++ lua/neotest-golang/runspec_dir.lua | 45 ++--------------------------- lua/neotest-golang/runspec_file.lua | 4 +-- 4 files changed, 45 insertions(+), 46 deletions(-) create mode 100644 lua/neotest-golang/path.lua diff --git a/lua/neotest-golang/parse.lua b/lua/neotest-golang/parse.lua index 2bc0090..424e5df 100644 --- a/lua/neotest-golang/parse.lua +++ b/lua/neotest-golang/parse.lua @@ -1,4 +1,5 @@ ---- Parse the output from the go test command. +--- This file is centered around the parsing/processing of test execution output +--- and assembling of the final results to hand back over to Neotest. local async = require("neotest.async") diff --git a/lua/neotest-golang/path.lua b/lua/neotest-golang/path.lua new file mode 100644 index 0000000..7077766 --- /dev/null +++ b/lua/neotest-golang/path.lua @@ -0,0 +1,39 @@ +--- Helpers around filepaths. + +local M = {} + +--- Find a file upwards in the directory tree and return its path, if found. +--- @param filename string +--- @param start_path string +--- @return string | nil +function M.find_file_upwards(filename, start_path) + local scan = require("plenary.scandir") + local cwd = vim.fn.getcwd() + local found_filepath = nil + while start_path ~= cwd do + local files = scan.scan_dir( + start_path, + { search_pattern = filename, hidden = true, depth = 1 } + ) + if #files > 0 then + found_filepath = files[1] + break + end + start_path = vim.fn.fnamemodify(start_path, ":h") -- go up one directory + end + + if found_filepath == nil then + -- check if filename exists in the current directory + local files = scan.scan_dir( + start_path, + { search_pattern = filename, hidden = true, depth = 1 } + ) + if #files > 0 then + found_filepath = files[1] + end + end + + return found_filepath +end + +return M diff --git a/lua/neotest-golang/runspec_dir.lua b/lua/neotest-golang/runspec_dir.lua index cb099d8..d62945b 100644 --- a/lua/neotest-golang/runspec_dir.lua +++ b/lua/neotest-golang/runspec_dir.lua @@ -2,6 +2,7 @@ --- a Go package. local cmd = require("neotest-golang.cmd") +local path = require("neotest-golang.path") local M = {} @@ -14,7 +15,7 @@ local M = {} --- @param pos neotest.Position --- @return neotest.RunSpec | nil function M.build(pos) - local go_mod_filepath = M.find_file_upwards("go.mod", pos.path) + local go_mod_filepath = path.find_file_upwards("go.mod", pos.path) if go_mod_filepath == nil then -- if no go.mod file was found up the directory tree, until reaching $CWD, -- then we cannot determine the Go project root. @@ -75,46 +76,4 @@ function M.fail_fast(pos) return run_spec end ---- Find a file upwards in the directory tree and return its path, if found. ---- @param filename string ---- @param start_path string ---- @return string | nil -function M.find_file_upwards(filename, start_path) - local scan = require("plenary.scandir") - local cwd = vim.fn.getcwd() - local found_filepath = nil - while start_path ~= cwd do - local files = scan.scan_dir( - start_path, - { search_pattern = filename, hidden = true, depth = 1 } - ) - if #files > 0 then - found_filepath = files[1] - break - end - start_path = vim.fn.fnamemodify(start_path, ":h") -- go up one directory - end - - if found_filepath == nil then - -- check if filename exists in the current directory - local files = scan.scan_dir( - start_path, - { search_pattern = filename, hidden = true, depth = 1 } - ) - if #files > 0 then - found_filepath = files[1] - end - end - - return found_filepath -end - -function M.remove_base_path(base_path, target_path) - if string.find(target_path, base_path, 1, true) == 1 then - return string.sub(target_path, string.len(base_path) + 2) - end - - return target_path -end - return M diff --git a/lua/neotest-golang/runspec_file.lua b/lua/neotest-golang/runspec_file.lua index 219dbfe..617582f 100644 --- a/lua/neotest-golang/runspec_file.lua +++ b/lua/neotest-golang/runspec_file.lua @@ -2,7 +2,7 @@ local cmd = require("neotest-golang.cmd") local convert = require("neotest-golang.convert") -local runspec_dir = require("neotest-golang.runspec_dir") +local path = require("neotest-golang.path") local M = {} @@ -15,7 +15,7 @@ function M.build(pos, tree) return M.fail_fast(pos) end - local go_mod_filepath = runspec_dir.find_file_upwards("go.mod", pos.path) + local go_mod_filepath = path.find_file_upwards("go.mod", pos.path) if go_mod_filepath == nil then -- if no go.mod file was found up the directory tree, until reaching $CWD, -- then we cannot determine the Go project root.