test.lua 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. #!/usr/bin/env lua5.1
  2. local tmp = "/tmp"
  3. local sep = string.match (package.config, "[^\n]+")
  4. local upper = ".."
  5. local is_unix = package.config:sub(1,1) == "/"
  6. local lfs = require"lfs"
  7. print (lfs._VERSION)
  8. io.write(".")
  9. io.flush()
  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
  27. -- Checking changing directories
  28. local current = assert (lfs.currentdir())
  29. local reldir = string.gsub (current, "^.*%"..sep.."([^"..sep.."])$", "%1")
  30. assert (lfs.chdir (upper), "could not change to upper directory")
  31. assert (lfs.chdir (reldir), "could not change back to current directory")
  32. assert (lfs.currentdir() == current, "error trying to change directories")
  33. assert (lfs.chdir ("this couldn't be an actual directory") == nil, "could change to a non-existent directory")
  34. io.write(".")
  35. io.flush()
  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
  46. io.write(".")
  47. io.flush()
  48. -- tries to create a directory
  49. assert (lfs.mkdir (tmpdir), "could not make a new directory")
  50. local attrib, errmsg = lfs.attributes (tmpdir)
  51. if not attrib then
  52. error ("could not get attributes of file `"..tmpdir.."':\n"..errmsg)
  53. end
  54. local f = io.open(tmpfile, "w")
  55. local data = "hello, file!"
  56. f:write(data)
  57. f:close()
  58. io.write(".")
  59. io.flush()
  60. -- Change access time
  61. local testdate = os.time({ year = 2007, day = 10, month = 2, hour=0})
  62. assert (lfs.touch (tmpfile, testdate))
  63. local new_att = assert (lfs.attributes (tmpfile))
  64. assert (new_att.access == testdate, "could not set access time")
  65. assert (new_att.modification == testdate, "could not set modification time")
  66. io.write(".")
  67. io.flush()
  68. -- Change access and modification time
  69. local testdate1 = os.time({ year = 2007, day = 10, month = 2, hour=0})
  70. local testdate2 = os.time({ year = 2007, day = 11, month = 2, hour=0})
  71. assert (lfs.touch (tmpfile, testdate2, testdate1))
  72. local new_att = assert (lfs.attributes (tmpfile))
  73. assert (new_att.access == testdate2, "could not set access time")
  74. assert (new_att.modification == testdate1, "could not set modification time")
  75. io.write(".")
  76. io.flush()
  77. if lfs.link (tmpfile, "_a_link_for_test_", true) then
  78. assert (lfs.attributes"_a_link_for_test_".mode == "file")
  79. assert (lfs.symlinkattributes"_a_link_for_test_".mode == "link")
  80. assert (lfs.symlinkattributes"_a_link_for_test_".target == tmpfile)
  81. assert (lfs.symlinkattributes("_a_link_for_test_", "target") == tmpfile)
  82. assert (lfs.symlinkattributes(tmpfile).mode == "file")
  83. assert (lfs.link (tmpfile, "_a_hard_link_for_test_"))
  84. assert (lfs.symlinkattributes"_a_hard_link_for_test_".mode == "file")
  85. local fd = io.open(tmpfile)
  86. assert(fd:read("*a") == data)
  87. fd:close()
  88. fd = io.open("_a_link_for_test_")
  89. assert(fd:read("*a") == data)
  90. fd:close()
  91. fd = io.open("_a_hard_link_for_test_")
  92. assert(fd:read("*a") == data)
  93. fd:close()
  94. fd = io.open("_a_hard_link_for_test_", "w+")
  95. local data2 = "write in hard link"
  96. fd:write(data2)
  97. fd:close()
  98. fd = io.open(tmpfile)
  99. assert(fd:read("*a") == data2)
  100. fd:close()
  101. if is_unix then
  102. assert (lfs.attributes (tmpfile, "nlink") == 2)
  103. end
  104. assert (os.remove"_a_link_for_test_")
  105. assert (os.remove"_a_hard_link_for_test_")
  106. end
  107. io.write(".")
  108. io.flush()
  109. -- Checking text/binary modes (only has an effect in Windows)
  110. local f = io.open(tmpfile, "w")
  111. local result, mode = lfs.setmode(f, "binary")
  112. assert(result) -- on non-Windows platforms, mode is always returned as "binary"
  113. result, mode = lfs.setmode(f, "text")
  114. assert(result and mode == "binary")
  115. f:close()
  116. local ok, err = pcall(lfs.setmode, f, "binary")
  117. assert(not ok, "could setmode on closed file")
  118. assert(err:find("closed file"), "bad error message for setmode on closed file")
  119. io.write(".")
  120. io.flush()
  121. -- Restore access time to current value
  122. assert (lfs.touch (tmpfile, attrib.access, attrib.modification))
  123. new_att = assert (lfs.attributes (tmpfile))
  124. assert (new_att.access == attrib.access)
  125. assert (new_att.modification == attrib.modification)
  126. io.write(".")
  127. io.flush()
  128. -- Check consistency of lfs.attributes values
  129. local attr = lfs.attributes (tmpfile)
  130. for key, value in pairs(attr) do
  131. assert (value == lfs.attributes (tmpfile, key),
  132. "lfs.attributes values not consistent")
  133. end
  134. -- Check that lfs.attributes accepts a table as second argument
  135. local attr2 = {}
  136. lfs.attributes(tmpfile, attr2)
  137. for key, value in pairs(attr2) do
  138. assert (value == lfs.attributes (tmpfile, key),
  139. "lfs.attributes values with table argument not consistent")
  140. end
  141. -- Check that extra arguments are ignored
  142. lfs.attributes(tmpfile, attr2, nil)
  143. -- Remove new file and directory
  144. assert (os.remove (tmpfile), "could not remove new file")
  145. assert (lfs.rmdir (tmpdir), "could not remove new directory")
  146. assert (lfs.mkdir (tmpdir..sep.."lfs_tmp_dir") == nil, "could create a directory inside a non-existent one")
  147. io.write(".")
  148. io.flush()
  149. -- Trying to get attributes of a non-existent file
  150. local attr_ok, err, errno = lfs.attributes("this couldn't be an actual file")
  151. assert(attr_ok == nil, "could get attributes of a non-existent file")
  152. assert(type(err) == "string", "failed lfs.attributes did not return an error message")
  153. assert(type(errno) == "number", "failed lfs.attributes did not return error code")
  154. assert (type(lfs.attributes (upper)) == "table", "couldn't get attributes of upper directory")
  155. io.write(".")
  156. io.flush()
  157. -- Stressing directory iterator
  158. count = 0
  159. for i = 1, 4000 do
  160. for file in lfs.dir (tmp) do
  161. count = count + 1
  162. end
  163. end
  164. io.write(".")
  165. io.flush()
  166. -- Stressing directory iterator, explicit version
  167. count = 0
  168. for i = 1, 4000 do
  169. local iter, dir = lfs.dir(tmp)
  170. local file = dir:next()
  171. while file do
  172. count = count + 1
  173. file = dir:next()
  174. end
  175. assert(not pcall(dir.next, dir))
  176. end
  177. io.write(".")
  178. io.flush()
  179. -- directory explicit close
  180. local iter, dir = lfs.dir(tmp)
  181. dir:close()
  182. assert(not pcall(dir.next, dir))
  183. print"Ok!"