Skip to content

Commit

Permalink
Chore: Add new location to load resources
Browse files Browse the repository at this point in the history
The new location in `resource-loader` enables testing in the official
rule template¹ using the same infrastructure than the core rules.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

¹ https://github.com/sonarwhal/sonarwhal-rule-template

Ref #528
Close #535
  • Loading branch information
Anton Molleda authored and alrra committed Sep 23, 2017
1 parent 64cceed commit e8faaa1
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/lib/utils/resource-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,9 @@ export const loadResource = (name: string, type: string) => {
const sources: Array<string> = [
path.normalize(`${PROJECT_ROOT}/dist/src/lib/${type}s/${name}/${name}.js`),
`@sonarwhal/${name}`,
`sonarwhal-${name}`
`sonarwhal-${name}`,
// This is needed to test an external rule using the official template
path.normalize(`${process.cwd()}/dist/src/${name}.js`)
];

let resource: any;
Expand Down
4 changes: 3 additions & 1 deletion tests/lib/utils/resource-loader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,16 @@ test('loadResource looks for resources in the right order (core > @sonarwhal > s
tryToLoadFromStub.onFirstCall().returns(null);
tryToLoadFromStub.onSecondCall().returns(null);
tryToLoadFromStub.onThirdCall().returns(null);
tryToLoadFromStub.onCall(3).returns(null);

t.throws(() => {
resourceLoader.loadResource(resourceName, resourceType);
});
t.true(tryToLoadFromStub.calledThrice, 'tryToLoadFromStub is called thrice');
t.true(tryToLoadFromStub.callCount === 4, 'tryToLoadFromStub is called four times');
t.true((tryToLoadFromStub.firstCall.args[0] as string).endsWith(path.normalize(`/dist/src/lib/${resourceType}s/${resourceName}/${resourceName}.js`)), 'Tries to load core first');
t.true((tryToLoadFromStub.secondCall.args[0] as string).endsWith(`@sonarwhal/${resourceName}`), 'Tries to load scoped package second');
t.true((tryToLoadFromStub.thirdCall.args[0] as string).endsWith(`sonarwhal-${resourceName}`), 'Tries to load prefixed package third');
t.true((tryToLoadFromStub.lastCall.args[0] as string).endsWith(path.normalize(`/dist/src/${resourceName}.js`)), 'Tries to load local testing fourth');

tryToLoadFromStub.restore();
});
Expand Down

0 comments on commit e8faaa1

Please sign in to comment.