UNPKG

4.95 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 const require_hacker = new Require_hacker({ debug: false })
28
29 // mount require() hook
30 const hook = require_hacker.hook('js', (path, fallback) =>
31 {
32 return `module.exports = "${fs.readFileSync(path).toString()}"`
33 })
34
35 // unmount require() hook
36 hook.unmount()
37 })
38
39 it('should hook into file extension loading', function()
40 {
41 const require_hacker = new Require_hacker({ debug: false })
42
43 // mount require() hook
44 const hook = require_hacker.hook('txt', (path, fallback) =>
45 {
46 return `module.exports = "${fs.readFileSync(path).toString()}"`
47 })
48
49 // mount overriding require() hook
50 const overriding_hook = require_hacker.hook('txt', (path, fallback) =>
51 {
52 return `module.exports = "whatever"`
53 })
54
55 // unmount overriding require() hook
56 overriding_hook.unmount()
57
58 // will output text file contents
59 require('./test.txt').should.equal('Hot threesome interracial with double penetration')
60
61 // unmount require() hook
62 hook.unmount()
63
64 // will throw "SyntaxError: Unexpected token ILLEGAL"
65 const would_fail = () => require('./another test.txt')
66 would_fail.should.throw(SyntaxError)
67 })
68
69 it('should hook into arbitrary path loading', function()
70 {
71 const require_hacker = new Require_hacker({ debug: false })
72
73 // mount require() hook
74 const hook = require_hacker.global_hook('textual', (path, flush_cache) =>
75 {
76 if (path.indexOf('http://xhamster.com') >= 0)
77 {
78 return `module.exports = "Free porn"`
79 }
80 })
81
82 // will output text file contents
83 require('http://xhamster.com').should.equal('Free porn')
84
85 // unmount require() hook
86 hook.unmount()
87
88 // will throw "Error: Cannot find module"
89 const would_fail = () => require('http://xhamster.com')
90 would_fail.should.throw('Cannot find module')
91 })
92
93 it('should hook into arbitrary path loading (preceding Node.js original loader)', function()
94 {
95 const require_hacker = new Require_hacker({ debug: false })
96
97 // mount require() hook
98 const hook = require_hacker.global_hook('javascript', (path, flush_cache) =>
99 {
100 if (path.indexOf('/dummy.js') >= 0)
101 {
102 return `module.exports = "Free porn"`
103 }
104 },
105 { precede_node_loader: true })
106
107 // will output text file contents
108 require('./dummy.js').should.equal('Free porn')
109
110 // unmount require() hook
111 hook.unmount()
112
113 // usual Node.js loader takes precedence
114 require('./dummy.js').should.equal('Hot lesbians making out')
115 // clear require() cache (just in case)
116 delete require.cache[path.resolve(__dirname, './dummy.js')]
117
118 // mount require() hook
119 const ignoring_hook = require_hacker.global_hook('javascript', (path, flush_cache) =>
120 {
121 return
122 },
123 { precede_node_loader: true })
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 // unmount require() hook
131 ignoring_hook.unmount()
132 })
133
134 it('should validate options', function()
135 {
136 const require_hacker = new Require_hacker({ debug: false })
137
138 // mount require() hook
139 const hook = (id, resolve) => () => require_hacker.global_hook(id, resolve)
140
141 hook().should.throw('You must specify global hook id')
142
143 hook('.js').should.throw('Invalid global hook id')
144
145 hook('js').should.throw('Resolve should be a function')
146
147 hook('js', true).should.throw('Resolve should be a function')
148
149 const hook_extension = (extension, handler) => () => require_hacker.hook(extension, handler)
150
151 hook_extension('.js').should.throw('Invalid file extension')
152 })
153
154 it('should fall back', function()
155 {
156 const require_hacker = new Require_hacker({ debug: false })
157
158 // mount require() hook
159 const hook = require_hacker.hook('js', (path, fallback) =>
160 {
161 fallback()
162 return 'whatever'
163 })
164
165 // will output text file contents
166 require('./dummy.js').should.equal('Hot lesbians making out')
167
168 // unmount require() hook
169 hook.unmount()
170 })
171
172 it('should convert to javascript module source', function()
173 {
174 Require_hacker.to_javascript_module_source().should.equal('module.exports = undefined')
175 Require_hacker.to_javascript_module_source('a').should.equal('module.exports = "a"')
176 Require_hacker.to_javascript_module_source('module.exports = "a"').should.equal('module.exports = "a"')
177 Require_hacker.to_javascript_module_source({ a: 1 }).should.equal('module.exports = {"a":1}')
178 })
179})
\No newline at end of file