Skip to content

Fix umbrella app detection #88

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions lua/elixir/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,10 @@ function M.root_dir(fname)
fname = vim.fn.getcwd()
end

local child_or_root_path =
vim.fs.dirname(vim.fs.find({ "mix.exs", ".git" }, { upward = true, path = fname })[1])
local maybe_umbrella_path =
vim.fs.dirname(vim.fs.find({ "mix.exs" }, { upward = true, path = child_or_root_path })[1])
local matches = vim.fs.find({ "mix.exs" }, { upward = true, limit = 2, path = fname })
local child_or_root_path, maybe_umbrella_path = unpack(matches)

if maybe_umbrella_path then
if not vim.startswith(child_or_root_path, Path:joinpath(maybe_umbrella_path, "apps"):absolute()) then
maybe_umbrella_path = nil
end
end

local path = maybe_umbrella_path or child_or_root_path or vim.loop.os_homedir()

return path
return vim.fs.dirname(maybe_umbrella_path or child_or_root_path)
end

return M
3 changes: 3 additions & 0 deletions tests/fixtures/project_a/lib/module.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule ProjectA.Module do

end
4 changes: 4 additions & 0 deletions tests/fixtures/project_a/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule ProjectA.MixProject do

end

3 changes: 3 additions & 0 deletions tests/fixtures/project_b/apps/app_a/lib/module.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
defmodule AppA.Module do

end
4 changes: 4 additions & 0 deletions tests/fixtures/project_b/apps/app_a/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule AppA.MixProject do

end

4 changes: 4 additions & 0 deletions tests/fixtures/project_b/mix.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
defmodule ProjectB.MixProject do

end

44 changes: 44 additions & 0 deletions tests/utils_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
local root_dir = vim.fn.getcwd()
local utils = require("elixir.utils")

describe("utils", function()

after_each(function()
vim.api.nvim_command("cd " .. root_dir)
end)

describe("root_dir", function()

it("finds elixir project root dir without a filename", function()

local project_dir = root_dir .. "/tests/fixtures/project_a"

vim.api.nvim_command("cd " .. project_dir)
local result = utils.root_dir()

assert.are.equal(project_dir, result)
end)

it("finds elixir project root dir", function()
local project_dir = root_dir .. "/tests/fixtures/project_a"
local result = utils.root_dir(project_dir .. "/lib/module.ex")

assert.are.equal(project_dir, result)
end)

it("finds elixir umbrella project root dir", function()
local project_dir = root_dir .. "/tests/fixtures/project_b"
local result = utils.root_dir(project_dir .. "/apps/app_a/lib/module.ex")

assert.are.equal(project_dir, result)
end)

it("returns nil if no elixir project root dir is found", function()
local result = utils.root_dir()

assert.are.equal(nil, result)
end)

end)
end)