-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathconvert.js
More file actions
122 lines (114 loc) · 4.19 KB
/
convert.js
File metadata and controls
122 lines (114 loc) · 4.19 KB
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
/* global describe, it, before, after, beforeEach, afterEach */
/*!
* The MIT License (MIT)
*
* Copyright (c) 2019 Mark van Seventer
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
// Strict mode.
'use strict'
// Standard lib.
const path = require('path')
// Package modules.
const expect = require('must')
const fs = require('fs-extra')
const tempy = require('tempy')
// Local modules.
const convert = require('../lib/convert')
const queue = require('../lib/queue')
const tile = require('../cmd/output')
// Test suite.
describe('convert', () => {
// Default input.
const input = path.join(__dirname, './fixtures/input.jpg')
describe('files', () => {
// Default output.
let copy, dest
before(() => { dest = tempy.directory() })
beforeEach(() => { copy = tempy.file() })
beforeEach((done) => { fs.copy(input, copy, done) })
afterEach(() => {
queue.length = 0 // Empty queue.
})
afterEach((done) => fs.remove(copy, done))
afterEach((done) => fs.emptyDir(dest, done))
after((done) => fs.remove(dest, done))
// Tests.
it('must convert a file', () => {
return convert
.files([input], dest, {})
.then(([info]) => expect(fs.existsSync(info.path)).to.be.true)
})
it('must convert a file and output to an existing directory', () => {
// Negative test for directory that does not exist.
const rand = '' + Math.random()
return convert
.files([input, input], rand, {})
.then(() => { throw new Error('STOP') })
.catch((err) => {
expect(err).to.exist()
expect(err).to.have.property('message')
expect(err.message).to.contain(`${rand}/input.jpg`)
expect(err.message).to.contain('No such file or directory')
})
})
it('must convert multiple files', () => {
return convert
.files([input, input], dest, {})
.then((info) => expect(info).to.have.length(2))
})
it('must support output templates', () => {
const rand = Math.random()
return convert
.files([input], path.join(dest, `{name}-${rand}{ext}`), {})
.then(([info]) => expect(info.path).to.contain(`input-${rand}.jpg`))
})
it('must allow the same file as input and output', () => {
return convert
.files([copy], path.dirname(copy), {})
})
it('must support tiled output', () => {
tile.handler({ container: 'zip' })
return convert
.files([input], dest, {})
})
it('must not write to disk if the dry flag is set', () => {
const out = path.join(dest, `{name}-${Math.random()}{ext}`)
return convert
.files([input], out, { dry: true })
.then(() => expect(fs.existsSync(out)).to.be.false())
})
})
describe('stream', () => {
// Default output.
let dest
beforeEach(() => { dest = tempy.file() })
afterEach((done) => fs.remove(dest, done))
// Tests.
it('must convert a file', () => {
return convert
.stream(fs.createReadStream(input), fs.createWriteStream(dest), {})
.then((info) => {
expect(info.format).to.exist()
expect(info.path).not.to.exist()
expect(fs.existsSync(dest)).to.be.true()
})
})
})
})