[英]Gulp postpone error occurring whilst running run-sequence to end of tasks list
我有一個Laravel應用程序的Gulpfile。 應用程序的單元測試通過Gulp執行。 為了正確執行測試,以下任務應以同步順序運行(通過運行順序)。
這很好用,但是,當PHPUnit執行並且一個或多個單元測試失敗時,會出現問題。 如果發生這種情況,則序列會中斷,結果不會發生切換回原始環境的情況,我們將陷入測試環境中。
這就是為什么我想將PHPUnit錯誤推遲到序列的末尾。 這樣,可以還原環境,但是出於CI的目的,構建仍將標記為失敗。
我自己嘗試了gulp-if和gulp-fail。 gulp-fail代碼已執行,但運行序列仍然繼續執行。 我不知道該如何解決。
這是我嘗試捕獲單元測試失敗的方法:
.on('error', function() {
didTestsFail = true;
this.emit('end');
})
通過此任務,我試圖將構建標記為失敗:
gulp.task('error-when-tests-failed', function() {
return gulp.src('./')
.pipe(gulpIf(didTestsFail, fail()));
});
因此,基本上,我正在使用全局變量來確定單元測試是否失敗。
這是完整的Gulpfile
:
var gulp = require('gulp'),
del = require('del'),
rename = require('gulp-rename'),
exec = require('gulp-exec'),
foreach = require('gulp-foreach'),
gulpSequence = require('gulp-sequence').use(gulp),
plumber = require('gulp-plumber'),
phpunit = require('gulp-phpunit'),
fail = require('gulp-fail'),
gulpIf = require('gulp-if'),
db = require('gulp-db')({
user: 'user',
password: 'some_password',
host: 'some_host',
port: 'some_port',
dialect: 'mysql'
}),
didTestsFail = false;
gulp.task('tests', function(cb) {
gulpSequence(
'back-up-active-env',
'switch-testing-env',
'create-test-database',
'migrate',
'phpunit',
'drop-test-database',
'restore-active-env',
'error-when-tests-failed'
)(cb);
});
gulp.task('phpunit', function(done) {
var options = {
debug: false,
statusLine: true
};
return gulp.src('phpunit.xml')
.pipe(plumber())
.pipe(phpunit('./vendor/bin/phpunit', options))
.on('error', function() {
didTestsFail = true;
this.emit('end');
});
});
gulp.task('back-up-active-env', function() {
del('env.temp');
return gulp.src('.env')
.pipe(plumber())
.pipe(rename('.env.temp'))
.pipe(gulp.dest('./'));
});
gulp.task('migrate', function() {
return gulp.src('./')
.pipe(plumber())
.pipe(exec('php artisan migrate'));
});
gulp.task('switch-testing-env', function() {
del('.env');
return gulp.src('.env.testing')
.pipe(plumber())
.pipe(rename('.env'))
.pipe(gulp.dest('./'))
.pipe(exec('php artisan config:cache'));
});
gulp.task('restore-active-env', function() {
del('.env');
return gulp.src('.env.temp')
.pipe(plumber())
.pipe(rename('.env'))
.pipe(gulp.dest('./'))
.pipe(exec('php artisan config:cache'));
});
gulp.task('error-when-tests-failed', function() {
return gulp.src('./')
.pipe(gulpIf(didTestsFail, fail()));
});
gulp.task('create-test-database', db.create('test_db'));;
gulp.task('drop-test-database', db.drop('test_db'));
經過一番擺弄之后,我想出了如何使其工作的方法:
error-when-tests-failed
無需執行error-when-tests-failed
,無論如何這都是很棘手的。
您可以將回調傳遞給gulpSequence函數,在此回調中,您可以檢查didTestsFail
var是否為true,如果這樣,則可能引發錯誤。 該代碼如下所示:
gulp.task('tests', function(done) {
gulpSequence(
'back-up-active-env',
'switch-testing-env',
'create-old-test-database',
'create-new-test-database',
'migrate',
'phpunit',
'drop-old-test-database',
'drop-new-test-database',
'restore-active-env',
function() {
if (didTestsFail) {
throw new Error('One or more unit tests failed!');
}
done();
}
);
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.