Fix has_permissions_to_access when file is not ignored but patter is a substring (#2011)

Co-authored-by: pre-commit-ci-lite[bot] <117423508+pre-commit-ci-lite[bot]@users.noreply.github.com>
This commit is contained in:
Joseph
2025-05-07 14:43:43 -03:00
committed by GitHub
parent 9c2db7d1cb
commit b2064a8f40
3 changed files with 55 additions and 1 deletions

1
.gitignore vendored
View File

@@ -33,6 +33,7 @@ temp/
# Environment variable files (if you use .env file to store API keys)
.env
.envrc
# If you use any build tools, you might need to ignore build output directories
build/

View File

@@ -728,12 +728,16 @@ function M.parse_gitignore(gitignore_path)
return ignore_patterns, negate_patterns
end
-- @param file string
-- @param ignore_patterns string[]
-- @param negate_patterns string[]
-- @return boolean
function M.is_ignored(file, ignore_patterns, negate_patterns)
for _, pattern in ipairs(negate_patterns) do
if file:match(pattern) then return false end
end
for _, pattern in ipairs(ignore_patterns) do
if file:match(pattern) then return true end
if file:match(pattern .. "/") or file:match(pattern .. "$") then return true end
end
return false
end

View File

@@ -0,0 +1,49 @@
local LlmToolHelpers = require("avante.llm_tools.helpers")
local Utils = require("avante.utils")
local stub = require("luassert.stub")
describe("has_permission_to_access", function()
local test_dir = "/tmp/test_llm_tools_helpers"
before_each(function()
os.execute("mkdir -p " .. test_dir)
-- create .gitignore file with test.idx file
os.execute("rm " .. test_dir .. "/.gitignore 2>/dev/null")
local gitignore_file = io.open(test_dir .. "/.gitignore", "w")
if gitignore_file then
gitignore_file:write("test.txt\n")
gitignore_file:write("data\n")
gitignore_file:close()
end
stub(Utils, "get_project_root", function() return test_dir end)
end)
after_each(function() os.execute("rm -rf " .. test_dir) end)
it("Basic ignored and not ignored", function()
local abs_path
abs_path = test_dir .. "/test.txt"
assert.is_false(LlmToolHelpers.has_permission_to_access(abs_path))
abs_path = test_dir .. "/test1.txt"
assert.is_true(LlmToolHelpers.has_permission_to_access(abs_path))
end)
it("Ignore files inside directories", function()
local abs_path
abs_path = test_dir .. "/data/test.txt"
assert.is_false(LlmToolHelpers.has_permission_to_access(abs_path))
abs_path = test_dir .. "/data/test1.txt"
assert.is_false(LlmToolHelpers.has_permission_to_access(abs_path))
end)
it("Do not ignore files with just similar paths", function()
local abs_path
abs_path = test_dir .. "/data_test/test.txt"
assert.is_false(LlmToolHelpers.has_permission_to_access(abs_path))
abs_path = test_dir .. "/data_test/test1.txt"
assert.is_true(LlmToolHelpers.has_permission_to_access(abs_path))
end)
end)