Skip to content

fix win errors #578

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions bin/helpers/readCypressConfigUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,20 @@ exports.convertTsConfig = (bsConfig, cypress_config_filepath, bstack_node_module
const cypress_config_filename = bsConfig.run_settings.cypress_config_filename
const working_dir = path.dirname(cypress_config_filepath);
const complied_js_dir = path.join(working_dir, config.compiledConfigJsDirName)
cp.execSync(`rm -rf ${config.compiledConfigJsDirName}`, { cwd: working_dir })
cp.execSync(`mkdir ${config.compiledConfigJsDirName}`, { cwd: working_dir })
if (fs.existsSync(complied_js_dir)) {
fs.rmdirSync(complied_js_dir, { recursive: true })
}
fs.mkdirSync(complied_js_dir, { recursive: true })

const typescript_path = path.join(bstack_node_modules_path, 'typescript', 'bin', 'tsc')

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}`
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}"`

if (/^win/.test(process.platform)) {
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}"`
}


let tsc_output
try {
logger.debug(`Running: ${tsc_command}`)
Expand Down Expand Up @@ -54,8 +64,13 @@ exports.convertTsConfig = (bsConfig, cypress_config_filepath, bstack_node_module
}

exports.loadJsFile = (cypress_config_filepath, bstack_node_modules_path) => {
const require_module_helper_path = `${__dirname}/requireModule.js`
cp.execSync(`NODE_PATH=${bstack_node_modules_path} node ${require_module_helper_path} ${cypress_config_filepath}`)
const require_module_helper_path = path.join(__dirname, 'requireModule.js')
let load_command = `NODE_PATH="${bstack_node_modules_path}" node "${require_module_helper_path}" "${cypress_config_filepath}"`
if (/^win/.test(process.platform)) {
load_command = `set NODE_PATH=${bstack_node_modules_path}&& node "${require_module_helper_path}" "${cypress_config_filepath}"`
}
logger.debug(`Running: ${load_command}`)
cp.execSync(load_command)
const cypress_config = JSON.parse(fs.readFileSync(config.configJsonFileName).toString())
if (fs.existsSync(config.configJsonFileName)) {
fs.unlinkSync(config.configJsonFileName)
Expand All @@ -67,7 +82,7 @@ exports.readCypressConfigFile = (bsConfig) => {
const cypress_config_filepath = path.resolve(bsConfig.run_settings.cypressConfigFilePath)
try {
const cypress_config_filename = bsConfig.run_settings.cypress_config_filename
const bstack_node_modules_path = `${path.resolve(config.packageDirName)}/node_modules`
const bstack_node_modules_path = path.join(path.resolve(config.packageDirName), 'node_modules')
const conf_lang = this.detectLanguage(cypress_config_filename)

logger.debug(`cypress config path: ${cypress_config_filepath}`);
Expand All @@ -92,7 +107,10 @@ exports.readCypressConfigFile = (bsConfig) => {
null
)
} finally {
const working_dir = path.dirname(cypress_config_filepath);
cp.execSync(`rm -rf ${config.compiledConfigJsDirName}`, { cwd: working_dir })
const working_dir = path.dirname(cypress_config_filepath)
const complied_js_dir = path.join(working_dir, config.compiledConfigJsDirName)
if (fs.existsSync(complied_js_dir)) {
fs.rmdirSync(complied_js_dir, { recursive: true })
}
}
}
43 changes: 40 additions & 3 deletions test/unit/bin/helpers/readCypressConfigUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
const chai = require("chai"),
expect = chai.expect,
sinon = require("sinon"),
path = require('path'),
EventEmitter = require('events');

const logger = require("../../../../bin/helpers/logger").winstonLogger;
Expand Down Expand Up @@ -40,14 +41,33 @@ describe("readCypressConfigUtil", () => {

describe('loadJsFile', () => {
it('should load js file', () => {
sandbox.stub(cp, "execSync").returns("random string");
const loadCommandStub = sandbox.stub(cp, "execSync").returns("random string");
const readFileSyncStub = sandbox.stub(fs, 'readFileSync').returns('{"e2e": {}}');
const existsSyncStub = sandbox.stub(fs, 'existsSync').returns(true);
const unlinkSyncSyncStub = sandbox.stub(fs, 'unlinkSync');
const requireModulePath = path.join(__dirname, '../../../../', 'bin', 'helpers', 'requireModule.js');

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

expect(result).to.eql({ e2e: {} });
sinon.assert.calledOnceWithExactly(loadCommandStub, `NODE_PATH="path/to/tmpBstackPackages" node "${requireModulePath}" "path/to/cypress.config.ts"`);
sinon.assert.calledOnce(readFileSyncStub);
sinon.assert.calledOnce(unlinkSyncSyncStub);
sinon.assert.calledOnce(existsSyncStub);
});

it('should load js file for win', () => {
sinon.stub(process, 'platform').value('win32');
const loadCommandStub = sandbox.stub(cp, "execSync").returns("random string");
const readFileSyncStub = sandbox.stub(fs, 'readFileSync').returns('{"e2e": {}}');
const existsSyncStub = sandbox.stub(fs, 'existsSync').returns(true);
const unlinkSyncSyncStub = sandbox.stub(fs, 'unlinkSync');
const requireModulePath = path.join(__dirname, '../../../../', 'bin', 'helpers', 'requireModule.js');

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

expect(result).to.eql({ e2e: {} });
sinon.assert.calledOnceWithExactly(loadCommandStub, `set NODE_PATH=path/to/tmpBstackPackages&& node "${requireModulePath}" "path/to/cypress.config.ts"`);
sinon.assert.calledOnce(readFileSyncStub);
sinon.assert.calledOnce(unlinkSyncSyncStub);
sinon.assert.calledOnce(existsSyncStub);
Expand All @@ -62,11 +82,28 @@ describe("readCypressConfigUtil", () => {
cypress_config_filename: 'cypress.config.ts'
}
};
sandbox.stub(cp, "execSync").returns("TSFILE: path/to/compiled/cypress.config.js");
const compileTsStub = sandbox.stub(cp, "execSync").returns("TSFILE: path/to/compiled/cypress.config.js");

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

expect(result).to.eql('path/to/compiled/cypress.config.js');
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' });
});

it('should compile cypress.config.ts to cypress.config.js for win', () => {
sinon.stub(process, 'platform').value('win32');
const bsConfig = {
run_settings: {
cypressConfigFilePath: 'path/to/cypress.config.ts',
cypress_config_filename: 'cypress.config.ts'
}
};
const compileTsStub = sandbox.stub(cp, "execSync").returns("TSFILE: path/to/compiled/cypress.config.js");

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

expect(result).to.eql('path/to/compiled/cypress.config.js');
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' });
});

it('should return null if compilation fails', () => {
Expand All @@ -92,7 +129,7 @@ describe("readCypressConfigUtil", () => {
};
const execSyncStub = sandbox.stub(cp, "execSync")
execSyncStub
.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' })
.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' })
.throws({
output: Buffer.from("Error: Some Error \n TSFILE: path/to/compiled/cypress.config.js")
});
Expand Down