@betterer/betterer
Advanced tools
Comparing version 0.5.0 to 0.5.1
import { ConstraintResult } from '@betterer/constraints'; | ||
import { Betterer, MaybeAsync } from '../types'; | ||
import { Betterer } from '../betterer'; | ||
import { MaybeAsync } from '../types'; | ||
import { BettererFile } from './file'; | ||
@@ -4,0 +5,0 @@ import { BettererFileMarksMap, BettererFileInfo } from './types'; |
@@ -5,6 +5,7 @@ "use strict"; | ||
var logger_1 = require("@betterer/logger"); | ||
var betterer_1 = require("../betterer"); | ||
var file_1 = require("./file"); | ||
function createFileBetterer(test) { | ||
var _this = this; | ||
return { | ||
return betterer_1.createBetterer({ | ||
test: function (config) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
@@ -25,3 +26,3 @@ var _a, _b, _c; | ||
diff: diff | ||
}; | ||
}); | ||
} | ||
@@ -28,0 +29,0 @@ exports.createFileBetterer = createFileBetterer; |
@@ -5,2 +5,3 @@ "use strict"; | ||
var logger_1 = require("@betterer/logger"); | ||
var betterer_1 = require("./betterer"); | ||
var printer_1 = require("./printer"); | ||
@@ -10,22 +11,21 @@ var reader_1 = require("./reader"); | ||
var serialiser_1 = require("./serialiser"); | ||
var test_1 = require("./test"); | ||
var writer_1 = require("./writer"); | ||
function run(config) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var configPaths, filters, resultsPath, tests, testsToRun, expectedResults, _a, obsoleteNames, stats, results, printed, printError, _b; | ||
var _c; | ||
var configPaths, resultsPath, _a, filters, betterers, only, expectedResults, _b, obsolete, stats, results, printed, printError, _c; | ||
var _d; | ||
var _this = this; | ||
return tslib_1.__generator(this, function (_d) { | ||
switch (_d.label) { | ||
return tslib_1.__generator(this, function (_e) { | ||
switch (_e.label) { | ||
case 0: | ||
configPaths = config.configPaths, filters = config.filters, resultsPath = config.resultsPath; | ||
tests = {}; | ||
configPaths = config.configPaths, resultsPath = config.resultsPath, _a = config.filters, filters = _a === void 0 ? [] : _a; | ||
betterers = []; | ||
return [4 /*yield*/, Promise.all(configPaths.map(function (configPath) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var moreTests; | ||
var more; | ||
return tslib_1.__generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: return [4 /*yield*/, getTests(configPath)]; | ||
case 0: return [4 /*yield*/, getBetterers(configPath)]; | ||
case 1: | ||
moreTests = _a.sent(); | ||
tests = tslib_1.__assign(tslib_1.__assign({}, tests), moreTests); | ||
more = _a.sent(); | ||
betterers = tslib_1.__spreadArrays(betterers, more); | ||
return [2 /*return*/]; | ||
@@ -36,62 +36,74 @@ } | ||
case 1: | ||
_d.sent(); | ||
testsToRun = Object.keys(tests).filter(function (testName) { | ||
return !filters || filters.some(function (filter) { return filter.test(testName); }); | ||
}); | ||
_e.sent(); | ||
if (filters.length) { | ||
betterers.forEach(function (betterer) { | ||
if (!filters.some(function (filter) { return filter.test(betterer.name); })) { | ||
betterer.skip(); | ||
} | ||
}); | ||
} | ||
only = betterers.filter(function (betterer) { return betterer.isOnly; }); | ||
expectedResults = {}; | ||
if (!resultsPath) return [3 /*break*/, 5]; | ||
_d.label = 2; | ||
_e.label = 2; | ||
case 2: | ||
_d.trys.push([2, 4, , 5]); | ||
_e.trys.push([2, 4, , 5]); | ||
return [4 /*yield*/, reader_1.read(resultsPath)]; | ||
case 3: | ||
expectedResults = _d.sent(); | ||
expectedResults = _e.sent(); | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
_a = _d.sent(); | ||
_b = _e.sent(); | ||
logger_1.error("could not read results from \"" + resultsPath + "\". \uD83D\uDE14"); | ||
throw new Error(); | ||
case 5: | ||
obsoleteNames = Object.keys(expectedResults).filter(function (testName) { return !tests[testName]; }); | ||
obsoleteNames.forEach(function (obsoleteName) { | ||
delete expectedResults[obsoleteName]; | ||
obsolete = Object.keys(expectedResults).filter(function (name) { return !betterers.find(function (betterer) { return betterer.name === name; }); }); | ||
obsolete.forEach(function (name) { | ||
delete expectedResults[name]; | ||
}); | ||
stats = statistics_1.initialise(); | ||
(_c = stats.obsolete).push.apply(_c, obsoleteNames); | ||
(_d = stats.obsolete).push.apply(_d, obsolete); | ||
results = tslib_1.__assign({}, expectedResults); | ||
return [4 /*yield*/, testsToRun.reduce(function (p, testName) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var _a, test, constraint, goal, diff, checkGoal, current, _b, serialisedPrevious, serialisedCurrent, comparison, isSame, isBetter; | ||
return tslib_1.__generator(this, function (_c) { | ||
switch (_c.label) { | ||
return [4 /*yield*/, betterers.reduce(function (p, betterer) { return tslib_1.__awaiter(_this, void 0, void 0, function () { | ||
var test, constraint, goal, diff, isSkipped, name, current, _a, serialisedPrevious, serialisedCurrent, comparison, isSame, isBetter; | ||
return tslib_1.__generator(this, function (_b) { | ||
switch (_b.label) { | ||
case 0: return [4 /*yield*/, p]; | ||
case 1: | ||
_c.sent(); | ||
_a = tests[testName], test = _a.test, constraint = _a.constraint, goal = _a.goal, diff = _a.diff; | ||
checkGoal = goal; | ||
_c.label = 2; | ||
_b.sent(); | ||
test = betterer.test, constraint = betterer.constraint, goal = betterer.goal, diff = betterer.diff, isSkipped = betterer.isSkipped, name = betterer.name; | ||
if (only.length && !only.includes(betterer)) { | ||
stats.skipped.push(name); | ||
return [2 /*return*/]; | ||
} | ||
if (isSkipped) { | ||
stats.skipped.push(name); | ||
return [2 /*return*/]; | ||
} | ||
_b.label = 2; | ||
case 2: | ||
_c.trys.push([2, 4, , 5]); | ||
logger_1.info("running \"" + testName + "\"!"); | ||
_b.trys.push([2, 4, , 5]); | ||
logger_1.info("running \"" + name + "\"!"); | ||
return [4 /*yield*/, test(config)]; | ||
case 3: | ||
current = _c.sent(); | ||
current = _b.sent(); | ||
return [3 /*break*/, 5]; | ||
case 4: | ||
_b = _c.sent(); | ||
stats.failed.push(testName); | ||
logger_1.error("\"" + testName + "\" failed to run. \uD83D\uDD25"); | ||
_a = _b.sent(); | ||
stats.failed.push(name); | ||
logger_1.error("\"" + name + "\" failed to run. \uD83D\uDD25"); | ||
return [2 /*return*/]; | ||
case 5: | ||
stats.ran.push(testName); | ||
serialisedPrevious = expectedResults[testName] | ||
? JSON.parse(expectedResults[testName].value) | ||
stats.ran.push(name); | ||
serialisedPrevious = expectedResults[name] | ||
? JSON.parse(expectedResults[name].value) | ||
: null; | ||
return [4 /*yield*/, serialiser_1.serialise(current)]; | ||
case 6: | ||
serialisedCurrent = _c.sent(); | ||
serialisedCurrent = _b.sent(); | ||
// New test: | ||
if (!Object.hasOwnProperty.call(expectedResults, testName)) { | ||
results[testName] = update(current); | ||
stats.new.push(testName); | ||
logger_1.success("\"" + testName + "\" got checked for the first time! \uD83C\uDF89"); | ||
if (!Object.hasOwnProperty.call(expectedResults, name)) { | ||
results[name] = update(current); | ||
stats.new.push(name); | ||
logger_1.success("\"" + name + "\" got checked for the first time! \uD83C\uDF89"); | ||
return [2 /*return*/]; | ||
@@ -101,9 +113,9 @@ } | ||
case 7: | ||
comparison = _c.sent(); | ||
comparison = _b.sent(); | ||
isSame = comparison === "same" /* same */; | ||
isBetter = comparison === "better" /* better */; | ||
// Same, but already met goal: | ||
if (isSame && checkGoal(serialisedCurrent)) { | ||
stats.completed.push(testName); | ||
logger_1.success("\"" + testName + "\" has already met its goal! \u2728"); | ||
if (isSame && goal(serialisedCurrent)) { | ||
stats.completed.push(name); | ||
logger_1.success("\"" + name + "\" has already met its goal! \u2728"); | ||
return [2 /*return*/]; | ||
@@ -113,4 +125,4 @@ } | ||
if (isSame) { | ||
stats.same.push(testName); | ||
logger_1.warn("\"" + testName + "\" stayed the same. \uD83D\uDE10"); | ||
stats.same.push(name); | ||
logger_1.warn("\"" + name + "\" stayed the same. \uD83D\uDE10"); | ||
return [2 /*return*/]; | ||
@@ -120,17 +132,17 @@ } | ||
if (isBetter) { | ||
results[testName] = update(current); | ||
stats.better.push(testName); | ||
results[name] = update(current); | ||
stats.better.push(name); | ||
// Newly met goal: | ||
if (checkGoal(serialisedCurrent)) { | ||
stats.completed.push(testName); | ||
logger_1.success("\"" + testName + "\" met its goal! \uD83C\uDF89"); | ||
if (goal(serialisedCurrent)) { | ||
stats.completed.push(name); | ||
logger_1.success("\"" + name + "\" met its goal! \uD83C\uDF89"); | ||
return [2 /*return*/]; | ||
} | ||
// Not reached goal yet: | ||
logger_1.success("\"" + testName + "\" got better! \uD83D\uDE0D"); | ||
logger_1.success("\"" + name + "\" got better! \uD83D\uDE0D"); | ||
return [2 /*return*/]; | ||
} | ||
// Worse: | ||
stats.worse.push(testName); | ||
logger_1.error("\"" + testName + "\" got worse. \uD83D\uDE14"); | ||
stats.worse.push(name); | ||
logger_1.error("\"" + name + "\" got worse. \uD83D\uDE14"); | ||
logger_1.br(); | ||
@@ -144,18 +156,18 @@ diff(current, serialisedCurrent, serialisedPrevious); | ||
case 6: | ||
_d.sent(); | ||
_e.sent(); | ||
statistics_1.report(stats); | ||
return [4 /*yield*/, printer_1.print(results)]; | ||
case 7: | ||
printed = _d.sent(); | ||
printed = _e.sent(); | ||
printError = ''; | ||
if (!resultsPath) return [3 /*break*/, 12]; | ||
_d.label = 8; | ||
_e.label = 8; | ||
case 8: | ||
_d.trys.push([8, 10, , 11]); | ||
_e.trys.push([8, 10, , 11]); | ||
return [4 /*yield*/, writer_1.write(printed, resultsPath)]; | ||
case 9: | ||
_d.sent(); | ||
_e.sent(); | ||
return [3 /*break*/, 11]; | ||
case 10: | ||
_b = _d.sent(); | ||
_c = _e.sent(); | ||
printError = "could not write results to \"" + resultsPath + "\". \uD83D\uDE14"; | ||
@@ -166,3 +178,3 @@ return [3 /*break*/, 11]; | ||
printError = "no `resultsPath` given. \uD83D\uDE14"; | ||
_d.label = 13; | ||
_e.label = 13; | ||
case 13: | ||
@@ -172,3 +184,3 @@ if (printError) { | ||
logger_1.error('printing to stdout instead:'); | ||
process.stdout.write("\n\n\n" + printed + "\n\n\n"); | ||
console.log("\n\n\n" + printed + "\n\n\n"); | ||
} | ||
@@ -181,5 +193,5 @@ return [2 /*return*/, stats]; | ||
exports.run = run; | ||
function getTests(configPath) { | ||
function getBetterers(configPath) { | ||
return tslib_1.__awaiter(this, void 0, void 0, function () { | ||
var imported, tests_1, _a; | ||
var imported, betterers_1, _a; | ||
return tslib_1.__generator(this, function (_b) { | ||
@@ -192,7 +204,8 @@ switch (_b.label) { | ||
imported = _b.sent(); | ||
tests_1 = imported.default ? imported.default : imported; | ||
Object.keys(tests_1).forEach(function (test) { | ||
tests_1[test] = test_1.createTest(tests_1[test]); | ||
}); | ||
return [2 /*return*/, tests_1]; | ||
betterers_1 = imported.default ? imported.default : imported; | ||
return [2 /*return*/, Object.keys(betterers_1).map(function (name) { | ||
var betterer = betterer_1.createBetterer(betterers_1[name]); | ||
betterer.name = name; | ||
return betterer; | ||
})]; | ||
case 2: | ||
@@ -202,3 +215,3 @@ _a = _b.sent(); | ||
case 3: | ||
logger_1.error("could not read tests from \"" + configPath + "\". \uD83D\uDE14"); | ||
logger_1.error("could not read \"" + configPath + "\". \uD83D\uDE14"); | ||
throw new Error(); | ||
@@ -205,0 +218,0 @@ } |
export declare type BettererStats = { | ||
obsolete: Array<string>; | ||
skipped: Array<string>; | ||
ran: Array<string>; | ||
@@ -4,0 +5,0 @@ failed: Array<string>; |
@@ -7,2 +7,3 @@ "use strict"; | ||
obsolete: [], | ||
skipped: [], | ||
ran: [], | ||
@@ -26,2 +27,3 @@ failed: [], | ||
var same = stats.same.length; | ||
var skipped = stats.skipped.length; | ||
var completed = stats.completed; | ||
@@ -52,2 +54,5 @@ logger_1.info(ran + " " + getThings(ran) + " got checked. \uD83E\uDD14"); | ||
} | ||
if (skipped) { | ||
logger_1.warn(skipped + " " + getThings(skipped) + " got skipped. \u274C"); | ||
} | ||
} | ||
@@ -54,0 +59,0 @@ exports.report = report; |
@@ -1,20 +0,2 @@ | ||
import { ConstraintResult } from '@betterer/constraints'; | ||
import { BettererConfig } from './config'; | ||
export declare type MaybeAsync<T> = T | Promise<T>; | ||
export declare type BettererTest<T> = (config: BettererConfig) => MaybeAsync<T>; | ||
export declare type BettererConstraint<T> = (current: T, previous: T) => MaybeAsync<ConstraintResult>; | ||
export declare type BettererGoalFunction<SerialisedType> = (current: SerialisedType) => MaybeAsync<boolean>; | ||
export declare type BettererGoal<SerialisedType> = SerialisedType | BettererGoalFunction<SerialisedType>; | ||
export declare type BettererDiff<TestType, SerialisedType> = (current: TestType, serialisedCurrent: SerialisedType, serialisedPrevious: SerialisedType | null) => MaybeAsync<void>; | ||
export declare type Betterer<TestType, SerialisedType = TestType> = { | ||
test: BettererTest<TestType>; | ||
constraint: BettererConstraint<SerialisedType>; | ||
goal: BettererGoal<SerialisedType>; | ||
diff: BettererDiff<TestType, SerialisedType>; | ||
skip?: boolean; | ||
only?: boolean; | ||
}; | ||
export declare type BettererTests = { | ||
[key: string]: Betterer<unknown, unknown>; | ||
}; | ||
export declare type BettererResult = { | ||
@@ -21,0 +3,0 @@ timestamp: number; |
{ | ||
"name": "@betterer/betterer", | ||
"description": "Main engine for runing betterer tests", | ||
"version": "0.5.0", | ||
"version": "0.5.1", | ||
"license": "MIT", | ||
@@ -36,3 +36,3 @@ "publishConfig": { | ||
}, | ||
"gitHead": "34f5170cce39e8dd684158066243d36b9e27aeb6" | ||
"gitHead": "e979203f3ceafc88eee497e83a3ecd7625051e4d" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
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
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
64305
63
890
0