NTest_file.lua 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. local N = ...
  2. N = (N or require "NTest")("file")
  3. local function cleanup()
  4. file.remove("testfile")
  5. file.remove("testfile2")
  6. local testfiles = {"testfile1&", "testFILE2"}
  7. for _, n in ipairs(testfiles) do
  8. file.remove(n,n)
  9. end
  10. end
  11. N.test('exist', function()
  12. cleanup()
  13. nok(file.exists("non existing file"), "non existing file")
  14. file.putcontents("testfile", "testcontents")
  15. ok(file.exists("testfile"), "existing file")
  16. end)
  17. N.test('fscfg', function()
  18. cleanup()
  19. local start, size = file.fscfg()
  20. ok(start, "start")
  21. ok(size, "size")
  22. end)
  23. N.test('fsinfo', function()
  24. cleanup()
  25. local remaining, used, total = file.fsinfo()
  26. ok(remaining, "remaining")
  27. ok(used, "used")
  28. ok(total, "total")
  29. ok(eq(remaining+used, total), "size maths")
  30. end)
  31. N.test('getcontents', function()
  32. cleanup()
  33. local testcontent = "some content \0 and more"
  34. file.putcontents("testfile", testcontent)
  35. local content = file.getcontents("testfile")
  36. ok(eq(testcontent, content),"contents")
  37. end)
  38. N.test('getcontents non existent file', function()
  39. cleanup()
  40. nok(file.getcontents("non existing file"), "non existent file")
  41. end)
  42. N.test('getcontents more than 1K', function()
  43. cleanup()
  44. local f = file.open("testfile", "w")
  45. for i = 1,100 do -- luacheck: ignore
  46. f:write("some text to test")
  47. end
  48. f:close()
  49. local content = file.getcontents("testfile")
  50. ok(eq(#content, 1700), "long contents")
  51. end)
  52. N.test('read more than 1K', function()
  53. cleanup()
  54. local f = file.open("testfile", "w")
  55. for i = 1,100 do -- luacheck: ignore
  56. f:write("some text to test")
  57. end
  58. f:close()
  59. f = file.open("testfile","r")
  60. local buffer = f:read()
  61. ok(eq(#buffer, 1024), "first block")
  62. buffer = f:read()
  63. f:close()
  64. ok(eq(#buffer, 1700-1024), "second block")
  65. end)
  66. local function makefile(name, num128)
  67. local s128 = "16 bytes written"
  68. s128 = s128..s128..s128..s128
  69. s128 = s128..s128
  70. local f = file.open(name, "w")
  71. for i = 1, num128 do -- luacheck: ignore
  72. f:write(s128)
  73. end
  74. f:close()
  75. end
  76. N.test('read 7*128 bytes', function()
  77. cleanup()
  78. makefile("testfile", 7)
  79. local f = file.open("testfile","r")
  80. local buffer = f:read()
  81. f:close()
  82. nok(eq(buffer, nil), "nil first block")
  83. ok(eq(#buffer, 128*7), "length first block")
  84. end)
  85. N.test('read 8*128 bytes long file', function()
  86. cleanup()
  87. makefile("testfile", 8)
  88. local f = file.open("testfile","r")
  89. local buffer = f:read()
  90. nok(eq(buffer, nil), "nil first block")
  91. ok(eq(#buffer, 128*8), "size first block")
  92. buffer = f:read()
  93. f:close()
  94. ok(eq(buffer, nil), "nil second block")
  95. end)
  96. N.test('read 9*128 bytes', function()
  97. cleanup()
  98. makefile("testfile", 9)
  99. local f = file.open("testfile","r")
  100. local buffer = f:read()
  101. nok(eq(buffer, nil), "nil first block")
  102. ok(eq(#buffer, 1024), "size first block")
  103. buffer = f:read()
  104. f:close()
  105. nok(eq(buffer, nil), "nil second block")
  106. ok(eq(#buffer, 128*9-1024), "size second block")
  107. end)
  108. N.test('list', function()
  109. cleanup()
  110. local function count(files)
  111. local filecount = 0
  112. for _,_ in pairs(files) do filecount = filecount+1 end
  113. return filecount
  114. end
  115. local files
  116. local function testfile(name)
  117. ok(eq(files[name],#name), "length matches name length")
  118. end
  119. local testfiles = {"testfile1&", "testFILE2"}
  120. for _, n in ipairs(testfiles) do
  121. file.putcontents(n,n)
  122. end
  123. files = file.list("testfile%.*")
  124. ok(eq(count(files), 1), "found file")
  125. testfile("testfile1&")
  126. files = file.list("^%l*%u+%d%.-")
  127. ok(eq(count(files), 1), "found file regexp")
  128. testfile("testFILE2")
  129. files = file.list()
  130. ok(count(files) >= 2, "several files found")
  131. end)
  132. N.test('open non existing', function()
  133. cleanup()
  134. local function testopen(test, filename, mode)
  135. test(file.open(filename, mode), mode)
  136. file.close()
  137. file.remove(filename)
  138. end
  139. testopen(nok, "testfile", "r")
  140. testopen(ok, "testfile", "w")
  141. testopen(ok, "testfile", "a")
  142. testopen(nok, "testfile", "r+")
  143. testopen(ok, "testfile", "w+")
  144. testopen(ok, "testfile", "a+")
  145. end)
  146. N.test('open existing', function()
  147. cleanup()
  148. local function testopen(mode, position)
  149. file.putcontents("testfile", "testcontent")
  150. ok(file.open("testfile", mode), mode)
  151. file.write("")
  152. ok(eq(file.seek(), position), "seek check")
  153. file.close()
  154. end
  155. testopen("r", 0)
  156. testopen("w", 0)
  157. testopen("a", 11)
  158. testopen("r+", 0)
  159. testopen("w+", 0)
  160. testopen("a+", 11)
  161. end)
  162. N.test('remove', function()
  163. cleanup()
  164. file.putcontents("testfile", "testfile")
  165. ok(file.remove("testfile") == nil, "existing file")
  166. ok(file.remove("testfile") == nil, "non existing file")
  167. end)
  168. N.test('rename', function()
  169. cleanup()
  170. file.putcontents("testfile", "testfile")
  171. ok(file.rename("testfile", "testfile2"), "rename existing")
  172. nok(file.exists("testfile"), "old file removed")
  173. ok(file.exists("testfile2"), "new file exists")
  174. nok(file.rename("testfile", "testfile3"), "rename non existing")
  175. file.putcontents("testfile", "testfile")
  176. nok(file.rename("testfile", "testfile2"), "rename to existing")
  177. ok(file.exists("testfile"), "from file exists")
  178. ok(file.exists("testfile2"), "to file exists")
  179. end)
  180. N.test('stat existing file', function()
  181. cleanup()
  182. file.putcontents("testfile", "testfile")
  183. local stat = file.stat("testfile")
  184. ok(stat, "stat existing")
  185. ok(eq(stat.size, 8), "size")
  186. ok(eq(stat.name, "testfile"), "name")
  187. ok(stat.time, "no time")
  188. ok(eq(stat.time.year, 1970), "year")
  189. ok(eq(stat.time.mon, 01), "mon")
  190. ok(eq(stat.time.day, 01), "day")
  191. ok(eq(stat.time.hour, 0), "hour")
  192. ok(eq(stat.time.min, 0), "min")
  193. ok(eq(stat.time.sec, 0), "sec")
  194. nok(stat.is_dir, "is_dir")
  195. nok(stat.is_rdonly, "is_rdonly")
  196. nok(stat.is_hidden, "is_hidden")
  197. nok(stat.is_sys, "is_sys")
  198. nok(stat.is_arch, "is_arch")
  199. end)
  200. N.test('stat non existing file', function()
  201. cleanup()
  202. local stat = file.stat("not existing file")
  203. ok(stat == nil, "stat empty")
  204. end)