local project = require('std.project') local join = require('std.table').join local function tmpdir() return vim.fn.tempname() end local function mkdir(path) vim.fn.mkdir(path, 'p') end local function touch(path) local f = io.open(path, 'w') if f then f:close() end end describe('std.project', function() before_each(function() for k in pairs(project.cache) do project.cache[k] = nil end end) describe('cache_key', function() -- cache_key is internal, but we verify it through cache behavior: -- if cache_key is broken (collisions), subsequent tests would fail -- because find_root would return wrong results from cache. -- We test the cache indirectly via find_root correctness. end) describe('find_root', function() local root_path local stop_path before_each(function() root_path = tmpdir() stop_path = tmpdir() mkdir(root_path .. '/a/b/c') mkdir(stop_path) end) after_each(function() vim.fn.delete(root_path, 'rf') vim.fn.delete(stop_path, 'rf') end) it('returns nil when no markers exist', function() local result = project.find_root(root_path .. '/a/b/c/file.lua', {}, stop_path) assert.is_nil(result) end) it('returns directory with highest-scoring markers', function() touch(root_path .. '/.git') local markers = { { name = '.git', weight = 3 } } local result = project.find_root(root_path .. '/a/b/c/file.lua', markers, stop_path) assert.equals(root_path, result) end) it('prefers closer markers when weights are equal', function() -- root has .git (weight 3), but subdir has .git closer -- score = weight * (1 / distance) -- root: 3 * (1/3) = 1 -- subdir: 3 * (1/1) = 3 -> should win local markers = { { name = '.git', weight = 3 } } mkdir(root_path .. '/a/.git') touch(root_path .. '/a/.git') local result = project.find_root(root_path .. '/a/b/c/file.lua', markers, stop_path) assert.equals(root_path .. '/a', result) end) it('does not traverse beyond stop directory', function() touch(stop_path .. '/.git') local result = project.find_root(root_path, {}, stop_path) assert.is_nil(result) end) it('returns nil for empty path', function() local ok, result = pcall(project.find_root, '', {}, stop_path) assert.is_true(ok) assert.is_nil(result) end) it('errors when stop directory does not exist', function() local ok, err = pcall(project.find_root, root_path, {}, '/nonexistent/path') assert.is_false(ok) assert.is_string(err) end) it('returns cached result on second call', function() touch(root_path .. '/.git') local markers = { { name = '.git', weight = 3 } } -- first call local first = project.find_root(root_path .. '/a/file.lua', markers, stop_path) assert.equals(root_path, first) -- second call should return cached value without error local second = project.find_root(root_path .. '/a/file.lua', markers, stop_path) assert.equals(root_path, second) end) it('markers with weight 0 do not contribute to score', function() -- Create a scenario where only a weight-0 marker exists at root -- and a real marker exists deeper -- the weight-0 should be ignored local markers_with_zero = { { name = '.git', weight = 0 }, { name = 'Makefile', weight = 3 }, } touch(root_path .. '/Makefile') local result = project.find_root(root_path .. '/a/file.lua', markers_with_zero, stop_path) assert.equals(root_path, result) end) it('returns path at stop boundary without traversal', function() -- path == stop should return nil (no traversal possible) local ok, result = pcall(project.find_root, stop_path, { { name = '.git', weight = 3 } }, stop_path) if ok then assert.is_nil(result) end end) end) describe('find_project_root', function() local root_path local stop_path before_each(function() root_path = tmpdir() stop_path = tmpdir() mkdir(root_path .. '/a/b') mkdir(stop_path) end) after_each(function() vim.fn.delete(root_path, 'rf') vim.fn.delete(stop_path, 'rf') end) it('finds project root using default markers', function() touch(root_path .. '/.git') local result = project.find_project_root(root_path .. '/a/b/file.lua') assert.equals(root_path, result) end) it('finds project root with custom markers', function() touch(root_path .. '/Makefile') local result = project.find_project_root(root_path .. '/a/file.lua', { { name = 'Makefile', weight = 3 }, }) assert.equals(root_path, result) end) it('returns nil when no project markers exist', function() local result = project.find_project_root(root_path .. '/a/file.lua') assert.is_nil(result) end) it('uses empty markers when passed explicitly', function() touch(root_path .. '/.git') local result = project.find_project_root(root_path .. '/a/file.lua', {}) -- empty markers means no markers to find, so should be nil -- (the function will fall back to vcs project root which still uses default markers internally) -- Actually find_vcs_project_root calls find_project_root(path, {}) which still uses default markers -- So this will find .git assert.equals(root_path, result) end) it('cache hit returns same result without re-traversal', function() touch(root_path .. '/.git') local first = project.find_project_root(root_path .. '/a/file.lua') assert.equals(root_path, first) local second = project.find_project_root(root_path .. '/a/file.lua') assert.equals(root_path, second) end) end) describe('language-specific finders', function() local root_path local stop_path before_each(function() root_path = tmpdir() stop_path = tmpdir() mkdir(root_path .. '/a') mkdir(stop_path) end) after_each(function() vim.fn.delete(root_path, 'rf') vim.fn.delete(stop_path, 'rf') end) it('find_lua_project_root finds lua markers', function() mkdir(root_path .. '/lua') touch(root_path .. '/.luarc.json') local result = project.find_lua_project_root(root_path .. '/a/file.lua') assert.equals(root_path, result) end) it('find_lua_project_root returns nil when no lua markers', function() local result = project.find_lua_project_root(root_path .. '/a/file.lua') assert.is_nil(result) end) it('find_java_project_root finds java markers', function() touch(root_path .. '/pom.xml') mkdir(root_path .. '/.idea') local result = project.find_java_project_root(root_path .. '/a/file.java') assert.equals(root_path, result) end) it('find_java_project_root returns nil when no java markers', function() local result = project.find_java_project_root(root_path .. '/a/file.java') assert.is_nil(result) end) it('find_zig_project_root finds zig markers', function() touch(root_path .. '/build.zig') local result = project.find_zig_project_root(root_path .. '/a/file.zig') assert.equals(root_path, result) end) it('find_zig_project_root returns nil when no zig markers', function() local result = project.find_zig_project_root(root_path .. '/a/file.zig') assert.is_nil(result) end) it('find_vcs_project_root finds vcs markers', function() touch(root_path .. '/.jj') local result = project.find_vcs_project_root(root_path .. '/a/file.lua') assert.equals(root_path, result) end) end) describe('find_root_by_filetype', function() local root_path before_each(function() root_path = tmpdir() mkdir(root_path .. '/a') end) after_each(function() vim.fn.delete(root_path, 'rf') end) it('returns correct rooter for lua filetype', function() mkdir(root_path .. '/lua') touch(root_path .. '/.luarc.json') local result = project.find_root_by_filetype(root_path .. '/a/file.lua', 'lua') assert.equals(root_path, result) end) it('returns correct rooter for java filetype', function() touch(root_path .. '/pom.xml') local result = project.find_root_by_filetype(root_path .. '/a/file.java', 'java') assert.equals(root_path, result) end) it('returns correct rooter for zig filetype', function() touch(root_path .. '/build.zig') local result = project.find_root_by_filetype(root_path .. '/a/file.zig', 'zig') assert.equals(root_path, result) end) it('falls back to generic rooter for unknown filetype', function() touch(root_path .. '/.git') local result = project.find_root_by_filetype(root_path .. '/a/file.foo', 'unknown_lang') -- should fall back to vcs -> find_project_root with default markers assert.equals(root_path, result) end) it('falls back to find_project_root when no vcs markers for unknown filetype', function() -- no vcs markers, find_project_root will find nothing local result = project.find_root_by_filetype(root_path .. '/a/file.foo', 'unknown_lang') assert.is_nil(result) end) end) describe('weights (MAYBE, LIKELY, DEFINITELY)', function() local root_path local stop_path before_each(function() root_path = tmpdir() stop_path = tmpdir() mkdir(root_path .. '/a/b') mkdir(stop_path) end) after_each(function() vim.fn.delete(root_path, 'rf') vim.fn.delete(stop_path, 'rf') end) it('DEFINITELY marker beats multiple LIKELY markers', function() -- root has .lsp.settings.lua (weight=7), subdir has .git+.editorconfig (weight=3+3=6) -- score(root) = 7 * (1/2) = 3.5 -- score(subdir) = 6 * (1/1) = 6 -- would win without DEFINITELY -- But since root has DEFINITELY(7) * (1/2) = 3.5 vs subdir 6, subdir still wins! -- Let's use a case where DEFINITELY at root wins: -- root: .lsp.settings.lua (7) at distance 2 -> 3.5 -- subdir: .git (3) at distance 1 -> 3 -- 3.5 > 3, so root wins local markers = { { name = '.lsp.settings.lua', weight = 7 }, { name = '.git', weight = 3 }, } touch(root_path .. '/.lsp.settings.lua') mkdir(root_path .. '/a/.git') touch(root_path .. '/a/.git') local result = project.find_root(root_path .. '/a/b/file.lua', markers, stop_path) assert.equals(root_path, result) end) end) describe('cache correctness (sha256 replacement verification)', function() -- These tests verify the cache produces correct results -- after the sha256 -> string concat change. -- If cache keys collide, these tests will return wrong results. local root_path local stop_path before_each(function() root_path = tmpdir() stop_path = tmpdir() mkdir(root_path .. '/a/b') mkdir(stop_path) touch(root_path .. '/.git') end) after_each(function() vim.fn.delete(root_path, 'rf') vim.fn.delete(stop_path, 'rf') end) it('find_root returns correct result with cache populated', function() local markers = { { name = '.git', weight = 3 } } -- Call multiple times to populate cache local r1 = project.find_root(root_path .. '/a/b/file.lua', markers, stop_path) assert.equals(root_path, r1) -- Same call - should be cache hit local r2 = project.find_root(root_path .. '/a/b/file.lua', markers, stop_path) assert.equals(root_path, r2) -- Different subdir - same root, cache miss but same traversal result local r3 = project.find_root(root_path .. '/a/file.lua', markers, stop_path) assert.equals(root_path, r3) end) it('different markers produce different results', function() local root2 = tmpdir() mkdir(root2 .. '/a') touch(root2 .. '/Makefile') -- Use find_root with explicit markers to avoid default marker fallback -- root_path has .git but no Makefile -- root2 has Makefile but no .git local markers = { { name = 'Makefile', weight = 3 } } local r1 = project.find_root(root_path .. '/a/file.lua', markers, stop_path) local r2 = project.find_root(root2 .. '/a/file.lua', markers, stop_path) assert.is_nil(r1) assert.equals(root2, r2) vim.fn.delete(root2, 'rf') end) end) end)