Skip to content

Commit c4ac733

Browse files
fix win errors
1 parent 3179953 commit c4ac733

File tree

2 files changed

+63
-11
lines changed

2 files changed

+63
-11
lines changed

bin/helpers/readCypressConfigUtil.js

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,20 @@ exports.convertTsConfig = (bsConfig, cypress_config_filepath, bstack_node_module
1717
const cypress_config_filename = bsConfig.run_settings.cypress_config_filename
1818
const working_dir = path.dirname(cypress_config_filepath);
1919
const complied_js_dir = path.join(working_dir, config.compiledConfigJsDirName)
20-
cp.execSync(`rm -rf ${config.compiledConfigJsDirName}`, { cwd: working_dir })
21-
cp.execSync(`mkdir ${config.compiledConfigJsDirName}`, { cwd: working_dir })
20+
if (fs.existsSync(complied_js_dir)) {
21+
fs.rmdirSync(complied_js_dir, { recursive: true })
22+
}
23+
fs.mkdirSync(complied_js_dir, { recursive: true })
24+
25+
const typescript_path = path.join(bstack_node_modules_path, 'typescript', 'bin', 'tsc')
2226

23-
let tsc_command = `NODE_PATH=${bstack_node_modules_path} ${bstack_node_modules_path}/typescript/bin/tsc --outDir ${complied_js_dir} --listEmittedFiles true --allowSyntheticDefaultImports --module commonjs --declaration false ${cypress_config_filepath}`
27+
let tsc_command = `NODE_PATH=${bstack_node_modules_path} node "${typescript_path}" --outDir "${complied_js_dir}" --listEmittedFiles true --allowSyntheticDefaultImports --module commonjs --declaration false "${cypress_config_filepath}"`
28+
29+
if (/^win/.test(process.platform)) {
30+
tsc_command = `set NODE_PATH=${bstack_node_modules_path}&& node "${typescript_path}" --outDir "${complied_js_dir}" --listEmittedFiles true --allowSyntheticDefaultImports --module commonjs --declaration false "${cypress_config_filepath}"`
31+
}
32+
33+
2434
let tsc_output
2535
try {
2636
logger.debug(`Running: ${tsc_command}`)
@@ -54,8 +64,13 @@ exports.convertTsConfig = (bsConfig, cypress_config_filepath, bstack_node_module
5464
}
5565

5666
exports.loadJsFile = (cypress_config_filepath, bstack_node_modules_path) => {
57-
const require_module_helper_path = `${__dirname}/requireModule.js`
58-
cp.execSync(`NODE_PATH=${bstack_node_modules_path} node ${require_module_helper_path} ${cypress_config_filepath}`)
67+
const require_module_helper_path = path.join(__dirname, 'requireModule.js')
68+
let load_command = `NODE_PATH="${bstack_node_modules_path}" node "${require_module_helper_path}" "${cypress_config_filepath}"`
69+
if (/^win/.test(process.platform)) {
70+
load_command = `set NODE_PATH=${bstack_node_modules_path}&& node "${require_module_helper_path}" "${cypress_config_filepath}"`
71+
}
72+
logger.debug(`Running: ${load_command}`)
73+
cp.execSync(load_command)
5974
const cypress_config = JSON.parse(fs.readFileSync(config.configJsonFileName).toString())
6075
if (fs.existsSync(config.configJsonFileName)) {
6176
fs.unlinkSync(config.configJsonFileName)
@@ -67,7 +82,7 @@ exports.readCypressConfigFile = (bsConfig) => {
6782
const cypress_config_filepath = path.resolve(bsConfig.run_settings.cypressConfigFilePath)
6883
try {
6984
const cypress_config_filename = bsConfig.run_settings.cypress_config_filename
70-
const bstack_node_modules_path = `${path.resolve(config.packageDirName)}/node_modules`
85+
const bstack_node_modules_path = path.join(path.resolve(config.packageDirName), 'node_modules')
7186
const conf_lang = this.detectLanguage(cypress_config_filename)
7287

7388
logger.debug(`cypress config path: ${cypress_config_filepath}`);
@@ -92,7 +107,10 @@ exports.readCypressConfigFile = (bsConfig) => {
92107
null
93108
)
94109
} finally {
95-
const working_dir = path.dirname(cypress_config_filepath);
96-
cp.execSync(`rm -rf ${config.compiledConfigJsDirName}`, { cwd: working_dir })
110+
const working_dir = path.dirname(cypress_config_filepath)
111+
const complied_js_dir = path.join(working_dir, config.compiledConfigJsDirName)
112+
if (fs.existsSync(complied_js_dir)) {
113+
fs.rmdirSync(complied_js_dir, { recursive: true })
114+
}
97115
}
98116
}

test/unit/bin/helpers/readCypressConfigUtil.js

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,14 +40,31 @@ describe("readCypressConfigUtil", () => {
4040

4141
describe('loadJsFile', () => {
4242
it('should load js file', () => {
43-
sandbox.stub(cp, "execSync").returns("random string");
43+
const loadCommandStub = sandbox.stub(cp, "execSync").returns("random string");
4444
const readFileSyncStub = sandbox.stub(fs, 'readFileSync').returns('{"e2e": {}}');
4545
const existsSyncStub = sandbox.stub(fs, 'existsSync').returns(true);
4646
const unlinkSyncSyncStub = sandbox.stub(fs, 'unlinkSync');
4747

4848
const result = readCypressConfigUtil.loadJsFile('path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
4949

5050
expect(result).to.eql({ e2e: {} });
51+
sinon.assert.calledOnceWithExactly(loadCommandStub, `NODE_PATH="path/to/tmpBstackPackages" node "/Users/prajwaldhawarikar/Developer/projects/temp/browserstack-cypress-cli/bin/helpers/requireModule.js" "path/to/cypress.config.ts"`);
52+
sinon.assert.calledOnce(readFileSyncStub);
53+
sinon.assert.calledOnce(unlinkSyncSyncStub);
54+
sinon.assert.calledOnce(existsSyncStub);
55+
});
56+
57+
it('should load js file for win', () => {
58+
sinon.stub(process, 'platform').value('win32');
59+
const loadCommandStub = sandbox.stub(cp, "execSync").returns("random string");
60+
const readFileSyncStub = sandbox.stub(fs, 'readFileSync').returns('{"e2e": {}}');
61+
const existsSyncStub = sandbox.stub(fs, 'existsSync').returns(true);
62+
const unlinkSyncSyncStub = sandbox.stub(fs, 'unlinkSync');
63+
64+
const result = readCypressConfigUtil.loadJsFile('path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
65+
66+
expect(result).to.eql({ e2e: {} });
67+
sinon.assert.calledOnceWithExactly(loadCommandStub, `set NODE_PATH=path/to/tmpBstackPackages&& node "/Users/prajwaldhawarikar/Developer/projects/temp/browserstack-cypress-cli/bin/helpers/requireModule.js" "path/to/cypress.config.ts"`);
5168
sinon.assert.calledOnce(readFileSyncStub);
5269
sinon.assert.calledOnce(unlinkSyncSyncStub);
5370
sinon.assert.calledOnce(existsSyncStub);
@@ -62,11 +79,28 @@ describe("readCypressConfigUtil", () => {
6279
cypress_config_filename: 'cypress.config.ts'
6380
}
6481
};
65-
sandbox.stub(cp, "execSync").returns("TSFILE: path/to/compiled/cypress.config.js");
82+
const compileTsStub = sandbox.stub(cp, "execSync").returns("TSFILE: path/to/compiled/cypress.config.js");
83+
84+
const result = readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
85+
86+
expect(result).to.eql('path/to/compiled/cypress.config.js');
87+
sinon.assert.calledOnceWithExactly(compileTsStub, `NODE_PATH=path/to/tmpBstackPackages node "path/to/tmpBstackPackages/typescript/bin/tsc" --outDir "path/to/tmpBstackCompiledJs" --listEmittedFiles true --allowSyntheticDefaultImports --module commonjs --declaration false "path/to/cypress.config.ts"`, { cwd: 'path/to' });
88+
});
89+
90+
it('should compile cypress.config.ts to cypress.config.js for win', () => {
91+
sinon.stub(process, 'platform').value('win32');
92+
const bsConfig = {
93+
run_settings: {
94+
cypressConfigFilePath: 'path/to/cypress.config.ts',
95+
cypress_config_filename: 'cypress.config.ts'
96+
}
97+
};
98+
const compileTsStub = sandbox.stub(cp, "execSync").returns("TSFILE: path/to/compiled/cypress.config.js");
6699

67100
const result = readCypressConfigUtil.convertTsConfig(bsConfig, 'path/to/cypress.config.ts', 'path/to/tmpBstackPackages');
68101

69102
expect(result).to.eql('path/to/compiled/cypress.config.js');
103+
sinon.assert.calledOnceWithExactly(compileTsStub, `set NODE_PATH=path/to/tmpBstackPackages&& node "path/to/tmpBstackPackages/typescript/bin/tsc" --outDir "path/to/tmpBstackCompiledJs" --listEmittedFiles true --allowSyntheticDefaultImports --module commonjs --declaration false "path/to/cypress.config.ts"`, { cwd: 'path/to' });
70104
});
71105

72106
it('should return null if compilation fails', () => {
@@ -92,7 +126,7 @@ describe("readCypressConfigUtil", () => {
92126
};
93127
const execSyncStub = sandbox.stub(cp, "execSync")
94128
execSyncStub
95-
.withArgs(`NODE_PATH=path/to/tmpBstackPackages path/to/tmpBstackPackages/typescript/bin/tsc --outDir path/to/tmpBstackCompiledJs --listEmittedFiles true --allowSyntheticDefaultImports --module commonjs --declaration false path/to/cypress.config.ts`, { cwd: 'path/to' })
129+
.withArgs(`NODE_PATH=path/to/tmpBstackPackages node "path/to/tmpBstackPackages/typescript/bin/tsc" --outDir "path/to/tmpBstackCompiledJs" --listEmittedFiles true --allowSyntheticDefaultImports --module commonjs --declaration false "path/to/cypress.config.ts"`, { cwd: 'path/to' })
96130
.throws({
97131
output: Buffer.from("Error: Some Error \n TSFILE: path/to/compiled/cypress.config.js")
98132
});

0 commit comments

Comments
 (0)