run_tests.coffee 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. {brew} = require '../src/brew'
  2. fs = require 'fs'
  3. path = require 'path'
  4. NUMBERS = 100
  5. TIME_TO_COMPRESS = 10 # ms; simulate a slow compress
  6. TIME_TO_JOIN = 10 # ms; simulate a slow join
  7. TIME_TO_COMPILE = 10 # ms; simulate a slow compile
  8. USE_LOGGER = true # set to true to watch what brew is doing
  9. # -----------------------------------------------------------------------------
  10. assertCompressedText = (test_name, b, target, max_wait, cb) ->
  11. t = Date.now()
  12. while (b.getCompressedText() isnt target) and (Date.now() - t < max_wait)
  13. await setTimeout defer(), 10
  14. ct = b.getCompressedText()
  15. if ct isnt target
  16. console.log "#{test_name} failed; target = #{target}; actual = #{ct}"
  17. console.log b._fs_watchers
  18. console.log "Try setting USE_LOGGER=true to see what brew is doing."
  19. process.exit 1
  20. else
  21. console.log "#{test_name} passed after #{Date.now() - t}ms"
  22. cb()
  23. # -----------------------------------------------------------------------------
  24. nPath = (num) ->
  25. if num % 2
  26. "#{__dirname}/cases/math/odds/#{num}.txt"
  27. else
  28. "#{__dirname}/cases/math/evens/#{num}.txt"
  29. # -----------------------------------------------------------------------------
  30. fullDeletionTest = (b, cb) ->
  31. console.log "STARTING DELETION TEST\n\n"
  32. for i in [0...NUMBERS]
  33. await fs.exists nPath(i), defer exists
  34. if exists
  35. await fs.unlink nPath(i), defer err
  36. if err? then console.log err
  37. await rmdir "#{__dirname}/cases/math/other/subdir", defer()
  38. await assertCompressedText "full deletion test", b, 0, 10000, defer()
  39. cb()
  40. # -----------------------------------------------------------------------------
  41. fullInsertionTest = (b, cb) ->
  42. console.log "STARTING INSERTION TEST\n\n"
  43. for i in [0...NUMBERS]
  44. await fs.writeFile nPath(i), i, defer err
  45. target = (NUMBERS) * (NUMBERS - 1) / 2
  46. await assertCompressedText "full insertion test", b, target, 10000, defer()
  47. cb()
  48. # -----------------------------------------------------------------------------
  49. subdirCreationTest = (b, cb) ->
  50. console.log "STARTING SUBDIR CREATION TEST\n\n"
  51. dirname = "#{__dirname}/cases/math/other/subdir"
  52. await
  53. fs.mkdir dirname, defer err
  54. if err
  55. console.log err
  56. process.exit 1
  57. for i in [0...NUMBERS]
  58. await fs.writeFile "#{dirname}/#{i}.txt", i, defer err
  59. target = (NUMBERS) * (NUMBERS - 1) / 2
  60. await assertCompressedText "subdir creation test", b, target, 10000, defer()
  61. cb()
  62. # -----------------------------------------------------------------------------
  63. subdirDeletionTest = (b, cb) ->
  64. console.log "STARTING SUBDIR DELETION TEST\n\n"
  65. dirname = "#{__dirname}/cases/math/other/subdir"
  66. await rmdir dirname, defer()
  67. await assertCompressedText "subdir deletion test", b, 0, 10000, defer()
  68. cb()
  69. # -----------------------------------------------------------------------------
  70. myCompress = (str, cb) ->
  71. ###
  72. adds up all the numbers in a comma-separated string
  73. ###
  74. await setTimeout defer(), TIME_TO_COMPRESS
  75. nums = str.split ","
  76. sum = 0
  77. if str.length then sum += parseInt n for n in nums
  78. cb null, sum
  79. # -----------------------------------------------------------------------------
  80. myJoin = (strs, cb) ->
  81. await setTimeout defer(), TIME_TO_JOIN
  82. cb null, strs.join ","
  83. # -----------------------------------------------------------------------------
  84. myCompile = (p, str, cb) ->
  85. await setTimeout defer(), TIME_TO_COMPILE
  86. cb null, str
  87. # -----------------------------------------------------------------------------
  88. rmdir = (dir, cb) ->
  89. await fs.readdir dir, defer err, list
  90. if not err
  91. for f in list
  92. filename = path.join dir, f
  93. await fs.stat filename, defer err, stat
  94. if not (f in ['.','..'])
  95. if stat.isDirectory()
  96. await rmdir filename, defer()
  97. else
  98. await fs.unlink filename, defer err
  99. await fs.rmdir dir, defer()
  100. cb()
  101. # -----------------------------------------------------------------------------
  102. await b = new brew {
  103. includes: ["./cases/math/"]
  104. excludes: []
  105. match: /^.*.txt$/
  106. join: (strs, cb) -> myJoin strs, cb
  107. compress: (str, cb) -> myCompress str, cb
  108. compile: (p, str, cb) -> myCompile p, str, cb
  109. logger: (line) -> if USE_LOGGER then console.log "brew speaks: #{line}"
  110. onReady: defer vh, txt, ctxt
  111. onChange: (vh, txt, ctxt) -> console.log "change: [#{vh}] #{txt} -> #{ctxt}"
  112. }
  113. d = Date.now()
  114. await fullDeletionTest b, defer()
  115. await fullInsertionTest b, defer()
  116. await fullDeletionTest b, defer()
  117. await fullDeletionTest b, defer()
  118. await fullInsertionTest b, defer()
  119. await fullDeletionTest b, defer()
  120. await subdirCreationTest b, defer()
  121. await subdirDeletionTest b, defer()
  122. await fullDeletionTest b, defer()
  123. console.log "\n\n\nSUCCESS; total time = #{Date.now() - d}ms\n\n\n"
  124. process.exit 1