baset-core
Advanced tools
Comparing version 0.4.1 to 0.5.0
@@ -6,2 +6,13 @@ # Change Log | ||
<a name="0.5.0"></a> | ||
# [0.5.0](https://github.com/Igmat/baset/compare/v0.4.1...v0.5.0) (2018-01-26) | ||
### Features | ||
* **solution:** plugins architecture initial implementation ([92f598c](https://github.com/Igmat/baset/commit/92f598c)), closes [#5](https://github.com/Igmat/baset/issues/5) | ||
<a name="0.4.1"></a> | ||
@@ -8,0 +19,0 @@ ## [0.4.1](https://github.com/Igmat/baset/compare/v0.4.0...v0.4.1) (2018-01-24) |
@@ -1,2 +0,17 @@ | ||
export declare function test(specs: string[], baselines: string[]): Promise<string>[]; | ||
export declare function accept(files: string[]): void; | ||
export interface IDictionary<T> { | ||
[index: string]: T; | ||
} | ||
export declare class Tester { | ||
private readers; | ||
constructor(readers: IDictionary<string[]>, pluginsOptions: IDictionary<any>); | ||
test(specs: string[], baselines: string[]): Promise<{ | ||
name: string; | ||
isPassed: boolean; | ||
expected: string; | ||
actual: string; | ||
}>[]; | ||
accept(files: string[]): Promise<string>[]; | ||
private testSpec; | ||
private acceptBase; | ||
private initPlugins; | ||
} |
@@ -12,65 +12,54 @@ "use strict"; | ||
const fs = require("fs"); | ||
const beautify = require("json-beautify"); | ||
const path = require("path"); | ||
const util_1 = require("util"); | ||
// tslint:disable-next-line:no-any | ||
function calculateValues(obj) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (util_1.isPrimitive(obj)) | ||
return obj; | ||
else if (obj instanceof Promise) | ||
return obj; | ||
else if (obj instanceof Function) | ||
return obj(); | ||
else if (obj instanceof Array) | ||
return yield Promise.all(obj.map(calculateValues)); | ||
return (yield Promise.all(Object.keys(obj) | ||
.map((key) => __awaiter(this, void 0, void 0, function* () { return ({ [key]: yield calculateValues(obj[key]) }); })))) | ||
.reduce((result, prop) => (Object.assign({}, result, prop)), {}); | ||
}); | ||
} | ||
function testSpec(spec) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const file = { | ||
name: spec, | ||
output: beautify(yield calculateValues(require(path.resolve(spec))), undefined, 4, 20), | ||
}; | ||
return yield new Promise((resolve, reject) => { | ||
const baselinePath = path.resolve(file.name.replace(/.spec.js$/, '.base')); | ||
const baseline = fs.existsSync(baselinePath) | ||
? fs.readFileSync(baselinePath, { encoding: 'utf-8' }) | ||
const writeFile = util_1.promisify(fs.writeFile); | ||
const readFile = util_1.promisify(fs.readFile); | ||
const isExists = util_1.promisify(fs.exists); | ||
const unlink = util_1.promisify(fs.unlink); | ||
class Tester { | ||
// tslint:disable-next-line:no-any | ||
constructor(readers, pluginsOptions) { | ||
this.testSpec = (name) => __awaiter(this, void 0, void 0, function* () { | ||
const reader = this.readers.find(({ pattern }) => pattern.test(name)); | ||
if (!reader) | ||
throw new Error(`No reader defined for ${name}!`); | ||
const output = yield reader.read(name); | ||
const ext = path.extname(name); | ||
const baselinePath = path.resolve(name.replace(new RegExp(`${ext}$`), '.base')); | ||
const baselineValue = (yield isExists(baselinePath)) | ||
? yield readFile(baselinePath, { encoding: 'utf-8' }) | ||
: false; | ||
fs.writeFile(path.resolve(file.name.replace(/.spec.js$/, '.base.tmp')), file.output, err => { | ||
if (err) | ||
return reject(err); | ||
console.log(`Temp baseline for ${file.name} is written.`); | ||
if (baseline === file.output) { | ||
resolve(`Test for ${file.name} is passed`); | ||
} | ||
else { | ||
reject({ | ||
name: file.name, | ||
expected: baseline || '', | ||
actual: file.output, | ||
}); | ||
} | ||
}); | ||
yield writeFile(path.resolve(`${baselinePath}.tmp`), output); | ||
return { | ||
name, | ||
isPassed: baselineValue === output, | ||
expected: baselineValue || '', | ||
actual: output, | ||
}; | ||
}); | ||
}); | ||
} | ||
function test(specs, baselines) { | ||
return specs.map(testSpec); | ||
} | ||
exports.test = test; | ||
function accept(files) { | ||
files.forEach(file => { | ||
const baseline = fs.readFileSync(path.resolve(file), { encoding: 'utf-8' }); | ||
const filePath = file.replace(/.tmp$/, ''); | ||
fs.writeFile(path.resolve(filePath), baseline, err => { | ||
console.log(err || `Baseline ${filePath} is written.`); | ||
fs.unlinkSync(path.resolve(file)); | ||
this.acceptBase = (name) => __awaiter(this, void 0, void 0, function* () { | ||
const baseline = yield readFile(path.resolve(name), { encoding: 'utf-8' }); | ||
const filePath = name.replace(/.tmp$/, ''); | ||
yield writeFile(path.resolve(filePath), baseline); | ||
yield unlink(path.resolve(name)); | ||
return filePath; | ||
}); | ||
}); | ||
// tslint:disable-next-line:no-any | ||
this.initPlugins = (plugins, pluginsOptions) => Object.keys(plugins).map(key => { | ||
const chain = plugins[key].map(plugin => new (require(path.resolve(plugin)).default)(pluginsOptions[plugin])); | ||
return { | ||
pattern: new RegExp(key), | ||
read: (filePath) => chain.reduce((result, plugin) => plugin.read(filePath, result), ''), | ||
}; | ||
}); | ||
this.readers = this.initPlugins(readers, pluginsOptions); | ||
} | ||
test(specs, baselines) { | ||
return specs.map(this.testSpec); | ||
} | ||
accept(files) { | ||
return files.map(this.acceptBase); | ||
} | ||
} | ||
exports.accept = accept; | ||
exports.Tester = Tester; | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "baset-core", | ||
"version": "0.4.1", | ||
"version": "0.5.0", | ||
"description": "Core library for BaseT project.", | ||
@@ -28,3 +28,3 @@ "keywords": [ | ||
"@types/node": "^9.3.0", | ||
"baset": "^0.4.1", | ||
"baset": "^0.5.0", | ||
"doctoc": "^1.3.0", | ||
@@ -31,0 +31,0 @@ "tslint": "^5.9.1", |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
10462
80