Skip to content

Commit a17c863

Browse files
authored
fix: umbrella app detection (#88)
- adds tests for `elixir.utils.root_dir`
1 parent ab7d028 commit a17c863

File tree

7 files changed

+65
-13
lines changed

7 files changed

+65
-13
lines changed

lua/elixir/utils.lua

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,10 @@ function M.root_dir(fname)
1616
fname = vim.fn.getcwd()
1717
end
1818

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

24-
if maybe_umbrella_path then
25-
if not vim.startswith(child_or_root_path, Path:joinpath(maybe_umbrella_path, "apps"):absolute()) then
26-
maybe_umbrella_path = nil
27-
end
28-
end
29-
30-
local path = maybe_umbrella_path or child_or_root_path or vim.loop.os_homedir()
31-
32-
return path
22+
return vim.fs.dirname(maybe_umbrella_path or child_or_root_path)
3323
end
3424

3525
return M
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
defmodule ProjectA.Module do
2+
3+
end

tests/fixtures/project_a/mix.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defmodule ProjectA.MixProject do
2+
3+
end
4+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
defmodule AppA.Module do
2+
3+
end
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defmodule AppA.MixProject do
2+
3+
end
4+

tests/fixtures/project_b/mix.exs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
defmodule ProjectB.MixProject do
2+
3+
end
4+

tests/utils_spec.lua

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
local root_dir = vim.fn.getcwd()
2+
local utils = require("elixir.utils")
3+
4+
describe("utils", function()
5+
6+
after_each(function()
7+
vim.api.nvim_command("cd " .. root_dir)
8+
end)
9+
10+
describe("root_dir", function()
11+
12+
it("finds elixir project root dir without a filename", function()
13+
14+
local project_dir = root_dir .. "/tests/fixtures/project_a"
15+
16+
vim.api.nvim_command("cd " .. project_dir)
17+
local result = utils.root_dir()
18+
19+
assert.are.equal(project_dir, result)
20+
end)
21+
22+
it("finds elixir project root dir", function()
23+
local project_dir = root_dir .. "/tests/fixtures/project_a"
24+
local result = utils.root_dir(project_dir .. "/lib/module.ex")
25+
26+
assert.are.equal(project_dir, result)
27+
end)
28+
29+
it("finds elixir umbrella project root dir", function()
30+
local project_dir = root_dir .. "/tests/fixtures/project_b"
31+
local result = utils.root_dir(project_dir .. "/apps/app_a/lib/module.ex")
32+
33+
assert.are.equal(project_dir, result)
34+
end)
35+
36+
it("returns nil if no elixir project root dir is found", function()
37+
local result = utils.root_dir()
38+
39+
assert.are.equal(nil, result)
40+
end)
41+
42+
end)
43+
end)
44+

0 commit comments

Comments
 (0)