UNPKG

5 kBJavaScriptView Raw
1import path from 'path'
2import chai from 'chai'
3
4import fs from 'fs'
5
6import require_hacker from '../source/index'
7
8import Log from '../source/tools/log'
9
10chai.should()
11
12// logging
13const log = new Log('testing', { debug: true })
14
15describe('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 // mount require() hook
28 const hook = require_hacker.hook('js', path =>
29 {
30 return `module.exports = "${fs.readFileSync(path).toString()}"`
31 })
32
33 // unmount require() hook
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 // mount require() hook
43 const hook = test_hook()
44
45 // verify that it guards the file extension
46 test_hook.should.throw('occupied')
47
48 // verify that it guards the file extension
49 test_global_hook.should.throw('occupied')
50
51 // unmount require() hook
52 hook.unmount()
53
54 // mount a global require() hook
55 const global_hook = test_global_hook()
56
57 // verify that it guards the file extension
58 test_hook.should.throw('occupied')
59
60 // verify that it guards the file extension
61 test_global_hook.should.throw('occupied')
62
63 // unmount the global require() hook
64 global_hook.unmount()
65 })
66
67 it('should hook into file extension loading', function()
68 {
69 // mount require() hook
70 const hook = require_hacker.hook('txt', path =>
71 {
72 return `module.exports = "${fs.readFileSync(path).toString()}"`
73 })
74
75 // will output text file contents
76 require('./test.txt').should.equal('Hot threesome interracial with double penetration')
77
78 // unmount require() hook
79 hook.unmount()
80
81 // will throw "SyntaxError: Unexpected token ILLEGAL"
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 // mount require() hook
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 // will output text file contents
98 require('http://xhamster.com').should.equal('Free porn')
99
100 // unmount require() hook
101 hook.unmount()
102
103 // will throw "Error: Cannot find module"
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 // mount require() hook
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 // will output text file contents
120 require('./dummy.js').should.equal('Free porn')
121
122 // unmount require() hook
123 hook.unmount()
124
125 // usual Node.js loader takes precedence
126 require('./dummy.js').should.equal('Hot lesbians making out')
127 // clear require() cache (just in case)
128 delete require.cache[path.resolve(__dirname, './dummy.js')]
129
130 // mount require() hook
131 const ignoring_hook = require_hacker.global_hook('javascript', path =>
132 {
133 return
134 })
135
136 // usual Node.js loader takes precedence
137 require('./dummy.js').should.equal('Hot lesbians making out')
138 // clear require() cache (just in case)
139 delete require.cache[path.resolve(__dirname, './dummy.js')]
140
141 // unmount require() hook
142 ignoring_hook.unmount()
143 })
144
145 it('should validate options', function()
146 {
147 // mount require() hook
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 // mount require() hook
166 const hook = require_hacker.hook('js', path =>
167 {
168 return
169 })
170
171 // will output text file contents
172 require('./dummy.js').should.equal('Hot lesbians making out')
173
174 // unmount require() hook
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