diff --git a/lua/plenary/path.lua b/lua/plenary/path.lua index eb46db08..83226e9c 100644 --- a/lua/plenary/path.lua +++ b/lua/plenary/path.lua @@ -917,14 +917,20 @@ end function Path:find_upwards(filename) local folder = Path:new(self) - while self:absolute() ~= path.root do + local root = path.root(folder:absolute()) + + while true do local p = folder:joinpath(filename) if p:exists() then return p end + if folder:absolute() == root then + break + end folder = folder:parent() end - return "" + + return nil end return Path diff --git a/tests/plenary/path_spec.lua b/tests/plenary/path_spec.lua index f5a90972..e4a7ef3a 100644 --- a/tests/plenary/path_spec.lua +++ b/tests/plenary/path_spec.lua @@ -720,6 +720,20 @@ SOFTWARE.]] assert.are.same(should, data) end) end) + + describe(":find_upwards", function() + it("finds files that exist", function() + local p = Path:new(debug.getinfo(1, "S").source:sub(2)) + local found = p:find_upwards "README.md" + assert.are.same(found:absolute(), Path:new("README.md"):absolute()) + end) + + it("returns nil if no file is found", function() + local p = Path:new(debug.getinfo(1, "S").source:sub(2)) + local found = p:find_upwards "asdf" + assert.are.same(found, nil) + end) + end) end) -- function TestPath:testIsDir()