-
Notifications
You must be signed in to change notification settings - Fork 89
/
Copy pathexecuteSpec.ts
203 lines (178 loc) · 8.21 KB
/
executeSpec.ts
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
import { buildInputFile, compare, execute, executeAndReturnOutputFile, executeOne, extractInfo } from '../src'
import { showImages } from './testUtil'
export default describe('execute', () => {
describe('executeOne', () => {
it('should run convert -resize', async done => {
const img1 = await buildInputFile('holocaust.jpg')
let info = await extractInfo(img1)
expect(info[0].image.formatDescription.toLowerCase()).toBe('jpeg')
expect(info[0].image.geometry.width).toBe(320)
expect(info[0].image.geometry.height).toBe(240)
const { outputFiles } = await executeOne({
inputFiles: [img1],
commands: [['convert', 'holocaust.jpg', '-resize', '123x321!', 'resized.png']],
})
info = await extractInfo(outputFiles[0])
expect(info[0].image.formatDescription.toLowerCase()).toBe('png')
expect(info[0].image.geometry.width).toBe(123)
expect(info[0].image.geometry.height).toBe(321)
done()
})
it('should support CLI like commands', async done => {
const img1 = await buildInputFile('holocaust.jpg')
const { outputFiles } = await executeOne({ inputFiles: [img1], commands: ['convert holocaust.jpg -resize 444x76! output.gif'] })
expect(outputFiles[0].name).toBe('output.gif')
const info = await extractInfo(outputFiles[0])
expect(info[0].image.formatDescription.toLowerCase()).toBe('gif')
expect(info[0].image.geometry.width).toBe(444)
expect(info[0].image.geometry.height).toBe(76)
done()
})
it('should return error property and empty outputFiles on error', async done => {
const { outputFiles, errors, exitCode, stderr } = await executeOne({ inputFiles: [await buildInputFile('fn.png')], commands: `convert nonExistent.png out.tiff` })
expect(exitCode).not.toBe(0)
expect(outputFiles.length).toBe(0)
expect(stderr.join('\n')).toContain(`'nonExistent.png': No such file or directory`)
expect(errors.length).toBe(1)
done()
})
})
describe('execute', () => {
it('should execute commands serially and output files are available to next commands', async done => {
const result = await execute({
inputFiles: [await buildInputFile('fn.png', 'image1.png')],
commands: [
['convert', 'image1.png', '-rotate', '70', 'image2.gif'],
// heads up: next command uses 'image2.gif' which was the output of previous command:
['convert', 'image2.gif', '-scale', '23%', 'image3.jpg'],
],
})
const result2 = await executeOne({
inputFiles: [await buildInputFile('fn.png', 'image1.png')],
commands: [['convert', 'image1.png', '-rotate', '70', '-scale', '23%', 'image2.gif']],
})
expect(await compare(result.outputFiles.find(f => f.name === 'image3.jpg'), result2.outputFiles[0])).toBe(true)
done()
})
it('supports CLI like commands', async done => {
const { outputFiles } = await execute({
inputFiles: [await buildInputFile('fn.png', 'image1.png')],
commands: [
'convert image1.png -rotate 70 image2.gif',
// heads up: the next command uses 'image2.gif' which was the output of previous command:
'convert image2.gif -scale 23% image3.jpg',
],
})
const result2 = await executeOne({
inputFiles: [await buildInputFile('fn.png', 'image1.png')],
commands: ['convert image1.png -rotate 70 -scale 23% image2.png'],
})
const image3 = outputFiles.find(f => f.name === 'image3.jpg')
const image2 = result2.outputFiles[0]
// await showImages([image3,image2])
expect(await compare(image3, image2)).toBe(true)
done()
})
it('supports just a command when no input files are necessary', async done => {
const { outputFiles } = await execute([
'convert rose: -rotate 70 image2.gif',
'convert image2.gif -scale 23% image3.jpg',
])
const result2 = await execute('convert rose: -rotate 70 -scale 23% image2.png')
const image3 = outputFiles.find(f => f.name === 'image3.jpg')
const image2 = result2.outputFiles[0]
await showImages([image3, image2])
expect(await compare(image3, image2)).toBe(true)
done()
})
it('convert won\'t replace input files', async done => {
const input = await buildInputFile('fn.png')
const result = await execute({
inputFiles: [input],
commands: ['convert fn.png -rotate 10 fn.png'],
})
const output = result.outputFiles.find(f => f.name === 'fn.png')
expect(output).toBeUndefined()
done()
})
it('mogrify will replace input files', async done => {
const input = await buildInputFile('fn.png')
const result = await execute({
inputFiles: [input],
commands: ['mogrify -rotate 10 fn.png'],
})
const output = result.outputFiles.find(f => f.name === 'fn.png')
expect(output).toBeDefined()
const converted = await executeAndReturnOutputFile({ inputFiles: [input], commands: 'convert fn.png -rotate 10 output.png' })
// await showImages([output, converted])
expect(await compare(output, converted)).toBe(true)
done()
})
it('supports single string CLI like command', async done => {
const { outputFiles } = await execute({
inputFiles: [await buildInputFile('fn.png', 'image1.png')],
commands: 'convert image1.png -rotate 70 image2.gif',
})
expect(outputFiles[0].name).toBe('image2.gif')
done()
})
it('can access stdout', async done => {
const { stdout } = await execute(`identify rose:`)
expect(stdout.join('\n')).toContain(`rose:=>ROSE PNM 70x46 70x46+0+0 8-bit`)
done()
})
describe('errors', () => {
it('should return error property and empty outputFiles on error', async done => {
const img = await buildInputFile('fn.png')
const result = await execute({ inputFiles: [img], commands: `convert nonExistent.png out.tiff` })
expect(result.outputFiles.length).toBe(0)
expect(result.results.length).toBe(1)
expect(result.stderr.join('\n')).toContain(`'nonExistent.png': No such file or directory`)
expect(result.errors.length).toBe(1)
expect(result.results[0].exitCode).not.toBe(0)
expect(result.results[0].stderr.join('\n')).toContain(`'nonExistent.png': No such file or directory`)
done()
})
it('should return errors per command', async done => {
const img = await buildInputFile('fn.png')
const result = await execute({
inputFiles: [img], commands: [
`convert fn.png out.gif`,
`convert nonExistent.png out.tiff`,
`convert out.gif foo.png`,
`identify rose:`,
],
})
expect(result.outputFiles.length).toBe(2)
expect(result.errors.length).toBe(4)
expect(result.exitCode).not.toBe(0)
expect(result.errors[0]).toBeUndefined()
expect(result.errors[1]).toBeDefined()
expect(result.errors[2]).toBeUndefined()
expect(result.errors[3]).toBeUndefined()
expect(result.stdout.join('\n')).toContain(`rose:=>ROSE PNM 70x46 70x46+0+0 8-bit`)
expect(result.stderr.join('\n')).toContain(`'nonExistent.png': No such file or directory`)
expect(result.results[3].stdout.join('\n')).toContain(`rose:=>ROSE PNM 70x46 70x46+0+0 8-bit`)
expect(result.results[3].errors[0]).toBeUndefined()
expect(result.results[3].exitCode).toBe(0)
expect(result.results[1].errors[0]).toBeDefined()
expect(result.results[1].stderr.join('\n')).toContain(`'nonExistent.png': No such file or directory`)
done()
})
})
})
describe('executeAndReturnOutputFile', () => {
it('should support using just a command when input files are not necessary', async done => {
const out = await executeAndReturnOutputFile('convert rose: -rotate 55 -resize 55% out.png')
expect(out.name).toBe('out.png')
const out2 = await executeAndReturnOutputFile(`
convert rose: -rotate 55 out1.png
convert out1.png -resize 55% out2.png
`, 'out2.png')
expect(out2.name).toBe('out2.png')
expect(await compare(out, out2)).toBe(true)
done()
})
})
xit('event emitter', () => { })
})