mirror of
https://github.com/skywind3000/z.lua
synced 2026-07-03 03:10:48 +00:00
add test
This commit is contained in:
+152
@@ -0,0 +1,152 @@
|
||||
local zmod = require('z')
|
||||
local windows = os.path.sep == '\\'
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- test normpath
|
||||
-----------------------------------------------------------------------
|
||||
function assert_posix(path, result)
|
||||
local x = os.path.normpath(path)
|
||||
print('[test] normpath: ('..path..') -> (' .. result .. ')')
|
||||
if x:gsub('\\', '/') ~= result then
|
||||
print('failed: "' .. x .. '" != "'..result.. '"')
|
||||
os.exit()
|
||||
else
|
||||
print('passed')
|
||||
print()
|
||||
end
|
||||
end
|
||||
|
||||
function test_normpath_posix()
|
||||
assert_posix("", ".")
|
||||
assert_posix("/", "/")
|
||||
assert_posix("///", "/")
|
||||
assert_posix("///foo/.//bar//", "/foo/bar")
|
||||
assert_posix("///foo/.//bar//.//..//.//baz", "/foo/baz")
|
||||
assert_posix("///..//./foo/.//bar", "/foo/bar")
|
||||
end
|
||||
|
||||
function assert_windows(path, result)
|
||||
local x = os.path.normpath(path)
|
||||
print('[test] normpath: ('..path..') -> (' .. result .. ')')
|
||||
if x ~= result then
|
||||
print('failed: "' .. x .. '" != "'..result.. '"')
|
||||
os.exit()
|
||||
else
|
||||
print('passed')
|
||||
print()
|
||||
end
|
||||
end
|
||||
|
||||
function test_normpath_windows()
|
||||
assert_windows('A//////././//.//B', 'A\\B')
|
||||
assert_windows('A/./B', 'A\\B')
|
||||
assert_windows('A/foo/../B', 'A\\B')
|
||||
assert_windows('C:A//B', 'C:A\\B')
|
||||
assert_windows('D:A/./B', 'D:A\\B')
|
||||
assert_windows('e:A/foo/../B', 'e:A\\B')
|
||||
assert_windows('C:///A//B', 'C:\\A\\B')
|
||||
assert_windows('D:///A/./B', 'D:\\A\\B')
|
||||
assert_windows('e:///A/foo/../B', 'e:\\A\\B')
|
||||
assert_windows('..', '..')
|
||||
assert_windows('.', '.')
|
||||
assert_windows('', '.')
|
||||
assert_windows('/', '\\')
|
||||
assert_windows('c:/', 'c:\\')
|
||||
assert_windows('/../.././..', '\\')
|
||||
assert_windows('c:/../../..', 'c:\\')
|
||||
assert_windows('../.././..', '..\\..\\..')
|
||||
assert_windows('K:../.././..', 'K:..\\..\\..')
|
||||
assert_windows('C:////a/b', 'C:\\a\\b')
|
||||
end
|
||||
|
||||
test_normpath_posix()
|
||||
|
||||
if windows then
|
||||
test_normpath_windows()
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- test path join
|
||||
-----------------------------------------------------------------------
|
||||
function assert_join_posix(segments, result, isnt)
|
||||
print('[test] join: '..zmod.dump(segments)..' -> (' .. result .. ')')
|
||||
local path = ''
|
||||
for _, item in ipairs(segments) do
|
||||
path = os.path.join(path, item)
|
||||
end
|
||||
if windows and (not isnt) then
|
||||
path = path:gsub('\\', '/')
|
||||
end
|
||||
if path ~= result then
|
||||
print('failed: "' .. path .. '"')
|
||||
os.exit()
|
||||
else
|
||||
print('passed')
|
||||
end
|
||||
end
|
||||
|
||||
function assert_join_windows(segments, result)
|
||||
assert_join_posix(segments, result, 1)
|
||||
end
|
||||
|
||||
function test_join_posix()
|
||||
assert_join_posix({"/foo", "bar", "/bar", "baz"}, "/bar/baz")
|
||||
assert_join_posix({"/foo", "bar", "baz"}, "/foo/bar/baz")
|
||||
assert_join_posix({"/foo/", "bar/", "baz/"}, "/foo/bar/baz/")
|
||||
end
|
||||
|
||||
function test_join_windows()
|
||||
assert_join_windows({""}, '')
|
||||
assert_join_windows({"", "", ""}, '')
|
||||
assert_join_windows({"a"}, 'a')
|
||||
assert_join_windows({"/a"}, '/a')
|
||||
assert_join_windows({"\\a"}, '\\a')
|
||||
assert_join_windows({"a:"}, 'a:')
|
||||
assert_join_windows({"a:", "\\b"}, 'a:\\b')
|
||||
assert_join_windows({"a", "\\b"}, '\\b')
|
||||
assert_join_windows({"a", "b", "c"}, 'a\\b\\c')
|
||||
assert_join_windows({"a\\", "b", "c"}, 'a\\b\\c')
|
||||
assert_join_windows({"a", "b\\", "c"}, 'a\\b\\c')
|
||||
assert_join_windows({"a", "b", "\\c"}, '\\c')
|
||||
assert_join_windows({"d:\\", "\\pleep"}, 'd:\\pleep')
|
||||
assert_join_windows({"d:\\", "a", "b"}, 'd:\\a\\b')
|
||||
|
||||
assert_join_windows({'', 'a'}, 'a')
|
||||
assert_join_windows({'', '', '', '', 'a'}, 'a')
|
||||
assert_join_windows({'a', ''}, 'a\\')
|
||||
assert_join_windows({'a', '', '', '', ''}, 'a\\')
|
||||
assert_join_windows({'a\\', ''}, 'a\\')
|
||||
assert_join_windows({'a\\', '', '', '', ''}, 'a\\')
|
||||
assert_join_windows({'a/', ''}, 'a/')
|
||||
|
||||
assert_join_windows({'a/b', 'x/y'}, 'a/b\\x/y')
|
||||
assert_join_windows({'/a/b', 'x/y'}, '/a/b\\x/y')
|
||||
assert_join_windows({'/a/b/', 'x/y'}, '/a/b/x/y')
|
||||
assert_join_windows({'c:', 'x/y'}, 'c:x/y')
|
||||
assert_join_windows({'c:a/b', 'x/y'}, 'c:a/b\\x/y')
|
||||
assert_join_windows({'c:a/b/', 'x/y'}, 'c:a/b/x/y')
|
||||
assert_join_windows({'c:/', 'x/y'}, 'c:/x/y')
|
||||
assert_join_windows({'c:/a/b', 'x/y'}, 'c:/a/b\\x/y')
|
||||
assert_join_windows({'c:/a/b/', 'x/y'}, 'c:/a/b/x/y')
|
||||
|
||||
assert_join_windows({'a/b', '/x/y'}, '/x/y')
|
||||
assert_join_windows({'/a/b', '/x/y'}, '/x/y')
|
||||
assert_join_windows({'c:', '/x/y'}, 'c:/x/y')
|
||||
assert_join_windows({'c:a/b', '/x/y'}, 'c:/x/y')
|
||||
assert_join_windows({'c:/', '/x/y'}, 'c:/x/y')
|
||||
assert_join_windows({'c:/a/b', '/x/y'}, 'c:/x/y')
|
||||
|
||||
assert_join_windows({'c:', 'C:x/y'}, 'C:x/y')
|
||||
assert_join_windows({'c:a/b', 'C:x/y'}, 'C:a/b\\x/y')
|
||||
assert_join_windows({'c:/', 'C:x/y'}, 'C:/x/y')
|
||||
assert_join_windows({'c:/a/b', 'C:x/y'}, 'C:/a/b\\x/y')
|
||||
end
|
||||
|
||||
test_join_posix()
|
||||
|
||||
if windows then
|
||||
test_join_windows()
|
||||
end
|
||||
|
||||
@@ -342,15 +342,28 @@ end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- get absolute path
|
||||
-- absolute path (simulated)
|
||||
-----------------------------------------------------------------------
|
||||
function os.path.absolute(path)
|
||||
local pwd = os.pwd()
|
||||
return os.path.normpath(os.path.join(pwd, path))
|
||||
end
|
||||
|
||||
|
||||
-----------------------------------------------------------------------
|
||||
-- absolute path (system call, can fall back to os.path.absolute)
|
||||
-----------------------------------------------------------------------
|
||||
function os.path.abspath(path)
|
||||
if path == '' then path = '.' end
|
||||
if windows then
|
||||
local script = 'FOR /f "delims=" %%i IN ("%s") DO @echo %%~fi'
|
||||
local script = string.format(script, path)
|
||||
local script = 'cmd.exe /C ' .. script .. ' 2> nul'
|
||||
local output = os.call(script)
|
||||
return output:gsub('%s$', '')
|
||||
local test = output:gsub('%s$', '')
|
||||
if test ~= nil and test ~= '' then
|
||||
return test
|
||||
end
|
||||
else
|
||||
local test = os.path.which('realpath')
|
||||
if test ~= nil and test ~= '' then
|
||||
@@ -387,6 +400,7 @@ function os.path.abspath(path)
|
||||
end
|
||||
end
|
||||
end
|
||||
return os.path.absolute(path)
|
||||
end
|
||||
|
||||
|
||||
@@ -505,12 +519,46 @@ end
|
||||
-- join two path
|
||||
-----------------------------------------------------------------------
|
||||
function os.path.join(path1, path2)
|
||||
if path2 == nil or path2 == '' then
|
||||
return path1
|
||||
if path1 == nil or path1 == '' then
|
||||
if path2 == nil or path2 == '' then
|
||||
return ''
|
||||
else
|
||||
return path2
|
||||
end
|
||||
elseif path2 == nil or path2 == '' then
|
||||
local head = path1:sub(-1, -1)
|
||||
if head == '/' or (windows and head == '\\') then
|
||||
return path1
|
||||
end
|
||||
return path1 .. os.path.sep
|
||||
elseif os.path.isabs(path2) then
|
||||
if windows then
|
||||
local head = path2:sub(1, 1)
|
||||
if head == '/' or head == '\\' then
|
||||
if path1:match('^%a:') then
|
||||
return path1:sub(1, 2) .. path2
|
||||
end
|
||||
end
|
||||
end
|
||||
return path2
|
||||
elseif path1 == nil or path1 == '' then
|
||||
return path2
|
||||
elseif windows then
|
||||
local d1 = path1:sub(1, 2)
|
||||
local d2 = path2:sub(1, 2)
|
||||
if not path1:match('^%a:') then
|
||||
d1 = ''
|
||||
end
|
||||
if not path2:match('^%a:') then
|
||||
d2 = ''
|
||||
end
|
||||
if d1 ~= '' then
|
||||
if d2 ~= '' then
|
||||
if d1:lower() == d2:lower() then
|
||||
return d2 .. os.path.join(path1:sub(3), path2:sub(3))
|
||||
else
|
||||
return path2
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
local postsep = true
|
||||
local len1 = path1:len()
|
||||
|
||||
Reference in New Issue
Block a user