Skip to content
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

[WIP]Bayheck master #8043

Closed
wants to merge 7 commits into from
Closed
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
4 changes: 4 additions & 0 deletions src/compiler/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,17 @@ export default class Compiler {
}

async _getTests ({ compiler, filename, code, compiledCode }) {
debugger;

if (compiledCode || this.esm && compiler.canCompileInEsm)
return await compiler.execute(compiledCode, filename);

return await compiler.compile(code, filename);
}

async _compileTestFiles (filenames) {


const testFilesInfo = await this._createTestFilesInfo(filenames);
const compilerTasks = this._getCompilerTasks(testFilesInfo);

Expand Down
76 changes: 55 additions & 21 deletions src/compiler/test-file/api-based.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ export default class APIBasedTestFileCompilerBase extends TestFileCompilerBase {
await eval(`import('${fileUrl}?${PREVENT_MODULE_CACHING_SUFFIX}=${Date.now()}')`);
}
else {
debugger;

const mod = new Module(filename, module.parent);

mod.filename = filename;
Expand All @@ -83,6 +85,8 @@ export default class APIBasedTestFileCompilerBase extends TestFileCompilerBase {
mod._compile(code, filename);

cacheProxy.stopExternalCaching();

return mod;
}
}

Expand Down Expand Up @@ -129,7 +133,13 @@ export default class APIBasedTestFileCompilerBase extends TestFileCompilerBase {
this.origRequireExtensions[ext] = origExt;

require.extensions[ext] = (mod, filename) => {
const hadGlobalAPI = this._hasGlobalAPI();
console.log('require:', ext, filename);

if (ext === '.ts') {
debugger;
}

const hadGlobalAPI = this._hasGlobalAPI();

// NOTE: remove global API so that it will be unavailable for the dependencies
if (APIBasedTestFileCompilerBase._isNodeModulesDep(filename) && hadGlobalAPI)
Expand Down Expand Up @@ -191,46 +201,56 @@ export default class APIBasedTestFileCompilerBase extends TestFileCompilerBase {
return global.fixture && global.test;
}

async _runCompiledCode (compiledCode, filename) {
_runCompiledCode (testFile, compiledCode, filename) {
return this._execAsModule(compiledCode, filename);
}

precompile (testFilesInfo) {
return this._compileCodeForTestFiles(testFilesInfo);
}

execute (compiledCode, filename) {
return this._runCompiledTestCode(compiledCode, filename);
}

async _runCompiledTestCode (compiledCode, filename) {
const testFile = new TestFile(filename);

// override in child
this._addGlobalAPI(testFile);
this._addExportAPI(testFile);

// TODO research and extract/override if needed
stackCleaningHook.enabled = true;

this._setupRequireHook(testFile);

try {
await this._execAsModule(compiledCode, filename);
}
catch (err) {
if (err.code === errRequireEsmErrorCode)
await this._runCompiledCode(testFile, compiledCode, filename);
} catch (err) {
// TODO extract as method and override in child
if (err.code === errRequireEsmErrorCode) {
throw new ImportESMInCommonJSError(err, filename);
}

if (!(err instanceof APIError))
if (!(err instanceof APIError)) {
throw new TestCompilationError(stackCleaningHook.cleanError(err));
}

throw err;
}
finally {
} finally {
this._removeRequireHook();
stackCleaningHook.enabled = false;

if (!this.esm)
this._removeGlobalAPI();
}

return testFile.getTests();
}

// research
stackCleaningHook.enabled = false;

precompile (testFilesInfo) {
return this._compileCodeForTestFiles(testFilesInfo);
}
if (!this.esm) {
this._removeGlobalAPI();
}

execute (compiledCode, filename) {
return this._runCompiledCode(compiledCode, filename);
return testFile.getTests();
}

async compile (code, filename) {
Expand All @@ -242,11 +262,25 @@ export default class APIBasedTestFileCompilerBase extends TestFileCompilerBase {
return Promise.resolve();
}

// async compileConfiguration (filename) {
// const [compiledCode] = await this.precompile([{ code: '', filename }]);
//
// if (compiledCode) {
// debugger;
// const compiledConfigurationModule = await this._runCompiledCode({}, compiledCode, filename);
// debugger;
//
// return compiledConfigurationModule.exports;
// }
//
// return Promise.resolve();
// }

_hasTests (code) {
return FIXTURE_RE.test(code) && TEST_RE.test(code);
}

cleanUp () {
this.cache = {};
// this.cache = {};
}
}
5 changes: 4 additions & 1 deletion src/compiler/test-file/base.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import { escapeRegExp as escapeRe, flatten } from 'lodash';
import { assertBaseUrl, getUrl } from '../../api/test-page-url';
import { assertType, is } from '../../errors/runtime/type-assertions';
import { EventEmitter } from 'events';

export default class TestFileCompilerBase {
export default class TestFileCompilerBase extends EventEmitter {
constructor ({ baseUrl }) {
super();

const escapedExt = flatten([this.getSupportedExtension()])
.map(ext => escapeRe(ext))
.join('|');
Expand Down
61 changes: 61 additions & 0 deletions src/compiler/test-file/formats/typescript/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,65 @@ export default class TypeScriptTestFileCompiler extends APIBasedTestFileCompiler
public getSupportedExtension (): string[] {
return [Extensions.ts, Extensions.tsx];
}

async compileConfiguration (filename: string) {
let compiledConfigurationModule = null;

debugger;

const [compiledCode] = await this.precompile([{ code: '', filename }]);

if (compiledCode) {
this._setupRequireHook({ });

try {
compiledConfigurationModule = await this._runCompiledCode({}, compiledCode, filename);
}
catch (err) {

}
finally {
this._removeRequireHook();
}

debugger;

return compiledConfigurationModule.exports;
}

return Promise.resolve();
}
}

// export class TypeScriptTestFileCompiler extends TypeScriptTestFileCompiler {
// //
// }
//
// export class TypeScriptConfigurationFileCompiler extends TypeScriptTestFileCompiler {
//
// // async compileConfiguration (filename: string) {
// // let compiledConfigurationModule = null;
// //
// // const [compiledCode] = await this.precompile([{ code: '', filename }]);
// //
// // if (compiledCode) {
// // this._setupRequireHook({ });
// //
// // try {
// // compiledConfigurationModule = await this._runCompiledCode({}, compiledCode, filename);
// // }
// // catch (err) {
// //
// // }
// // finally {
// // this._removeRequireHook();
// // }
// //
// // return compiledConfigurationModule.exports;
// // }
// //
// // return Promise.resolve();
// // }
// }
//
//
51 changes: 50 additions & 1 deletion src/configuration/configuration-base.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { extname, isAbsolute } from 'path';

import {
extname, isAbsolute,
} from 'path';
import debug from 'debug';

import JSON5 from 'json5';

import {
Expand All @@ -21,6 +25,7 @@ import Extensions from './formats';
import { ReadConfigFileError } from '../errors/runtime';
import { RUNTIME_ERRORS } from '../errors/types';


const DEBUG_LOGGER = debug('testcafe:configuration');

export default class Configuration {
Expand Down Expand Up @@ -155,6 +160,7 @@ export default class Configuration {
}

public async _load (): Promise<null | object> {

if (!this.defaultPaths?.length)
return null;

Expand All @@ -166,6 +172,8 @@ export default class Configuration {

if (this._isJSConfiguration(filePath))
options = this._readJsConfigurationFileContent(filePath);
else if (this._isTSConfiguration(filePath))
options = await this._readTsConfigurationFileContent(filePath);
else {
const configurationFileContent = await this._readConfigurationFileContent(filePath);

Expand All @@ -178,6 +186,7 @@ export default class Configuration {

const existedConfigs = configs.filter(config => !!config.options);


if (!existedConfigs.length)
return null;

Expand Down Expand Up @@ -210,6 +219,10 @@ export default class Configuration {
return Configuration._hasExtension(filePath, Extensions.js) || Configuration._hasExtension(filePath, Extensions.cjs);
}

protected _isTSConfiguration (filePath = this.filePath): boolean {
return Configuration._hasExtension(filePath, Extensions.ts) || Configuration._hasExtension(filePath, Extensions.cjs);
}

protected _isJSONConfiguration (filePath = this.filePath): boolean {
return Configuration._hasExtension(filePath, Extensions.json);
}
Expand All @@ -229,6 +242,42 @@ export default class Configuration {
return null;
}

public async _readTsConfigurationFileContent (filePath = this.filePath): Promise<object | null> {
if (filePath) {
delete require.cache[filePath];

// TODO: change require to import
const TypeScriptFileCompiler = require('../compiler/test-file/formats/typescript/compiler');

const compiler = new TypeScriptFileCompiler();

// const waitUntilCompiled = new Promise((resolve, reject) => {
// compiler.on('module-compiled', (options:any) => {
// console.log('!!!!!!!!!!!!!');
// resolve(options);
// });
// });

debugger;

const options = await compiler.compileConfiguration(filePath);

debugger;

// @ts-ignore
return options;

// const options = require(filePath as string);


// return {};
// return options;
}

return null;
}


public async _readConfigurationFileContent (filePath = this.filePath): Promise<Buffer | null> {
try {
return await readFile(filePath);
Expand Down
1 change: 1 addition & 0 deletions src/configuration/formats.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ enum Extensions {
js = '.js',
json = '.json',
cjs = '.cjs',
ts = '.ts',
}

export default Extensions;
12 changes: 6 additions & 6 deletions src/runner/bootstrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ export default class Bootstrapper {
const cwd = process.cwd();
const sourceList = await parseFileList(this.sources, cwd);

if (!sourceList.length)
throw new GeneralError(RUNTIME_ERRORS.testFilesNotFound, cwd, getConcatenatedValuesString(this.sources, '\n', ''));
// if (!sourceList.length)
// throw new GeneralError(RUNTIME_ERRORS.testFilesNotFound, cwd, getConcatenatedValuesString(this.sources, '\n', ''));

let tests = await guardTimeExecution(
async () => await this._compileTests({ sourceList, compilerOptions: this.compilerOptions, runnableConfigurationId: id }),
Expand All @@ -324,14 +324,14 @@ export default class Bootstrapper {
if (testsWithOnlyFlag.length)
tests = testsWithOnlyFlag;

if (!tests.length)
throw new GeneralError(RUNTIME_ERRORS.noTestsToRun);
// if (!tests.length)
// throw new GeneralError(RUNTIME_ERRORS.noTestsToRun);

if (this.filter)
tests = await this._filterTests(tests, this.filter);

if (!tests.length)
throw new GeneralError(RUNTIME_ERRORS.noTestsToRunDueFiltering);
// if (!tests.length)
// throw new GeneralError(RUNTIME_ERRORS.noTestsToRunDueFiltering);

this._setGlobalHooksToTests(tests);

Expand Down
6 changes: 5 additions & 1 deletion test/server/compiler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,9 @@ describe('Compiler', function () {
});
});

it('Incorrect callsite stack in error report if "import" is used (GH-1226)', function () {
it.only('Incorrect callsite stack in error report if "import" is used (GH-1226)', function () {
debugger;

return compile('test/server/data/test-suites/regression-gh-1226/testfile.js')
.then(compiled => {
return compiled.tests[0].fn(testRunMock);
Expand All @@ -1052,6 +1054,8 @@ describe('Compiler', function () {
const err = errList.items[0];
const stack = err.callsite.stackFrames.filter(createStackFilter(stackTraceLimit));

debugger;

expect(stack.length).eql(8);

const lastStackItem = stack.pop();
Expand Down
Loading