Skip to content

Commit 2bf4d37

Browse files
authored
fix: cli init translation use correct actor instead of I (#3848)
* fix: cli init translation use correct actor instead of I * fix: test runner by ensuring sandbox folder exist * fix: break loop when actor is set
1 parent 53f7ab3 commit 2bf4d37

File tree

2 files changed

+100
-6
lines changed

2 files changed

+100
-6
lines changed

lib/command/init.js

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,13 @@ module.exports = function (initPath) {
201201
// no extra step file for typescript (as it doesn't match TS conventions)
202202
const stepFile = `./steps_file.${extension}`;
203203
fs.writeFileSync(path.join(testsPath, stepFile), extension === 'ts' ? defaultActorTs : defaultActor);
204-
config.include.I = isTypeScript === true ? './steps_file' : stepFile;
204+
205+
if (isTypeScript) {
206+
config.include = _actorTranslation('./steps_file', config.translation);
207+
} else {
208+
config.include = _actorTranslation(stepFile, config.translation);
209+
}
210+
205211
print(`Steps file created at ${stepFile}`);
206212

207213
let configSource;
@@ -316,7 +322,7 @@ module.exports = function (initPath) {
316322
};
317323

318324
print('Configure helpers...');
319-
inquirer.prompt(helperConfigs).then((helperResult) => {
325+
inquirer.prompt(helperConfigs).then(async (helperResult) => {
320326
if (helperResult.Playwright_browser === 'electron') {
321327
delete helperResult.Playwright_url;
322328
delete helperResult.Playwright_show;
@@ -336,12 +342,12 @@ module.exports = function (initPath) {
336342
});
337343

338344
print('');
339-
finish();
345+
await finish();
340346
});
341347
});
342348
};
343349

344-
function install(dependencies, verbose) {
350+
function install(dependencies) {
345351
let command;
346352
let args;
347353

@@ -374,9 +380,39 @@ function install(dependencies, verbose) {
374380
].concat(dependencies);
375381
}
376382

383+
if (process.env._INIT_DRY_RUN_INSTALL) {
384+
args.push('--dry-run');
385+
}
386+
377387
const { status } = spawn.sync(command, args, { stdio: 'inherit' });
378388
if (status !== 0) {
379389
throw new Error(`${command} ${args.join(' ')} failed`);
380390
}
381391
return true;
382392
}
393+
394+
function _actorTranslation(stepFile, translationSelected) {
395+
let actor;
396+
397+
for (const translationAvailable of translations) {
398+
if (actor) {
399+
break;
400+
}
401+
402+
if (translationSelected === translationAvailable) {
403+
const nameOfActor = require('../../translations')[translationAvailable].I;
404+
405+
actor = {
406+
[nameOfActor]: stepFile,
407+
};
408+
}
409+
}
410+
411+
if (!actor) {
412+
actor = {
413+
I: stepFile,
414+
};
415+
}
416+
417+
return actor;
418+
}

test/runner/init_test.js

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,78 @@
11
const { DOWN, ENTER } = require('inquirer-test');
22
const run = require('inquirer-test');
33
const path = require('path');
4+
const fs = require('fs');
5+
const mkdirp = require('mkdirp');
46

57
const runner = path.join(__dirname, '../../bin/codecept.js');
8+
const codecept_dir = path.join(__dirname, '/../data/sandbox/configs/init');
69

710
describe('Init Command', function () {
811
this.timeout(20000);
912

10-
it('steps are showing', async () => {
11-
const result = await run([runner, 'init'], ['Y', ENTER, ENTER, DOWN, DOWN, DOWN, ENTER, 'y']);
13+
beforeEach(() => {
14+
mkdirp.sync(codecept_dir);
15+
process.env._INIT_DRY_RUN_INSTALL = true;
16+
});
17+
18+
afterEach(() => {
19+
try {
20+
fs.unlinkSync(`${codecept_dir}/codecept.conf.ts`);
21+
fs.unlinkSync(`${codecept_dir}/steps_file.ts`);
22+
fs.unlinkSync(`${codecept_dir}/tsconfig.json`);
23+
} catch (e) {
24+
// continue regardless of error
25+
}
26+
27+
try {
28+
fs.unlinkSync(`${codecept_dir}/codecept.conf.js`);
29+
fs.unlinkSync(`${codecept_dir}/steps_file.js`);
30+
fs.unlinkSync(`${codecept_dir}/jsconfig.json`);
31+
} catch (e) {
32+
// continue regardless of error
33+
}
34+
35+
delete process.env._INIT_DRY_RUN_INSTALL;
36+
});
37+
38+
it('should init Codecept with TypeScript REST JSONResponse English', async () => {
39+
const result = await run([runner, 'init', codecept_dir], ['Y', ENTER, ENTER, DOWN, DOWN, DOWN, ENTER, 'y', ENTER, codecept_dir, ENTER, ENTER, ENTER, ENTER]);
40+
1241
result.should.include('Welcome to CodeceptJS initialization tool');
1342
result.should.include('It will prepare and configure a test environment for you');
1443
result.should.include('Installing to');
1544
result.should.include('? Do you plan to write tests in TypeScript? (y/N)');
1645
result.should.include('Where are your tests located? ./*_test.ts');
1746
result.should.include('What helpers do you want to use? REST');
1847
result.should.include('? Do you want to use JSONResponse helper for assertions on JSON responses?');
48+
result.should.include('? Where should logs, screenshots, and reports to be stored?');
49+
result.should.include('? Do you want to enable localization for tests?');
50+
51+
const config = fs.readFileSync(`${codecept_dir}/codecept.conf.ts`).toString();
52+
config.should.include('I: \'./steps_file\'');
53+
54+
fs.accessSync(`${codecept_dir}/steps_file.ts`, fs.constants.R_OK);
55+
fs.accessSync(`${codecept_dir}/tsconfig.json`, fs.constants.R_OK);
56+
});
57+
58+
it('should init Codecept with JavaScript REST JSONResponse de-DE', async () => {
59+
const result = await run([runner, 'init', codecept_dir], [ENTER, ENTER, DOWN, DOWN, DOWN, ENTER, 'y', ENTER, codecept_dir, ENTER, DOWN, ENTER, ENTER, ENTER]);
60+
61+
result.should.include('Welcome to CodeceptJS initialization tool');
62+
result.should.include('It will prepare and configure a test environment for you');
63+
result.should.include('Installing to');
64+
result.should.include('? Do you plan to write tests in TypeScript? (y/N)');
65+
result.should.include('Where are your tests located? ./*_test.js');
66+
result.should.include('What helpers do you want to use? REST');
67+
result.should.include('? Do you want to use JSONResponse helper for assertions on JSON responses?');
68+
result.should.include('? Where should logs, screenshots, and reports to be stored?');
69+
result.should.include('? Do you want to enable localization for tests?');
70+
result.should.include('de-DE');
71+
72+
const config = fs.readFileSync(`${codecept_dir}/codecept.conf.js`).toString();
73+
config.should.include('Ich: \'./steps_file.js\'');
74+
75+
fs.accessSync(`${codecept_dir}/steps_file.js`, fs.constants.R_OK);
76+
fs.accessSync(`${codecept_dir}/jsconfig.json`, fs.constants.R_OK);
1977
});
2078
});

0 commit comments

Comments
 (0)