dest.test.coffee 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. fs = require 'fs'
  2. path = require 'path'
  3. gulp = require 'gulp'
  4. tap = require '../'
  5. tapTest = require 'tap'
  6. deleteDirectory = (p) ->
  7. if fs.existsSync p
  8. fs.readdirSync(p).forEach (file) ->
  9. curP = (p + '/' + file)
  10. if fs.lstatSync(curP).isDirectory()
  11. deleteDirectory curP
  12. else
  13. fs.unlinkSync curP
  14. fs.rmdirSync p
  15. clean = ->
  16. deleteDirectory 'assets'
  17. deleteDirectory 'scripts'
  18. deleteDirectory 'sass'
  19. # helper function to get a path relative to the root
  20. getPath = (rel) -> path.resolve __dirname, '..', rel
  21. tapTest.test "gulp-tap can change dest in the middle of stream", (test) ->
  22. test.tearDown(clean)
  23. test.plan 3
  24. destinations =
  25. 'scss': 'sass'
  26. 'js': 'scripts'
  27. 'img': 'assets/images'
  28. fixturePath = getPath 'tests/fixtures/'
  29. where = (file, t) ->
  30. match = (p) ->
  31. ext = (path.extname p)
  32. .substr 1 # remove leading '.'
  33. if( ext in ['jpg', 'png', 'svg', 'gif'] )
  34. ext = 'img'
  35. destinations[ext] or false
  36. # for debugging
  37. # console.log 'destPath', destPath, file.path
  38. destPath = match file.path
  39. if destPath
  40. t.through gulp.dest, [destPath]
  41. fs.readdir fixturePath, (err, files) ->
  42. if err
  43. console.trace('Can not read fixtures from ' + fixturePath)
  44. test.end()
  45. gulp.src files.map (p) -> (fixturePath + '/' + p)
  46. .pipe tap where
  47. .on 'end', ->
  48. setTimeout (->
  49. test.ok fs.existsSync getPath 'assets/images/img.png'
  50. test.ok fs.existsSync getPath 'scripts/js.js'
  51. test.ok fs.existsSync getPath 'sass/s.scss'
  52. test.end()
  53. ), 500 # give gulp.dest a (500ms) chance to write the files
  54. .on 'error', (err) ->
  55. console.trace(err)
  56. test.end()
  57. .pipe tap(->) # We must provide a pipe to make stream end
  58. ### stub for https://github.com/geejs/gulp-tap/issues/3
  59. exports.sameIOFileName =
  60. 'test for infinite loop, issue #3': (test) ->
  61. test.plan 1
  62. fixturePath = getPath 'tests/fixtures/'
  63. gulp.src fixturePath + '/*'
  64. test.ok true
  65. test.end()###