1 #!/usr
/local/bin
/lua5
.1
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
20 for name
, value
in pairs(attr
) do
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")
47 -- tries to create a directory
48 assert (lfs
.mkdir (tmpdir
), "could not make a new directory")
49 local attrib
, errmsg
= lfs
.attributes (tmpdir
)
51 error ("could not get attributes of file `"..tmpdir
.."':\n"..errmsg
)
53 local f
= io
.open(tmpfile
, "w")
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_")
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"))
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
109 for file
in lfs
.dir (tmp
) do
114 -- Stressing directory iterator, explicit version
117 local iter
, dir
= lfs
.dir(tmp
)
118 local file
= dir
:next()
123 assert(not pcall(dir
.next, dir
))
126 -- directory explicit close
127 local iter
, dir
= lfs
.dir(tmp
)
129 assert(not pcall(dir
.next, dir
))