filesystem.lua 13 KB

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