Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Dec 24, 2024
1 parent a04021d commit eebc2f6
Show file tree
Hide file tree
Showing 17 changed files with 136 additions and 97 deletions.
13 changes: 8 additions & 5 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@
"@swc/core": "^1.3.35",
"@types/mocha": "10",
"@types/node": "22",
"assert-file": "^1.0.0",
"coffee": "^5.5.1",
"cpy": "^8.1.2",
"cpy-cli": "^5.0.0",
"egg": "beta",
"egg-bin": "6",
"esbuild": "^0.17.7",
"esbuild-register": "^3.4.2",
"eslint": "8",
Expand All @@ -81,10 +82,12 @@
"scripts": {
"postinstall": "node scripts/postinstall.mjs",
"lint": "eslint --cache src test --ext .ts",
"pretest": "npm run clean && npm run lint -- --fix",
"test": "egg-bin test",
"preci": "npm run clean && npm run lint",
"ci": "egg-bin cov && npm run prepublishOnly",
"pretest": "npm run clean && npm run lint -- --fix && npm run prepublishOnly",
"test": "npm run test-local",
"test-local": "node dist/esm/bin/cli.js test",
"preci": "npm run clean && npm run lint && npm run prepublishOnly",
"cov": "c8 -r lcov -r text-summary -x 'test/**' npm run test-local -- --timeout 120000",
"ci": "npm run cov",
"clean": "rimraf dist",
"copyScripts": "rimraf dist/scripts && cpy scripts dist",
"prepublishOnly": "tshy && tshy-after && attw --pack && rimraf dist/commonjs && npm run copyScripts"
Expand Down
6 changes: 5 additions & 1 deletion src/bin/cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ function main() {
exclude.push('dist');
}

start({ exclude, baseDir });
start({
binName: 'egg-bin',
exclude,
baseDir,
});
}

main();
21 changes: 10 additions & 11 deletions test/cmd/cov.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,14 @@ import path from 'node:path';
import fs from 'node:fs/promises';
import assertFile from 'assert-file';
import { mock } from '@eggjs/mock';

import coffee from '../coffee';
import coffee from '../coffee.js';
import { getFixtures, getRootDirname } from '../helper.js';

const version = Number(process.version.substring(1, 3));

describe('test/cmd/cov.test.ts', () => {
const eggBin = path.join(__dirname, '../../src/bin/cli.ts');
const fixtures = path.join(__dirname, '../fixtures');
const cwd = path.join(fixtures, 'test-files');
const eggBin = path.join(getRootDirname(), 'dist/esm/bin/cli.js');
const cwd = getFixtures('test-files');

async function assertCoverage(baseDir: string) {
assertFile(path.join(baseDir, 'coverage/coverage-final.json'));
Expand All @@ -38,7 +37,7 @@ describe('test/cmd/cov.test.ts', () => {
});

it('should success on ts', async () => {
const cwd = path.join(fixtures, 'example-ts');
const cwd = getFixtures('example-ts');
await coffee.fork(eggBin, [ 'cov' ], { cwd })
// .debug()
.expect('stdout', /should work/)
Expand Down Expand Up @@ -139,15 +138,15 @@ describe('test/cmd/cov.test.ts', () => {
});

it('should run cov when no test files', () => {
const cwd = path.join(fixtures, 'prerequire');
const cwd = getFixtures('prerequire');
return coffee.fork(eggBin, [ 'cov', '--ts=false' ], { cwd, env: { TESTS: 'noexist.js' } })
// .debug()
.expect('code', 0)
.end();
});

it('should set EGG_BIN_PREREQUIRE', async () => {
const cwd = path.join(fixtures, 'prerequire');
const cwd = getFixtures('prerequire');
await coffee.fork(eggBin, [ 'cov', '--ts=false' ], { cwd, env: { TESTS: 'test/**/*.test.js' } })
// .debug()
.expect('stdout', /EGG_BIN_PREREQUIRE undefined/)
Expand All @@ -166,7 +165,7 @@ describe('test/cmd/cov.test.ts', () => {
it('test parallel', () => {
if (process.platform === 'win32') return;
return coffee.fork(eggBin, [ 'cov', '--parallel', '--ts=false' ], {
cwd: path.join(fixtures, 'test-demo-app'),
cwd: getFixtures('test-demo-app'),
env: { TESTS: 'test/**/*.test.js' },
})
// .debug()
Expand All @@ -177,7 +176,7 @@ describe('test/cmd/cov.test.ts', () => {
});

it('should run cov on ts-esm module', () => {
const cwd = path.join(fixtures, 'mocha-test-ts-esm');
const cwd = getFixtures('mocha-test-ts-esm');
return coffee.fork(eggBin, [ 'cov' ], {
cwd,
})
Expand All @@ -192,7 +191,7 @@ describe('test/cmd/cov.test.ts', () => {
if (version < 18 || version > 20) return;
mock(process.env, 'NODE_ENV', 'development');
return coffee.fork(eggBin, [ 'cov' ], {
cwd: path.join(__dirname, '../fixtures/egg-revert'),
cwd: getFixtures('egg-revert'),
})
.debug()
.expect('stdout', /SECURITY WARNING: Reverting CVE-2023-46809: Marvin attack on PKCS#1 padding/)
Expand Down
8 changes: 4 additions & 4 deletions test/cmd/debug.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'node:path';
import coffee from '../coffee';
import coffee from '../coffee.js';
import { getFixtures, getRootDirname } from '../helper.js';

describe('test/cmd/debug.test.ts', () => {
const eggBin = path.join(__dirname, '../../src/bin/cli.ts');
const fixtures = path.join(__dirname, '../fixtures');
const cwd = path.join(fixtures, 'demo-app');
const eggBin = path.join(getRootDirname(), 'dist/esm/bin/cli.js');
const cwd = getFixtures('demo-app');

it('should startCluster success', () => {
return coffee.fork(eggBin, [ 'debug' ], { cwd })
Expand Down
32 changes: 16 additions & 16 deletions test/cmd/dev.test.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import path from 'node:path';
import net from 'node:net';
import detect from 'detect-port';
import net, { Server } from 'node:net';
import { detect } from 'detect-port';
import { mm } from '@eggjs/mock';
import coffee from '../coffee';
import coffee from '../coffee.js';
import { getRootDirname, getFixtures } from '../helper.js';

const version = Number(process.version.substring(1, 3));

describe('test/cmd/dev.test.ts', () => {
const eggBin = path.join(__dirname, '../../src/bin/cli.ts');
const fixtures = path.join(__dirname, '../fixtures');
const cwd = path.join(fixtures, 'demo-app');
const eggBin = path.join(getRootDirname(), 'dist/esm/bin/cli.js');
const cwd = getFixtures('demo-app');

it('should startCluster success', () => {
return coffee.fork(eggBin, [ 'dev' ], {
Expand Down Expand Up @@ -37,7 +37,7 @@ describe('test/cmd/dev.test.ts', () => {
});

it('should dev start work with declarations = true', () => {
const cwd = path.join(fixtures, 'example-declarations');
const cwd = getFixtures('example-declarations');
return coffee.fork(eggBin, [ 'dev' ], { cwd })
.debug()
.expect('stdout', /"workers":1/)
Expand Down Expand Up @@ -115,7 +115,7 @@ describe('test/cmd/dev.test.ts', () => {
});

it('should startCluster with custom yadan framework', () => {
const baseDir = path.join(fixtures, 'custom-framework-app');
const baseDir = getFixtures('custom-framework-app');
return coffee.fork(eggBin, [ 'dev' ], { cwd: baseDir })
// .debug()
.expect('stdout', /yadan start:/)
Expand All @@ -135,7 +135,7 @@ describe('test/cmd/dev.test.ts', () => {
});

it('should support --require', () => {
const script = path.join(fixtures, 'require-script');
const script = getFixtures('require-script');
return coffee.fork(eggBin, [ 'dev', '--require', script ], { cwd })
// .debug()
.expect('stdout', /hey, you require me by --require/)
Expand All @@ -145,7 +145,7 @@ describe('test/cmd/dev.test.ts', () => {

it('should support egg.require', () => {
return coffee.fork(eggBin, [ 'dev' ], {
cwd: path.join(fixtures, 'egg-require'),
cwd: getFixtures('egg-require'),
})
// .debug()
.expect('stdout', /hey, you require me by --require/)
Expand All @@ -154,12 +154,12 @@ describe('test/cmd/dev.test.ts', () => {
});

describe('auto detect available port', () => {
let server;
let serverPort;
let server: Server;
let serverPort: number;
before(async () => {
serverPort = await detect(7001);
server = net.createServer();
await new Promise(resolve => {
await new Promise<void>(resolve => {
server.listen(serverPort, resolve);
});
});
Expand All @@ -169,7 +169,7 @@ describe('test/cmd/dev.test.ts', () => {
it('should auto detect available port', done => {
coffee.fork(eggBin, [ 'dev' ], {
cwd,
env: { EGG_BIN_DEFAULT_PORT: serverPort },
env: { EGG_BIN_DEFAULT_PORT: String(serverPort) },
})
// .debug()
.expect('stderr', /\[egg-bin] server port \d+ is in use, now using port \d+/)
Expand All @@ -179,7 +179,7 @@ describe('test/cmd/dev.test.ts', () => {
});

describe('obtain the port from config.*.js', () => {
const cwd = path.join(fixtures, 'example-port');
const cwd = getFixtures('example-port');
it('should obtain the port from config.default.js', () => {
coffee.fork(eggBin, [ 'dev' ], {
cwd,
Expand All @@ -194,7 +194,7 @@ describe('test/cmd/dev.test.ts', () => {
if (version < 18 || version > 20) return;
mm(process.env, 'NODE_ENV', 'development');
return coffee.fork(eggBin, [ 'dev' ], {
cwd: path.join(__dirname, '../fixtures/egg-revert'),
cwd: getFixtures('egg-revert'),
})
// .debug()
.expect('stdout', /SECURITY WARNING: Reverting CVE-2023-46809: Marvin attack on PKCS#1 padding/)
Expand Down
32 changes: 16 additions & 16 deletions test/cmd/test.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import path from 'node:path';
import coffee from '../coffee';
import coffee from '../coffee.js';
import { getFixtures, getRootDirname } from '../helper.js';

const version = Number(process.version.substring(1, 3));

describe('test/cmd/test.test.ts', () => {
const eggBin = path.join(__dirname, '../../src/bin/cli.ts');
const fixtures = path.join(__dirname, '../fixtures');
const cwd = path.join(fixtures, 'test-files');
const eggBin = path.join(getRootDirname(), 'dist/esm/bin/cli.js');
const cwd = getFixtures('test-files');

describe('egg-bin test', () => {
it('should success js', () => {
Expand All @@ -21,7 +21,7 @@ describe('test/cmd/test.test.ts', () => {
});

it('should success on ts', async () => {
const cwd = path.join(fixtures, 'example-ts');
const cwd = getFixtures('example-ts');
await coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
.expect('stdout', /should work/)
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('test/cmd/test.test.ts', () => {
});

it('should ignore node_modules and fixtures', () => {
return coffee.fork(eggBin, [ 'test' ], { cwd: path.join(fixtures, 'test-files-glob') })
return coffee.fork(eggBin, [ 'test' ], { cwd: getFixtures('test-files-glob') })
// .debug()
.expect('stdout', /should test index/)
.expect('stdout', /should test sub/)
Expand Down Expand Up @@ -145,15 +145,15 @@ describe('test/cmd/test.test.ts', () => {
});

it('should force exit', () => {
const cwd = path.join(fixtures, 'no-exit');
const cwd = getFixtures('no-exit');
return coffee.fork(eggBin, [ 'test' ], { cwd })
// .debug()
.expect('code', 0)
.end();
});

it('run not test with dry-run option', () => {
const cwd = path.join(fixtures, 'mocha-test');
const cwd = getFixtures('mocha-test');
return coffee.fork(eggBin, [ 'test', '--timeout=12345', '--dry-run' ], {
cwd,
env: {
Expand All @@ -171,7 +171,7 @@ describe('test/cmd/test.test.ts', () => {
});

it('should run test on ts-esm module', () => {
const cwd = path.join(fixtures, 'mocha-test-ts-esm');
const cwd = getFixtures('mocha-test-ts-esm');
return coffee.fork(eggBin, [ 'test' ], {
cwd,
})
Expand All @@ -185,7 +185,7 @@ describe('test/cmd/test.test.ts', () => {

it('should success js on unhandled-rejection', () => {
if (version >= 20 && process.platform === 'win32') return;
return coffee.fork(eggBin, [ 'test' ], { cwd: path.join(fixtures, 'test-unhandled-rejection') })
return coffee.fork(eggBin, [ 'test' ], { cwd: getFixtures('test-unhandled-rejection') })
.debug()
.expect('stdout', / Uncaught Error: mock error/)
.expect('code', 1)
Expand All @@ -195,7 +195,7 @@ describe('test/cmd/test.test.ts', () => {
it('test parallel', () => {
if (process.platform === 'win32') return;
return coffee.fork(eggBin, [ 'test', '--parallel' ], {
cwd: path.join(fixtures, 'test-demo-app'),
cwd: getFixtures('test-demo-app'),
})
// .debug()
.expect('stdout', /should work/)
Expand All @@ -206,9 +206,9 @@ describe('test/cmd/test.test.ts', () => {

it('env.MOCHA_FILE should work', () => {
return coffee.fork(eggBin, [ 'test', '--parallel' ], {
cwd: path.join(fixtures, 'test-demo-app'),
cwd: getFixtures('test-demo-app'),
env: {
MOCHA_FILE: path.join(fixtures, 'bin/fake_mocha.js'),
MOCHA_FILE: getFixtures('bin/fake_mocha.js'),
},
})
// .debug()
Expand All @@ -223,7 +223,7 @@ describe('test/cmd/test.test.ts', () => {
describe('run test/.setup.js|ts first', () => {
it('should auto require test/.setup.js', () => {
return coffee.fork(eggBin, [ 'test', '--no-typescript' ], {
cwd: path.join(fixtures, 'setup-js'),
cwd: getFixtures('setup-js'),
env: {
TESTS: 'test/a.test.js',
},
Expand All @@ -238,7 +238,7 @@ describe('test/cmd/test.test.ts', () => {

it('should auto require test/.setup.ts', () => {
return coffee.fork(eggBin, [ 'test', '--typescript' ], {
cwd: path.join(fixtures, 'setup-ts'),
cwd: getFixtures('setup-ts'),
env: {
TESTS: 'test/a.test.ts',
},
Expand Down Expand Up @@ -311,7 +311,7 @@ describe('test/cmd/test.test.ts', () => {
it('should support egg.revert', () => {
if (version < 18 || version > 20) return;
return coffee.fork(eggBin, [ 'test' ], {
cwd: path.join(__dirname, '../fixtures/egg-revert'),
cwd: getFixtures('egg-revert'),
})
.debug()
.expect('stdout', /SECURITY WARNING: Reverting CVE-2023-46809: Marvin attack on PKCS#1 padding/)
Expand Down
6 changes: 5 additions & 1 deletion test/coffee.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ import coffee from 'coffee';
export default {
fork(modulePath: string, args: string[], options: ForkOptions = {}) {
options.execArgv = [
'--require', 'ts-node/register/transpile-only',
// '--require', 'ts-node/register/transpile-only',
'--import', 'ts-node/register/transpile-only',
'--no-warnings',
'--loader', 'ts-node/esm',
...(options.execArgv ?? []),
];
options.env = {
// EGG_TYPESCRIPT: 'true',
NODE_DEBUG: process.env.NODE_DEBUG,
PATH: process.env.PATH,
...options.env,
Expand Down
6 changes: 3 additions & 3 deletions test/egg-bin.test.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import path from 'node:path';
import coffee from './coffee.js';
import { getRootDirname, getFixtures } from './helper.js';

describe('test/egg-bin.test.ts', () => {
const eggBin = path.join(__dirname, '../src/bin/cli.ts');
const fixtures = path.join(__dirname, 'fixtures');
const cwd = path.join(fixtures, 'test-files');
const eggBin = path.join(getRootDirname(), 'dist/esm/bin/cli.js');
const cwd = getFixtures('test-files');

describe('global options', () => {
it('should show version', () => {
Expand Down
12 changes: 11 additions & 1 deletion test/fixtures/my-egg-bin/bin/my-egg-bin.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
#!/usr/bin/env node

import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { start } from '@artus-cli/artus-cli';

start();
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
const baseDir = path.join(__dirname, '..');

console.error(baseDir);

start({
baseDir,
});
Loading

0 comments on commit eebc2f6

Please sign in to comment.