test.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var rename = require('gulp-rename')
  2. var srcStream = require('./')
  3. var vfs = require('vinyl-fs')
  4. var test = require('tape')
  5. var path = require('path')
  6. var fs = require('fs')
  7. var through = require('through2');
  8. function upper() {
  9. return through(function(chunk, _, cb) {
  10. var str = chunk.toString().toUpperCase();
  11. cb(null, new Buffer(str));
  12. });
  13. }
  14. test('capitalizing test file', function(t) {
  15. fs.createReadStream(__filename)
  16. .pipe(srcStream(__filename))
  17. .pipe(through.obj(function(file, _, cb) {
  18. file.contents = file.contents.pipe(upper());
  19. cb(null, file);
  20. }))
  21. .pipe(rename("fixture.js"))
  22. .pipe(vfs.dest('.'))
  23. .once('end', function() {
  24. // gulp.dest finishes before writing
  25. // the file is complete...
  26. setTimeout(function() {
  27. t.pass('reached pipline "end" event')
  28. t.equal(
  29. fs.readFileSync(__dirname + '/fixture.js', 'utf8')
  30. , fs.readFileSync(__filename, 'utf8').toUpperCase()
  31. , 'transformed contents as expected'
  32. )
  33. fs.unlink(__dirname + '/fixture.js', function(err) {
  34. t.ifError(err, 'removed fixture successfully')
  35. t.end()
  36. })
  37. }, 1500)
  38. })
  39. })
  40. test('baseDir: defaults to process.cwd()', function(t) {
  41. process.chdir(path.resolve(__dirname, '..', '..'))
  42. fs.createReadStream(__filename)
  43. .pipe(srcStream(path.basename(__filename)))
  44. .on('data', function(file) {
  45. t.equal(process.cwd(), path.dirname(file.path), 'defaults to process.cwd()')
  46. process.chdir(__dirname)
  47. t.end()
  48. })
  49. })