filesystem.lua 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. -- love.filesystem
  2. -- love.filesystem.append
  3. love.test.filesystem.append = function(test)
  4. -- create a new file to test with
  5. love.filesystem.write('filesystem.append.txt', 'foo')
  6. -- try appending text and check new file contents/size matches
  7. local success, message = love.filesystem.append('filesystem.append.txt', 'bar')
  8. test:assertNotEquals(false, success, 'check success')
  9. test:assertEquals(nil, message, 'check no error msg')
  10. local contents, size = love.filesystem.read('filesystem.append.txt')
  11. test:assertEquals(contents, 'foobar', 'check file contents')
  12. test:assertEquals(size, 6, 'check file size')
  13. -- check appending a specific no. of bytes
  14. love.filesystem.append('filesystem.append.txt', 'foobarfoobarfoo', 6)
  15. contents, size = love.filesystem.read('filesystem.append.txt')
  16. test:assertEquals(contents, 'foobarfoobar', 'check appended contents')
  17. test:assertEquals(size, 12, 'check appended size')
  18. -- cleanup
  19. love.filesystem.remove('filesystem.append.txt')
  20. end
  21. -- love.filesystem.areSymlinksEnabled
  22. -- @NOTE best can do here is just check not nil
  23. love.test.filesystem.areSymlinksEnabled = function(test)
  24. test:assertNotNil(love.filesystem.areSymlinksEnabled())
  25. end
  26. -- love.filesystem.createDirectory
  27. love.test.filesystem.createDirectory = function(test)
  28. -- try creating a dir + subdir and check both exist
  29. local success = love.filesystem.createDirectory('foo/bar')
  30. test:assertNotEquals(false, success, 'check success')
  31. test:assertNotEquals(nil, love.filesystem.getInfo('foo', 'directory'), 'check directory created')
  32. test:assertNotEquals(nil, love.filesystem.getInfo('foo/bar', 'directory'), 'check subdirectory created')
  33. -- cleanup
  34. love.filesystem.remove('foo/bar')
  35. love.filesystem.remove('foo')
  36. end
  37. -- love.filesystem.getAppdataDirectory
  38. -- @NOTE i think this is too platform dependent to be tested nicely
  39. love.test.filesystem.getAppdataDirectory = function(test)
  40. test:assertNotNil(love.filesystem.getAppdataDirectory())
  41. end
  42. -- love.filesystem.getCRequirePath
  43. love.test.filesystem.getCRequirePath = function(test)
  44. -- check default value from documentation
  45. test:assertEquals('??', love.filesystem.getCRequirePath(), 'check default value')
  46. end
  47. -- love.filesystem.getDirectoryItems
  48. love.test.filesystem.getDirectoryItems = function(test)
  49. -- create a dir + subdir with 2 files
  50. love.filesystem.createDirectory('foo/bar')
  51. love.filesystem.write('foo/file1.txt', 'file1')
  52. love.filesystem.write('foo/bar/file2.txt', 'file2')
  53. -- check both the file + subdir exist in the item list
  54. local files = love.filesystem.getDirectoryItems('foo')
  55. local hasfile = false
  56. local hasdir = false
  57. for _,v in ipairs(files) do
  58. local info = love.filesystem.getInfo('foo/'..v)
  59. if v == 'bar' and info.type == 'directory' then hasdir = true end
  60. if v == 'file1.txt' and info.type == 'file' then hasfile = true end
  61. end
  62. test:assertEquals(true, hasfile, 'check file exists')
  63. test:assertEquals(true, hasdir, 'check directory exists')
  64. -- cleanup
  65. love.filesystem.remove('foo/file1.txt')
  66. love.filesystem.remove('foo/bar/file2.txt')
  67. love.filesystem.remove('foo/bar')
  68. love.filesystem.remove('foo')
  69. end
  70. -- love.filesystem.getIdentity
  71. love.test.filesystem.getIdentity = function(test)
  72. -- check setting identity matches
  73. local original = love.filesystem.getIdentity()
  74. love.filesystem.setIdentity('lover')
  75. test:assertEquals('lover', love.filesystem.getIdentity(), 'check identity matches')
  76. -- put back to original value
  77. love.filesystem.setIdentity(original)
  78. end
  79. -- love.filesystem.getRealDirectory
  80. love.test.filesystem.getRealDirectory = function(test)
  81. -- make a test dir + file first
  82. love.filesystem.createDirectory('foo')
  83. love.filesystem.write('foo/test.txt', 'test')
  84. -- check save dir matches the real dir we just wrote to
  85. test:assertEquals(love.filesystem.getSaveDirectory(),
  86. love.filesystem.getRealDirectory('foo/test.txt'), 'check directory matches')
  87. -- cleanup
  88. love.filesystem.remove('foo/test.txt')
  89. love.filesystem.remove('foo')
  90. end
  91. -- love.filesystem.getRequirePath
  92. love.test.filesystem.getRequirePath = function(test)
  93. test:assertEquals('?.lua;?/init.lua',
  94. love.filesystem.getRequirePath(), 'check default value')
  95. end
  96. -- love.filesystem.getSource
  97. -- @NOTE i dont think we can test this cos love calls it first
  98. love.test.filesystem.getSource = function(test)
  99. test:skipTest('not sure can be tested as used internally')
  100. end
  101. -- love.filesystem.getSourceBaseDirectory
  102. -- @NOTE i think this is too platform dependent to be tested nicely
  103. love.test.filesystem.getSourceBaseDirectory = function(test)
  104. test:assertNotNil(love.filesystem.getSourceBaseDirectory())
  105. end
  106. -- love.filesystem.getUserDirectory
  107. -- @NOTE i think this is too platform dependent to be tested nicely
  108. love.test.filesystem.getUserDirectory = function(test)
  109. test:assertNotNil(love.filesystem.getUserDirectory())
  110. end
  111. -- love.filesystem.getWorkingDirectory
  112. -- @NOTE i think this is too platform dependent to be tested nicely
  113. love.test.filesystem.getWorkingDirectory = function(test)
  114. test:assertNotNil(love.filesystem.getWorkingDirectory())
  115. end
  116. -- love.filesystem.getSaveDirectory
  117. -- @NOTE i think this is too platform dependent to be tested nicely
  118. love.test.filesystem.getSaveDirectory = function(test)
  119. test:assertNotNil(love.filesystem.getSaveDirectory())
  120. end
  121. -- love.filesystem.getInfo
  122. love.test.filesystem.getInfo = function(test)
  123. -- create a dir and subdir with a file
  124. love.filesystem.createDirectory('foo/bar')
  125. love.filesystem.write('foo/bar/file2.txt', 'file2')
  126. -- check getinfo returns the correct values
  127. test:assertEquals(nil, love.filesystem.getInfo('foo/bar/file2.txt', 'directory'), 'check not directory')
  128. test:assertNotEquals(nil, love.filesystem.getInfo('foo/bar/file2.txt'), 'check info not nil')
  129. test:assertEquals(love.filesystem.getInfo('foo/bar/file2.txt').size, 5, 'check info size match')
  130. -- @TODO test modified timestamp from info.modtime?
  131. -- cleanup
  132. love.filesystem.remove('foo/bar/file2.txt')
  133. love.filesystem.remove('foo/bar')
  134. love.filesystem.remove('foo')
  135. end
  136. -- love.filesystem.isFused
  137. love.test.filesystem.isFused = function(test)
  138. -- kinda assuming you'd run the testsuite in a non-fused game
  139. test:assertEquals(love.filesystem.isFused(), false, 'check not fused')
  140. end
  141. -- love.filesystem.lines
  142. love.test.filesystem.lines = function(test)
  143. -- check lines returns the 3 lines expected
  144. love.filesystem.write('file.txt', 'line1\nline2\nline3')
  145. local linenum = 1
  146. for line in love.filesystem.lines('file.txt') do
  147. test:assertEquals('line' .. tostring(linenum), line, 'check line matches')
  148. -- also check it removes newlines like the docs says it does
  149. test:assertEquals(nil, string.find(line, '\n'), 'check newline removed')
  150. linenum = linenum + 1
  151. end
  152. -- cleanup
  153. love.filesystem.remove('file.txt')
  154. end
  155. -- love.filesystem.load
  156. love.test.filesystem.load = function(test)
  157. -- setup some fake lua files
  158. love.filesystem.write('test1.lua', 'function test()\nreturn 1\nend\nreturn test()')
  159. love.filesystem.write('test2.lua', 'function test()\nreturn 1')
  160. -- check file that doesn't exist
  161. local chunk, errormsg = love.filesystem.load('faker.lua')
  162. test:assertEquals(nil, chunk, 'check file doesnt exist')
  163. -- check valid lua file
  164. chunk, errormsg = love.filesystem.load('test1.lua')
  165. test:assertEquals(nil, errormsg, 'check no error message')
  166. test:assertEquals(1, chunk(), 'check lua file runs')
  167. -- check invalid lua file
  168. local ok, chunk, err = pcall(love.filesystem.load, 'test2.lua')
  169. test:assertEquals(false, ok, 'check invalid lua file')
  170. -- cleanup
  171. love.filesystem.remove('test1.lua')
  172. love.filesystem.remove('test2.lua')
  173. end
  174. -- love.filesystem.mount
  175. love.test.filesystem.mount = function(test)
  176. -- write an example zip to savedir to use
  177. local contents, size = love.filesystem.read('resources/test.zip') -- contains test.txt
  178. love.filesystem.write('test.zip', contents, size)
  179. -- check mounting file and check contents are mounted
  180. local success = love.filesystem.mount('test.zip', 'test')
  181. test:assertEquals(true, success, 'check success')
  182. test:assertNotEquals(nil, love.filesystem.getInfo('test'), 'check mount not nil')
  183. test:assertEquals('directory', love.filesystem.getInfo('test').type, 'check directory made')
  184. test:assertNotEquals(nil, love.filesystem.getInfo('test/test.txt'), 'check file not nil')
  185. test:assertEquals('file', love.filesystem.getInfo('test/test.txt').type, 'check file type')
  186. -- cleanup
  187. love.filesystem.remove('test/test.txt')
  188. love.filesystem.remove('test')
  189. love.filesystem.remove('test.zip')
  190. end
  191. -- love.filesystem.openFile
  192. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  193. love.test.filesystem.openFile = function(test)
  194. test:assertNotNil(love.filesystem.openFile('file2', 'r'))
  195. test:assertNotNil(love.filesystem.openFile('file2', 'w'))
  196. test:assertNotNil(love.filesystem.openFile('file2', 'a'))
  197. test:assertNotNil(love.filesystem.openFile('file2', 'c'))
  198. end
  199. -- love.filesystem.newFileData
  200. -- @NOTE this is just basic nil checking, full obj test are in objects.lua
  201. love.test.filesystem.newFileData = function(test)
  202. test:assertNotNil(love.filesystem.newFileData('helloworld', 'file1'))
  203. end
  204. -- love.filesystem.read
  205. love.test.filesystem.read = function(test)
  206. -- check reading a full file
  207. local content, size = love.filesystem.read('resources/test.txt')
  208. test:assertNotEquals(nil, content, 'check not nil')
  209. test:assertEquals('helloworld', content, 'check content match')
  210. test:assertEquals(10, size, 'check size match')
  211. -- check reading partial file
  212. content, size = love.filesystem.read('resources/test.txt', 5)
  213. test:assertNotEquals(nil, content, 'check not nil')
  214. test:assertEquals('hello', content, 'check content match')
  215. test:assertEquals(5, size, 'check size match')
  216. end
  217. -- love.filesystem.remove
  218. love.test.filesystem.remove = function(test)
  219. -- create a dir + subdir with a file
  220. love.filesystem.createDirectory('foo/bar')
  221. love.filesystem.write('foo/bar/file2.txt', 'helloworld')
  222. -- check removing files + dirs (should fail to remove dir if file inside)
  223. test:assertEquals(false, love.filesystem.remove('foo'), 'check fail when file inside')
  224. test:assertEquals(false, love.filesystem.remove('foo/bar'), 'check fail when file inside')
  225. test:assertEquals(true, love.filesystem.remove('foo/bar/file2.txt'), 'check file removed')
  226. test:assertEquals(true, love.filesystem.remove('foo/bar'), 'check subdirectory removed')
  227. test:assertEquals(true, love.filesystem.remove('foo'), 'check directory removed')
  228. -- cleanup not needed here hopefully...
  229. end
  230. -- love.filesystem.setCRequirePath
  231. love.test.filesystem.setCRequirePath = function(test)
  232. -- check setting path val is returned
  233. love.filesystem.setCRequirePath('/??')
  234. test:assertEquals('/??', love.filesystem.getCRequirePath(), 'check crequirepath value')
  235. love.filesystem.setCRequirePath('??')
  236. end
  237. -- love.filesystem.setIdentity
  238. love.test.filesystem.setIdentity = function(test)
  239. -- check setting identity val is returned
  240. local original = love.filesystem.getIdentity()
  241. love.filesystem.setIdentity('lover')
  242. test:assertEquals('lover', love.filesystem.getIdentity(), 'check indentity value')
  243. -- return value to original
  244. love.filesystem.setIdentity(original)
  245. end
  246. -- love.filesystem.setRequirePath
  247. love.test.filesystem.setRequirePath = function(test)
  248. -- check setting path val is returned
  249. love.filesystem.setRequirePath('?.lua;?/start.lua')
  250. test:assertEquals('?.lua;?/start.lua', love.filesystem.getRequirePath(), 'check require path')
  251. -- reset to default
  252. love.filesystem.setRequirePath('?.lua;?/init.lua')
  253. end
  254. -- love.filesystem.setSource
  255. -- @NOTE dont think can test this cos used internally?
  256. love.test.filesystem.setSource = function(test)
  257. test:skipTest('not sure can be tested as used internally')
  258. end
  259. -- love.filesystem.unmount
  260. love.test.filesystem.unmount = function(test)
  261. -- create a zip file mounted to use
  262. local contents, size = love.filesystem.read('resources/test.zip') -- contains test.txt
  263. love.filesystem.write('test.zip', contents, size)
  264. love.filesystem.mount('test.zip', 'test')
  265. -- check mounted, unmount, then check its unmounted
  266. test:assertNotEquals(nil, love.filesystem.getInfo('test/test.txt'), 'check mount exists')
  267. love.filesystem.unmount('test.zip')
  268. test:assertEquals(nil, love.filesystem.getInfo('test/test.txt'), 'check unmounted')
  269. -- cleanup
  270. love.filesystem.remove('test/test.txt')
  271. love.filesystem.remove('test')
  272. love.filesystem.remove('test.zip')
  273. end
  274. -- love.filesystem.write
  275. love.test.filesystem.write = function(test)
  276. -- check writing a bunch of files matches whats read back
  277. love.filesystem.write('test1.txt', 'helloworld')
  278. love.filesystem.write('test2.txt', 'helloworld', 10)
  279. love.filesystem.write('test3.txt', 'helloworld', 5)
  280. test:assertEquals('helloworld', love.filesystem.read('test1.txt'), 'check read file')
  281. test:assertEquals('helloworld', love.filesystem.read('test2.txt'), 'check read all')
  282. test:assertEquals('hello', love.filesystem.read('test3.txt'), 'check read partial')
  283. -- cleanup
  284. love.filesystem.remove('test1.txt')
  285. love.filesystem.remove('test2.txt')
  286. love.filesystem.remove('test3.txt')
  287. end