Skip to content

Commit

Permalink
feat(core): abstract environment api
Browse files Browse the repository at this point in the history
  • Loading branch information
Igmat committed Mar 7, 2018
1 parent 484fab6 commit f37f42f
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
8 changes: 6 additions & 2 deletions packages/baset-core/src/abstractEnvironment.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
export abstract class AbstractEnvironmet {
import { IDictionary } from './utils';

export abstract class AbstractEnvironment {
constructor(public options: any) { }
abstract getContextImport(sandbox: IDictionary<any>): string;
abstract dispose(): void;
}

export type IEnvironmentConstructor =
new (options: any) => AbstractEnvironmet;
new (options: any) => AbstractEnvironment;
2 changes: 1 addition & 1 deletion packages/baset-core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import * as utils from './utils';
export { AbstractBaseliner } from './abstractBaseliner';
export { AbstractReader, AddHook, AddFileResolver } from './abstractReader';
export { AbstractResolver } from './abstractResolver';
export { AbstractEnvironmet } from './abstractEnvironment';
export { AbstractEnvironment } from './abstractEnvironment';
export { circularReference, ITestGroupOptions } from './testGroup';
export { Tester } from './tester';
export {
Expand Down
28 changes: 18 additions & 10 deletions packages/baset-core/src/testGroup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import fs from 'fs';
import path from 'path';
import { isPrimitive } from 'util';
import { AbstractBaseliner, IBaselinerConstructor } from './abstractBaseliner';
import { AbstractEnvironmet } from './abstractEnvironment';
import { AbstractEnvironment, IEnvironmentConstructor } from './abstractEnvironment';
import { AbstractReader, IHookOptions, IReaderConstructor } from './abstractReader';
import { AbstractResolver, IResolverConstructor } from './abstractResolver';
import { IDictionary, isExists, readFile } from './utils';
Expand All @@ -20,12 +20,11 @@ export interface ITestGroupOptions {

export class TestGroup {
private baseliner: AbstractBaseliner;
// private environemt: AbstractEnvironmet;
private environment?: AbstractEnvironment;
private references = new WeakMap<object, string>();
private pattern: RegExp;
private readerChain: AbstractReader[];
private resolvers: AbstractResolver[];
private allImports: string[];
private indexOfResolver: (obj: any, context: NodeVM, sandbox: IDictionary<any>) => Promise<number>;
constructor(
pattern: string | RegExp,
Expand All @@ -48,6 +47,12 @@ export class TestGroup {
return new resolver(pluginsOptions[resolverName]);
});

if (options.environment) {
const environment: IEnvironmentConstructor = require(path.resolve(options.environment)).default;

this.environment = new environment(pluginsOptions[options.environment] || pluginsOptions[path.basename(options.environment)]);
}

const resolveMatchers = this.resolvers
.map((resolver, index) =>
async (toMatch: any, context: NodeVM, sandbox: IDictionary<any>) => resolver.match(toMatch, context, sandbox));
Expand All @@ -56,11 +61,6 @@ export class TestGroup {
resolveMatchers
.map(matcher => matcher(obj, context, sandbox))))
.indexOf(true);

this.allImports = [
options.environment,
...options.imports,
].filter((importName): importName is string => !!importName);
}

match = (filePath: string) =>
Expand All @@ -70,12 +70,17 @@ export class TestGroup {
const resolvedPath = path.resolve(filePath);
const compiler = this.getCompiler();
const sandbox: IDictionary<any> = {};
const envImport = this.environment && this.environment.getContextImport(sandbox);
const imports = [
envImport,
...this.options.imports,
].filter((importName): importName is string => !!importName);
const context = new NodeVM({
require: {
builtin: ['*'],
context: 'sandbox',
external: true,
import: this.allImports,
import: imports,
},
sandbox: { basetSandbox: sandbox },
compiler: compiler.compile,
Expand All @@ -97,9 +102,12 @@ export class TestGroup {
? readFile(baselinePath, { encoding: 'utf-8' })
: new Promise<string>(resolve => resolve(''));

const output = await this.baseliner.compare(testsResults, baselineValue);
this.environment && this.environment.dispose();

return {
path: baselinePath,
output: await this.baseliner.compare(testsResults, baselineValue),
output,
};
}
// tslint:disable-next-line:no-any
Expand Down

0 comments on commit f37f42f

Please sign in to comment.