forked from atom/atom
-
Notifications
You must be signed in to change notification settings - Fork 0
/
module-cache-spec.coffee
93 lines (82 loc) · 3 KB
/
module-cache-spec.coffee
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
path = require 'path'
Module = require 'module'
fs = require 'fs-plus'
temp = require 'temp'
ModuleCache = require '../src/module-cache'
describe 'ModuleCache', ->
beforeEach ->
spyOn(Module, '_findPath').andCallThrough()
it 'resolves Electron module paths without hitting the filesystem', ->
builtins = ModuleCache.cache.builtins
expect(Object.keys(builtins).length).toBeGreaterThan 0
for builtinName, builtinPath of builtins
expect(require.resolve(builtinName)).toBe builtinPath
expect(fs.isFileSync(require.resolve(builtinName))).toBeTruthy()
expect(Module._findPath.callCount).toBe 0
it 'resolves relative core paths without hitting the filesystem', ->
ModuleCache.add atom.getLoadSettings().resourcePath, {
_atomModuleCache:
extensions:
'.json': [
path.join('spec', 'fixtures', 'module-cache', 'file.json')
]
}
expect(require('./fixtures/module-cache/file.json').foo).toBe 'bar'
expect(Module._findPath.callCount).toBe 0
it 'resolves module paths when a compatible version is provided by core', ->
packagePath = fs.realpathSync(temp.mkdirSync('atom-package'))
ModuleCache.add packagePath, {
_atomModuleCache:
folders: [{
paths: [
''
]
dependencies:
'underscore-plus': '*'
}]
}
ModuleCache.add atom.getLoadSettings().resourcePath, {
_atomModuleCache:
dependencies: [{
name: 'underscore-plus'
version: require('underscore-plus/package.json').version
path: path.join('node_modules', 'underscore-plus', 'lib', 'underscore-plus.js')
}]
}
indexPath = path.join(packagePath, 'index.js')
fs.writeFileSync indexPath, """
exports.load = function() { require('underscore-plus'); };
"""
packageMain = require(indexPath)
Module._findPath.reset()
packageMain.load()
expect(Module._findPath.callCount).toBe 0
it 'does not resolve module paths when no compatible version is provided by core', ->
packagePath = fs.realpathSync(temp.mkdirSync('atom-package'))
ModuleCache.add packagePath, {
_atomModuleCache:
folders: [{
paths: [
''
]
dependencies:
'underscore-plus': '0.0.1'
}]
}
ModuleCache.add atom.getLoadSettings().resourcePath, {
_atomModuleCache:
dependencies: [{
name: 'underscore-plus'
version: require('underscore-plus/package.json').version
path: path.join('node_modules', 'underscore-plus', 'lib', 'underscore-plus.js')
}]
}
indexPath = path.join(packagePath, 'index.js')
fs.writeFileSync indexPath, """
exports.load = function() { require('underscore-plus'); };
"""
spyOn(process, 'cwd').andReturn('/') # Required when running this test from CLI
packageMain = require(indexPath)
Module._findPath.reset()
expect(-> packageMain.load()).toThrow()
expect(Module._findPath.callCount).toBe 1