Skip to content

Commit 06714c5

Browse files
committed
Added minimal test of server CLI
1 parent decccaf commit 06714c5

File tree

8 files changed

+145
-25
lines changed

8 files changed

+145
-25
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ In order to use this package on a headless server, you need to use `xvfb`. See `
301301

302302
## Changes
303303

304-
### O.5.0 (in development)
304+
### O.5.0
305305

306306
- upgraded Docker to NodeJS 10
307307
- reduced size of Docker image and simplified Xvfb management

dist/cli.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,14 @@ if (bounds !== null) {
102102
raiseError("Bounds must be west,south,east,north. Invalid value found: ".concat((0, _toConsumableArray2["default"])(bounds)));
103103
}
104104

105+
bounds.forEach(function (b) {
106+
if (Number.isNaN(b)) {
107+
raiseError("Bounds must be west,south,east,north. Invalid value found: ".concat((0, _toConsumableArray2["default"])(bounds)));
108+
}
109+
110+
return null;
111+
});
112+
105113
var _bounds = (0, _slicedToArray2["default"])(bounds, 4),
106114
west = _bounds[0],
107115
south = _bounds[1],

dist/server.js

Lines changed: 41 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,17 @@
33

44
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
55

6+
Object.defineProperty(exports, "__esModule", {
7+
value: true
8+
});
9+
exports["default"] = exports.server = void 0;
10+
11+
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
12+
613
var _toConsumableArray2 = _interopRequireDefault(require("@babel/runtime/helpers/toConsumableArray"));
714

15+
var _fs = _interopRequireDefault(require("fs"));
16+
817
var _restify = _interopRequireDefault(require("restify"));
918

1019
var _nodeRestifyValidation = _interopRequireDefault(require("node-restify-validation"));
@@ -15,12 +24,17 @@ var _commander = _interopRequireDefault(require("commander"));
1524

1625
var _package = require("../package.json");
1726

18-
var _render = _interopRequireDefault(require("./render"));
27+
var _render = require("./render");
1928

2029
var parseListToFloat = function parseListToFloat(text) {
2130
return text.split(',').map(Number);
2231
};
2332

33+
var raiseError = function raiseError(msg) {
34+
console.error('ERROR:', msg);
35+
process.exit(1);
36+
};
37+
2438
var PARAMS = {
2539
style: {
2640
isRequired: true,
@@ -136,6 +150,21 @@ var renderImage = function renderImage(params, response, next, tilePath) {
136150

137151
return null;
138152
});
153+
154+
var _bounds = bounds,
155+
_bounds2 = (0, _slicedToArray2["default"])(_bounds, 4),
156+
west = _bounds2[0],
157+
south = _bounds2[1],
158+
east = _bounds2[2],
159+
north = _bounds2[3];
160+
161+
if (west === east) {
162+
return next(new _restifyErrors["default"].BadRequestError("Bounds west and east coordinate are the same value"));
163+
}
164+
165+
if (south === north) {
166+
return next(new _restifyErrors["default"].BadRequestError("Bounds south and north coordinate are the same value"));
167+
}
139168
}
140169

141170
if (bearing !== null) {
@@ -155,7 +184,7 @@ var renderImage = function renderImage(params, response, next, tilePath) {
155184
}
156185

157186
try {
158-
(0, _render["default"])(style, parseInt(width, 10), parseInt(height, 10), {
187+
(0, _render.render)(style, parseInt(width, 10), parseInt(height, 10), {
159188
zoom: zoom,
160189
center: center,
161190
bounds: bounds,
@@ -203,6 +232,7 @@ var server = _restify["default"].createServer({
203232
ignoreTrailingSlash: true
204233
});
205234

235+
exports.server = server;
206236
server.use(_restify["default"].plugins.queryParser());
207237
server.use(_restify["default"].plugins.bodyParser());
208238
server.use(_nodeRestifyValidation["default"].validationPlugin({
@@ -248,9 +278,17 @@ server.get({
248278
});
249279

250280
if (tilePath !== null) {
281+
if (!_fs["default"].existsSync(tilePath)) {
282+
raiseError("Path to mbtiles files does not exist: ".concat(tilePath));
283+
}
284+
251285
console.log('Using local mbtiles in: %j', tilePath);
252286
}
253287

254288
server.listen(port, function () {
255289
console.log('Mapbox GL static rendering server started and listening at %s', server.url);
256-
});
290+
});
291+
var _default = {
292+
server: server
293+
};
294+
exports["default"] = _default;

src/cli.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,18 @@ if (bounds !== null) {
121121
]}`
122122
)
123123
}
124+
125+
bounds.forEach(b => {
126+
if (Number.isNaN(b)) {
127+
raiseError(
128+
`Bounds must be west,south,east,north. Invalid value found: ${[
129+
...bounds,
130+
]}`
131+
)
132+
}
133+
return null
134+
})
135+
124136
const [west, south, east, north] = bounds
125137
if (west === east) {
126138
raiseError(`Bounds west and east coordinate are the same value`)

src/server.js

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
#!/usr/bin/env node
2+
import fs from 'fs'
23
import restify from 'restify'
34
import restifyValidation from 'node-restify-validation'
45
import restifyErrors from 'restify-errors'
56
import cli from 'commander'
67

78
import { version } from '../package.json'
8-
import render from './render'
9+
import { render } from './render'
910

1011
const parseListToFloat = text => text.split(',').map(Number)
1112

13+
const raiseError = msg => {
14+
console.error('ERROR:', msg)
15+
process.exit(1)
16+
}
17+
1218
const PARAMS = {
1319
style: { isRequired: true, isString: true },
1420
width: { isRequired: true, isInt: true },
@@ -119,6 +125,22 @@ const renderImage = (params, response, next, tilePath) => {
119125
}
120126
return null
121127
})
128+
129+
const [west, south, east, north] = bounds
130+
if (west === east) {
131+
return next(
132+
new restifyErrors.BadRequestError(
133+
`Bounds west and east coordinate are the same value`
134+
)
135+
)
136+
}
137+
if (south === north) {
138+
return next(
139+
new restifyErrors.BadRequestError(
140+
`Bounds south and north coordinate are the same value`
141+
)
142+
)
143+
}
122144
}
123145

124146
if (bearing !== null) {
@@ -208,7 +230,7 @@ cli.version(version)
208230

209231
const { port = 8000, tiles: tilePath = null } = cli
210232

211-
const server = restify.createServer({
233+
export const server = restify.createServer({
212234
ignoreTrailingSlash: true,
213235
})
214236
server.use(restify.plugins.queryParser())
@@ -258,6 +280,10 @@ server.get({ url: '/' }, (req, res) => {
258280
})
259281

260282
if (tilePath !== null) {
283+
if (!fs.existsSync(tilePath)) {
284+
raiseError(`Path to mbtiles files does not exist: ${tilePath}`)
285+
}
286+
261287
console.log('Using local mbtiles in: %j', tilePath)
262288
}
263289

@@ -267,3 +293,5 @@ server.listen(port, () => {
267293
server.url
268294
)
269295
})
296+
297+
export default { server }

tests/cli.test.js

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import dotenv from 'dotenv-flow'
66
import sharp from 'sharp'
77
import { createTempDir } from 'jest-fixtures'
88

9-
import { imageDiff } from './util'
9+
import { imageDiff, cliEndpoint } from './util'
1010

1111
import { version } from '../package.json'
1212

@@ -20,23 +20,7 @@ if (!MAPBOX_API_TOKEN) {
2020
)
2121
}
2222

23-
// from: https://medium.com/@ole.ersoy/unit-testing-commander-scripts-with-jest-bc32465709d6
24-
function cli(args, cwd) {
25-
return new Promise(resolve => {
26-
exec(
27-
`node ${path.resolve('./dist/cli')} ${args.join(' ')}`,
28-
{ cwd },
29-
(error, stdout, stderr) => {
30-
resolve({
31-
code: error && error.code ? error.code : 0,
32-
error,
33-
stdout,
34-
stderr,
35-
})
36-
}
37-
)
38-
})
39-
}
23+
const cli = cliEndpoint('./dist/cli')
4024

4125
test('returns correct version', async () => {
4226
const { stdout } = await cli(['-V'], '.')

tests/server.test.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { cliEndpoint } from './util'
2+
3+
import { version } from '../package.json'
4+
5+
const cli = cliEndpoint('./dist/server')
6+
7+
test('returns correct version', async () => {
8+
const { stdout } = await cli(['-V'], '.')
9+
expect(stdout).toContain(version)
10+
})
11+
12+
test('fails with invalid tile path', async () => {
13+
const { stderr } = await cli(['-t', './bad-tile-path'], '.')
14+
expect(stderr).toContain('ERROR: Path to mbtiles files does not exist')
15+
})

tests/util.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import path from 'path'
2+
import { exec } from 'child_process'
13
import sharp from 'sharp'
4+
25
import pixelmatch from 'pixelmatch'
36

47
/**
@@ -25,4 +28,36 @@ export async function imageDiff(pngData, expectedPath) {
2528
return pixelmatch(rawData, expected, null, width, height)
2629
}
2730

28-
export default { imageDiff }
31+
/**
32+
* Create a test endpoint for the CLI at the passed in path.
33+
*
34+
* @param {String} cliPath - path to CLI to test (BUILT version)
35+
*/
36+
export function cliEndpoint(cliPath) {
37+
/**
38+
* Test the CLI (built version) with the passed in arguments.
39+
* Derived from from: https://medium.com/@ole.ersoy/unit-testing-commander-scripts-with-jest-bc32465709d6
40+
*
41+
*
42+
* @param {String} args - arguments to pass to CLI command
43+
* @param {String} cwd - current working directory
44+
*/
45+
return function cli(args, cwd) {
46+
return new Promise(resolve => {
47+
exec(
48+
`node ${path.resolve(cliPath)} ${args.join(' ')}`,
49+
{ cwd },
50+
(error, stdout, stderr) => {
51+
resolve({
52+
code: error && error.code ? error.code : 0,
53+
error,
54+
stdout,
55+
stderr,
56+
})
57+
}
58+
)
59+
})
60+
}
61+
}
62+
63+
export default { imageDiff, cliEndpoint }

0 commit comments

Comments
 (0)