Skip to content

Commit e02858a

Browse files
authoredJan 27, 2021
fix(plugins): refactor instantiatePlugin from preproprocessor (#3628)
add unit test for plugin.js add comments on role of function and cache
1 parent 8d589ed commit e02858a

File tree

5 files changed

+219
-208
lines changed

5 files changed

+219
-208
lines changed
 

‎lib/plugin.js

+37-1
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,40 @@ function resolve (plugins, emitter) {
5050
return modules
5151
}
5252

53-
exports.resolve = resolve
53+
/**
54+
Create a function to handle errors in plugin loading.
55+
@param {Object} injector, the dict of dependency injection objects.
56+
@return function closed over injector, which reports errors.
57+
*/
58+
function createInstantiatePlugin (injector) {
59+
const emitter = injector.get('emitter')
60+
// Cache to avoid report errors multiple times per plugin.
61+
const pluginInstances = new Map()
62+
return function instantiatePlugin (kind, name) {
63+
if (pluginInstances.has(name)) {
64+
return pluginInstances.get(name)
65+
}
66+
67+
let p
68+
try {
69+
p = injector.get(`${kind}:${name}`)
70+
if (!p) {
71+
log.error(`Failed to instantiate ${kind} ${name}`)
72+
emitter.emit('load_error', kind, name)
73+
}
74+
} catch (e) {
75+
if (e.message.includes(`No provider for "${kind}:${name}"`)) {
76+
log.error(`Cannot load "${name}", it is not registered!\n Perhaps you are missing some plugin?`)
77+
} else {
78+
log.error(`Cannot load "${name}"!\n ` + e.stack)
79+
}
80+
emitter.emit('load_error', kind, name)
81+
}
82+
pluginInstances.set(name, p, `${kind}:${name}`)
83+
return p
84+
}
85+
}
86+
87+
createInstantiatePlugin.$inject = ['injector']
88+
89+
module.exports = { resolve, createInstantiatePlugin }

‎lib/preprocessor.js

+4-32
Original file line numberDiff line numberDiff line change
@@ -70,36 +70,8 @@ async function runProcessors (preprocessors, file, content) {
7070
file.sha = CryptoUtils.sha1(content)
7171
}
7272

73-
function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath, injector) {
74-
const emitter = injector.get('emitter')
75-
const instances = new Map()
76-
77-
function instantiatePreprocessor (name) {
78-
if (instances.has(name)) {
79-
return instances.get(name)
80-
}
81-
82-
let p
83-
try {
84-
p = injector.get('preprocessor:' + name)
85-
if (!p) {
86-
log.error(`Failed to instantiate preprocessor ${name}`)
87-
emitter.emit('load_error', 'preprocessor', name)
88-
}
89-
} catch (e) {
90-
if (e.message.includes(`No provider for "preprocessor:${name}"`)) {
91-
log.error(`Can not load "${name}", it is not registered!\n Perhaps you are missing some plugin?`)
92-
} else {
93-
log.error(`Can not load "${name}"!\n ` + e.stack)
94-
}
95-
emitter.emit('load_error', 'preprocessor', name)
96-
}
97-
98-
instances.set(name, p)
99-
return p
100-
}
101-
_.union.apply(_, Object.values(config)).forEach(instantiatePreprocessor)
102-
73+
function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath, instantiatePlugin) {
74+
_.union.apply(_, Object.values(config)).forEach((name) => instantiatePlugin('preprocessor', name))
10375
return async function preprocess (file) {
10476
const buffer = await tryToRead(file.originalPath, log)
10577
let isBinary = file.isBinary
@@ -121,7 +93,7 @@ function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath
12193
.sort((a, b) => b[1] - a[1])
12294
.map((duo) => duo[0])
12395
.reduce((preProcs, name) => {
124-
const p = instantiatePreprocessor(name)
96+
const p = instantiatePlugin('preprocessor', name)
12597

12698
if (!isBinary || (p && p.handleBinaryFiles)) {
12799
preProcs.push(p)
@@ -135,5 +107,5 @@ function createPriorityPreprocessor (config = {}, preprocessorPriority, basePath
135107
}
136108
}
137109

138-
createPriorityPreprocessor.$inject = ['config.preprocessors', 'config.preprocessor_priority', 'config.basePath', 'injector']
110+
createPriorityPreprocessor.$inject = ['config.preprocessors', 'config.preprocessor_priority', 'config.basePath', 'instantiatePlugin']
139111
exports.createPriorityPreprocessor = createPriorityPreprocessor

‎lib/server.js

+1
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ class Server extends KarmaEventEmitter {
7676
watcher: ['value', watcher],
7777
launcher: ['factory', Launcher.factory],
7878
config: ['value', config],
79+
instantiatePlugin: ['factory', plugin.createInstantiatePlugin],
7980
preprocess: ['factory', preprocessor.createPriorityPreprocessor],
8081
fileList: ['factory', FileList.factory],
8182
webServer: ['factory', createWebServer],

‎test/unit/plugin.spec.js

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
'use strict'
2+
3+
const createInstantiatePlugin = require('../../lib/plugin').createInstantiatePlugin
4+
5+
describe('plugin', () => {
6+
describe('createInstantiatePlugin', () => {
7+
it('creates the instantiatePlugin function', () => {
8+
const fakeGet = sinon.stub()
9+
const fakeInjector = { get: fakeGet }
10+
11+
expect(typeof createInstantiatePlugin(fakeInjector)).to.be.equal('function')
12+
expect(fakeGet).to.have.been.calledWith('emitter')
13+
})
14+
15+
it('creates the instantiatePlugin function', () => {
16+
const fakes = {
17+
emitter: { emit: sinon.stub() }
18+
}
19+
const fakeInjector = { get: (id) => fakes[id] }
20+
21+
const instantiatePlugin = createInstantiatePlugin(fakeInjector)
22+
expect(typeof instantiatePlugin('kind', 'name')).to.be.equal('undefined')
23+
expect(fakes.emitter.emit).to.have.been.calledWith('load_error', 'kind', 'name')
24+
})
25+
26+
it('caches plugins', () => {
27+
const fakes = {
28+
emitter: { emit: sinon.stub() },
29+
'kind:name': { my: 'plugin' }
30+
}
31+
const fakeInjector = {
32+
get: (id) => {
33+
return fakes[id]
34+
}
35+
}
36+
37+
const instantiatePlugin = createInstantiatePlugin(fakeInjector)
38+
expect(instantiatePlugin('kind', 'name')).to.be.equal(fakes['kind:name'])
39+
fakeInjector.get = (id) => { throw new Error('failed to cache') }
40+
expect(instantiatePlugin('kind', 'name')).to.be.equal(fakes['kind:name'])
41+
})
42+
43+
it('errors if the injector errors', () => {
44+
const fakes = {
45+
emitter: { emit: sinon.stub() }
46+
}
47+
const fakeInjector = {
48+
get: (id) => {
49+
if (id in fakes) {
50+
return fakes[id]
51+
}
52+
throw new Error('fail')
53+
}
54+
}
55+
56+
const instantiatePlugin = createInstantiatePlugin(fakeInjector)
57+
expect(typeof instantiatePlugin('unknown', 'name')).to.be.equal('undefined')
58+
expect(fakes.emitter.emit).to.have.been.calledWith('load_error', 'unknown', 'name')
59+
})
60+
})
61+
})

‎test/unit/preprocessor.spec.js

+116-175
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
'use strict'
22

33
const mocks = require('mocks')
4-
const di = require('di')
54
const path = require('path')
65

7-
const events = require('../../lib/events')
8-
96
describe('preprocessor', () => {
107
let m
118
let mockFs
12-
let emitterSetting
139
// mimic first few bytes of a pdf file
1410
const binarydata = Buffer.from([0x25, 0x50, 0x44, 0x66, 0x46, 0x00])
1511

12+
// Each test will define a spy; the fakeInstatiatePlugin will return it.
13+
let fakePreprocessor
14+
15+
const simpleFakeInstantiatePlugin = () => { return fakePreprocessor }
16+
1617
beforeEach(() => {
1718
mockFs = mocks.fs.create({
1819
some: {
@@ -32,22 +33,16 @@ describe('preprocessor', () => {
3233
'graceful-fs': mockFs,
3334
minimatch: require('minimatch')
3435
}
35-
emitterSetting = { emitter: ['value', new events.EventEmitter()] }
3636
m = mocks.loadFile(path.join(__dirname, '/../../lib/preprocessor.js'), mocks_)
3737
})
3838

3939
it('should preprocess matching file', async () => {
40-
const fakePreprocessor = sinon.spy((content, file, done) => {
40+
fakePreprocessor = sinon.spy((content, file, done) => {
4141
file.path = file.path + '-preprocessed'
4242
done(null, 'new-content')
4343
})
4444

45-
const injector = new di.Injector([{
46-
'preprocessor:fake': [
47-
'factory', function () { return fakePreprocessor }
48-
]
49-
}, emitterSetting])
50-
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
45+
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, simpleFakeInstantiatePlugin)
5146

5247
const file = { originalPath: '/some/a.js', path: 'path' }
5348

@@ -58,15 +53,12 @@ describe('preprocessor', () => {
5853
})
5954

6055
it('should match directories starting with a dot', async () => {
61-
const fakePreprocessor = sinon.spy((content, file, done) => {
56+
fakePreprocessor = sinon.spy((content, file, done) => {
6257
file.path = file.path + '-preprocessed'
6358
done(null, 'new-content')
6459
})
6560

66-
const injector = new di.Injector([{
67-
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
68-
}, emitterSetting])
69-
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
61+
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, simpleFakeInstantiatePlugin)
7062

7163
const file = { originalPath: '/some/.dir/a.js', path: 'path' }
7264

@@ -77,15 +69,12 @@ describe('preprocessor', () => {
7769
})
7870

7971
it('should get content if preprocessor is an async function or return Promise with content', async () => {
80-
const fakePreprocessor = sinon.spy(async (content, file, done) => {
72+
fakePreprocessor = sinon.spy(async (content, file, done) => {
8173
file.path = file.path + '-preprocessed'
8274
return 'new-content'
8375
})
8476

85-
const injector = new di.Injector([{
86-
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
87-
}, emitterSetting])
88-
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
77+
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, simpleFakeInstantiatePlugin)
8978

9079
const file = { originalPath: '/some/.dir/a.js', path: 'path' }
9180

@@ -96,15 +85,12 @@ describe('preprocessor', () => {
9685
})
9786

9887
it('should get content if preprocessor is an async function still calling done()', async () => {
99-
const fakePreprocessor = sinon.spy(async (content, file, done) => {
88+
fakePreprocessor = sinon.spy(async (content, file, done) => {
10089
file.path = file.path + '-preprocessed'
10190
done(null, 'new-content')
10291
})
10392

104-
const injector = new di.Injector([{
105-
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
106-
}, emitterSetting])
107-
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
93+
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, simpleFakeInstantiatePlugin)
10894

10995
const file = { originalPath: '/some/.dir/a.js', path: 'path' }
11096

@@ -115,16 +101,13 @@ describe('preprocessor', () => {
115101
})
116102

117103
it('should check patterns after creation when invoked', async () => {
118-
const fakePreprocessor = sinon.spy((content, file, done) => {
104+
fakePreprocessor = sinon.spy((content, file, done) => {
119105
file.path = file.path + '-preprocessed'
120106
done(null, 'new-content')
121107
})
122108

123-
const injector = new di.Injector([{
124-
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
125-
}, emitterSetting])
126109
const config = { '**/*.txt': ['fake'] }
127-
const pp = m.createPriorityPreprocessor(config, {}, null, injector)
110+
const pp = m.createPriorityPreprocessor(config, {}, null, simpleFakeInstantiatePlugin)
128111

129112
const file = { originalPath: '/some/a.js', path: 'path' }
130113

@@ -137,14 +120,11 @@ describe('preprocessor', () => {
137120
})
138121

139122
it('should ignore not matching file', async () => {
140-
const fakePreprocessor = sinon.spy((content, file, done) => {
123+
fakePreprocessor = sinon.spy((content, file, done) => {
141124
done(null, '')
142125
})
143126

144-
const injector = new di.Injector([{
145-
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
146-
}, emitterSetting])
147-
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
127+
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, simpleFakeInstantiatePlugin)
148128

149129
const file = { originalPath: '/some/a.txt', path: 'path' }
150130

@@ -153,34 +133,33 @@ describe('preprocessor', () => {
153133
})
154134

155135
it('should apply all preprocessors', async () => {
156-
const fakePreprocessor1 = sinon.spy((content, file, done) => {
157-
file.path = file.path + '-p1'
158-
done(null, content + '-c1')
159-
})
160-
161-
const fakePreprocessor2 = sinon.spy((content, file, done) => {
162-
file.path = file.path + '-p2'
163-
done(content + '-c2')
164-
})
165-
166-
const injector = new di.Injector([{
167-
'preprocessor:fake1': ['factory', function () { return fakePreprocessor1 }],
168-
'preprocessor:fake2': ['factory', function () { return fakePreprocessor2 }]
169-
}, emitterSetting])
136+
const fakes = {
137+
fake1: sinon.spy((content, file, done) => {
138+
file.path = file.path + '-p1'
139+
done(null, content + '-c1')
140+
}),
141+
fake2: sinon.spy((content, file, done) => {
142+
file.path = file.path + '-p2'
143+
done(content + '-c2')
144+
})
145+
}
146+
function fakeInstatiatePlugin (kind, name) {
147+
return fakes[name]
148+
}
170149

171-
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake1', 'fake2'] }, {}, null, injector)
150+
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake1', 'fake2'] }, {}, null, fakeInstatiatePlugin)
172151

173152
const file = { originalPath: '/some/a.js', path: 'path' }
174153

175154
await pp(file)
176-
expect(fakePreprocessor1).to.have.been.calledOnce
177-
expect(fakePreprocessor2).to.have.been.calledOnce
155+
expect(fakes.fake1).to.have.been.calledOnce
156+
expect(fakes.fake2).to.have.been.calledOnce
178157
expect(file.path).to.equal('path-p1-p2')
179158
expect(file.content).to.equal('content-c1-c2')
180159
})
181160

182161
it('should compute SHA', async () => {
183-
const pp = m.createPriorityPreprocessor({}, {}, null, new di.Injector([emitterSetting]))
162+
const pp = m.createPriorityPreprocessor({}, {}, null, simpleFakeInstantiatePlugin)
184163
const file = { originalPath: '/some/a.js', path: 'path' }
185164

186165
await pp(file)
@@ -198,15 +177,11 @@ describe('preprocessor', () => {
198177
})
199178

200179
it('should compute SHA from content returned by a processor', async () => {
201-
const fakePreprocessor = sinon.spy((content, file, done) => {
180+
fakePreprocessor = sinon.spy((content, file, done) => {
202181
done(null, content + '-processed')
203182
})
204183

205-
const injector = new di.Injector([{
206-
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
207-
}, emitterSetting])
208-
209-
const pp = m.createPriorityPreprocessor({ '**/a.js': ['fake'] }, {}, null, injector)
184+
const pp = m.createPriorityPreprocessor({ '**/a.js': ['fake'] }, {}, null, simpleFakeInstantiatePlugin)
210185

211186
const fileProcess = { originalPath: '/some/a.js', path: 'path' }
212187
const fileSkip = { originalPath: '/some/b.js', path: 'path' }
@@ -221,15 +196,11 @@ describe('preprocessor', () => {
221196
})
222197

223198
it('should return error if any preprocessor fails', () => {
224-
const failingPreprocessor = sinon.spy((content, file, done) => {
199+
fakePreprocessor = sinon.spy((content, file, done) => {
225200
done(new Error('Some error'), null)
226201
})
227202

228-
const injector = new di.Injector([{
229-
'preprocessor:failing': ['factory', function () { return failingPreprocessor }]
230-
}, emitterSetting])
231-
232-
const pp = m.createPriorityPreprocessor({ '**/*.js': ['failing'] }, {}, null, injector)
203+
const pp = m.createPriorityPreprocessor({ '**/*.js': ['failing'] }, {}, null, simpleFakeInstantiatePlugin)
233204

234205
const file = { originalPath: '/some/a.js', path: 'path' }
235206

@@ -241,20 +212,20 @@ describe('preprocessor', () => {
241212
})
242213

243214
it('should stop preprocessing after an error', async () => {
244-
const failingPreprocessor = sinon.spy((content, file, done) => {
245-
done(new Error('Some error'), null)
246-
})
247-
248-
const fakePreprocessor = sinon.spy((content, file, done) => {
249-
done(null, content)
250-
})
215+
const fakes = {
216+
failing: sinon.spy((content, file, done) => {
217+
done(new Error('Some error'), null)
218+
}),
219+
fake: sinon.spy((content, file, done) => {
220+
done(null, content)
221+
})
222+
}
251223

252-
const injector = new di.Injector([{
253-
'preprocessor:failing': ['factory', function () { return failingPreprocessor }],
254-
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
255-
}, emitterSetting])
224+
function fakeInstantiatePlugin (kind, name) {
225+
return fakes[name]
226+
}
256227

257-
const pp = m.createPriorityPreprocessor({ '**/*.js': ['failing', 'fake'] }, {}, null, injector)
228+
const pp = m.createPriorityPreprocessor({ '**/*.js': ['failing', 'fake'] }, {}, null, fakeInstantiatePlugin)
258229

259230
const file = { originalPath: '/some/a.js', path: 'path' }
260231

@@ -263,7 +234,7 @@ describe('preprocessor', () => {
263234
}, (err) => {
264235
expect(err.message).to.equal('Some error')
265236
})
266-
expect(fakePreprocessor).not.to.have.been.called
237+
expect(fakes.fake).not.to.have.been.called
267238
})
268239

269240
describe('when fs.readFile fails', () => {
@@ -274,15 +245,11 @@ describe('preprocessor', () => {
274245
})
275246

276247
it('should retry up to 3 times', async () => {
277-
const fakePreprocessor = sinon.spy((content, file, done) => {
248+
fakePreprocessor = sinon.spy((content, file, done) => {
278249
done(null, content)
279250
})
280251

281-
const injector = new di.Injector([{
282-
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
283-
}, emitterSetting])
284-
285-
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, injector)
252+
const pp = m.createPriorityPreprocessor({ '**/*.js': ['fake'] }, {}, null, simpleFakeInstantiatePlugin)
286253

287254
await pp(file).then(() => {
288255
throw new Error('Should be rejected')
@@ -295,9 +262,7 @@ describe('preprocessor', () => {
295262
})
296263

297264
it('should throw after 3 retries', async () => {
298-
const injector = new di.Injector([{}, emitterSetting])
299-
300-
const pp = m.createPriorityPreprocessor({ '**/*.js': [] }, {}, null, injector)
265+
const pp = m.createPriorityPreprocessor({ '**/*.js': [] }, {}, null, simpleFakeInstantiatePlugin)
301266

302267
await pp(file).then(() => {
303268
throw new Error('Should be rejected')
@@ -309,15 +274,11 @@ describe('preprocessor', () => {
309274
})
310275

311276
it('should not preprocess binary files by default', async () => {
312-
const fakePreprocessor = sinon.spy((content, file, done) => {
277+
fakePreprocessor = sinon.spy((content, file, done) => {
313278
done(null, content)
314279
})
315280

316-
const injector = new di.Injector([{
317-
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
318-
}, emitterSetting])
319-
320-
const pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, injector)
281+
const pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, simpleFakeInstantiatePlugin)
321282

322283
const file = { originalPath: '/some/photo.png', path: 'path' }
323284

@@ -327,15 +288,11 @@ describe('preprocessor', () => {
327288
})
328289

329290
it('should not preprocess files configured to be binary', async () => {
330-
const fakePreprocessor = sinon.spy((content, file, done) => {
291+
fakePreprocessor = sinon.spy((content, file, done) => {
331292
done(null, content)
332293
})
333294

334-
const injector = new di.Injector([{
335-
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
336-
}, emitterSetting])
337-
338-
const pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, injector)
295+
const pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, simpleFakeInstantiatePlugin)
339296

340297
const file = { originalPath: '/some/proto.pb', path: 'path', isBinary: true }
341298

@@ -345,15 +302,11 @@ describe('preprocessor', () => {
345302
})
346303

347304
it('should preprocess files configured not to be binary', async () => {
348-
const fakePreprocessor = sinon.spy((content, file, done) => {
305+
fakePreprocessor = sinon.spy((content, file, done) => {
349306
done(null, content)
350307
})
351308

352-
const injector = new di.Injector([{
353-
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
354-
}, emitterSetting])
355-
356-
const pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, injector)
309+
const pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, simpleFakeInstantiatePlugin)
357310

358311
// Explicit false for isBinary
359312
const file = { originalPath: '/some/proto.pb', path: 'path', isBinary: false }
@@ -364,16 +317,12 @@ describe('preprocessor', () => {
364317
})
365318

366319
it('should preprocess binary files if handleBinaryFiles=true', async () => {
367-
const fakePreprocessor = sinon.spy((content, file, done) => {
320+
fakePreprocessor = sinon.spy((content, file, done) => {
368321
done(null, content)
369322
})
370323
fakePreprocessor.handleBinaryFiles = true
371324

372-
const injector = new di.Injector([{
373-
'preprocessor:fake': ['factory', function () { return fakePreprocessor }]
374-
}, emitterSetting])
375-
376-
const pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, injector)
325+
const pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, simpleFakeInstantiatePlugin)
377326

378327
const file = { originalPath: '/some/photo.png', path: 'path' }
379328

@@ -383,15 +332,11 @@ describe('preprocessor', () => {
383332
})
384333

385334
it('should not preprocess binary files with capital letters in extension', async () => {
386-
const fakePreprocessor = sinon.spy((content, file, done) => {
335+
fakePreprocessor = sinon.spy((content, file, done) => {
387336
done(null, content)
388337
})
389338

390-
const injector = new di.Injector([{
391-
'preprocessor:fake': ['factory', function () { fakePreprocessor }]
392-
}, emitterSetting])
393-
394-
const pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, injector)
339+
const pp = m.createPriorityPreprocessor({ '**/*': ['fake'] }, {}, null, simpleFakeInstantiatePlugin)
395340

396341
const file = { originalPath: '/some/CAM_PHOTO.JPG', path: 'path' }
397342

@@ -402,88 +347,84 @@ describe('preprocessor', () => {
402347

403348
it('should merge lists of preprocessors using default priority', async () => {
404349
const callOrder = []
405-
const fakePreprocessorA = sinon.spy((content, file, done) => {
406-
callOrder.push('a')
407-
done(null, content)
408-
})
409-
const fakePreprocessorB = sinon.spy((content, file, done) => {
410-
callOrder.push('b')
411-
done(null, content)
412-
})
413-
const fakePreprocessorC = sinon.spy((content, file, done) => {
414-
callOrder.push('c')
415-
done(null, content)
416-
})
417-
const fakePreprocessorD = sinon.spy((content, file, done) => {
418-
callOrder.push('d')
419-
done(null, content)
420-
})
421-
422-
const injector = new di.Injector([{
423-
'preprocessor:fakeA': ['factory', function () { return fakePreprocessorA }],
424-
'preprocessor:fakeB': ['factory', function () { return fakePreprocessorB }],
425-
'preprocessor:fakeC': ['factory', function () { return fakePreprocessorC }],
426-
'preprocessor:fakeD': ['factory', function () { return fakePreprocessorD }]
427-
}, emitterSetting])
350+
const fakes = {
351+
fakeA: sinon.spy((content, file, done) => {
352+
callOrder.push('a')
353+
done(null, content)
354+
}),
355+
fakeB: sinon.spy((content, file, done) => {
356+
callOrder.push('b')
357+
done(null, content)
358+
}),
359+
fakeC: sinon.spy((content, file, done) => {
360+
callOrder.push('c')
361+
done(null, content)
362+
}),
363+
fakeD: sinon.spy((content, file, done) => {
364+
callOrder.push('d')
365+
done(null, content)
366+
})
367+
}
368+
function fakeInstantiatePlugin (kind, name) {
369+
return fakes[name]
370+
}
428371

429372
const pp = m.createPriorityPreprocessor({
430373
'/*/a.js': ['fakeA', 'fakeB'],
431374
'/some/*': ['fakeB', 'fakeC'],
432375
'/some/a.js': ['fakeD']
433-
}, {}, null, injector)
376+
}, {}, null, fakeInstantiatePlugin)
434377

435378
const file = { originalPath: '/some/a.js', path: 'path' }
436379

437380
await pp(file)
438-
expect(fakePreprocessorA).to.have.been.called
439-
expect(fakePreprocessorB).to.have.been.called
440-
expect(fakePreprocessorC).to.have.been.called
441-
expect(fakePreprocessorD).to.have.been.called
381+
expect(fakes.fakeA).to.have.been.called
382+
expect(fakes.fakeB).to.have.been.called
383+
expect(fakes.fakeC).to.have.been.called
384+
expect(fakes.fakeD).to.have.been.called
442385

443386
expect(callOrder).to.eql(['a', 'b', 'c', 'd'])
444387
})
445388

446389
it('should merge lists of preprocessors obeying priority', async () => {
447390
const callOrder = []
448-
const fakePreprocessorA = sinon.spy((content, file, done) => {
449-
callOrder.push('a')
450-
done(null, content)
451-
})
452-
const fakePreprocessorB = sinon.spy((content, file, done) => {
453-
callOrder.push('b')
454-
done(null, content)
455-
})
456-
const fakePreprocessorC = sinon.spy((content, file, done) => {
457-
callOrder.push('c')
458-
done(null, content)
459-
})
460-
const fakePreprocessorD = sinon.spy((content, file, done) => {
461-
callOrder.push('d')
462-
done(null, content)
463-
})
464-
465-
const injector = new di.Injector([{
466-
'preprocessor:fakeA': ['factory', function () { return fakePreprocessorA }],
467-
'preprocessor:fakeB': ['factory', function () { return fakePreprocessorB }],
468-
'preprocessor:fakeC': ['factory', function () { return fakePreprocessorC }],
469-
'preprocessor:fakeD': ['factory', function () { return fakePreprocessorD }]
470-
}, emitterSetting])
391+
const fakes = {
392+
fakeA: sinon.spy((content, file, done) => {
393+
callOrder.push('a')
394+
done(null, content)
395+
}),
396+
fakeB: sinon.spy((content, file, done) => {
397+
callOrder.push('b')
398+
done(null, content)
399+
}),
400+
fakeC: sinon.spy((content, file, done) => {
401+
callOrder.push('c')
402+
done(null, content)
403+
}),
404+
fakeD: sinon.spy((content, file, done) => {
405+
callOrder.push('d')
406+
done(null, content)
407+
})
408+
}
409+
function fakeInstantiatePlugin (kind, name) {
410+
return fakes[name]
411+
}
471412

472413
const priority = { fakeA: -1, fakeB: 1, fakeD: 100 }
473414

474415
const pp = m.createPriorityPreprocessor({
475416
'/*/a.js': ['fakeA', 'fakeB'],
476417
'/some/*': ['fakeB', 'fakeC'],
477418
'/some/a.js': ['fakeD']
478-
}, priority, null, injector)
419+
}, priority, null, fakeInstantiatePlugin)
479420

480421
const file = { originalPath: '/some/a.js', path: 'path' }
481422

482423
await pp(file)
483-
expect(fakePreprocessorA).to.have.been.called
484-
expect(fakePreprocessorB).to.have.been.called
485-
expect(fakePreprocessorC).to.have.been.called
486-
expect(fakePreprocessorD).to.have.been.called
424+
expect(fakes.fakeA).to.have.been.called
425+
expect(fakes.fakeB).to.have.been.called
426+
expect(fakes.fakeC).to.have.been.called
427+
expect(fakes.fakeD).to.have.been.called
487428

488429
expect(callOrder).to.eql(['d', 'b', 'c', 'a'])
489430
})

0 commit comments

Comments
 (0)
Please sign in to comment.