1 | import path from 'path'
|
2 | import chai from 'chai'
|
3 |
|
4 | import fs from 'fs'
|
5 |
|
6 | import require_hacker from '../source/index'
|
7 |
|
8 | import Log from '../source/tools/log'
|
9 |
|
10 | chai.should()
|
11 |
|
12 |
|
13 | const log = new Log('testing', { debug: true })
|
14 |
|
15 | describe('require hacker', function()
|
16 | {
|
17 | beforeEach(function()
|
18 | {
|
19 | })
|
20 |
|
21 | after(function()
|
22 | {
|
23 | })
|
24 |
|
25 | it('should hook into js extension loading', function()
|
26 | {
|
27 |
|
28 | const hook = require_hacker.hook('js', path =>
|
29 | {
|
30 | return `module.exports = "${fs.readFileSync(path).toString()}"`
|
31 | })
|
32 |
|
33 |
|
34 | hook.unmount()
|
35 | })
|
36 |
|
37 | it('shouldn\'t allow already occupied file extension override', function()
|
38 | {
|
39 | const test_hook = () => require_hacker.hook('test', path => {})
|
40 | const test_global_hook = () => require_hacker.global_hook('test', path => {})
|
41 |
|
42 |
|
43 | const hook = test_hook()
|
44 |
|
45 |
|
46 | test_hook.should.throw('occupied')
|
47 |
|
48 |
|
49 | test_global_hook.should.throw('occupied')
|
50 |
|
51 |
|
52 | hook.unmount()
|
53 |
|
54 |
|
55 | const global_hook = test_global_hook()
|
56 |
|
57 |
|
58 | test_hook.should.throw('occupied')
|
59 |
|
60 |
|
61 | test_global_hook.should.throw('occupied')
|
62 |
|
63 |
|
64 | global_hook.unmount()
|
65 | })
|
66 |
|
67 | it('should hook into file extension loading', function()
|
68 | {
|
69 |
|
70 | const hook = require_hacker.hook('txt', path =>
|
71 | {
|
72 | return `module.exports = "${fs.readFileSync(path).toString()}"`
|
73 | })
|
74 |
|
75 |
|
76 | require('./test.txt').should.equal('Hot threesome interracial with double penetration')
|
77 |
|
78 |
|
79 | hook.unmount()
|
80 |
|
81 |
|
82 | const would_fail = () => require('./another test.txt')
|
83 | would_fail.should.throw(SyntaxError)
|
84 | })
|
85 |
|
86 | it('should hook into arbitrary path loading', function()
|
87 | {
|
88 |
|
89 | const hook = require_hacker.global_hook('textual', path =>
|
90 | {
|
91 | if (path.indexOf('http://xhamster.com') >= 0)
|
92 | {
|
93 | return `module.exports = "Free porn"`
|
94 | }
|
95 | })
|
96 |
|
97 |
|
98 | require('http://xhamster.com').should.equal('Free porn')
|
99 |
|
100 |
|
101 | hook.unmount()
|
102 |
|
103 |
|
104 | const would_fail = () => require('http://xhamster.com')
|
105 | would_fail.should.throw('Cannot find module')
|
106 | })
|
107 |
|
108 | it('should hook into arbitrary path loading (preceding Node.js original loader)', function()
|
109 | {
|
110 |
|
111 | const hook = require_hacker.global_hook('javascript', path =>
|
112 | {
|
113 | if (path.indexOf('/dummy.js') >= 0)
|
114 | {
|
115 | return `module.exports = "Free porn"`
|
116 | }
|
117 | })
|
118 |
|
119 |
|
120 | require('./dummy.js').should.equal('Free porn')
|
121 |
|
122 |
|
123 | hook.unmount()
|
124 |
|
125 |
|
126 | require('./dummy.js').should.equal('Hot lesbians making out')
|
127 |
|
128 | delete require.cache[path.resolve(__dirname, './dummy.js')]
|
129 |
|
130 |
|
131 | const ignoring_hook = require_hacker.global_hook('javascript', path =>
|
132 | {
|
133 | return
|
134 | })
|
135 |
|
136 |
|
137 | require('./dummy.js').should.equal('Hot lesbians making out')
|
138 |
|
139 | delete require.cache[path.resolve(__dirname, './dummy.js')]
|
140 |
|
141 |
|
142 | ignoring_hook.unmount()
|
143 | })
|
144 |
|
145 | it('should validate options', function()
|
146 | {
|
147 |
|
148 | const hook = (id, resolve) => () => require_hacker.global_hook(id, resolve)
|
149 |
|
150 | hook().should.throw('You must specify global hook id')
|
151 |
|
152 | hook('.js').should.throw('Invalid global hook id')
|
153 |
|
154 | hook('js').should.throw('Resolve should be a function')
|
155 |
|
156 | hook('js', true).should.throw('Resolve should be a function')
|
157 |
|
158 | const hook_extension = (extension, handler) => () => require_hacker.hook(extension, handler)
|
159 |
|
160 | hook_extension('.js').should.throw('Invalid file extension')
|
161 | })
|
162 |
|
163 | it('should fall back', function()
|
164 | {
|
165 |
|
166 | const hook = require_hacker.hook('js', path =>
|
167 | {
|
168 | return
|
169 | })
|
170 |
|
171 |
|
172 | require('./dummy.js').should.equal('Hot lesbians making out')
|
173 |
|
174 |
|
175 | hook.unmount()
|
176 | })
|
177 |
|
178 | it('should convert to javascript module source', function()
|
179 | {
|
180 | require_hacker.to_javascript_module_source().should.equal('module.exports = undefined')
|
181 | require_hacker.to_javascript_module_source('a').should.equal('module.exports = "a"')
|
182 | require_hacker.to_javascript_module_source('module.exports = "a"').should.equal('module.exports = "a"')
|
183 | require_hacker.to_javascript_module_source({ a: 1 }).should.equal('module.exports = {"a":1}')
|
184 | })
|
185 | }) |
\ | No newline at end of file |