aboutsummaryrefslogtreecommitdiffstats
path: root/test/lua/dir.lua
diff options
context:
space:
mode:
authorHadriel Kaplan <hadrielk@yahoo.com>2014-03-26 02:24:51 -0400
committerAnders Broman <a.broman58@gmail.com>2014-03-26 07:37:34 +0000
commit8c2bb805742c918b933923947a533d098774da75 (patch)
treeb3859068c4fdd09bd26ab01168835a042715644b /test/lua/dir.lua
parentda1af6e549856c8e213dadc1ec37a41a50246e1d (diff)
Add various functions for Lua directory handling and path info
This adds new functions to get plugins path info, find out if a directory exists, make a new one, remove one, etc. It also creates a file environment for user-supplied Lua scripts, to prevent global variable contamination as well as supply the script-specific file name. Some other minor cleanup was done as I found them. A new testsuite was added to test the existing and new directory functions. Change-Id: I19bd587b5e8a73d89b8521af73670e023314fb33 Reviewed-on: https://code.wireshark.org/review/832 Reviewed-by: Anders Broman <a.broman58@gmail.com>
Diffstat (limited to 'test/lua/dir.lua')
-rw-r--r--test/lua/dir.lua202
1 files changed, 202 insertions, 0 deletions
diff --git a/test/lua/dir.lua b/test/lua/dir.lua
new file mode 100644
index 0000000000..88ecbe2ce9
--- /dev/null
+++ b/test/lua/dir.lua
@@ -0,0 +1,202 @@
+-- test script for wslua Dir functions
+
+------------- helper funcs ------------
+
+local total_tests = 0
+
+local function testing(name)
+ print("---- Testing "..name.." ---- ")
+end
+
+local function test(name, ...)
+ total_tests = total_tests + 1
+ io.stdout:write("test "..name.."-"..total_tests.."...")
+ if (...) == true then
+ io.stdout:write("passed\n")
+ return true
+ else
+ io.stdout:write("failed!\n")
+ error(name.." test failed!")
+ end
+end
+
+-- the following are so we can use pcall (which needs a function to call)
+local function callDirFuncBase(name, t)
+ t.result = Dir[name]()
+ return true
+end
+
+local function callDirFunc(name, val, t)
+ t.result = Dir[name](val)
+ return true
+end
+
+local function makeFile(filename)
+ local f = io.open(filename, "w")
+ if not f then
+ error ("failed to make file"..filename.." in directory\n"..
+ "make sure to delete 'temp' directory before running again")
+ end
+ f:write("fooobarrloo")
+ f:close()
+ return true
+end
+
+--------------------------
+
+-- for our called function results
+local t = {}
+
+testing("Dir basics")
+
+test("global", _G.Dir ~= nil)
+test("global", type(Dir.make) == 'function')
+test("global", type(Dir.remove) == 'function')
+test("global", type(Dir.remove_all) == 'function')
+test("global", type(Dir.open) == 'function')
+test("global", type(Dir.close) == 'function')
+test("global", type(Dir.exists) == 'function')
+test("global", type(Dir.personal_config_path) == 'function')
+test("global", type(Dir.global_config_path) == 'function')
+test("global", type(Dir.personal_plugins_path) == 'function')
+test("global", type(Dir.global_plugins_path) == 'function')
+
+testing("Dir paths/filenames")
+
+test("Dir.__FILE__", __FILE__ ~= nil)
+test("Dir.__DIR__", __DIR__ ~= nil)
+test("Dir.exists", pcall(callDirFunc, "exists", "temp", t))
+test("Dir.personal_config_path", pcall(callDirFuncBase, "personal_config_path", t))
+test("Dir.global_config_path", pcall(callDirFuncBase, "global_config_path", t))
+test("Dir.personal_plugins_path", pcall(callDirFuncBase, "personal_plugins_path", t))
+test("Dir.global_plugins_path", pcall(callDirFuncBase, "global_plugins_path", t))
+
+print("\nFor your information, I got the following info:\n")
+print("__FILE__ = '" .. __FILE__ .. "'")
+print("__DIR__ = '" .. __DIR__ .. "'")
+print("personal_config_path = '" .. Dir.personal_config_path() .. "'")
+print("global_config_path = '" .. Dir.global_config_path() .. "'")
+print("personal_plugins_path = '" .. Dir.personal_plugins_path() .. "'")
+print("global_plugins_path = '" .. Dir.global_plugins_path() .. "'")
+print("\n")
+
+testing("Directory manipulation")
+
+test("Dir.exists", pcall(callDirFunc, "exists", "temp", t))
+
+if t.result == true or t.result == false then
+ error("this testsuite requires there be no 'temp' directory or file; please remove it")
+end
+
+testing("Dir.make")
+
+test("Dir.make", pcall(callDirFunc, "make", "temp", t) and t.result == true)
+test("Dir.exists", pcall(callDirFunc, "exists", "temp", t) and t.result == true)
+-- make the same dir, should give false
+test("Dir.make", pcall(callDirFunc, "make", "temp", t) and t.result == false)
+
+testing("Dir.remove")
+
+test("Dir.remove", pcall(callDirFunc, "remove", "temp", t) and t.result == true)
+test("Dir.exists", pcall(callDirFunc, "exists", "temp", t) and t.result == nil)
+test("Dir.remove", pcall(callDirFunc, "remove", "temp", t) and t.result == false)
+
+Dir.make("temp")
+makeFile("temp/file.txt")
+
+-- will return nil because temp has a file
+test("Dir.remove", pcall(callDirFunc, "remove", "temp", t) and t.result == nil)
+
+testing("Dir.remove_all")
+
+test("Dir.remove_all", pcall(callDirFunc, "remove_all", "temp", t) and t.result == true)
+test("Dir.remove_all", pcall(callDirFunc, "remove_all", "temp", t) and t.result == false)
+
+Dir.make("temp")
+makeFile("temp/file1.txt")
+makeFile("temp/file2.txt")
+makeFile("temp/file3.txt")
+test("Dir.remove_all", pcall(callDirFunc, "remove_all", "temp", t) and t.result == true)
+test("Dir.remove_all", pcall(callDirFunc, "remove_all", "temp", t) and t.result == false)
+
+testing("Dir.open")
+
+Dir.make("temp")
+makeFile("temp/file1.txt")
+makeFile("temp/file2.txt")
+makeFile("temp/file3.txt")
+test("Dir.open", pcall(callDirFunc, "open", "temp", t))
+test("Dir.open", type(t.result) == 'userdata')
+test("Dir.open", typeof(t.result) == 'Dir')
+
+io.stdout:write("calling Dir object...")
+local dir = t.result
+local file = dir()
+io.stdout:write("passed\n")
+
+test("Dir.call", file == "file1.txt")
+file = dir()
+test("Dir.call", file == "file2.txt")
+file = dir()
+test("Dir.call", file == "file3.txt")
+file = dir()
+test("Dir.call", file == nil)
+file = dir()
+test("Dir.call", file == nil)
+
+testing("Dir.close")
+
+test("Dir.close", pcall(callDirFunc, "close", dir, t))
+test("Dir.close", pcall(callDirFunc, "close", dir, t))
+
+testing("Negative testing 1")
+-- now try breaking it
+test("Dir.open", pcall(callDirFunc, "open", "temp", t))
+dir = t.result
+-- call dir() now
+file = dir()
+test("Dir.call", file == "file1.txt")
+
+Dir.remove_all("temp")
+
+-- call it again
+file = dir()
+test("Dir.call", file == "file2.txt")
+test("Dir.close", pcall(callDirFunc, "close", dir, t))
+
+testing("Negative testing 2")
+-- do it again, but this time don't do dir() until after removing the files
+Dir.make("temp")
+makeFile("temp/file1.txt")
+makeFile("temp/file2.txt")
+makeFile("temp/file3.txt")
+
+test("Dir.open", pcall(callDirFunc, "open", "temp", t))
+dir = t.result
+
+Dir.remove_all("temp")
+-- now do it
+file = dir()
+test("Dir.call", file == nil)
+test("Dir.close", pcall(callDirFunc, "close", dir, t))
+
+
+-- negative tests
+testing("Negative testing 3")
+
+-- invalid args
+test("Dir.make", not pcall(callDirFunc, "make", {}, t))
+test("Dir.make", not pcall(callDirFunc, "make", nil, t))
+test("Dir.remove", not pcall(callDirFunc, "remove", {}, t))
+test("Dir.remove", not pcall(callDirFunc, "remove", nil, t))
+test("Dir.remove_all", not pcall(callDirFunc, "remove_all", {}, t))
+test("Dir.remove_all", not pcall(callDirFunc, "remove_all", nil, t))
+test("Dir.open", not pcall(callDirFunc, "open", {}, t))
+test("Dir.open", not pcall(callDirFunc, "open", nil, t))
+test("Dir.close", not pcall(callDirFunc, "close", "dir", t))
+test("Dir.close", not pcall(callDirFunc, "close", nil, t))
+
+
+print("\n-----------------------------\n")
+
+print("All tests passed!\n\n")