UNPKG

8.77 kBJavaScriptView Raw
1/* @flow */
2
3import 'babel-polyfill'
4
5import EpsToPdf from '../../src/Rules/EpsToPdf'
6import { initializeRule } from '../helpers'
7
8import type { RuleDefinition } from '../helpers'
9
10async function initialize ({
11 RuleClass = EpsToPdf,
12 parameters = [{
13 filePath: 'EncapsulatedPostScript.eps'
14 }, {
15 filePath: 'x.y-Nil'
16 }],
17 ...rest }: RuleDefinition = {}) {
18 return initializeRule({ RuleClass, parameters, ...rest })
19}
20
21describe('EpsToPdf', () => {
22 describe('appliesToParameters', () => {
23 it('returns true if EPS file is the main source file.', async (done) => {
24 const { rule, options } = await initialize({
25 filePath: 'file-types/EncapsulatedPostScript.eps'
26 })
27
28 expect(await EpsToPdf.appliesToParameters(rule.state, 'build', 'execute', options, ...rule.parameters)).toBe(true)
29
30 done()
31 })
32
33 it('returns false if EPS file is not the main source file.', async (done) => {
34 const { rule, options } = await initialize()
35
36 expect(await EpsToPdf.appliesToParameters(rule.state, 'build', 'execute', options, ...rule.parameters)).toBe(false)
37
38 done()
39 })
40
41 it('returns true if there is a matching epstopdf call in the log.', async (done) => {
42 const { rule, options } = await initialize({
43 parameters: [{
44 filePath: 'EncapsulatedPostScript.eps'
45 }, {
46 filePath: 'LaTeX.log-ParsedLaTeXLog',
47 value: {
48 inputs: [],
49 outputs: [],
50 messages: [],
51 calls: [{
52 args: ['epstopdf', 'EncapsulatedPostScript.eps'],
53 options: { epstopdf: '' },
54 status: 'executed (allowed)'
55 }]
56 }
57 }]
58 })
59
60 expect(await EpsToPdf.appliesToParameters(rule.state, 'build', 'execute', options, ...rule.parameters)).toBe(true)
61
62 done()
63 })
64
65 it('returns false if there is a matching epstopdf call in the log.', async (done) => {
66 const { rule, options } = await initialize({
67 parameters: [{
68 filePath: 'EncapsulatedPostScript.eps'
69 }, {
70 filePath: 'LaTeX.log-ParsedLaTeXLog',
71 value: {
72 inputs: [],
73 outputs: [],
74 messages: [],
75 calls: [{
76 args: ['epstopdf', 'foo.eps'],
77 options: { epstopdf: '' },
78 status: 'executed (allowed)'
79 }]
80 }
81 }]
82 })
83
84 expect(await EpsToPdf.appliesToParameters(rule.state, 'build', 'execute', options, ...rule.parameters)).toBe(false)
85
86 done()
87 })
88 })
89
90 describe('getFileActions', () => {
91 it('returns a run action for an EPS file.', async (done) => {
92 const { rule } = await initialize()
93 const file = await rule.getFile('EncapsulatedPostScript.eps')
94
95 if (file) {
96 const actions = await rule.getFileActions(rule.firstParameter)
97 expect(actions).toEqual(['run'])
98 }
99
100 done()
101 })
102
103 it('returns a no actions for a latex log file.', async (done) => {
104 const { rule } = await initialize()
105 const file = await rule.getFile('LaTeX.log-ParsedLaTeXLog')
106
107 if (file) {
108 const actions = await rule.getFileActions(file)
109 expect(actions).toEqual([])
110 }
111
112 done()
113 })
114 })
115
116 describe('preEvaluate', () => {
117 it('retains run action if no epstopdf calls present.', async (done) => {
118 const { rule } = await initialize()
119
120 rule.addActions()
121 await rule.preEvaluate()
122 expect(rule.actions.has('run')).toBe(true)
123
124 done()
125 })
126
127 it('removes run action if epstopdf call present.', async (done) => {
128 const { rule } = await initialize({
129 parameters: [{
130 filePath: 'EncapsulatedPostScript.eps'
131 }, {
132 filePath: 'LaTeX.log-ParsedLaTeXLog',
133 value: {
134 inputs: [],
135 outputs: [],
136 messages: [],
137 calls: [{
138 args: ['epstopdf', 'EncapsulatedPostScript.eps'],
139 options: {},
140 status: 'executed (allowed)'
141 }]
142 }
143 }]
144 })
145
146 rule.addActions()
147 await rule.preEvaluate()
148 expect(rule.actions.has('run')).toBe(false)
149
150 done()
151 })
152
153 it('retains run action if epstopdf call failed.', async (done) => {
154 const { rule } = await initialize({
155 parameters: [{
156 filePath: 'EncapsulatedPostScript.eps'
157 }, {
158 filePath: 'LaTeX.log-ParsedLaTeXLog',
159 value: {
160 inputs: [],
161 outputs: [],
162 messages: [],
163 calls: [{
164 args: ['epstopdf', 'EncapsulatedPostScript.eps'],
165 options: {},
166 status: 'clobbered'
167 }]
168 }
169 }]
170 })
171
172 rule.addActions()
173 await rule.preEvaluate()
174 expect(rule.actions.has('run')).toBe(true)
175
176 done()
177 })
178
179 it('retains run action if epstopdf call was for another file.', async (done) => {
180 const { rule } = await initialize({
181 parameters: [{
182 filePath: 'EncapsulatedPostScript.eps'
183 }, {
184 filePath: 'LaTeX.log-ParsedLaTeXLog',
185 value: {
186 inputs: [],
187 outputs: [],
188 messages: [],
189 calls: [{
190 args: ['epstopdf', 'foo.eps'],
191 options: {},
192 status: 'execute (allowed)'
193 }]
194 }
195 }]
196 })
197
198 rule.addActions()
199 await rule.preEvaluate()
200 expect(rule.actions.has('run')).toBe(true)
201
202 done()
203 })
204 })
205
206 describe('initialize', () => {
207 it('verifies that epstopdf call overrides default options.', async (done) => {
208 const { rule } = await initialize({
209 parameters: [{
210 filePath: 'EncapsulatedPostScript.eps'
211 }, {
212 filePath: 'LaTeX.log-ParsedLaTeXLog',
213 value: {
214 inputs: [],
215 outputs: [],
216 messages: [],
217 calls: [{
218 args: ['epstopdf', 'EncapsulatedPostScript.eps'],
219 options: {
220 hires: true,
221 outfile: 'foo.pdf',
222 restricted: true
223 },
224 status: 'executed (allowed)'
225 }]
226 }
227 }]
228 })
229
230 expect(rule.options.epstopdfBoundingBox).toBe('hires')
231 expect(rule.options.epstopdfOutputPath).toBe('foo.pdf')
232 expect(rule.options.epstopdfRestricted).toBe(true)
233
234 done()
235 })
236
237 it('verifies that epstopdf with --exact call overrides default options.', async (done) => {
238 const { rule } = await initialize({
239 parameters: [{
240 filePath: 'EncapsulatedPostScript.eps'
241 }, {
242 filePath: 'LaTeX.log-ParsedLaTeXLog',
243 value: {
244 inputs: [],
245 outputs: [],
246 messages: [],
247 calls: [{
248 args: ['epstopdf', 'EncapsulatedPostScript.eps'],
249 options: {
250 exact: true
251 },
252 status: 'executed (allowed)'
253 }]
254 }
255 }]
256 })
257
258 expect(rule.options.epstopdfBoundingBox).toBe('exact')
259
260 done()
261 })
262 })
263
264 describe('constructCommand', () => {
265 it('returns correct arguments and command options for index file.', async (done) => {
266 const { rule } = await initialize()
267
268 expect(rule.constructCommand()).toEqual({
269 args: [
270 'epstopdf',
271 '--outfile={{$DIR_0/$NAME_0.pdf}}',
272 '{{$FILEPATH_0}}'
273 ],
274 cd: '$ROOTDIR',
275 severity: 'error',
276 outputs: ['$DIR_0/$NAME_0.pdf']
277 })
278
279 done()
280 })
281
282 it('sets correct output path when epstopdfOutputPath is set.', async (done) => {
283 const { rule } = await initialize({
284 options: { epstopdfOutputPath: 'foo.pdf' }
285 })
286
287 expect(rule.constructCommand().args).toContain('--outfile={{foo.pdf}}')
288
289 done()
290 })
291
292 it('add --hires to command line when epstopdfBoundingBox is set to hires.', async (done) => {
293 const { rule } = await initialize({
294 options: { epstopdfBoundingBox: 'hires' }
295 })
296
297 expect(rule.constructCommand().args).toContain('--hires')
298
299 done()
300 })
301
302 it('add --exact to command line when epstopdfBoundingBox is set to exact.', async (done) => {
303 const { rule } = await initialize({
304 options: { epstopdfBoundingBox: 'exact' }
305 })
306
307 expect(rule.constructCommand().args).toContain('--exact')
308
309 done()
310 })
311
312 it('add --restricted to command line when epstopdfRestricted is set.', async (done) => {
313 const { rule } = await initialize({
314 options: { epstopdfRestricted: true }
315 })
316
317 expect(rule.constructCommand().args).toContain('--restricted')
318
319 done()
320 })
321 })
322})