Skip to content
This repository was archived by the owner on Oct 16, 2020. It is now read-only.

Commit 16bd116

Browse files
committed
fix: Fix and clean up tests
1 parent 21ac43e commit 16bd116

File tree

2 files changed

+18
-26
lines changed

2 files changed

+18
-26
lines changed

src/plugins.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,11 @@ type RequireResult = { module: {}, error: undefined } | { module: undefined, err
5353

5454
export class PluginLoader {
5555

56-
public allowLocalPluginLoads: boolean = false;
57-
public globalPlugins: string[] = [];
58-
public pluginProbeLocations: string[] = [];
59-
public require: (path: string) => any = require;
56+
private allowLocalPluginLoads: boolean = false;
57+
private globalPlugins: string[] = [];
58+
private pluginProbeLocations: string[] = [];
6059

61-
constructor(private rootFilePath: string, private fs: ts.ModuleResolutionHost, initializationOptions?: InitializationOptions, private logger = new NoopLogger()) {
60+
constructor(private rootFilePath: string, private fs: ts.ModuleResolutionHost, initializationOptions?: InitializationOptions, private logger = new NoopLogger(), private requireModule: (moduleName: string) => any = require) {
6261
if (initializationOptions) {
6362
this.allowLocalPluginLoads = initializationOptions.allowLocalPluginLoads || false;
6463
this.globalPlugins = initializationOptions.globalPlugins || [];
@@ -143,7 +142,7 @@ export class PluginLoader {
143142
private requirePlugin(initialDir: string, moduleName: string): RequireResult {
144143
const modulePath = this.resolveJavaScriptModule(moduleName, initialDir, this.fs);
145144
try {
146-
return { module: this.require(modulePath), error: undefined };
145+
return { module: this.requireModule(modulePath), error: undefined };
147146
} catch (error) {
148147
return { module: undefined, error };
149148
}

src/test/plugins.test.ts

Lines changed: 13 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,13 @@
1-
import * as assert from 'assert';
1+
import * as path from 'path';
22
import * as sinon from 'sinon';
33
import * as ts from 'typescript';
44
import {InMemoryFileSystem} from '../memfs';
55
import {PluginLoader, PluginModule, PluginModuleFactory} from '../plugins';
66
import {InitializationOptions} from '../request-type';
7+
import { path2uri } from '../util';
78

89
describe('plugins', () => {
9-
describe('constructor', () => {
10-
it('should use defaults if no initializationOptions are provided', () => {
11-
const memfs = new InMemoryFileSystem('/');
12-
13-
const loader = new PluginLoader('/', memfs);
14-
assert(!loader.allowLocalPluginLoads);
15-
assert.equal(loader.globalPlugins.length, 0);
16-
assert.equal(loader.pluginProbeLocations.length, 0);
17-
});
18-
});
19-
describe('loader', () => {
10+
describe('loadPlugins()', () => {
2011
it('should do nothing if no plugins are configured', () => {
2112
const memfs = new InMemoryFileSystem('/');
2213

@@ -29,16 +20,18 @@ describe('plugins', () => {
2920

3021
it('should load a global plugin if specified', () => {
3122
const memfs = new InMemoryFileSystem('/');
32-
memfs.add('file:///Users/tomv/Projects/sourcegraph/node_modules/some-plugin/package.json', '{ "name": "some-plugin", "version": "0.1.1", "main": "plugin.js"}');
33-
memfs.add('file:///Users/tomv/Projects/sourcegraph/node_modules/some-plugin/plugin.js', 'module.exports = function (modules) { return 5; };');
23+
const peerPackagesPath = path.resolve(__filename, '../../../../');
24+
const peerPackagesUri = path2uri(peerPackagesPath);
25+
memfs.add(peerPackagesUri + '/node_modules/some-plugin/package.json', '{ "name": "some-plugin", "version": "0.1.1", "main": "plugin.js"}');
26+
memfs.add(peerPackagesUri + '/node_modules/some-plugin/plugin.js', '');
3427
const initializationOptions: InitializationOptions = {
3528
globalPlugins: ['some-plugin'],
3629
allowLocalPluginLoads: false,
3730
pluginProbeLocations: []
3831
};
3932
const pluginFactoryFunc = (modules: any) => 5;
40-
const loader = new PluginLoader('/', memfs, initializationOptions);
41-
loader.require = (path: string) => pluginFactoryFunc;
33+
const fakeRequire = (path: string) => pluginFactoryFunc;
34+
const loader = new PluginLoader('/', memfs, initializationOptions, undefined, fakeRequire);
4235
const compilerOptions: ts.CompilerOptions = {};
4336
const applyProxy = sinon.spy();
4437
loader.loadPlugins(compilerOptions, applyProxy);
@@ -49,15 +42,15 @@ describe('plugins', () => {
4942
it('should load a local plugin if specified', () => {
5043
const memfs = new InMemoryFileSystem('/some-project');
5144
memfs.add('file:///some-project/node_modules/some-plugin/package.json', '{ "name": "some-plugin", "version": "0.1.1", "main": "plugin.js"}');
52-
memfs.add('file:///some-project/node_modules/some-plugin/plugin.js', 'module.exports = function (modules) { return 5; };');
45+
memfs.add('file:///some-project/node_modules/some-plugin/plugin.js', '');
5346
const initializationOptions: InitializationOptions = {
5447
globalPlugins: [],
5548
allowLocalPluginLoads: true,
5649
pluginProbeLocations: []
5750
};
5851
const pluginFactoryFunc = (modules: any) => 5;
59-
const loader = new PluginLoader('/some-project', memfs, initializationOptions);
60-
loader.require = (path: string) => pluginFactoryFunc;
52+
const fakeRequire = (path: string) => pluginFactoryFunc;
53+
const loader = new PluginLoader('/some-project', memfs, initializationOptions, undefined, fakeRequire);
6154
const pluginOption: ts.PluginImport = {
6255
name: 'some-plugin'
6356
};
@@ -67,7 +60,7 @@ describe('plugins', () => {
6760
const applyProxy = sinon.spy();
6861
loader.loadPlugins(compilerOptions, applyProxy);
6962
sinon.assert.calledOnce(applyProxy);
70-
sinon.assert.calledWithExactly(applyProxy, pluginFactoryFunc, sinon.match({ name: 'some-plugin'}));
63+
sinon.assert.calledWithExactly(applyProxy, pluginFactoryFunc, sinon.match(pluginOption));
7164
});
7265

7366
});

0 commit comments

Comments
 (0)