-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.js
156 lines (129 loc) · 3.29 KB
/
test.js
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import MemoryFs from 'memory-fs'
import ScreepsModules from 'screeps-modules'
import test from 'ava'
import webpack from 'webpack'
import Compilation from 'webpack/lib/Compilation'
import ScreepsWebpackPlugin from './index'
const debug = require('debug')('screeps-webpack-plugin')
function compile (options) {
const compiler = webpack(Object.assign({
target: 'node',
entry: {
main: ['index.js'],
etc: ['foo.js', 'bar.js']
},
resolve: {
modules: ['./fixtures']
},
output: {
path: '/',
filename: '[name]'
},
plugins: []
}, options))
compiler.outputFileSystem = new MemoryFs()
return new Promise((resolve, reject) => {
compiler.run((err, stats) => {
debug(stats.toJson())
if (err) return reject(err)
if (stats.hasErrors() || stats.hasWarnings()) {
return reject(stats.compilation.errors)
}
resolve({compiler, stats})
})
})
}
const plugin = (name, ...args) => (
new (class {
apply (compiler) {
compiler.plugin('compilation', (compilation) => {
compilation.plugin(name, ...args)
})
}
})()
)
const checkError = (t, err, ...checks) => {
t.is(err.name, 'ScreepsWebpackPluginError')
for (const check of checks) {
t.truthy(err.toString().match(check))
}
}
test('Test Webpack compiler setup', async t => {
t.plan(1)
class TestPlugin {
apply (compiler) {
compiler.plugin('done', () => {
t.pass()
})
}
}
await compile({plugins: [new TestPlugin()]})
})
test(`Test requires target 'node'`, async t => {
try {
await compile({
target: 'web',
plugins: [
new ScreepsWebpackPlugin()
]
})
t.fail()
} catch ([e]) {
checkError(t, e, 'target', 'node')
}
})
test('Test commit', async t => {
t.plan(10)
const collectModules = plugin('screeps-webpack-plugin-collect-modules',
({modules, plugin, compilation}, cb) => {
t.deepEqual(Object.keys(modules), ['etc', 'main'])
t.truthy(modules.main.match(/foobar/))
t.truthy(modules.etc.match(/foobar/))
t.true(plugin instanceof ScreepsWebpackPlugin)
t.true(compilation instanceof Compilation)
modules.quux = 'norf'
cb(null, {modules, plugin, compilation})
}
)
const configureClient = plugin('screeps-webpack-plugin-configure-client',
(client, plugin) => {
t.true(client instanceof ScreepsModules)
t.true(plugin instanceof ScreepsWebpackPlugin)
client.commit = (...args) => {
return Promise.resolve('foobar')
}
return client
}
)
const beforeCommit = plugin('screeps-webpack-plugin-before-commit',
(branch, modules) => {
t.is(branch, 'test')
t.is(modules.quux, 'norf')
}
)
const afterCommit = plugin('screeps-webpack-plugin-after-commit',
(body) => {
t.is(body, 'foobar')
}
)
await compile({
plugins: [
new ScreepsWebpackPlugin({
branch: 'test',
email: 'foobar',
password: 'barbaz',
token: 'quuxnorf',
serverUrl: 'https://foo.com',
gzip: true
}),
collectModules,
configureClient,
beforeCommit,
afterCommit
]
})
})
test('Test constructor', t => {
t.notThrows(() => new ScreepsWebpackPlugin())
t.truthy(new ScreepsWebpackPlugin().options)
})