Skip to content

Commit

Permalink
Revert "chore: test context"
Browse files Browse the repository at this point in the history
This reverts commit 905a02b.
  • Loading branch information
slimee committed Oct 24, 2024
1 parent 0d39ca7 commit 6f4374d
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 40 deletions.
25 changes: 7 additions & 18 deletions src/context.js
Original file line number Diff line number Diff line change
@@ -1,28 +1,15 @@
const Metadata = require('./metadata');

module.exports = class Context {
constructor({ debugMode = false } = {}) {
constructor() {
this._bag = {};
this._bag.assertPresent = this._makeAssertPresent(this._bag).bind(this);
this._metadata = new Metadata();
this._missings = {};
this._debugMode = debugMode;
}

_addMissing(path, keys) {
this._missings[path] = keys;
}

_hasMissing() {
return Object.keys(this._missings).length > 0;
}

seal() {
this.flushPrivates('');
this._metadata.seal();
if (this._debugMode && this._hasMissing()) {
throw new Error(`Missing dependencies: ${JSON.stringify(this._missings)}`);
}
}

get() { return this._bag; }
Expand All @@ -37,10 +24,12 @@ module.exports = class Context {
const keys = Object.keys(requisites);
const missingKeys = keys
.filter((key) => !Object.prototype.hasOwnProperty.call(bag, key));
if (missingKeys.length > 0) {
if (this._debugMode) this._addMissing(this._metadata.getCurrentPath(), missingKeys);
else throw new Error(`Asserting dependencies on path "${this._metadata.getCurrentPath()}": Missing dependencies: "${missingKeys}"`);
}
if (missingKeys.length > 0) throw new Error(`Asserting dependencies on path "${this._metadata.getCurrentPath()}": Missing dependencies: "${missingKeys}"`);

const undefinedKeys = keys
.filter((key) => bag[key] === undefined);

if (undefinedKeys.length > 0) throw new Error(`Asserting dependencies on path "${this._metadata.getCurrentPath()}": Undefined dependencies: "${undefinedKeys}"`);

this._metadata.setRequisites(keys);
return true;
Expand Down
4 changes: 1 addition & 3 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,7 @@ export interface PlanEntry<T = any> {
options?: EntryOptions;
}

declare class Context {
constructor({ debugMode }: { debugMode: boolean });
}
declare class Context {}

declare class Plan {
constructor(entries: PlanEntry[], verbose?: boolean);
Expand Down
7 changes: 2 additions & 5 deletions src/plan.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const Context = require('./context');

const METADATA_HOOK = 'metadata-hook';

class Plan {
module.exports = class Plan {
constructor(_entries = [], verbose = false) {
this._entries = _entries;
this._stepsWalk = [];
Expand Down Expand Up @@ -400,7 +400,4 @@ class Plan {
this._addEntry(Symbol(METADATA_HOOK), METADATA_HOOK, hook);
return this;
}
}

module.exports = Plan;
module.exports.Context = Context;
};
14 changes: 0 additions & 14 deletions test/integration/plan.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1143,18 +1143,4 @@ describe('Plan', () => {
expect(entries).toStrictEqual(expectedEntries);
});
});

it('override assertPresent to list dependencies', () => {
const factory = ({ assertPresent, phantom }) => {
assertPresent({ phantom });
return 'toto';
};
const plan = (p) => p
.addUsingFunction('miss1', factory)
.addUsingFunction('miss2', factory)
.addValue('phantom', 'phantom')
.addUsingFunction('ok', factory);

expect(() => execute(plan, new Context({ debugMode: true }))).toThrow('Missing dependencies: {"/miss1":["phantom"],"/miss2":["phantom"]}');
});
});

0 comments on commit 6f4374d

Please sign in to comment.