optimize: make relative (#1529)

This commit is contained in:
yetone
2025-03-08 21:15:07 +08:00
committed by GitHub
parent 4766e76a31
commit 3741460541
6 changed files with 268 additions and 9 deletions

View File

@@ -0,0 +1,81 @@
local utils = require("avante.utils")
describe("get_parent_path", function()
-- Define path separator for our tests, using the same logic as in the utils module
local path_sep = jit.os:find("Windows") ~= nil and "\\" or "/"
it("should return the parent directory of a file path", function()
local filepath = "foo" .. path_sep .. "bar" .. path_sep .. "baz.txt"
local expected = "foo" .. path_sep .. "bar"
assert.are.equal(expected, utils.get_parent_path(filepath))
end)
it("should return the parent directory of a directory path", function()
local dirpath = "foo" .. path_sep .. "bar" .. path_sep .. "baz"
local expected = "foo" .. path_sep .. "bar"
assert.are.equal(expected, utils.get_parent_path(dirpath))
end)
it("should handle trailing separators", function()
local dirpath = "foo" .. path_sep .. "bar" .. path_sep .. "baz" .. path_sep
local expected = "foo" .. path_sep .. "bar"
assert.are.equal(expected, utils.get_parent_path(dirpath))
end)
it("should return '.' for a single file or directory", function()
assert.are.equal(".", utils.get_parent_path("foo.txt"))
assert.are.equal(".", utils.get_parent_path("dir"))
end)
it("should handle paths with multiple levels", function()
local filepath = "a" .. path_sep .. "b" .. path_sep .. "c" .. path_sep .. "d" .. path_sep .. "file.txt"
local expected = "a" .. path_sep .. "b" .. path_sep .. "c" .. path_sep .. "d"
assert.are.equal(expected, utils.get_parent_path(filepath))
end)
it("should return empty string for root directory", function()
-- Root directory on Unix-like systems
if path_sep == "/" then
assert.are.equal("/", utils.get_parent_path("/foo"))
else
-- Windows uses drive letters, so parent of "C:\foo" is "C:"
local winpath = "C:" .. path_sep .. "foo"
assert.are.equal("C:", utils.get_parent_path(winpath))
end
end)
it("should return empty string for an empty string", function() assert.are.equal("", utils.get_parent_path("")) end)
it("should throw an error for nil input", function()
assert.has_error(function() utils.get_parent_path(nil) end, "filepath cannot be nil")
end)
it("should handle paths with spaces", function()
local filepath = "path with spaces" .. path_sep .. "file name.txt"
local expected = "path with spaces"
assert.are.equal(expected, utils.get_parent_path(filepath))
end)
it("should handle special characters in paths", function()
local filepath = "folder-name!" .. path_sep .. "file_#$%&.txt"
local expected = "folder-name!"
assert.are.equal(expected, utils.get_parent_path(filepath))
end)
it("should handle absolute paths", function()
if path_sep == "/" then
-- Unix-like paths
local filepath = path_sep .. "home" .. path_sep .. "user" .. path_sep .. "file.txt"
local expected = path_sep .. "home" .. path_sep .. "user"
assert.are.equal(expected, utils.get_parent_path(filepath))
-- Root directory edge case
assert.are.equal("", utils.get_parent_path(path_sep))
else
-- Windows paths
local filepath = "C:" .. path_sep .. "Users" .. path_sep .. "user" .. path_sep .. "file.txt"
local expected = "C:" .. path_sep .. "Users" .. path_sep .. "user"
assert.are.equal(expected, utils.get_parent_path(filepath))
end
end)
end)

View File

@@ -0,0 +1,54 @@
local assert = require("luassert")
local utils = require("avante.utils")
describe("join_paths", function()
it("should join multiple path segments with proper separator", function()
local result = utils.join_paths("path", "to", "file.lua")
assert.equals("path" .. utils.path_sep .. "to" .. utils.path_sep .. "file.lua", result)
end)
it("should handle empty path segments", function()
local result = utils.join_paths("", "to", "file.lua")
assert.equals("to" .. utils.path_sep .. "file.lua", result)
end)
it("should handle nil path segments", function()
local result = utils.join_paths(nil, "to", "file.lua")
assert.equals("to" .. utils.path_sep .. "file.lua", result)
end)
it("should handle empty path segments", function()
local result = utils.join_paths("path", "", "file.lua")
assert.equals("path" .. utils.path_sep .. "file.lua", result)
end)
it("should use absolute path when encountered", function()
local absolute_path = utils.is_win() and "C:\\absolute\\path" or "/absolute/path"
local result = utils.join_paths("relative", "path", absolute_path)
assert.equals(absolute_path, result)
end)
it("should handle paths with trailing separators", function()
local path_with_sep = "path" .. utils.path_sep
local result = utils.join_paths(path_with_sep, "file.lua")
assert.equals("path" .. utils.path_sep .. "file.lua", result)
end)
it("should return empty string when no paths provided", function()
local result = utils.join_paths()
assert.equals("", result)
end)
it("should return first path when only one path provided", function()
local result = utils.join_paths("path")
assert.equals("path", result)
end)
it("should handle path with mixed separators", function()
-- This test is more relevant on Windows where both / and \ are valid separators
local mixed_path = utils.is_win() and "path\\to/file" or "path/to/file"
local result = utils.join_paths("base", mixed_path)
-- The function should use utils.path_sep for joining
assert.equals("base" .. utils.path_sep .. mixed_path, result)
end)
end)

View File

@@ -0,0 +1,58 @@
local assert = require("luassert")
local utils = require("avante.utils")
describe("make_relative_path", function()
it("should remove base directory from filepath", function()
local test_filepath = "/path/to/project/src/file.lua"
local test_base_dir = "/path/to/project"
local result = utils.make_relative_path(test_filepath, test_base_dir)
assert.equals("src/file.lua", result)
end)
it("should handle trailing dot-slash in base_dir", function()
local test_filepath = "/path/to/project/src/file.lua"
local test_base_dir = "/path/to/project/."
local result = utils.make_relative_path(test_filepath, test_base_dir)
assert.equals("src/file.lua", result)
end)
it("should handle trailing dot-slash in filepath", function()
local test_filepath = "/path/to/project/src/."
local test_base_dir = "/path/to/project"
local result = utils.make_relative_path(test_filepath, test_base_dir)
assert.equals("src", result)
end)
it("should handle both having trailing dot-slash", function()
local test_filepath = "/path/to/project/src/."
local test_base_dir = "/path/to/project/."
local result = utils.make_relative_path(test_filepath, test_base_dir)
assert.equals("src", result)
end)
it("should return the filepath when base_dir is not a prefix", function()
local test_filepath = "/path/to/project/src/file.lua"
local test_base_dir = "/different/path"
local result = utils.make_relative_path(test_filepath, test_base_dir)
assert.equals("/path/to/project/src/file.lua", result)
end)
it("should handle identical paths", function()
local test_filepath = "/path/to/project"
local test_base_dir = "/path/to/project"
local result = utils.make_relative_path(test_filepath, test_base_dir)
assert.equals(".", result)
end)
it("should handle empty strings", function()
local result = utils.make_relative_path("", "")
assert.equals(".", result)
end)
it("should preserve trailing slash in filepath", function()
local test_filepath = "/path/to/project/src/"
local test_base_dir = "/path/to/project"
local result = utils.make_relative_path(test_filepath, test_base_dir)
assert.equals("src/", result)
end)
end)