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 | const require_hacker = new Require_hacker({ debug: false })
|
28 |
|
29 |
|
30 | const hook = require_hacker.hook('js', (path, fallback) =>
|
31 | {
|
32 | return `module.exports = "${fs.readFileSync(path).toString()}"`
|
33 | })
|
34 |
|
35 |
|
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 |
|
44 | const hook = require_hacker.hook('txt', (path, fallback) =>
|
45 | {
|
46 | return `module.exports = "${fs.readFileSync(path).toString()}"`
|
47 | })
|
48 |
|
49 |
|
50 | const overriding_hook = require_hacker.hook('txt', (path, fallback) =>
|
51 | {
|
52 | return `module.exports = "whatever"`
|
53 | })
|
54 |
|
55 |
|
56 | overriding_hook.unmount()
|
57 |
|
58 |
|
59 | require('./test.txt').should.equal('Hot threesome interracial with double penetration')
|
60 |
|
61 |
|
62 | hook.unmount()
|
63 |
|
64 |
|
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 |
|
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 |
|
83 | require('http://xhamster.com').should.equal('Free porn')
|
84 |
|
85 |
|
86 | hook.unmount()
|
87 |
|
88 |
|
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 |
|
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 |
|
108 | require('./dummy.js').should.equal('Free porn')
|
109 |
|
110 |
|
111 | hook.unmount()
|
112 |
|
113 |
|
114 | require('./dummy.js').should.equal('Hot lesbians making out')
|
115 |
|
116 | delete require.cache[path.resolve(__dirname, './dummy.js')]
|
117 |
|
118 |
|
119 | const ignoring_hook = require_hacker.global_hook('javascript', (path, flush_cache) =>
|
120 | {
|
121 | return
|
122 | },
|
123 | { precede_node_loader: true })
|
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 | ignoring_hook.unmount()
|
132 | })
|
133 |
|
134 | it('should validate options', function()
|
135 | {
|
136 | const require_hacker = new Require_hacker({ debug: false })
|
137 |
|
138 |
|
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 |
|
159 | const hook = require_hacker.hook('js', (path, fallback) =>
|
160 | {
|
161 | fallback()
|
162 | return 'whatever'
|
163 | })
|
164 |
|
165 |
|
166 | require('./dummy.js').should.equal('Hot lesbians making out')
|
167 |
|
168 |
|
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 |