节点(Gulp)process.stdout.write到文件 [英] Node (Gulp) process.stdout.write to file

查看:1228
本文介绍了节点(Gulp)process.stdout.write到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的单元测试进行大量的处理,并将测试覆盖率输出到 .lcov 文件中。



这是我到目前为止:

  gulp.task('test',function( ){
var test = fs.createWriteStream('./ test.lcov',{flags:'a'});
return gulp.src('./ assets / js / test / test。 js',{read:false})
.pipe(mocha({reporter:'mocha-lcov-reporter'}))
.pipe(test);
});

mocha-lcov-reporter 代码可以在这里找到:
https:// github .com / StevenLooman / mocha-lcov-reporter / blob / master / lib / lcov.js

通过过程输出结果.stdout.write()



但是,当我将它传递给 WriteStream I有以下错误:

  TypeError:无效的非字符串/缓冲区块
在validChunk(_stream_writable.js:152 :14)
在WriteStream.Writable.write(_stream_writable.js:181:12)
在Stream.ondata(stream.js:51:26)
在Stream.emit(events。 js:95:17)
(/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/node_modules/through/index.js:36:16)
在Stream.stream.queue.stream.push(/ Users / braunromain / Documents / dev / should-i-go / node_modules / gulp-mocha / node_modules / through / i ndex.js:45:5)Stream.stream
(/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/index.js:27:8)
在Stream.stream.write(/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/node_modules/through/index.js:26:11)
at write(/ Users / braunromain / Documents / dev / should-i-go / node_modules / gulp / node_modules / vinyl-fs / node_modules / through2 / node_modules / readable-stream / lib / _stream_readable.js:623:24)
at flow(/用户/ braunromain / Documents / dev / should-i-go / node_modules / gulp / node_modules / vinyl-fs / node_modules / through2 / node_modules / readable-stream / lib / _stream_readable.js:632:7)


解决方案

看起来gulp-mocha并非完全设置为真正的直通流,实际上它看起来只是将源代码输入到一个Mocha实例中,并让Mocha去做它。



第一件事情就是为了做一个简单的事情在bash中重定向...

  $ gulp test | grep -Ev^ \ [[0-9:] {0,8} \]> ./test.lcov 

当然,假设所有与gulp相关的输出都以 [00:00:00] (其中 00 是当前系统时间)。如果不是这种情况,您最终可能会在文件的顶部和底部输出gulp。



如果您正在寻找更普遍的答案(阅读:使用nodejs)你可以重写 process.stdout.write 。这可能是:(大多数情况下,但它可以工作,诀窍是你不能覆盖 process.stdout 作为另一个流,因为它在内部被写为getter。但是你可以重写 stdout.write 函数,事实上我只是为了我正在开发的一个项目做了这个,所以我可以查看其他开发人员的gulp日志他们在构建系统时遇到了问题。



我选择了一种不那么异步的解决方案,因为与nodejs中的其他所有元素不同, stdout stderr 都是阻塞流,并且不会像您习惯的异步代码那样使用这种技术,您的任务最终会寻找某种东西像这样:

  gulp.task('test',function(){
//清除旧覆盖文件
fs.writeFileSync('./ test.lcov','');

//如果你仍然想在控制台中看到输出
//你需要一个副本原始写入函数
var ogWrite = process.stdout.write;

process.stdout.write = function(chunk){
fs.appendFile('./test.lcov',chunk);

//这会将输出写入控制台
ogWrite.apply(this,arguments);
};

return gulp.src('./ assets / js / test / test.js',{read:false})
.pipe(mocha({reporter:'mocha-lcov-记者'}));
});


I'm trying to have gulp taking care of my unit tests for me, and outputting my test coverage to a .lcov file.

This is what I have so far :

gulp.task('test', function () {
    var test = fs.createWriteStream('./test.lcov', {flags: 'a'});
    return gulp.src('./assets/js/test/test.js', {read: false})
        .pipe(mocha({reporter: 'mocha-lcov-reporter'}))
        .pipe(test);
});

The mocha-lcov-reporter code can be found here : https://github.com/StevenLooman/mocha-lcov-reporter/blob/master/lib/lcov.js

It outputs results through process.stdout.write()

But when I pipe this to a WriteStream I have the following error :

TypeError: Invalid non-string/buffer chunk
    at validChunk (_stream_writable.js:152:14)
    at WriteStream.Writable.write (_stream_writable.js:181:12)
    at Stream.ondata (stream.js:51:26)
    at Stream.emit (events.js:95:17)
    at drain (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/node_modules/through/index.js:36:16)
    at Stream.stream.queue.stream.push (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/node_modules/through/index.js:45:5)
    at Stream.stream (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/index.js:27:8)
    at Stream.stream.write (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp-mocha/node_modules/through/index.js:26:11)
    at write (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:623:24)
    at flow (/Users/braunromain/Documents/dev/should-i-go/node_modules/gulp/node_modules/vinyl-fs/node_modules/through2/node_modules/readable-stream/lib/_stream_readable.js:632:7)

解决方案

Looks like gulp-mocha isn't totally set up to be a true through stream, in fact it looks like it just pipes the source into a Mocha instance and lets Mocha do it's thing.

The first thing that comes to my head just to do a simple redirect in bash...

$ gulp test | grep -Ev "^\[[0-9:]{0,8}\]" > ./test.lcov

Of course this assumes all the output that is gulp related will start with [00:00:00] (with the 00 being the current system time). If this isn't the case you may end up with gulp output at the top and bottom of your file.

If you're looking for a more universal answer (read: using nodejs) you can do a rewrite of process.stdout.write. This is probably :( upon by most but it would work. The trick is that you can't overwrite process.stdout as another stream because it is written as a getter internally. You can however rewrite the stdout.write function. As a matter of fact I just did this for a project I'm working on so I can review gulp logs of other developers in case they have issues with the build system.

I chose to go with a not so async solution because unlike most everything else in nodejs, stdout and stderr are both blocking streams and don't act like the asynchronous code you're used to. Using this technique your task would end up looking something like this:

gulp.task('test', function () {
  // clear out old coverage file
  fs.writeFileSync('./test.lcov', '');

  // if you still want to see output in the console
  //   you need a copy of the original write function
  var ogWrite = process.stdout.write;

  process.stdout.write = function( chunk ){
    fs.appendFile( './test.lcov', chunk );

    // this will write the output to the console
    ogWrite.apply( this, arguments );
  };

  return gulp.src('./assets/js/test/test.js', {read: false})
    .pipe(mocha({reporter: 'mocha-lcov-reporter'}));
});

这篇关于节点(Gulp)process.stdout.write到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆