NTest_file.lua 5.8 KB

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