Skip to content

Commit

Permalink
feat(*): [RO-18670] add ability to ignore dependencies (#78)
Browse files Browse the repository at this point in the history
* feat(*): RO-18670 Extend spur-ioc to add the ability to skip dependency registration
add support for skipping dependency injection by setting

* feat(*): RO-18670 Extend spur-ioc to add the ability to skip dependency registration
improve logging output when warning against dependency registration collisions

* feat(*): RO-18670 Extend spur-ioc to add the ability to skip dependency registration
fix passing incorrect parameters
improve logging

* feat(*): RO-18670 Extend spur-ioc to add the ability to skip dependency registration
add warning for ignored dependencies

* feat(*): RO-18670 Extend spur-ioc to add the ability to skip dependency registration
add test cases for registerFolders and addResolvableDependency

* feat(*): RO-18670 Extend spur-ioc to add the ability to skip dependency registration
add test cases for addDependency

* feat(*): RO-18670 Extend spur-ioc to add the ability to skip dependency registration
update FileFilterExpression to ignore .d.ts files
add test

* feat(*): RO-18670 Extend spur-ioc to add the ability to skip dependency registration
fix file filter causing issues with some imports
update logging to print primitive types

* feat(*): RO-18670 Extend spur-ioc to add the ability to skip dependency registration
added missing lodash.get dependency
  • Loading branch information
terry-au authored May 17, 2023
1 parent 23411f1 commit d72cf6c
Show file tree
Hide file tree
Showing 11 changed files with 185 additions and 10 deletions.
7 changes: 3 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
"lodash.bindall": "^4.4.0",
"lodash.compact": "^3.0.1",
"lodash.foreach": "^4.5.0",
"lodash.get": "^4.4.2",
"lodash.isfunction": "^3.0.9",
"lodash.isobject": "^3.0.2",
"lodash.keys": "^4.2.0",
Expand Down
53 changes: 48 additions & 5 deletions src/ContainerManagement.js
Original file line number Diff line number Diff line change
@@ -1,35 +1,74 @@
const _forEach = require('lodash.foreach');
const _get = require('lodash.get');
const _isFunction = require('lodash.isfunction');
const _isObject = require('lodash.isobject');
const Dependency = require('./Dependency');

const rall = /.+/;

module.exports = {

warnIfNeeded(name) {
getDependencySourceHint(dependency) {
if (_isFunction(dependency)) {
return _get(dependency, 'name') || '<anonymous function>';
}

if (_isObject(dependency)) {
return _get(dependency, 'constructor.name') || '<object>';
}

return `<${typeof dependency}>`;
},

warnOverrideIfNeeded(name, dependency) {
if (this.hasDependency(name)) {
this.logger.warn(`warning: ${name} dependency is being overwritten in ${this.name} injector`);
const hint = this.getDependencySourceHint(dependency);
this.logger.warn(`warning: ${name} (${hint}) dependency is being overwritten in ${this.name} injector`);
}
},

warnIgnoredDependency(name, dependency) {
const hint = this.getDependencySourceHint(dependency);
this.logger.warn(`warning: ignoring ${name} (${hint}) dependency in ${this.name} injector`);
},

addResolvableDependency(name, dependency, suppressWarning = false) {
if (this.shouldIgnoreDependency(dependency)) {
if (!suppressWarning) {
this.warnIgnoredDependency(name, dependency);
}
return this;
}
if (!suppressWarning) {
this.warnIfNeeded(name);
this.warnOverrideIfNeeded(name, dependency);
}
this.dependencies[name] = Dependency.resolvableDependency(name, dependency);
return this;
},

addDependency(name, dependency, suppressWarning = false) {
if (this.shouldIgnoreDependency(dependency)) {
if (!suppressWarning) {
this.warnIgnoredDependency(name, dependency);
}
return this;
}
if (!suppressWarning) {
this.warnIfNeeded(name);
this.warnOverrideIfNeeded(name, dependency);
}
this.dependencies[name] = Dependency.dependency(name, dependency);
return this;
},

addConstructedDependency(name, dependency, suppressWarning = false) {
if (this.shouldIgnoreDependency(dependency)) {
return this;
}
if (!suppressWarning) {
this.warnIfNeeded(name);
if (!suppressWarning) {
this.warnIgnoredDependency(name, dependency);
}
this.warnOverrideIfNeeded(name, dependency);
}
this.dependencies[name] = dependency;
return this;
Expand All @@ -48,6 +87,10 @@ module.exports = {
return !!this.dependencies[name];
},

shouldIgnoreDependency(dependency) {
return Boolean(_get(dependency, 'spurIocIgnore', false));
},

merge(otherInjector, suppressWarning = false) {
const dependencies = otherInjector.dependencies;

Expand Down
2 changes: 1 addition & 1 deletion src/FileFilterExpression.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ TypeScript is not officially supported, but will be available through a
beta/release candidate version.
*/

const expression = /(.+)\.(js|ts|json)$/;
const expression = /^(?!.*\.d\.ts$)([^\.].*)\.(js|json|ts)?$$/;

module.exports = expression;
3 changes: 3 additions & 0 deletions test/fixtures/Fence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
return 'Fence';
};
3 changes: 3 additions & 0 deletions test/fixtures/Wall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = function () {
return 'Wall';
};
5 changes: 5 additions & 0 deletions test/fixtures/sub/Fence.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function () {
return 'Fence ignored';
};

module.exports.spurIocIgnore = true;
5 changes: 5 additions & 0 deletions test/fixtures/sub/Wall.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.exports = function () {
return 'Wall override';
};

module.exports.spurIocIgnore = false;
34 changes: 34 additions & 0 deletions test/unit/ContainerSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,38 @@ describe('injector - Container Management', () => {
}))
.to.throw('Resolver encountered errors');
});

describe('ignored dependencies', () => {
const initialDep = function () {
return 'initial';
};

const ignoredDep = function() {
throw new Error('this should not be called');
return 'ignored';
};
ignoredDep.spurIocIgnore = true;

const replacementDep = function () {
return 'updated';
};

it('ignores registered dependencies which have spurIocIgnore=true', function () {
this.injector.addDependency('a', initialDep);
this.injector.addDependency('a', ignoredDep);

const dep = this.injector.getDependency('a');
expect(dep.name).to.deep.equal('a');
expect(dep.instance).to.be.eq(initialDep);
});

it('overrides registered dependencies which have spurIocIgnore=false', function() {
this.injector.addDependency('a', initialDep);
this.injector.addDependency('a', replacementDep);

const dep = this.injector.getDependency('a');
expect(dep.name).to.equal('a');
expect(dep.instance).to.be.eq(replacementDep);
});
});
});
8 changes: 8 additions & 0 deletions test/unit/FileFilterExpressionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ describe('FileFilterExpression tests', function () {
expect(subject.test(path2)).to.be.true;
});

it('should match a d.ts file path', () => {
const path1 = 'src/some/path/FileFilterExpression.d.ts';
const path2 = 'src\\somepath\\FileFilterExpression.d.ts';

expect(subject.test(path1)).to.be.false;
expect(subject.test(path2)).to.be.false;
});

it('should not match partial matches', () => {
const path1 = 'src/some/path/FileFilterExpression.js2';
const path2 = 'src/some/path/FileFilterExpression.json2';
Expand Down
74 changes: 74 additions & 0 deletions test/unit/InjectorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,80 @@ describe('Injector', () => {
this.injector.inject(function (_, chai, $injector, foo) {});
});

describe('ignored dependencies', () => {
const initialDep = function () {
return 'initial';
};

const ignoredDep = function() {
throw new Error('this should not be called');
return 'ignored';
};
ignoredDep.spurIocIgnore = true;

const replacementDep = function () {
return 'updated';
};

describe('spurIocIgnore=true', () => {
it('ignores registered dependencies which have spurIocIgnore=true', function () {
this.injector.registerDependencies({ a: initialDep, });
this.injector.registerDependencies({ a: ignoredDep });

this.injector.inject(function (a) {
expect(a).to.deep.equal(initialDep);
expect(a()).to.be.eq('initial');
});
});

it('ignores registered folder dependencies which have spurIocIgnore=true', function() {
this.injector.registerFolders(__dirname, ['../fixtures']);

this.injector.inject(function (Wall) {
expect(Wall).to.equal('Wall override');
});
});

it('ignores resolvable dependencies which have spurIocIgnore=true', function() {
this.injector.addResolvableDependency('b', initialDep);
this.injector.addResolvableDependency('b', ignoredDep);

this.injector.inject(function ($injector) {
expect($injector.get('b')).to.equal('initial');
});
});
});

describe('explicitly marked not to be ignored', () => {
it('overrides registered dependencies which have spurIocIgnore=false', function() {
this.injector.registerDependencies({ a: initialDep, });
this.injector.registerDependencies({ a: replacementDep });

this.injector.inject(function (a) {
expect(a).to.equal(replacementDep);
expect(a()).to.be.eq('updated');
});
});

it('overrides registered folder dependencies which have spurIocIgnore=false', function() {
this.injector.registerFolders(__dirname, ['../fixtures']);

this.injector.inject(function (Wall) {
expect(Wall).to.equal('Wall override');
});
});

it('overrides resolvable dependencies which have spurIocIgnore=false', function() {
this.injector.addResolvableDependency('b', initialDep);
this.injector.addResolvableDependency('b', replacementDep);

this.injector.inject(function ($injector) {
expect($injector.get('b')).to.equal('updated');
});
});
});
});

describe('expose and link', () => {
beforeEach(function () {
this.injector.registerDependencies({
Expand Down

0 comments on commit d72cf6c

Please sign in to comment.