filesystem.lua 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  1. -- love.filesystem
  2. --------------------------------------------------------------------------------
  3. --------------------------------------------------------------------------------
  4. ------------------------------------OBJECTS-------------------------------------
  5. --------------------------------------------------------------------------------
  6. --------------------------------------------------------------------------------
  7. -- File (love.filesystem.newFile)
  8. love.test.filesystem.File = function(test)
  9. -- setup a file to play with
  10. local file1 = love.filesystem.openFile('data.txt', 'w')
  11. file1:write('helloworld')
  12. test:assertObject(file1)
  13. file1:close()
  14. -- test read mode
  15. file1:open('r')
  16. test:assertEquals('r', file1:getMode(), 'check read mode')
  17. local contents, size = file1:read()
  18. test:assertEquals('helloworld', contents)
  19. test:assertEquals(10, size, 'check file read')
  20. test:assertEquals(10, file1:getSize())
  21. local ok1, err1 = file1:write('hello')
  22. test:assertNotEquals(nil, err1, 'check cant write in read mode')
  23. local iterator = file1:lines()
  24. test:assertNotEquals(nil, iterator, 'check can read lines')
  25. test:assertEquals('data.txt', file1:getFilename(), 'check filename matches')
  26. file1:close()
  27. -- test write mode
  28. file1:open('w')
  29. test:assertEquals('w', file1:getMode(), 'check write mode')
  30. contents, size = file1:read()
  31. test:assertEquals(nil, contents, 'check cant read file in write mode')
  32. test:assertEquals('string', type(size), 'check err message shown')
  33. local ok2, err2 = file1:write('helloworld')
  34. test:assertTrue(ok2, 'check file write')
  35. test:assertEquals(nil, err2, 'check no err writing')
  36. -- test open/closing
  37. file1:open('r')
  38. test:assertTrue(file1:isOpen(), 'check file is open')
  39. file1:close()
  40. test:assertFalse(file1:isOpen(), 'check file gets closed')
  41. file1:close()
  42. -- test buffering and flushing
  43. file1:open('w')
  44. local ok3, err3 = file1:setBuffer('full', 10000)
  45. test:assertTrue(ok3)
  46. test:assertEquals('full', file1:getBuffer())
  47. file1:write('replacedcontent')
  48. file1:flush()
  49. file1:close()
  50. file1:open('r')
  51. contents, size = file1:read()
  52. test:assertEquals('replacedcontent', contents, 'check buffered content was written')
  53. file1:close()
  54. -- loop through file data with seek/tell until EOF
  55. file1:open('r')
  56. local counter = 0
  57. for i=1,100 do
  58. file1:seek(i)
  59. test:assertEquals(i, file1:tell())
  60. if file1:isEOF() == true then
  61. counter = i
  62. break
  63. end
  64. end
  65. test:assertEquals(counter, 15)
  66. file1:close()
  67. end
  68. -- FileData (love.filesystem.newFileData)
  69. love.test.filesystem.FileData = function(test)
  70. -- create new obj
  71. local fdata = love.filesystem.newFileData('helloworld', 'test.txt')
  72. test:assertObject(fdata)
  73. test:assertEquals('test.txt', fdata:getFilename())
  74. test:assertEquals('txt', fdata:getExtension())
  75. -- check properties match expected
  76. test:assertEquals('helloworld', fdata:getString(), 'check data string')
  77. test:assertEquals(10, fdata:getSize(), 'check data size')
  78. -- check cloning the bytedata
  79. local clonedfdata = fdata:clone()
  80. test:assertObject(clonedfdata)
  81. test:assertEquals('helloworld', clonedfdata:getString(), 'check cloned data')
  82. test:assertEquals(10, clonedfdata:getSize(), 'check cloned size')
  83. end
  84. --------------------------------------------------------------------------------
  85. --------------------------------------------------------------------------------
  86. ------------------------------------METHODS-------------------------------------
  87. --------------------------------------------------------------------------------
  88. --------------------------------------------------------------------------------
  89. -- love.filesystem.append
  90. love.test.filesystem.append = function(test)
  91. -- create a new file to test with
  92. love.filesystem.write('filesystem.append.txt', 'foo')
  93. -- try appending text and check new file contents/size matches
  94. local success, message = love.filesystem.append('filesystem.append.txt', 'bar')
  95. test:assertNotEquals(false, success, 'check success')
  96. test:assertEquals(nil, message, 'check no error msg')
  97. local contents, size = love.filesystem.read('filesystem.append.txt')
  98. test:assertEquals(contents, 'foobar', 'check file contents')
  99. test:assertEquals(size, 6, 'check file size')
  100. -- check appending a specific no. of bytes
  101. love.filesystem.append('filesystem.append.txt', 'foobarfoobarfoo', 6)
  102. contents, size = love.filesystem.read('filesystem.append.txt')
  103. test:assertEquals(contents, 'foobarfoobar', 'check appended contents')
  104. test:assertEquals(size, 12, 'check appended size')
  105. -- cleanup
  106. love.filesystem.remove('filesystem.append.txt')
  107. end
  108. -- love.filesystem.areSymlinksEnabled
  109. -- @NOTE best can do here is just check not nil
  110. love.test.filesystem.areSymlinksEnabled = function(test)
  111. test:assertNotNil(love.filesystem.areSymlinksEnabled())
  112. end
  113. -- love.filesystem.createDirectory
  114. love.test.filesystem.createDirectory = function(test)
  115. -- try creating a dir + subdir and check both exist
  116. local success = love.filesystem.createDirectory('foo/bar')
  117. test:assertNotEquals(false, success, 'check success')
  118. test:assertNotEquals(nil, love.filesystem.getInfo('foo', 'directory'), 'check directory created')
  119. test:assertNotEquals(nil, love.filesystem.getInfo('foo/bar', 'directory'), 'check subdirectory created')
  120. -- cleanup
  121. love.filesystem.remove('foo/bar')
  122. love.filesystem.remove('foo')
  123. end
  124. -- love.filesystem.getAppdataDirectory
  125. -- @NOTE i think this is too platform dependent to be tested nicely
  126. love.test.filesystem.getAppdataDirectory = function(test)
  127. test:assertNotNil(love.filesystem.getAppdataDirectory())
  128. end
  129. -- love.filesystem.getCRequirePath
  130. love.test.filesystem.getCRequirePath = function(test)
  131. -- check default value from documentation
  132. test:assertEquals('??', love.filesystem.getCRequirePath(), 'check default value')
  133. end
  134. -- love.filesystem.getDirectoryItems
  135. love.test.filesystem.getDirectoryItems = function(test)
  136. -- create a dir + subdir with 2 files
  137. love.filesystem.createDirectory('foo/bar')
  138. love.filesystem.write('foo/file1.txt', 'file1')
  139. love.filesystem.write('foo/bar/file2.txt', 'file2')
  140. -- check both the file + subdir exist in the item list
  141. local files = love.filesystem.getDirectoryItems('foo')
  142. local hasfile = false
  143. local hasdir = false
  144. for _,v in ipairs(files) do
  145. local info = love.filesystem.getInfo('foo/'..v)
  146. if v == 'bar' and info.type == 'directory' then hasdir = true end
  147. if v == 'file1.txt' and info.type == 'file' then hasfile = true end
  148. end
  149. test:assertTrue(hasfile, 'check file exists')
  150. test:assertTrue(hasdir, 'check directory exists')
  151. -- cleanup
  152. love.filesystem.remove('foo/file1.txt')
  153. love.filesystem.remove('foo/bar/file2.txt')
  154. love.filesystem.remove('foo/bar')
  155. love.filesystem.remove('foo')
  156. end
  157. -- love.filesystem.getIdentity
  158. love.test.filesystem.getIdentity = function(test)
  159. -- check setting identity matches
  160. local original = love.filesystem.getIdentity()
  161. love.filesystem.setIdentity('lover')
  162. test:assertEquals('lover', love.filesystem.getIdentity(), 'check identity matches')
  163. -- put back to original value
  164. love.filesystem.setIdentity(original)
  165. end
  166. -- love.filesystem.getRealDirectory
  167. love.test.filesystem.getRealDirectory = function(test)
  168. -- make a test dir + file first
  169. love.filesystem.createDirectory('foo')
  170. love.filesystem.write('foo/test.txt', 'test')
  171. -- check save dir matches the real dir we just wrote to
  172. test:assertEquals(love.filesystem.getSaveDirectory(),
  173. love.filesystem.getRealDirectory('foo/test.txt'), 'check directory matches')
  174. -- cleanup
  175. love.filesystem.remove('foo/test.txt')
  176. love.filesystem.remove('foo')
  177. end
  178. -- love.filesystem.getRequirePath
  179. love.test.filesystem.getRequirePath = function(test)
  180. test:assertEquals('?.lua;?/init.lua',
  181. love.filesystem.getRequirePath(), 'check default value')
  182. end
  183. -- love.filesystem.getSource
  184. -- @NOTE i dont think we can test this cos love calls it first
  185. love.test.filesystem.getSource = function(test)
  186. test:skipTest('used internally')
  187. end
  188. -- love.filesystem.getSourceBaseDirectory
  189. -- @NOTE i think this is too platform dependent to be tested nicely
  190. love.test.filesystem.getSourceBaseDirectory = function(test)
  191. test:assertNotNil(love.filesystem.getSourceBaseDirectory())
  192. end
  193. -- love.filesystem.getUserDirectory
  194. -- @NOTE i think this is too platform dependent to be tested nicely
  195. love.test.filesystem.getUserDirectory = function(test)
  196. test:assertNotNil(love.filesystem.getUserDirectory())
  197. end
  198. -- love.filesystem.getWorkingDirectory
  199. -- @NOTE i think this is too platform dependent to be tested nicely
  200. love.test.filesystem.getWorkingDirectory = function(test)
  201. test:assertNotNil(love.filesystem.getWorkingDirectory())
  202. end
  203. -- love.filesystem.getSaveDirectory
  204. -- @NOTE i think this is too platform dependent to be tested nicely
  205. love.test.filesystem.getSaveDirectory = function(test)
  206. test:assertNotNil(love.filesystem.getSaveDirectory())
  207. end
  208. -- love.filesystem.getInfo
  209. love.test.filesystem.getInfo = function(test)
  210. -- create a dir and subdir with a file
  211. love.filesystem.createDirectory('foo/bar')
  212. love.filesystem.write('foo/bar/file2.txt', 'file2')
  213. -- check getinfo returns the correct values
  214. test:assertEquals(nil, love.filesystem.getInfo('foo/bar/file2.txt', 'directory'), 'check not directory')
  215. test:assertNotEquals(nil, love.filesystem.getInfo('foo/bar/file2.txt'), 'check info not nil')
  216. test:assertEquals(love.filesystem.getInfo('foo/bar/file2.txt').size, 5, 'check info size match')
  217. -- @TODO test modified timestamp from info.modtime?
  218. -- cleanup
  219. love.filesystem.remove('foo/bar/file2.txt')
  220. love.filesystem.remove('foo/bar')
  221. love.filesystem.remove('foo')
  222. end
  223. -- love.filesystem.isFused
  224. love.test.filesystem.isFused = function(test)
  225. -- kinda assuming you'd run the testsuite in a non-fused game
  226. test:assertEquals(love.filesystem.isFused(), false, 'check not fused')
  227. end
  228. -- love.filesystem.lines
  229. love.test.filesystem.lines = function(test)
  230. -- check lines returns the 3 lines expected
  231. love.filesystem.write('file.txt', 'line1\nline2\nline3')
  232. local linenum = 1
  233. for line in love.filesystem.lines('file.txt') do
  234. test:assertEquals('line' .. tostring(linenum), line, 'check line matches')
  235. -- also check it removes newlines like the docs says it does
  236. test:assertEquals(nil, string.find(line, '\n'), 'check newline removed')
  237. linenum = linenum + 1
  238. end
  239. -- cleanup
  240. love.filesystem.remove('file.txt')
  241. end
  242. -- love.filesystem.load
  243. love.test.filesystem.load = function(test)
  244. -- setup some fake lua files
  245. love.filesystem.write('test1.lua', 'function test()\nreturn 1\nend\nreturn test()')
  246. love.filesystem.write('test2.lua', 'function test()\nreturn 1')
  247. -- check file that doesn't exist
  248. local chunk, errormsg = love.filesystem.load('faker.lua')
  249. test:assertEquals(nil, chunk, 'check file doesnt exist')
  250. -- check valid lua file
  251. chunk, errormsg = love.filesystem.load('test1.lua')
  252. test:assertEquals(nil, errormsg, 'check no error message')
  253. test:assertEquals(1, chunk(), 'check lua file runs')
  254. -- check invalid lua file
  255. local ok, chunk, err = pcall(love.filesystem.load, 'test2.lua')
  256. test:assertFalse(ok, 'check invalid lua file')
  257. -- cleanup
  258. love.filesystem.remove('test1.lua')
  259. love.filesystem.remove('test2.lua')
  260. end
  261. -- love.filesystem.mount
  262. love.test.filesystem.mount = function(test)
  263. -- write an example zip to savedir to use
  264. local contents, size = love.filesystem.read('resources/test.zip') -- contains test.txt
  265. love.filesystem.write('test.zip', contents, size)
  266. -- check mounting file and check contents are mounted
  267. local success = love.filesystem.mount('test.zip', 'test')
  268. test:assertTrue(success, 'check success')
  269. test:assertNotEquals(nil, love.filesystem.getInfo('test'), 'check mount not nil')
  270. test:assertEquals('directory', love.filesystem.getInfo('test').type, 'check directory made')
  271. test:assertNotEquals(nil, love.filesystem.getInfo('test/test.txt'), 'check file not nil')
  272. test:assertEquals('file', love.filesystem.getInfo('test/test.txt').type, 'check file type')
  273. -- cleanup
  274. love.filesystem.remove('test/test.txt')
  275. love.filesystem.remove('test')
  276. love.filesystem.remove('test.zip')
  277. end
  278. -- love.filesystem.openFile
  279. -- @NOTE this is just basic nil checking, objs have their own test method
  280. love.test.filesystem.openFile = function(test)
  281. test:assertNotNil(love.filesystem.openFile('file2.txt', 'w'))
  282. test:assertNotNil(love.filesystem.openFile('file2.txt', 'r'))
  283. test:assertNotNil(love.filesystem.openFile('file2.txt', 'a'))
  284. test:assertNotNil(love.filesystem.openFile('file2.txt', 'c'))
  285. love.filesystem.remove('file2.txt')
  286. end
  287. -- love.filesystem.newFileData
  288. -- @NOTE this is just basic nil checking, objs have their own test method
  289. love.test.filesystem.newFileData = function(test)
  290. test:assertNotNil(love.filesystem.newFileData('helloworld', 'file1'))
  291. end
  292. -- love.filesystem.read
  293. love.test.filesystem.read = function(test)
  294. -- check reading a full file
  295. local content, size = love.filesystem.read('resources/test.txt')
  296. test:assertNotEquals(nil, content, 'check not nil')
  297. test:assertEquals('helloworld', content, 'check content match')
  298. test:assertEquals(10, size, 'check size match')
  299. -- check reading partial file
  300. content, size = love.filesystem.read('resources/test.txt', 5)
  301. test:assertNotEquals(nil, content, 'check not nil')
  302. test:assertEquals('hello', content, 'check content match')
  303. test:assertEquals(5, size, 'check size match')
  304. end
  305. -- love.filesystem.remove
  306. love.test.filesystem.remove = function(test)
  307. -- create a dir + subdir with a file
  308. love.filesystem.createDirectory('foo/bar')
  309. love.filesystem.write('foo/bar/file2.txt', 'helloworld')
  310. -- check removing files + dirs (should fail to remove dir if file inside)
  311. test:assertFalse(love.filesystem.remove('foo'), 'check fail when file inside')
  312. test:assertFalse(love.filesystem.remove('foo/bar'), 'check fail when file inside')
  313. test:assertTrue(love.filesystem.remove('foo/bar/file2.txt'), 'check file removed')
  314. test:assertTrue(love.filesystem.remove('foo/bar'), 'check subdirectory removed')
  315. test:assertTrue(love.filesystem.remove('foo'), 'check directory removed')
  316. -- cleanup not needed here hopefully...
  317. end
  318. -- love.filesystem.setCRequirePath
  319. love.test.filesystem.setCRequirePath = function(test)
  320. -- check setting path val is returned
  321. love.filesystem.setCRequirePath('/??')
  322. test:assertEquals('/??', love.filesystem.getCRequirePath(), 'check crequirepath value')
  323. love.filesystem.setCRequirePath('??')
  324. end
  325. -- love.filesystem.setIdentity
  326. love.test.filesystem.setIdentity = function(test)
  327. -- check setting identity val is returned
  328. local original = love.filesystem.getIdentity()
  329. love.filesystem.setIdentity('lover')
  330. test:assertEquals('lover', love.filesystem.getIdentity(), 'check indentity value')
  331. -- return value to original
  332. love.filesystem.setIdentity(original)
  333. end
  334. -- love.filesystem.setRequirePath
  335. love.test.filesystem.setRequirePath = function(test)
  336. -- check setting path val is returned
  337. love.filesystem.setRequirePath('?.lua;?/start.lua')
  338. test:assertEquals('?.lua;?/start.lua', love.filesystem.getRequirePath(), 'check require path')
  339. -- reset to default
  340. love.filesystem.setRequirePath('?.lua;?/init.lua')
  341. end
  342. -- love.filesystem.setSource
  343. love.test.filesystem.setSource = function(test)
  344. test:skipTest('used internally')
  345. end
  346. -- love.filesystem.unmount
  347. love.test.filesystem.unmount = function(test)
  348. -- create a zip file mounted to use
  349. local contents, size = love.filesystem.read('resources/test.zip') -- contains test.txt
  350. love.filesystem.write('test.zip', contents, size)
  351. love.filesystem.mount('test.zip', 'test')
  352. -- check mounted, unmount, then check its unmounted
  353. test:assertNotEquals(nil, love.filesystem.getInfo('test/test.txt'), 'check mount exists')
  354. love.filesystem.unmount('test.zip')
  355. test:assertEquals(nil, love.filesystem.getInfo('test/test.txt'), 'check unmounted')
  356. -- cleanup
  357. love.filesystem.remove('test/test.txt')
  358. love.filesystem.remove('test')
  359. love.filesystem.remove('test.zip')
  360. end
  361. -- love.filesystem.write
  362. love.test.filesystem.write = function(test)
  363. -- check writing a bunch of files matches whats read back
  364. love.filesystem.write('test1.txt', 'helloworld')
  365. love.filesystem.write('test2.txt', 'helloworld', 10)
  366. love.filesystem.write('test3.txt', 'helloworld', 5)
  367. test:assertEquals('helloworld', love.filesystem.read('test1.txt'), 'check read file')
  368. test:assertEquals('helloworld', love.filesystem.read('test2.txt'), 'check read all')
  369. test:assertEquals('hello', love.filesystem.read('test3.txt'), 'check read partial')
  370. -- cleanup
  371. love.filesystem.remove('test1.txt')
  372. love.filesystem.remove('test2.txt')
  373. love.filesystem.remove('test3.txt')
  374. end