我怎样才能“中止”一口气

时间:2023-01-24 18:31:03

In my gulp build I would like to "abort" the build process in the event that my server unit tests fail but I'm not sure how to accomplish this.

在我的gulp构建中,我想在我的服务器单元测试失败的情况下“中止”构建过程,但我不知道如何实现这一点。

Currently, I'm using node's request module to run some server side unit tests like so:

目前,我正在使用节点的请求模块来运行一些服务器端单元测试,如下所示:

gulp.task("run-server-tests", function(){

    var serverTestUrl = "http://myurl"; // returns test results in json format

    request(serverTestUrl, function (error, response, body) {

        var responseData = JSON.parse(body);

        if(responseData.isSuccess){

            console.log(responseData.message);

            // nice! continue with rest of build (js, css tasks, etc.)

        }

        else {

            open(serverTestUrl + "&render"); // show unit test failures

            // err.... gulp.abortProcessing(); ????

        }

    });

});

==============================================================

**UPDATE

After the helpful responses from Martin and Nick I have stripped my build down to the most basic example I could come up with to attempt both suggestions, which where:

在马丁和尼克的有用回应之后,我将我的构建剥离到最基本的例子,我可以想出两个建议,其中:

  1. Use the callback method automatically passed to the task function to abort the build, and
  2. 使用自动传递给任务函数的回调方法来中止构建,并且

  3. Use promises to abort the build
  4. 使用promises中止构建

Here is my updated gulpfile.js:

这是我更新的gulpfile.js:

var gulp = require("gulp");
var plugins = require('gulp-load-plugins')();
var Q = require("q");

gulp.task("task-1-option-1", function(callback){

    plugins.util.log("task 1 running");

    callback("unit test failed, stop build"); // let's just assume the unit tests fail

});

gulp.task("task-1-option-2", function(callback){

    plugins.util.log("task 1 running");

    var deferred = Q.defer();

    setTimeout(function(){

        deferred.reject("unit test failed, stop build"); // again, let's assume the unit tests fail

    }, 2000);

    return deferred.promise;

});

gulp.task("task-2-option-1", ["task-1-option-1"], function(){

    // if this runs, the build didn't abort as expected
    plugins.util.log("task 2 running");

});

gulp.task("task-2-option-2", ["task-1-option-2"], function(){

    // if this runs, the build didn't abort as expected
    plugins.util.log("task 2 running");

});


// trigger option-1 (Nick's suggestion)
gulp.task("option-1", ["task-1-option-1", "task-2-option-1"]);

// trigger option-2 (Martin's suggestion)
gulp.task("option-2", ["task-1-option-2", "task-2-option-2"]);

================================================================

The problem is that while both of these approaches appear to "abort" the build, they both also cause gulp to throw an "hard error" in the terminal (cygwin). I'm wondering if this is because I'm running these tests on windows, because well, from my limited experience everything node/gulp seems to break for me on windows.

问题是虽然这两种方法似乎都“中止”了构建,但它们都会导致gulp在终端(cygwin)中抛出“硬错误”。我想知道这是不是因为我在Windows上运行这些测试,因为从我有限的经验来看,所有节点/ gulp似乎在Windows上为我打破了。

This is the output I receive for each task. First, the callback option:

这是我为每项任务收到的​​输出。首先,回调选项:

$ gulp option-1
[10:45:32] Using gulpfile C:\users\bfitzgerald\desktop\gulp-test\gulpfile.js
[10:45:32] Starting 'task-1-option-1'...
[10:45:32] task 1 running
[10:45:32] 'task-1-option-1' errored after 79 ms
[10:45:32] Error: unit test failed, stop build
    at formatError (C:\Users\bfitzgerald\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:169:10)
    at Gulp.<anonymous> (C:\Users\bfitzgerald\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:195:15)
    at Gulp.emit (events.js:107:17)
    at Gulp.Orchestrator._emitTaskDone (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:264:8)
    at C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:275:23
    at finish (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:21:8)
    at cb (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:29:3)
    at Gulp.<anonymous> (C:\users\bfitzgerald\desktop\gulp-test\gulpfile.js:9:2)
    at module.exports (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:34:7)
    at Gulp.Orchestrator._runTask (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:273:3)

And here is the output for the promises option:

以下是promises选项的输出:

$ gulp option-2
[10:46:50] Using gulpfile C:\users\bfitzgerald\desktop\gulp-test\gulpfile.js
[10:46:50] Starting 'task-1-option-2'...
[10:46:50] task 1 running
[10:46:52] 'task-1-option-2' errored after 2.08 s
[10:46:52] Error: unit test failed, stop build
    at formatError (C:\Users\bfitzgerald\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:169:10)
    at Gulp.<anonymous> (C:\Users\bfitzgerald\AppData\Roaming\npm\node_modules\gulp\bin\gulp.js:195:15)
    at Gulp.emit (events.js:107:17)
    at Gulp.Orchestrator._emitTaskDone (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:264:8)
    at C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\index.js:275:23
    at finish (C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:21:8)
    at C:\users\bfitzgerald\desktop\gulp-test\node_modules\gulp\node_modules\orchestrator\lib\runTask.js:45:4
    at _rejected (C:\users\bfitzgerald\desktop\gulp-test\node_modules\q\q.js:804:24)
    at C:\users\bfitzgerald\desktop\gulp-test\node_modules\q\q.js:830:30
    at Promise.when (C:\users\bfitzgerald\desktop\gulp-test\node_modules\q\q.js:1064:31)

So the suggestions seem correct in that they stop the second task from running, but something still seems wrong since gulp is kicking up some kind of hard error. Can anyone shed some light on this for me?

所以这些建议似乎是正确的,因为它们阻止了第二个任务的运行,但是由于gulp正在引发某种硬错误,所以仍然有些错误。任何人都可以为我阐明这一点吗?

3 个解决方案

#1


3  

I'm not quite sure I understand what you are trying to accomplish with the your test url, but you can invoke the callback passed into your task with a non null to have it abort the task with a notification:

我不太确定我理解你要用你的测试网址完成什么,但是你可以用非null来调用传递给你的任务的回调,让它通过一个通知中止任务:

gulp.task("run-server-tests", function(callback) {
    var serverTestUrl = "http://myurl";

    request(serverTestUrl, function (error, response, body) {
    var responseData = JSON.parse(body);

    if (responseData.isSuccess) {
         // snip...
         callback();
    } else {
        open(serverTestUrl + "&render"); // show unit test failures
        callback('Unit test error');
    }
  });
});

gulp docs

#2


2  

A gulp task can return a promise to indicate that some async work is occurring.

gulp任务可以返回一个承诺,表明正在进行某些异步工作。

If you then reject the promise it will as abort further processing.

如果您拒绝承诺,它将中止进一步处理。

In your current code you are not telling gulp about the async work so it can't be canceled.

在您当前的代码中,您并没有告诉我们有关异步工作的信息,因此无法取消。

#3


2  

When invoking your callback or rejecting your deferred, instead of passing a string argument, pass a new Error('error msg'). This will stop your build and print a stack trace for the error you just created.

在调用您的回调或拒绝延迟时,而不是传递字符串参数,传递一个新的错误('错误消息')。这将停止构建并为刚刚创建的错误打印堆栈跟踪。

For handling errors from 3rd party plugins while piping in a task, I recommend you read this great article: http://www.artandlogic.com/blog/2014/05/error-handling-in-gulp/

为了在管理任务时处理来自第三方插件的错误,我建议您阅读这篇很棒的文章:http://www.artandlogic.com/blog/2014/05/error-handling-in-gulp/

Also, to get great colored log messages, try using the colors utility in gulp-utils. Something like plugins.util.log(plugins.util.colors.bgRed.white('unit test failed, stop build'); would print the message with a red background and white text. Much easier to spot in the terminal.

另外,要获得色彩鲜艳的日志消息,请尝试使用gulp-utils中的colors实用程序。像plugins.util.log这样的东西(plugins.util.colors.bgRed.white('单元测试失败,停止构建');会打印出红色背景和白色文本的消息。在终端中更容易发现。

#1


3  

I'm not quite sure I understand what you are trying to accomplish with the your test url, but you can invoke the callback passed into your task with a non null to have it abort the task with a notification:

我不太确定我理解你要用你的测试网址完成什么,但是你可以用非null来调用传递给你的任务的回调,让它通过一个通知中止任务:

gulp.task("run-server-tests", function(callback) {
    var serverTestUrl = "http://myurl";

    request(serverTestUrl, function (error, response, body) {
    var responseData = JSON.parse(body);

    if (responseData.isSuccess) {
         // snip...
         callback();
    } else {
        open(serverTestUrl + "&render"); // show unit test failures
        callback('Unit test error');
    }
  });
});

gulp docs

#2


2  

A gulp task can return a promise to indicate that some async work is occurring.

gulp任务可以返回一个承诺,表明正在进行某些异步工作。

If you then reject the promise it will as abort further processing.

如果您拒绝承诺,它将中止进一步处理。

In your current code you are not telling gulp about the async work so it can't be canceled.

在您当前的代码中,您并没有告诉我们有关异步工作的信息,因此无法取消。

#3


2  

When invoking your callback or rejecting your deferred, instead of passing a string argument, pass a new Error('error msg'). This will stop your build and print a stack trace for the error you just created.

在调用您的回调或拒绝延迟时,而不是传递字符串参数,传递一个新的错误('错误消息')。这将停止构建并为刚刚创建的错误打印堆栈跟踪。

For handling errors from 3rd party plugins while piping in a task, I recommend you read this great article: http://www.artandlogic.com/blog/2014/05/error-handling-in-gulp/

为了在管理任务时处理来自第三方插件的错误,我建议您阅读这篇很棒的文章:http://www.artandlogic.com/blog/2014/05/error-handling-in-gulp/

Also, to get great colored log messages, try using the colors utility in gulp-utils. Something like plugins.util.log(plugins.util.colors.bgRed.white('unit test failed, stop build'); would print the message with a red background and white text. Much easier to spot in the terminal.

另外,要获得色彩鲜艳的日志消息,请尝试使用gulp-utils中的colors实用程序。像plugins.util.log这样的东西(plugins.util.colors.bgRed.white('单元测试失败,停止构建');会打印出红色背景和白色文本的消息。在终端中更容易发现。