From f320f569ea36dfc9e03a30d5045aefe1015c1ef8 Mon Sep 17 00:00:00 2001 From: killagu Date: Mon, 19 Feb 2024 15:48:50 +0800 Subject: [PATCH] feat: support configure egg.revert in package.json BREAKING CHANGE: Refactor to AsyncFunction. --- .github/workflows/nodejs.yml | 4 +- .github/workflows/release.yml | 2 +- .gitignore | 1 + lib/cmd/start.js | 60 ++- lib/cmd/stop.js | 8 +- lib/helper.js | 4 +- package.json | 4 +- .../egg-revert/config/config.default.js | 8 + .../node_modules/custom-framework/index.js | 21 + .../custom-framework/package.json | 7 + test/fixtures/egg-revert/package.json | 11 + test/start.test.js | 407 ++++++++++-------- test/stop.test.js | 154 +++---- test/ts.test.js | 40 +- test/utils.js | 6 +- 15 files changed, 419 insertions(+), 318 deletions(-) create mode 100644 test/fixtures/egg-revert/config/config.default.js create mode 100644 test/fixtures/egg-revert/node_modules/custom-framework/index.js create mode 100644 test/fixtures/egg-revert/node_modules/custom-framework/package.json create mode 100644 test/fixtures/egg-revert/package.json diff --git a/.github/workflows/nodejs.yml b/.github/workflows/nodejs.yml index 5c216b8..648a735 100644 --- a/.github/workflows/nodejs.yml +++ b/.github/workflows/nodejs.yml @@ -12,7 +12,7 @@ on: jobs: Job: name: Node.js - uses: artusjs/github-actions/.github/workflows/node-test.yml@v1 + uses: node-modules/github-actions/.github/workflows/node-test.yml@master with: os: 'ubuntu-latest, macos-latest, windows-latest' - version: '14, 16, 18' + version: '16, 18, 20' diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 1612587..58f7b94 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -9,7 +9,7 @@ on: jobs: release: name: Node.js - uses: artusjs/github-actions/.github/workflows/node-release.yml@v1 + uses: eggjs/github-actions/.github/workflows/node-release.yml@master secrets: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} GIT_TOKEN: ${{ secrets.GIT_TOKEN }} diff --git a/.gitignore b/.gitignore index 0c5a36f..03934de 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ run/ test/fixtures/ts/app/controller/home.js test/fixtures/ts-pkg/app/controller/home.js !test/fixtures/**/node_modules +package-lock.json diff --git a/lib/cmd/start.js b/lib/cmd/start.js index add1e2a..57d2fa2 100644 --- a/lib/cmd/start.js +++ b/lib/cmd/start.js @@ -76,7 +76,7 @@ class StartCommand extends Command { return 'Start server at prod mode'; } - * run(context) { + async run(context) { context.execArgvObj = context.execArgvObj || {}; const { argv, env, cwd, execArgvObj } = context; const HOME = homedir(); @@ -91,12 +91,12 @@ class StartCommand extends Command { const isDaemon = argv.daemon; - argv.framework = yield this.getFrameworkPath({ + argv.framework = await this.getFrameworkPath({ framework: argv.framework, baseDir, }); - this.frameworkName = yield this.getFrameworkName(argv.framework); + this.frameworkName = await this.getFrameworkName(argv.framework); const pkgInfo = require(path.join(baseDir, 'package.json')); argv.title = argv.title || `egg-server-${pkgInfo.name}`; @@ -121,7 +121,7 @@ class StartCommand extends Command { // for alinode env.ENABLE_NODE_LOG = 'YES'; env.NODE_LOG_DIR = env.NODE_LOG_DIR || path.join(logDir, 'alinode'); - yield mkdirp(env.NODE_LOG_DIR); + await mkdirp(env.NODE_LOG_DIR); // cli argv -> process.env.EGG_SERVER_ENV -> `undefined` then egg will use `prod` if (argv.env) { @@ -132,6 +132,14 @@ class StartCommand extends Command { // additional execArgv execArgvObj.deprecation = false; // --no-deprecation execArgvObj.traceWarnings = true; // --trace-warnings + const eggInfo = pkgInfo.egg || {}; + if (eggInfo.revert) { + context.execArgvObj['security-revert'] = context.execArgvObj['security-revert'] || []; + const reverts = Array.isArray(eggInfo.revert) ? eggInfo.revert : [ eggInfo.revert ]; + for (const revert of reverts) { + context.execArgvObj['security-revert'].push(revert); + } + } const command = argv.node || 'node'; @@ -154,7 +162,10 @@ class StartCommand extends Command { // whether run in the background. if (isDaemon) { this.logger.info(`Save log file to ${logDir}`); - const [ stdout, stderr ] = yield [ getRotatelog(argv.stdout), getRotatelog(argv.stderr) ]; + const [ stdout, stderr ] = await Promise.all([ + getRotatelog(argv.stdout), + getRotatelog(argv.stderr), + ]); options.stdio = [ 'ignore', stdout, stderr, 'ipc' ]; options.detached = true; @@ -173,7 +184,7 @@ class StartCommand extends Command { }); // check start status - yield this.checkStatus(argv); + await this.checkStatus(argv); } else { options.stdio = [ 'inherit', 'inherit', 'inherit', 'ipc' ]; debug('Run spawn `%s %s`', command, eggArgs.join(' ')); @@ -194,11 +205,24 @@ class StartCommand extends Command { } } - * getFrameworkPath(params) { + async getFrameworkPath(params) { return utils.getFrameworkPath(params); } - * getFrameworkName(framework) { + async getFrameworkName(framework) { + const pkgPath = path.join(framework, 'package.json'); + let name = 'egg'; + try { + const pkg = require(pkgPath); + /* istanbul ignore else */ + if (pkg.name) name = pkg.name; + } catch (_) { + /* istanbul next */ + } + return name; + } + + async getRevert(framework) { const pkgPath = path.join(framework, 'package.json'); let name = 'egg'; try { @@ -211,14 +235,14 @@ class StartCommand extends Command { return name; } - * checkStatus({ stderr, timeout, 'ignore-stderr': ignoreStdErr }) { + async checkStatus({ stderr, timeout, 'ignore-stderr': ignoreStdErr }) { let count = 0; let hasError = false; let isSuccess = true; timeout = timeout / 1000; while (!this.isReady) { try { - const stat = yield fs.stat(stderr); + const stat = await fs.stat(stderr); if (stat && stat.size > 0) { hasError = true; break; @@ -233,7 +257,7 @@ class StartCommand extends Command { break; } - yield sleep(1000); + await sleep(1000); this.logger.log('Wait Start: %d...', ++count); } @@ -241,7 +265,7 @@ class StartCommand extends Command { try { const args = [ '-n', '100', stderr ]; this.logger.error('tail %s', args.join(' ')); - const [ stdout ] = yield execFile('tail', args); + const [ stdout ] = await execFile('tail', args); this.logger.error('Got error when startup: '); this.logger.error(stdout); } catch (err) { @@ -254,23 +278,23 @@ class StartCommand extends Command { if (!isSuccess) { this.child.kill('SIGTERM'); - yield sleep(1000); + await sleep(1000); this.exit(1); } } } -function* getRotatelog(logfile) { - yield mkdirp(path.dirname(logfile)); +async function getRotatelog(logfile) { + await mkdirp(path.dirname(logfile)); - if (yield fs.exists(logfile)) { + if (await fs.exists(logfile)) { // format style: .20150602.193100 const timestamp = moment().format('.YYYYMMDD.HHmmss'); // Note: rename last log to next start time, not when last log file created - yield fs.rename(logfile, logfile + timestamp); + await fs.rename(logfile, logfile + timestamp); } - return yield fs.open(logfile, 'a'); + return await fs.open(logfile, 'a'); } function stringify(obj, ignore) { diff --git a/lib/cmd/stop.js b/lib/cmd/stop.js index a15fc0e..40d4607 100644 --- a/lib/cmd/stop.js +++ b/lib/cmd/stop.js @@ -29,13 +29,13 @@ class StopCommand extends Command { return 'Stop server'; } - * run(context) { + async run(context) { const { argv } = context; this.logger.info(`stopping egg application ${argv.title ? `with --title=${argv.title}` : ''}`); // node /Users/tz/Workspaces/eggjs/egg-scripts/lib/start-cluster {"title":"egg-server","workers":4,"port":7001,"baseDir":"/Users/tz/Workspaces/eggjs/test/showcase","framework":"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/egg"} - let processList = yield this.helper.findNodeProcess(item => { + let processList = await this.helper.findNodeProcess(item => { const cmd = item.cmd; return argv.title ? cmd.includes('start-cluster') && cmd.includes(util.format(osRelated.titleTemplate, argv.title)) : @@ -47,7 +47,7 @@ class StopCommand extends Command { this.logger.info('got master pid %j', pids); this.helper.kill(pids); // wait for 5s to confirm whether any worker process did not kill by master - yield sleep(argv.timeout || '5s'); + await sleep(argv.timeout || '5s'); } else { this.logger.warn('can\'t detect any running egg process'); } @@ -55,7 +55,7 @@ class StopCommand extends Command { // node --debug-port=5856 /Users/tz/Workspaces/eggjs/test/showcase/node_modules/_egg-cluster@1.8.0@egg-cluster/lib/agent_worker.js {"framework":"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/egg","baseDir":"/Users/tz/Workspaces/eggjs/test/showcase","port":7001,"workers":2,"plugins":null,"https":false,"key":"","cert":"","title":"egg-server","clusterPort":52406} // node /Users/tz/Workspaces/eggjs/test/showcase/node_modules/_egg-cluster@1.8.0@egg-cluster/lib/app_worker.js {"framework":"/Users/tz/Workspaces/eggjs/test/showcase/node_modules/egg","baseDir":"/Users/tz/Workspaces/eggjs/test/showcase","port":7001,"workers":2,"plugins":null,"https":false,"key":"","cert":"","title":"egg-server","clusterPort":52406} - processList = yield this.helper.findNodeProcess(item => { + processList = await this.helper.findNodeProcess(item => { const cmd = item.cmd; return argv.title ? (cmd.includes(osRelated.appWorkerPath) || cmd.includes(osRelated.agentWorkerPath)) && cmd.includes(util.format(osRelated.titleTemplate, argv.title)) : diff --git a/lib/helper.js b/lib/helper.js index d9de722..fbbbc03 100644 --- a/lib/helper.js +++ b/lib/helper.js @@ -4,12 +4,12 @@ const runScript = require('runscript'); const isWin = process.platform === 'win32'; const REGEX = isWin ? /^(.*)\s+(\d+)\s*$/ : /^\s*(\d+)\s+(.*)/; -exports.findNodeProcess = function* (filterFn) { +exports.findNodeProcess = async function(filterFn) { const command = isWin ? 'wmic Path win32_process Where "Name = \'node.exe\'" Get CommandLine,ProcessId' : // command, cmd are alias of args, not POSIX standard, so we use args 'ps -wweo "pid,args"'; - const stdio = yield runScript(command, { stdio: 'pipe' }); + const stdio = await runScript(command, { stdio: 'pipe' }); const processList = stdio.stdout.toString().split('\n') .reduce((arr, line) => { if (!!line && !line.includes('/bin/sh') && line.includes('node')) { diff --git a/package.json b/package.json index b9dcbd7..dcb79cb 100644 --- a/package.json +++ b/package.json @@ -9,7 +9,7 @@ }, "dependencies": { "await-event": "^2.1.0", - "common-bin": "^2.8.0", + "common-bin": "^3.0.1", "debug": "^4.1.0", "egg-utils": "^2.4.1", "moment": "^2.23.0", @@ -36,7 +36,7 @@ "node": ">=6.0.0" }, "ci": { - "version": "14, 16, 18" + "version": "16, 18, 20" }, "scripts": { "contributor": "git-contributor", diff --git a/test/fixtures/egg-revert/config/config.default.js b/test/fixtures/egg-revert/config/config.default.js new file mode 100644 index 0000000..98de4f0 --- /dev/null +++ b/test/fixtures/egg-revert/config/config.default.js @@ -0,0 +1,8 @@ +'use strict'; + +exports.keys = '123456'; + +exports.logger = { + level: 'WARN', + consoleLevel: 'WARN', +}; diff --git a/test/fixtures/egg-revert/node_modules/custom-framework/index.js b/test/fixtures/egg-revert/node_modules/custom-framework/index.js new file mode 100644 index 0000000..071acea --- /dev/null +++ b/test/fixtures/egg-revert/node_modules/custom-framework/index.js @@ -0,0 +1,21 @@ +'use strict'; + +const egg = require('../../../../../node_modules/egg'); + +const originStartCluster = egg.startCluster; + +module.exports = Object.assign(egg, { + Application: class CustomApplication extends egg.Application { + get [Symbol.for('egg#eggPath')]() { + return __dirname; + } + }, + startCluster(...args) { + if (process.env.CUSTOM_ENV && !process.env.EGG_SERVER_ENV) { + console.log('## EGG_SERVER_ENV is not pass'); + console.log('## CUSTOM_ENV:', process.env.CUSTOM_ENV); + process.env.EGG_SERVER_ENV = process.env.CUSTOM_ENV; + } + return originStartCluster(...args); + }, +}); diff --git a/test/fixtures/egg-revert/node_modules/custom-framework/package.json b/test/fixtures/egg-revert/node_modules/custom-framework/package.json new file mode 100644 index 0000000..a9328f7 --- /dev/null +++ b/test/fixtures/egg-revert/node_modules/custom-framework/package.json @@ -0,0 +1,7 @@ +{ + "name": "custom-framework", + "version": "1.0.0", + "dependencies": { + "egg": "*" + } +} \ No newline at end of file diff --git a/test/fixtures/egg-revert/package.json b/test/fixtures/egg-revert/package.json new file mode 100644 index 0000000..71801ee --- /dev/null +++ b/test/fixtures/egg-revert/package.json @@ -0,0 +1,11 @@ +{ + "name": "example", + "version": "1.0.0", + "dependencies": { + "egg": "^1.0.0" + }, + "egg": { + "framework": "custom-framework", + "revert": "CVE-2023-46809" + } +} diff --git a/test/start.test.js b/test/start.test.js index e0aa759..1989315 100644 --- a/test/start.test.js +++ b/test/start.test.js @@ -12,6 +12,7 @@ const mm = require('mm'); const utils = require('./utils'); const awaitEvent = require('await-event'); const isWin = process.platform === 'win32'; +const version = Number(process.version.substring(1, 3)); describe('test/start.test.js', () => { const eggBin = require.resolve('../bin/egg-scripts.js'); @@ -20,11 +21,11 @@ describe('test/start.test.js', () => { const logDir = path.join(homePath, 'logs'); const waitTime = '10s'; - before(function* () { - yield mkdirp(homePath); + before(async () => { + await mkdirp(homePath); }); - after(function* () { - yield rimraf(homePath); + after(async () => { + await rimraf(homePath); }); beforeEach(() => mm(process.env, 'MOCK_HOME_DIR', homePath)); afterEach(mm.restore); @@ -34,22 +35,22 @@ describe('test/start.test.js', () => { let app; let fixturePath; - before(function* () { + before(async () => { fixturePath = path.join(__dirname, 'fixtures/pkg-config'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should --require', function* () { + it('should --require', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=1', '--require=./inject2.js' ], { cwd: fixturePath }); app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/@@@ inject script/)); @@ -57,13 +58,13 @@ describe('test/start.test.js', () => { assert(app.stdout.match(/@@@ inject script2/)); }); - it('inject incorrect script', function* () { + it('inject incorrect script', async () => { const script = './inject3.js'; app = coffee.fork(eggBin, [ 'start', '--workers=1', `--require=${script}` ], { cwd: fixturePath }); app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr.includes(`Cannot find module '${path.join(fixturePath, script)}'`)); }); @@ -73,22 +74,22 @@ describe('test/start.test.js', () => { let app; let fixturePath; - before(function* () { + before(async () => { fixturePath = path.join(__dirname, 'fixtures/pkg-config-sourcemap'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should not enable sourcemap-support', function* () { + it('should not enable sourcemap-support', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=1' ], { cwd: fixturePath }); app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(!/--require .*\/node_modules\/.*source-map-support/.test(app.stdout)); }); }); @@ -96,21 +97,21 @@ describe('test/start.test.js', () => { describe('full path', () => { let app; - before(function* () { - yield utils.cleanup(fixturePath); + before(async () => { + await utils.cleanup(fixturePath); }); - afterEach(function* () { + afterEach(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=2', fixturePath ]); app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(!app.stdout.includes('DeprecationWarning:')); @@ -119,23 +120,23 @@ describe('test/start.test.js', () => { assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); assert(app.stdout.includes('app_worker#2:')); assert(!app.stdout.includes('app_worker#3:')); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, egg'); }); - it('should start --trace-warnings work', function* () { + it('should start --trace-warnings work', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=1', path.join(__dirname, 'fixtures/trace-warnings') ]); app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr.includes('MaxListenersExceededWarning:')); assert(app.stderr.includes('app.js:10:9')); // should had trace assert(!app.stdout.includes('DeprecationWarning:')); }); - it.skip('should get ready', function* () { + it.skip('should get ready', async () => { app = coffee.fork(path.join(__dirname, './fixtures/ipc-bin/start.js'), [], { env: { BASE_DIR: fixturePath, @@ -145,7 +146,7 @@ describe('test/start.test.js', () => { app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.includes('READY!!!')); @@ -160,22 +161,22 @@ describe('test/start.test.js', () => { describe('child exit with 1', () => { let app; - before(function* () { - yield utils.cleanup(fixturePath); + before(async () => { + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should emit spawn error', function* () { + it('should emit spawn error', async () => { const srv = require('http').createServer(() => {}); srv.listen(7007); app = coffee.fork(eggBin, [ 'start', '--port=7007', '--workers=2', fixturePath ]); - yield sleep(waitTime); + await sleep(waitTime); srv.close(); assert(app.code === 1); }); @@ -184,25 +185,25 @@ describe('test/start.test.js', () => { describe('relative path', () => { let app; - before(function* () { - yield utils.cleanup(fixturePath); + before(async () => { + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=2', path.relative(process.cwd(), fixturePath) ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, egg'); }); }); @@ -210,25 +211,25 @@ describe('test/start.test.js', () => { describe('without baseDir', () => { let app; - before(function* () { - yield utils.cleanup(fixturePath); + before(async () => { + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=2' ], { cwd: fixturePath }); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, egg'); }); }); @@ -236,25 +237,25 @@ describe('test/start.test.js', () => { describe('--framework', () => { let app; - before(function* () { - yield utils.cleanup(fixturePath); + before(async () => { + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--framework=yadan', '--workers=2', fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/yadan started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, yadan'); }); }); @@ -262,21 +263,21 @@ describe('test/start.test.js', () => { describe('--title', () => { let app; - before(function* () { - yield utils.cleanup(fixturePath); + before(async () => { + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=egg-test', fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.includes('--title=egg-test')); @@ -284,7 +285,7 @@ describe('test/start.test.js', () => { assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); assert(app.stdout.includes('app_worker#2:')); assert(!app.stdout.includes('app_worker#3:')); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, egg'); }); }); @@ -292,25 +293,25 @@ describe('test/start.test.js', () => { describe('--port', () => { let app; - before(function* () { - yield utils.cleanup(fixturePath); + before(async () => { + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--port=7002', '--workers=2', fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7002/)); - const result = yield httpclient.request('http://127.0.0.1:7002'); + const result = await httpclient.request('http://127.0.0.1:7002'); assert(result.data.toString() === 'hi, egg'); }); }); @@ -318,25 +319,25 @@ describe('test/start.test.js', () => { describe('process.env.PORT', () => { let app; - before(function* () { - yield utils.cleanup(fixturePath); + before(async () => { + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=2', fixturePath ], { env: Object.assign({}, process.env, { PORT: 7002 }) }); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7002/)); - const result = yield httpclient.request('http://127.0.0.1:7002'); + const result = await httpclient.request('http://127.0.0.1:7002'); assert(result.data.toString() === 'hi, egg'); }); }); @@ -344,25 +345,25 @@ describe('test/start.test.js', () => { describe('--env', () => { let app; - before(function* () { - yield utils.cleanup(fixturePath); + before(async () => { + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=2', '--env=pre', fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001/env'); + const result = await httpclient.request('http://127.0.0.1:7001/env'); assert(result.data.toString() === 'pre, true'); }); }); @@ -370,30 +371,30 @@ describe('test/start.test.js', () => { describe('custom env', () => { let app; - before(function* () { - yield utils.cleanup(fixturePath); + before(async () => { + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should start', function* () { + it('should start', async () => { mm(process.env, 'CUSTOM_ENV', 'pre'); app = coffee.fork(eggBin, [ 'start', '--workers=2', fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.includes('## EGG_SERVER_ENV is not pass')); assert(app.stdout.includes('## CUSTOM_ENV: pre')); assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); - let result = yield httpclient.request('http://127.0.0.1:7001/env'); + let result = await httpclient.request('http://127.0.0.1:7001/env'); assert(result.data.toString() === 'pre, true'); - result = yield httpclient.request('http://127.0.0.1:7001/path'); + result = await httpclient.request('http://127.0.0.1:7001/path'); const appBinPath = path.join(fixturePath, 'node_modules/.bin'); assert(result.data.toString().startsWith(`${appBinPath}${path.delimiter}`)); }); @@ -402,38 +403,38 @@ describe('test/start.test.js', () => { describe('--stdout --stderr', () => { let app; - before(function* () { - yield utils.cleanup(fixturePath); - yield rimraf(logDir); - yield rimraf(path.join(fixturePath, 'start-fail')); - yield mkdirp(logDir); + before(async () => { + await utils.cleanup(fixturePath); + await rimraf(logDir); + await rimraf(path.join(fixturePath, 'start-fail')); + await mkdirp(logDir); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); - yield rimraf(path.join(fixturePath, 'stdout.log')); - yield rimraf(path.join(fixturePath, 'stderr.log')); - yield rimraf(path.join(fixturePath, 'start-fail')); + await utils.cleanup(fixturePath); + await rimraf(path.join(fixturePath, 'stdout.log')); + await rimraf(path.join(fixturePath, 'stderr.log')); + await rimraf(path.join(fixturePath, 'start-fail')); }); - it('should start', function* () { + it('should start', async () => { const stdout = path.join(fixturePath, 'stdout.log'); const stderr = path.join(fixturePath, 'stderr.log'); app = coffee.fork(eggBin, [ 'start', '--workers=1', '--daemon', `--stdout=${stdout}`, `--stderr=${stderr}`, fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); - let content = yield fs.readFile(stdout, 'utf-8'); + let content = await fs.readFile(stdout, 'utf-8'); assert(content.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); - content = yield fs.readFile(stderr, 'utf-8'); + content = await fs.readFile(stderr, 'utf-8'); assert(content === ''); }); - it('should start with insecurity --stderr argument', function* () { + it('should start with insecurity --stderr argument', async () => { const cwd = path.join(__dirname, 'fixtures/status'); mm(process.env, 'ERROR', 'error message'); @@ -447,13 +448,13 @@ describe('test/start.test.js', () => { ]); // app.debug(); - yield sleep(waitTime); + await sleep(waitTime); - const content = yield fs.readFile(stdout, 'utf-8'); + const content = await fs.readFile(stdout, 'utf-8'); assert(!content.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); - let exists = yield fs.exists(stderr); + let exists = await fs.exists(stderr); assert(!exists); - exists = yield fs.exists(malicious); + exists = await fs.exists(malicious); assert(!exists); }); }); @@ -461,59 +462,59 @@ describe('test/start.test.js', () => { describe('--node', () => { let app; - beforeEach(function* () { - yield utils.cleanup(fixturePath); + beforeEach(async () => { + await utils.cleanup(fixturePath); }); - beforeEach(function* () { + beforeEach(async () => { app && app.proc && app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); describe('daemon', () => { - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--daemon', '--framework=yadan', '--workers=2', `--node=${process.execPath}`, fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/yadan started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, yadan'); }); - it('should error if node path invalid', function* () { + it('should error if node path invalid', async () => { app = coffee.fork(eggBin, [ 'start', '--daemon', '--framework=yadan', '--workers=2', '--node=invalid', fixturePath ]); // app.debug(); app.expect('code', 1); - yield sleep(3000); + await sleep(3000); assert(app.stderr.includes('spawn invalid ENOENT')); }); }); describe('not daemon', () => { - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--framework=yadan', '--workers=2', `--node=${process.execPath}`, fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/yadan started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, yadan'); }); - it('should error if node path invalid', function* () { + it('should error if node path invalid', async () => { app = coffee.fork(eggBin, [ 'start', '--framework=yadan', '--workers=2', '--node=invalid', fixturePath ]); // app.debug(); app.expect('code', 1); - yield sleep(3000); + await sleep(3000); assert(app.stderr.includes('spawn invalid ENOENT')); }); }); @@ -523,27 +524,27 @@ describe('test/start.test.js', () => { let app; let fixturePath; - before(function* () { + before(async () => { fixturePath = path.join(__dirname, 'fixtures/cluster-config'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=2', fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/egg started on http:\/\/127\.0\.0\.1:8000/)); assert(!app.stdout.includes('app_worker#3:')); - const result = yield httpclient.request('http://127.0.0.1:8000'); + const result = await httpclient.request('http://127.0.0.1:8000'); assert(result.data.toString() === 'hi, egg'); }); }); @@ -552,52 +553,80 @@ describe('test/start.test.js', () => { let app; let fixturePath; - before(function* () { + before(async () => { fixturePath = path.join(__dirname, 'fixtures/egg-scripts-node-options'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=1', fixturePath ]); app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/maxHeaderSize: 20000/)); }); }); + describe('read egg.revert', () => { + if (version < 18) return; + if (isWin) return; + let app; + let fixturePath; + + before(async () => { + fixturePath = path.join(__dirname, 'fixtures/egg-revert'); + await utils.cleanup(fixturePath); + }); + + after(async () => { + app.proc.kill('SIGTERM'); + await utils.cleanup(fixturePath); + }); + + it('should start', async () => { + app = coffee.fork(eggBin, [ 'start', '--workers=1', fixturePath ]); + app.debug(); + app.expect('code', 0); + + await sleep(waitTime); + + assert(app.stderr === ''); + assert(app.stdout.match(/SECURITY WARNING: Reverting CVE-2023-46809: Marvin attack on PKCS#1 padding/)); + }); + }); + describe('subDir as baseDir', () => { let app; const rootDir = path.join(__dirname, '..'); const subDir = path.join(__dirname, 'fixtures/subdir-as-basedir/base-dir'); - before(function* () { - yield utils.cleanup(rootDir); + before(async () => { + await utils.cleanup(rootDir); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(rootDir); + await utils.cleanup(rootDir); }); - it('should start', function* () { + it('should start', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=2', subDir ], { cwd: rootDir }); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/egg started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, egg'); }); }); @@ -606,17 +635,17 @@ describe('test/start.test.js', () => { let app; let fixturePath; - before(function* () { + before(async () => { fixturePath = path.join(__dirname, 'fixtures/custom-node-dir'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - after(function* () { + after(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should start', function* () { + it('should start', async () => { const expectPATH = [ path.join(fixturePath, 'node_modules/.bin'), path.join(fixturePath, '.node/bin'), @@ -625,12 +654,12 @@ describe('test/start.test.js', () => { // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/egg started on http:\/\/127\.0\.0\.1:7002/)); assert(!app.stdout.includes('app_worker#3:')); - const result = yield httpclient.request('http://127.0.0.1:7002'); + const result = await httpclient.request('http://127.0.0.1:7002'); assert(result.data.toString().startsWith(`hi, ${expectPATH}`)); }); }); @@ -638,20 +667,20 @@ describe('test/start.test.js', () => { describe('kill command', () => { let app; - before(function* () { - yield utils.cleanup(fixturePath); + before(async () => { + await utils.cleanup(fixturePath); }); - after(function* () { - yield utils.cleanup(fixturePath); + after(async () => { + await utils.cleanup(fixturePath); }); - it('should wait child process exit', function* () { + it('should wait child process exit', async () => { app = coffee.fork(eggBin, [ 'start', '--port=7007', '--workers=2', fixturePath ]); - yield sleep(waitTime); + await sleep(waitTime); const exitEvent = awaitEvent(app.proc, 'exit'); app.proc.kill('SIGTERM'); - const code = yield exitEvent; + const code = await exitEvent; if (isWin) { assert(code === null); } else { @@ -663,23 +692,23 @@ describe('test/start.test.js', () => { describe('start with daemon', () => { let cwd; - beforeEach(function* () { - if (cwd) yield utils.cleanup(cwd); - yield rimraf(logDir); - yield mkdirp(logDir); - yield fs.writeFile(path.join(logDir, 'master-stdout.log'), 'just for test'); - yield fs.writeFile(path.join(logDir, 'master-stderr.log'), 'just for test'); + beforeEach(async () => { + if (cwd) await utils.cleanup(cwd); + await rimraf(logDir); + await mkdirp(logDir); + await fs.writeFile(path.join(logDir, 'master-stdout.log'), 'just for test'); + await fs.writeFile(path.join(logDir, 'master-stderr.log'), 'just for test'); }); - afterEach(function* () { - yield coffee.fork(eggBin, [ 'stop', cwd ]) + afterEach(async () => { + await coffee.fork(eggBin, [ 'stop', cwd ]) // .debug() .end(); - yield utils.cleanup(cwd); + await utils.cleanup(cwd); }); - it('should start custom-framework', function* () { + it('should start custom-framework', async () => { cwd = fixturePath; - yield coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2', '--port=7002', cwd ]) + await coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2', '--port=7002', cwd ]) // .debug() .expect('stdout', /Starting custom-framework application/) .expect('stdout', /custom-framework started on http:\/\/127\.0\.0\.1:7002/) @@ -687,23 +716,23 @@ describe('test/start.test.js', () => { .end(); // master log - const stdout = yield fs.readFile(path.join(logDir, 'master-stdout.log'), 'utf-8'); - const stderr = yield fs.readFile(path.join(logDir, 'master-stderr.log'), 'utf-8'); + const stdout = await fs.readFile(path.join(logDir, 'master-stdout.log'), 'utf-8'); + const stderr = await fs.readFile(path.join(logDir, 'master-stderr.log'), 'utf-8'); assert(stderr === ''); assert(stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7002/)); // should rotate log - const fileList = yield fs.readdir(logDir); + const fileList = await fs.readdir(logDir); assert(fileList.some(name => name.match(/master-stdout\.log\.\d+\.\d+/))); assert(fileList.some(name => name.match(/master-stderr\.log\.\d+\.\d+/))); - const result = yield httpclient.request('http://127.0.0.1:7002'); + const result = await httpclient.request('http://127.0.0.1:7002'); assert(result.data.toString() === 'hi, egg'); }); - it('should start default egg', function* () { + it('should start default egg', async () => { cwd = path.join(__dirname, 'fixtures/egg-app'); - yield coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2', cwd ]) + await coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2', cwd ]) // .debug() .expect('stdout', /Starting egg application/) .expect('stdout', /egg started on http:\/\/127\.0\.0\.1:7001/) @@ -718,16 +747,16 @@ describe('test/start.test.js', () => { cwd = path.join(__dirname, 'fixtures/status'); }); - after(function* () { - yield coffee.fork(eggBin, [ 'stop', cwd ]) + after(async () => { + await coffee.fork(eggBin, [ 'stop', cwd ]) // .debug() .end(); - yield utils.cleanup(cwd); + await utils.cleanup(cwd); }); - it('should status check success, exit with 0', function* () { + it('should status check success, exit with 0', async () => { mm(process.env, 'WAIT_TIME', 5000); - yield coffee.fork(eggBin, [ 'start', '--daemon', '--workers=1' ], { cwd }) + await coffee.fork(eggBin, [ 'start', '--daemon', '--workers=1' ], { cwd }) // .debug() .expect('stdout', /Wait Start: 5.../) .expect('stdout', /custom-framework started/) @@ -735,7 +764,7 @@ describe('test/start.test.js', () => { .end(); }); - it('should status check fail `--ignore-stderr`, exit with 0', function* () { + it('should status check fail `--ignore-stderr`, exit with 0', async () => { mm(process.env, 'WAIT_TIME', 5000); mm(process.env, 'ERROR', 'error message'); @@ -746,12 +775,12 @@ describe('test/start.test.js', () => { // app.debug(); // TODO: find a windows replacement for tail command if (!isWin) app.expect('stderr', /nodejs.Error: error message/); - yield app.expect('stderr', new RegExp(`Start got error, see ${stderr}`)) + await app.expect('stderr', new RegExp(`Start got error, see ${stderr}`)) .expect('code', 0) .end(); }); - it('should status check fail `--ignore-stderr` in package.json, exit with 0', function* () { + it('should status check fail `--ignore-stderr` in package.json, exit with 0', async () => { cwd = path.join(__dirname, 'fixtures/egg-scripts-config'); mm(process.env, 'WAIT_TIME', 5000); mm(process.env, 'ERROR', 'error message'); @@ -763,12 +792,12 @@ describe('test/start.test.js', () => { // app.debug(); // TODO: find a windows replacement for tail command if (!isWin) app.expect('stderr', /nodejs.Error: error message/); - yield app.expect('stderr', new RegExp(`Start got error, see ${stderr}`)) + await app.expect('stderr', new RegExp(`Start got error, see ${stderr}`)) .expect('code', 0) .end(); }); - it('should status check fail, exit with 1', function* () { + it('should status check fail, exit with 1', async () => { mm(process.env, 'WAIT_TIME', 5000); mm(process.env, 'ERROR', 'error message'); @@ -779,16 +808,16 @@ describe('test/start.test.js', () => { // app.debug(); // TODO: find a windows replacement for tail command if (!isWin) app.expect('stderr', /nodejs.Error: error message/); - yield app.expect('stderr', new RegExp(`Start got error, see ${stderr}`)) + await app.expect('stderr', new RegExp(`Start got error, see ${stderr}`)) .expect('stderr', /Got error when startup/) .expect('code', 1) .end(); }); - it('should status check timeout and exit with code 1', function* () { + it('should status check timeout and exit with code 1', async () => { mm(process.env, 'WAIT_TIME', 10000); - yield coffee.fork(eggBin, [ 'start', '--daemon', '--workers=1', '--timeout=5000' ], { cwd }) + await coffee.fork(eggBin, [ 'start', '--daemon', '--workers=1', '--timeout=5000' ], { cwd }) // .debug() .expect('stdout', /Wait Start: 1.../) .expect('stderr', /Start failed, 5s timeout/) diff --git a/test/stop.test.js b/test/stop.test.js index f15560c..dc72121 100644 --- a/test/stop.test.js +++ b/test/stop.test.js @@ -20,11 +20,11 @@ describe('test/stop.test.js', () => { const logDir = path.join(homePath, 'logs'); const waitTime = '15s'; - before(function* () { - yield mkdirp(homePath); + before(async () => { + await mkdirp(homePath); }); - after(function* () { - yield rimraf(homePath); + after(async () => { + await rimraf(homePath); }); beforeEach(() => mm(process.env, 'MOCK_HOME_DIR', homePath)); afterEach(() => mm.restore); @@ -33,31 +33,31 @@ describe('test/stop.test.js', () => { let app; let killer; - beforeEach(function* () { - yield utils.cleanup(fixturePath); + beforeEach(async () => { + await utils.cleanup(fixturePath); app = coffee.fork(eggBin, [ 'start', '--workers=2', fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, egg'); }); - afterEach(function* () { + afterEach(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should stop', function* () { + it('should stop', async () => { killer = coffee.fork(eggBin, [ 'stop', fixturePath ]); killer.debug(); killer.expect('code', 0); - // yield killer.end(); - yield sleep(waitTime); + // await killer.end(); + await sleep(waitTime); // make sure is kill not auto exist assert(!app.stdout.includes('exist by env')); @@ -76,33 +76,33 @@ describe('test/stop.test.js', () => { }); describe('stop with daemon', () => { - beforeEach(function* () { - yield utils.cleanup(fixturePath); - yield rimraf(logDir); - yield coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2', fixturePath ]) + beforeEach(async () => { + await utils.cleanup(fixturePath); + await rimraf(logDir); + await coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2', fixturePath ]) .debug() .expect('code', 0) .end(); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, egg'); }); - afterEach(function* () { - yield utils.cleanup(fixturePath); + afterEach(async () => { + await utils.cleanup(fixturePath); }); - it('should stop', function* () { - yield coffee.fork(eggBin, [ 'stop', fixturePath ]) + it('should stop', async () => { + await coffee.fork(eggBin, [ 'stop', fixturePath ]) .debug() .expect('stdout', /\[egg-scripts] stopping egg application/) .expect('stdout', /got master pid \["\d+\"\]/i) .expect('code', 0) .end(); - yield sleep(waitTime); + await sleep(waitTime); // master log - const stdout = yield fs.readFile(path.join(logDir, 'master-stdout.log'), 'utf-8'); + const stdout = await fs.readFile(path.join(logDir, 'master-stdout.log'), 'utf-8'); // no way to handle the SIGTERM signal in windows ? if (!isWin) { @@ -111,7 +111,7 @@ describe('test/stop.test.js', () => { assert(stdout.includes('[app_worker] exit with code:0')); } - yield coffee.fork(eggBin, [ 'stop', fixturePath ]) + await coffee.fork(eggBin, [ 'stop', fixturePath ]) .debug() .expect('stderr', /can't detect any running egg process/) .expect('code', 0) @@ -120,9 +120,9 @@ describe('test/stop.test.js', () => { }); describe('stop with not exist', () => { - it('should work', function* () { - yield utils.cleanup(fixturePath); - yield coffee.fork(eggBin, [ 'stop', fixturePath ]) + it('should work', async () => { + await utils.cleanup(fixturePath); + await coffee.fork(eggBin, [ 'stop', fixturePath ]) .debug() .expect('stdout', /\[egg-scripts] stopping egg application/) .expect('stderr', /can't detect any running egg process/) @@ -135,27 +135,27 @@ describe('test/stop.test.js', () => { let app; let killer; - beforeEach(function* () { - yield utils.cleanup(fixturePath); + beforeEach(async () => { + await utils.cleanup(fixturePath); app = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=example', fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, egg'); }); - afterEach(function* () { + afterEach(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('shoud stop only if the title matches exactly', function* () { + it('shoud stop only if the title matches exactly', async () => { // Because of'exmaple'.inclues('exmap') === true,if egg-scripts <= 2.1.0 and you run `.. stop --title=exmap`,the process with 'title:example' will also be killed unexpectedly - yield coffee.fork(eggBin, [ 'stop', '--title=examp', fixturePath ]) + await coffee.fork(eggBin, [ 'stop', '--title=examp', fixturePath ]) .debug() .expect('stdout', /\[egg-scripts] stopping egg application with --title=examp/) .expect('stderr', /can't detect any running egg process/) @@ -163,7 +163,7 @@ describe('test/stop.test.js', () => { .end(); // stop only if the title matches exactly - yield coffee.fork(eggBin, [ 'stop', '--title=example', fixturePath ]) + await coffee.fork(eggBin, [ 'stop', '--title=example', fixturePath ]) .debug() .expect('stdout', /\[egg-scripts] stopping egg application with --title=example/) .expect('stdout', /\[egg-scripts\] got master pid \[/) @@ -171,8 +171,8 @@ describe('test/stop.test.js', () => { .end(); }); - it('should stop', function* () { - yield coffee.fork(eggBin, [ 'stop', '--title=random', fixturePath ]) + it('should stop', async () => { + await coffee.fork(eggBin, [ 'stop', '--title=random', fixturePath ]) .debug() .expect('stdout', /\[egg-scripts] stopping egg application with --title=random/) .expect('stderr', /can't detect any running egg process/) @@ -183,8 +183,8 @@ describe('test/stop.test.js', () => { killer.debug(); killer.expect('code', 0); - // yield killer.end(); - yield sleep(waitTime); + // await killer.end(); + await sleep(waitTime); // make sure is kill not auto exist assert(!app.stdout.includes('exist by env')); @@ -207,8 +207,8 @@ describe('test/stop.test.js', () => { let app2; let killer; - beforeEach(function* () { - yield utils.cleanup(fixturePath); + beforeEach(async () => { + await utils.cleanup(fixturePath); app = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=example', fixturePath ]); // app.debug(); app.expect('code', 0); @@ -216,32 +216,32 @@ describe('test/stop.test.js', () => { app2 = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=test', '--port=7002', fixturePath ]); app2.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, egg'); assert(app2.stderr === ''); assert(app2.stdout.match(/custom-framework started on http:\/\/127\.0\.0\.1:7002/)); - const result2 = yield httpclient.request('http://127.0.0.1:7002'); + const result2 = await httpclient.request('http://127.0.0.1:7002'); assert(result2.data.toString() === 'hi, egg'); }); - afterEach(function* () { + afterEach(async () => { app.proc.kill('SIGTERM'); app2.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should stop', function* () { + it('should stop', async () => { killer = coffee.fork(eggBin, [ 'stop' ], { cwd: fixturePath }); killer.debug(); killer.expect('code', 0); - // yield killer.end(); - yield sleep(waitTime); + // await killer.end(); + await sleep(waitTime); // make sure is kill not auto exist assert(!app.stdout.includes('exist by env')); @@ -272,32 +272,32 @@ describe('test/stop.test.js', () => { let app; let killer; this.timeout(17000); - beforeEach(function* () { - yield utils.cleanup(timeoutPath); + beforeEach(async () => { + await utils.cleanup(timeoutPath); app = coffee.fork(eggBin, [ 'start', '--workers=2', '--title=stop-timeout', timeoutPath ]); app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001'); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, egg'); }); - afterEach(function* () { + afterEach(async () => { app.proc.kill('SIGTERM'); - yield utils.cleanup(timeoutPath); + await utils.cleanup(timeoutPath); }); - it('should stop error without timeout', function* () { + it('should stop error without timeout', async () => { killer = coffee.fork(eggBin, [ 'stop' ], { cwd: timeoutPath }); killer.debug(); killer.expect('code', 0); - // yield killer.end(); - yield sleep(waitTime); + // await killer.end(); + await sleep(waitTime); // make sure is kill not auto exist assert(!app.stdout.includes('exist by env')); @@ -313,13 +313,13 @@ describe('test/stop.test.js', () => { assert(killer.stdout.match(/got master pid \["\d+\"]/i)); }); - it('should stop success', function* () { + it('should stop success', async () => { killer = coffee.fork(eggBin, [ 'stop', '--timeout=10s' ], { cwd: timeoutPath }); killer.debug(); killer.expect('code', 0); - // yield killer.end(); - yield sleep(waitTime); + // await killer.end(); + await sleep(waitTime); // make sure is kill not auto exist assert(!app.stdout.includes('exist by env')); @@ -339,10 +339,10 @@ describe('test/stop.test.js', () => { describe('stop with symlink', () => { const baseDir = path.join(__dirname, 'fixtures/tmp'); - beforeEach(function* () { + beforeEach(async () => { // if we can't create a symlink, skip the test try { - yield fs.symlink(fixturePath, baseDir, 'dir'); + await fs.symlink(fixturePath, baseDir, 'dir'); } catch (err) { // may get Error: EPERM: operation not permitted on windows console.log(`test skiped, can't create symlink: ${err}`); @@ -352,28 +352,28 @@ describe('test/stop.test.js', () => { // *unix get the real path of symlink, but windows wouldn't const appPathInRegexp = isWin ? baseDir.replace(/\\/g, '\\\\') : fixturePath; - yield utils.cleanup(fixturePath); - yield rimraf(logDir); - yield coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2' ], { cwd: baseDir }) + await utils.cleanup(fixturePath); + await rimraf(logDir); + await coffee.fork(eggBin, [ 'start', '--daemon', '--workers=2' ], { cwd: baseDir }) .debug() .expect('stdout', new RegExp(`Starting custom-framework application at ${appPathInRegexp}`)) .expect('code', 0) .end(); - yield rimraf(baseDir); - const result = yield httpclient.request('http://127.0.0.1:7001'); + await rimraf(baseDir); + const result = await httpclient.request('http://127.0.0.1:7001'); assert(result.data.toString() === 'hi, egg'); }); - afterEach(function* () { - yield utils.cleanup(fixturePath); - yield rimraf(baseDir); + afterEach(async () => { + await utils.cleanup(fixturePath); + await rimraf(baseDir); }); - it('should stop', function* () { - yield rimraf(baseDir); - yield fs.symlink(path.join(__dirname, 'fixtures/status'), baseDir); + it('should stop', async () => { + await rimraf(baseDir); + await fs.symlink(path.join(__dirname, 'fixtures/status'), baseDir); - yield coffee.fork(eggBin, [ 'stop', baseDir ]) + await coffee.fork(eggBin, [ 'stop', baseDir ]) .debug() .expect('stdout', /\[egg-scripts] stopping egg application/) .expect('stdout', /got master pid \["\d+\"\]/i) diff --git a/test/ts.test.js b/test/ts.test.js index ca0ad96..9d2cfdd 100644 --- a/test/ts.test.js +++ b/test/ts.test.js @@ -26,56 +26,56 @@ describe('test/ts.test.js', () => { describe('should display correct stack traces', () => { let app; - beforeEach(function* () { + beforeEach(async () => { fixturePath = path.join(__dirname, 'fixtures/ts'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); const result = cp.spawnSync('npm', [ 'run', isWin ? 'windows-build' : 'build' ], { cwd: fixturePath, shell: isWin }); assert(!result.stderr.toString()); }); - afterEach(function* () { + afterEach(async () => { app && app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('--ts', function* () { + it('--ts', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=1', '--ts', fixturePath ]); app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/egg started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001', { dataType: 'json' }); + const result = await httpclient.request('http://127.0.0.1:7001', { dataType: 'json' }); // console.log(result.data); assert(result.data.stack.includes(path.normalize('app/controller/home.ts:6:13'))); }); - it('--typescript', function* () { + it('--typescript', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=1', '--typescript', fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/egg started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001', { dataType: 'json' }); + const result = await httpclient.request('http://127.0.0.1:7001', { dataType: 'json' }); // console.log(result.data); assert(result.data.stack.includes(path.normalize('app/controller/home.ts:6:13'))); }); - it('--sourcemap', function* () { + it('--sourcemap', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=1', '--sourcemap', fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/egg started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001', { dataType: 'json' }); + const result = await httpclient.request('http://127.0.0.1:7001', { dataType: 'json' }); // console.log(result.data); assert(result.data.stack.includes(path.normalize('app/controller/home.ts:6:13'))); }); @@ -83,28 +83,28 @@ describe('test/ts.test.js', () => { describe('pkg.egg.typescript', () => { let app; - beforeEach(function* () { + beforeEach(async () => { fixturePath = path.join(__dirname, 'fixtures/ts-pkg'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); const result = cp.spawnSync('npm', [ 'run', isWin ? 'windows-build' : 'build' ], { cwd: fixturePath, shell: isWin }); assert(!result.stderr.toString()); }); - afterEach(function* () { + afterEach(async () => { app && app.proc.kill('SIGTERM'); - yield utils.cleanup(fixturePath); + await utils.cleanup(fixturePath); }); - it('should got correct stack', function* () { + it('should got correct stack', async () => { app = coffee.fork(eggBin, [ 'start', '--workers=1', fixturePath ]); // app.debug(); app.expect('code', 0); - yield sleep(waitTime); + await sleep(waitTime); assert(app.stderr === ''); assert(app.stdout.match(/egg started on http:\/\/127\.0\.0\.1:7001/)); - const result = yield httpclient.request('http://127.0.0.1:7001', { dataType: 'json' }); + const result = await httpclient.request('http://127.0.0.1:7001', { dataType: 'json' }); // console.log(result.data); assert(result.data.stack.includes(path.normalize('app/controller/home.ts:6:13'))); }); diff --git a/test/utils.js b/test/utils.js index 7e21f75..51bde7a 100644 --- a/test/utils.js +++ b/test/utils.js @@ -4,8 +4,8 @@ const helper = require('../lib/helper'); const sleep = require('mz-modules/sleep'); const isWin = process.platform === 'win32'; -exports.cleanup = function* (baseDir) { - const processList = yield helper.findNodeProcess(x => { +exports.cleanup = async function(baseDir) { + const processList = await helper.findNodeProcess(x => { const dir = isWin ? baseDir.replace(/\\/g, '\\\\') : baseDir; const prefix = isWin ? '\\"baseDir\\":\\"' : '"baseDir":"'; return x.cmd.includes(`${prefix}${dir}`); @@ -36,6 +36,6 @@ exports.cleanup = function* (baseDir) { } } - yield sleep('5s'); + await sleep('5s'); } };