1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
-- This test file expects to be ran from 'run.lua' in the root Penlight directory.
local dir = require( "pl.dir" )
local file = require( "pl.file" )
local path = require( "pl.path" )
local asserteq = require( "pl.test" ).asserteq
local lfs = require("lfs")
asserteq(dir.fnmatch("foobar", "foo*bar"), true)
asserteq(dir.fnmatch("afoobar", "foo*bar"), false)
asserteq(dir.fnmatch("foobars", "foo*bar"), false)
asserteq(dir.fnmatch("foonbar", "foo*bar"), true)
asserteq(dir.fnmatch("foo'n'bar", "foo*bar"), true)
asserteq(dir.fnmatch("foonbar", "foo?bar"), true)
asserteq(dir.fnmatch("foo'n'bar", "foo?bar"), false)
asserteq(dir.fnmatch("foo", "FOO"), path.is_windows)
asserteq(dir.fnmatch("FOO", "foo"), path.is_windows)
local filtered = dir.filter({"foobar", "afoobar", "foobars", "foonbar"}, "foo*bar")
asserteq(filtered, {"foobar", "foonbar"})
local normpath = path.normpath
local doc_files = dir.getfiles(normpath "docs/", "*.css")
asserteq(doc_files, {normpath "docs/ldoc_fixed.css"})
local all_doc_files = dir.getallfiles(normpath "docs/", "*.css")
asserteq(all_doc_files, {normpath "docs/ldoc_fixed.css"})
local test_samples = dir.getallfiles(normpath "tests/lua")
table.sort(test_samples)
asserteq(test_samples, {
normpath "tests/lua/animal.lua",
normpath "tests/lua/bar.lua",
normpath "tests/lua/foo/args.lua",
normpath "tests/lua/mod52.lua",
normpath "tests/lua/mymod.lua"
})
-- Test move files -----------------------------------------
-- Create a dummy file
local fileName = path.tmpname() .. "Xx"
file.write( fileName, string.rep( "poot ", 1000 ) )
local newFileName = path.tmpname() .. "Xx"
local err, msg = dir.movefile( fileName, newFileName )
-- Make sure the move is successful
assert( err, msg )
-- Check to make sure the original file is gone
asserteq( path.exists( fileName ), false )
-- Check to make sure the new file is there
asserteq( path.exists( newFileName ) , newFileName )
-- Test existence again, but explicitly check for correct casing
local files = dir.getfiles(path.dirname(newFileName))
local found = false
for i, filename in ipairs(files) do
if filename == newFileName then
found = true
break
end
end
assert(found, "file was not found in directory, check casing: " .. newFileName)
-- Try to move the original file again (which should fail)
local newFileName2 = path.tmpname()
local err, msg = dir.movefile( fileName, newFileName2 )
asserteq( err, false )
-- Clean up
file.delete( newFileName )
-- Test copy files -----------------------------------------
-- Create a dummy file
local fileName = path.tmpname()
file.write( fileName, string.rep( "poot ", 1000 ) )
local newFileName = path.tmpname() .. "xX"
local err, msg = dir.copyfile( fileName, newFileName )
-- Make sure the move is successful
assert( err, msg )
-- Check to make sure the new file is there
asserteq( path.exists( newFileName ) , newFileName )
-- Test existence again, but explicitly check for correct casing
local files = dir.getfiles(path.dirname(newFileName))
local found = false
for i, filename in ipairs(files) do
if filename == newFileName then
found = true
break
end
end
assert(found, "file was not found in directory, check casing: " .. newFileName)
-- Try to move a non-existant file (which should fail)
local fileName2 = 'blub'
local newFileName2 = 'snortsh'
local err, msg = dir.copyfile( fileName2, newFileName2 )
asserteq( err, false )
-- Clean up the files
file.delete( fileName )
file.delete( newFileName )
-- Test make directory -----------------------------------------
-- Create a dummy file
local dirName = path.tmpname() .. "xX"
local fullPath = dirName .. "/and/one/more"
if path.is_windows then
fullPath = fullPath:gsub("/", "\\")
end
local err, msg = dir.makepath(fullPath)
-- Make sure the move is successful
assert( err, msg )
-- Check to make sure the new file is there
assert(path.isdir(dirName))
assert(path.isdir(fullPath))
-- Test existence again, but explicitly check for correct casing
local files = dir.getdirectories(path.dirname(path.tmpname()))
local found = false
for i, filename in ipairs(files) do
if filename == dirName then
found = true
break
end
end
assert(found, "dir was not found in directory, check casing: " .. newFileName)
-- Try to move a non-existant file (which should fail)
local fileName2 = 'blub'
local newFileName2 = 'snortsh'
local err, msg = dir.copyfile( fileName2, newFileName2 )
asserteq( err, false )
-- Clean up the files
file.delete( fileName )
file.delete( newFileName )
-- Test rmtree -----------------------------------------
do
local dirName = path.tmpname()
os.remove(dirName)
assert(dir.makepath(dirName))
assert(file.write(path.normpath(dirName .. "/file_base.txt"), "hello world"))
assert(dir.makepath(path.normpath(dirName .. "/sub1")))
assert(file.write(path.normpath(dirName .. "/sub1/file_sub1.txt"), "hello world"))
assert(dir.makepath(path.normpath(dirName .. "/sub2")))
assert(file.write(path.normpath(dirName .. "/sub2/file_sub2.txt"), "hello world"))
local linkTarget = path.tmpname()
os.remove(linkTarget)
assert(dir.makepath(linkTarget))
local linkFile = path.normpath(linkTarget .. "/file.txt")
assert(file.write(linkFile, "hello world"))
local linkSource = path.normpath(dirName .. "/link1")
assert(lfs.link(linkTarget, linkSource, true))
-- test: rmtree will not follow symlinks
local ok, err = dir.rmtree(linkSource)
asserteq(ok, false)
asserteq(err, "will not follow symlink")
-- test: rmtree removes a tree without following symlinks in that tree
local ok, err = dir.rmtree(dirName)
asserteq(err, nil)
asserteq(ok, true)
asserteq(path.exists(dirName), false) -- tree is gone, including symlink
assert(path.exists(linkFile), "expected linked-to file to still exist") -- symlink target file is still there
-- cleanup
assert(dir.rmtree(linkTarget))
end
-- have NO idea why forcing the return code is necessary here (Windows 7 64-bit)
os.exit(0)
|