Skip to content

Commit

Permalink
refactor: move shared logic into new path lib (#98)
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrikaverpil authored Jul 5, 2024
1 parent 6d205fd commit 0aaf524
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 46 deletions.
3 changes: 2 additions & 1 deletion lua/neotest-golang/parse.lua
Original file line number Diff line number Diff line change
@@ -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")

Expand Down
39 changes: 39 additions & 0 deletions lua/neotest-golang/path.lua
Original file line number Diff line number Diff line change
@@ -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
45 changes: 2 additions & 43 deletions lua/neotest-golang/runspec_dir.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
--- a Go package.

local cmd = require("neotest-golang.cmd")
local path = require("neotest-golang.path")

local M = {}

Expand All @@ -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.
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions lua/neotest-golang/runspec_file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

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

0 comments on commit 0aaf524

Please sign in to comment.