/tests/test-fs-append-file.lua

https://github.com/darkFunction/luvit · Lua · 140 lines · 93 code · 26 blank · 21 comment · 13 complexity · a52116f1c064a9f9ccf0fee1b46355f8 MD5 · raw file

  1. --[[
  2. Copyright 2012 The Luvit Authors. All Rights Reserved.
  3. Licensed under the Apache License, Version 2.0 (the "License")
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS-IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. --]]
  13. require("helper")
  14. local FS = require('fs')
  15. local join = require('path').join
  16. local Buffer = require('buffer').Buffer
  17. local filename = join(__dirname, 'fixtures', 'append.txt')
  18. p('appending to ' .. filename)
  19. local currentFileData = 'ABCD'
  20. local n = 220
  21. local s = '南越国是前203年至前111年存在于岭南地区的一个国家,国都位于番禺,疆域包括今天中国的广东、' ..
  22. '广西两省区的大部份地区,福建省、湖南、贵州、云南的一小部份地区和越南的北部。' ..
  23. '南越国是秦朝灭亡后,由南海郡尉赵佗于前203年起兵兼并桂林郡和象郡后建立。' ..
  24. '前196年和前179年,南越国曾先后两次名义上臣属于西汉,成为西汉的“外臣”。前112年,' ..
  25. '南越国末代君主赵建德与西汉发生战争,被汉武帝于前111年所灭。南越国共存在93年,' ..
  26. '历经五代君主。南越国是岭南地区的第一个有记载的政权国家,采用封建制和郡县制并存的制度,' ..
  27. '它的建立保证了秦末乱世岭南地区社会秩序的稳定,有效的改善了岭南地区落后的政治、##济现状。\n'
  28. local ncallbacks = 0
  29. -- test that empty file will be created and have content added
  30. FS.appendFile(filename, s, function(e)
  31. if e then
  32. return e
  33. end
  34. ncallbacks = ncallbacks + 1
  35. p('appended to file')
  36. FS.readFile(filename, function(e, buffer)
  37. if e then
  38. return e
  39. end
  40. p('file read')
  41. ncallbacks = ncallbacks + 1
  42. assert(#buffer == #s)
  43. end)
  44. end)
  45. -- test that appends data to a non empty file
  46. local filename2 = join(__dirname, 'fixtures', 'append2.txt')
  47. FS.writeFileSync(filename2, currentFileData)
  48. FS.appendFile(filename2, s, function(e)
  49. if e then
  50. return e
  51. end
  52. ncallbacks = ncallbacks + 1
  53. p('appended to file2')
  54. FS.readFile(filename2, function(e, buffer)
  55. if e then
  56. return e
  57. end
  58. p('file2 read')
  59. ncallbacks = ncallbacks + 1
  60. assert(#buffer == #s + #currentFileData)
  61. end)
  62. end)
  63. -- test that appendFile accepts buffers
  64. local filename3 = join(__dirname, 'fixtures', 'append3.txt')
  65. FS.writeFileSync(filename3, currentFileData)
  66. local buf = Buffer:new(s)
  67. p('appending to ' .. filename3)
  68. FS.appendFile(filename3, buf:toString(), function(e)
  69. if e then
  70. return e
  71. end
  72. ncallbacks = ncallbacks + 1
  73. p('appended to file3')
  74. FS.readFile(filename3, function(e, buffer)
  75. if e then
  76. return e
  77. end
  78. p('file3 read')
  79. ncallbacks = ncallbacks + 1
  80. assert(#buffer == buf.length + #currentFileData)
  81. end)
  82. end)
  83. -- test that appendFile accepts numbers.
  84. local filename4 = join(__dirname, 'fixtures', 'append4.txt')
  85. FS.writeFileSync(filename4, currentFileData)
  86. p('appending to ' .. filename4)
  87. FS.appendFile(filename4, tostring(n), function(e)
  88. if e then
  89. return e
  90. end
  91. ncallbacks = ncallbacks + 1
  92. p('appended to file4')
  93. FS.readFile(filename4, function(e, buffer)
  94. if e then
  95. return e
  96. end
  97. p('file4 read')
  98. ncallbacks = ncallbacks + 1
  99. assert(#buffer == #tostring(n) + #currentFileData)
  100. end)
  101. end)
  102. process:on('exit', function()
  103. p('done')
  104. assert(ncallbacks == 8)
  105. FS.unlinkSync(filename)
  106. FS.unlinkSync(filename2)
  107. FS.unlinkSync(filename3)
  108. FS.unlinkSync(filename4)
  109. end)