beta-0.89.2
[luatex.git] / source / texk / web2c / luatexdir / luafilesystem / tests / test.lua
blob71110749c06a41ae516dea5cfbf976256e0c747c
1 #!/usr/local/bin/lua5.1
3 local tmp = "/tmp"
4 local sep = "/"
5 local upper = ".."
7 require"lfs"
8 print (lfs._VERSION)
10 function attrdir (path)
11 for file in lfs.dir(path) do
12 if file ~= "." and file ~= ".." then
13 local f = path..sep..file
14 print ("\t=> "..f.." <=")
15 local attr = lfs.attributes (f)
16 assert (type(attr) == "table")
17 if attr.mode == "directory" then
18 attrdir (f)
19 else
20 for name, value in pairs(attr) do
21 print (name, value)
22 end
23 end
24 end
25 end
26 end
28 -- Checking changing directories
29 local current = assert (lfs.currentdir())
30 local reldir = string.gsub (current, "^.*%"..sep.."([^"..sep.."])$", "%1")
31 assert (lfs.chdir (upper), "could not change to upper directory")
32 assert (lfs.chdir (reldir), "could not change back to current directory")
33 assert (lfs.currentdir() == current, "error trying to change directories")
34 assert (lfs.chdir ("this couldn't be an actual directory") == nil, "could change to a non-existent directory")
36 -- Changing creating and removing directories
37 local tmpdir = current..sep.."lfs_tmp_dir"
38 local tmpfile = tmpdir..sep.."tmp_file"
39 -- Test for existence of a previous lfs_tmp_dir
40 -- that may have resulted from an interrupted test execution and remove it
41 if lfs.chdir (tmpdir) then
42 assert (lfs.chdir (upper), "could not change to upper directory")
43 assert (os.remove (tmpfile), "could not remove file from previous test")
44 assert (lfs.rmdir (tmpdir), "could not remove directory from previous test")
45 end
47 -- tries to create a directory
48 assert (lfs.mkdir (tmpdir), "could not make a new directory")
49 local attrib, errmsg = lfs.attributes (tmpdir)
50 if not attrib then
51 error ("could not get attributes of file `"..tmpdir.."':\n"..errmsg)
52 end
53 local f = io.open(tmpfile, "w")
54 f:close()
56 -- Change access time
57 local testdate = os.time({ year = 2007, day = 10, month = 2, hour=0})
58 assert (lfs.touch (tmpfile, testdate))
59 local new_att = assert (lfs.attributes (tmpfile))
60 assert (new_att.access == testdate, "could not set access time")
61 assert (new_att.modification == testdate, "could not set modification time")
63 -- Change access and modification time
64 local testdate1 = os.time({ year = 2007, day = 10, month = 2, hour=0})
65 local testdate2 = os.time({ year = 2007, day = 11, month = 2, hour=0})
67 assert (lfs.touch (tmpfile, testdate2, testdate1))
68 local new_att = assert (lfs.attributes (tmpfile))
69 assert (new_att.access == testdate2, "could not set access time")
70 assert (new_att.modification == testdate1, "could not set modification time")
72 local res, err = lfs.symlinkattributes(tmpfile)
73 if err ~= "symlinkattributes not supported on this platform" then
74 -- Checking symbolic link information (does not work in Windows)
75 assert (os.execute ("ln -s "..tmpfile.." _a_link_for_test_"))
76 assert (lfs.attributes"_a_link_for_test_".mode == "file")
77 assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link")
78 assert (os.remove"_a_link_for_test_")
79 end
81 if lfs.setmode then
82 -- Checking text/binary modes (works only in Windows)
83 local f = io.open(tmpfile, "w")
84 local result, mode = lfs.setmode(f, "binary")
85 assert((result and mode == "text") or (not result and mode == "setmode not supported on this platform"))
86 result, mode = lfs.setmode(f, "text")
87 assert((result and mode == "binary") or (not result and mode == "setmode not supported on this platform"))
88 f:close()
89 end
91 -- Restore access time to current value
92 assert (lfs.touch (tmpfile, attrib.access, attrib.modification))
93 new_att = assert (lfs.attributes (tmpfile))
94 assert (new_att.access == attrib.access)
95 assert (new_att.modification == attrib.modification)
97 -- Remove new file and directory
98 assert (os.remove (tmpfile), "could not remove new file")
99 assert (lfs.rmdir (tmpdir), "could not remove new directory")
100 assert (lfs.mkdir (tmpdir..sep.."lfs_tmp_dir") == nil, "could create a directory inside a non-existent one")
102 -- Trying to get attributes of a non-existent file
103 assert (lfs.attributes ("this couldn't be an actual file") == nil, "could get attributes of a non-existent file")
104 assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory")
106 -- Stressing directory iterator
107 count = 0
108 for i = 1, 4000 do
109 for file in lfs.dir (tmp) do
110 count = count + 1
114 -- Stressing directory iterator, explicit version
115 count = 0
116 for i = 1, 4000 do
117 local iter, dir = lfs.dir(tmp)
118 local file = dir:next()
119 while file do
120 count = count + 1
121 file = dir:next()
123 assert(not pcall(dir.next, dir))
126 -- directory explicit close
127 local iter, dir = lfs.dir(tmp)
128 dir:close()
129 assert(not pcall(dir.next, dir))
130 print"Ok!"