Skip to content

Commit ddc48d6

Browse files
author
张星宇
committed
fix: working-directory invalid
Signed-off-by: 张星宇 <neil.zxy@alibaba-inc.com>
1 parent d36f5b8 commit ddc48d6

2 files changed

Lines changed: 21 additions & 0 deletions

File tree

packages/engine/__tests__/failure.test.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ test('某一步执行失败,错误信息记录在context.error', async () => {
1616
expect(res.error).toBeInstanceOf(Error);
1717
});
1818

19+
test('路径错误,正常抛出', async () => {
20+
const steps = [
21+
{ run: 'npm install', id: 'xhello', "working-directory": './xsadads' },
22+
] as IStepOptions[];
23+
const engine = new Engine({ steps, logConfig: { logPrefix } });
24+
const res: IContext | undefined = await engine.start();
25+
expect(res.error).toBeInstanceOf(Error);
26+
});
27+
1928
test('某一步执行失败,后续步骤执行状态为skip', async () => {
2029
const steps = [
2130
{ run: 'echo "hello"', id: 'xhello' },

packages/engine/src/index.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { parsePlugin, getProcessTime, getDefaultInitLog, getLogPath, getPluginRe
88
import { INIT_STEP_COUNT, INIT_STEP_NAME, COMPLETED_STEP_COUNT, DEFAULT_COMPLETED_LOG, SERVERLESS_CD_KEY, SERVERLESS_CD_VALUE } from './constants';
99
import execDaemon from './exec-daemon';
1010
import { filter, join } from 'lodash';
11+
import fs from 'fs';
1112

1213
export { IStepOptions, IContext } from './types';
1314

@@ -382,6 +383,14 @@ class Engine {
382383
}
383384
}
384385
}
386+
private validateWorkingDirectory(path: string) {
387+
if (!fs.existsSync(path)) {
388+
throw new Error(`Invalid working directory: ${path}`);
389+
}
390+
if (!fs.statSync(path).isDirectory()) {
391+
throw new Error(`Path is not a directory: ${path}`);
392+
}
393+
}
385394
private outputErrorLog(error: Error) {
386395
const logConfig = this.options.logConfig as ILogConfig;
387396
const { customLogger } = logConfig;
@@ -398,6 +407,9 @@ class Engine {
398407
if (runItem.run) {
399408
debug(`run: ${runItem.run}`);
400409
let execPath = runItem['working-directory'] || this.context.cwd;
410+
if (execPath) {
411+
this.validateWorkingDirectory(execPath);
412+
}
401413
execPath = path.isAbsolute(execPath) ? execPath : path.join(this.context.cwd, execPath);
402414
this.logName(item);
403415
runItem.run = this.doArtTemplateCompile(runItem.run);

0 commit comments

Comments
 (0)