|
| 1 | +var fs = require('fs'); |
| 2 | +var path = require('path'); |
| 3 | + |
| 4 | +var request = require('request'); |
| 5 | +var test = require('tape'); |
| 6 | + |
| 7 | +var constants = require('../../tasks/util/constants'); |
| 8 | +var getOptions = require('../../tasks/util/get_image_request_options'); |
| 9 | + |
| 10 | +var userFileName = process.argv[2]; |
| 11 | + |
| 12 | +/** |
| 13 | + * Image formats to test |
| 14 | + * |
| 15 | + * N.B. 'png' is tested in `compare_pixels_test.js` |
| 16 | + * |
| 17 | + * TODO figure why 'jpeg' and 'webp' lead to errors |
| 18 | + * |
| 19 | + */ |
| 20 | +var FORMATS = ['svg', 'pdf', 'eps']; |
| 21 | + |
| 22 | +/** |
| 23 | + * The tests below determine whether the images are properly |
| 24 | + * exported by (only) checking the file size of the generated images. |
| 25 | + * |
| 26 | + * MIN_SIZE is the minimum satisfactory file size. |
| 27 | + */ |
| 28 | +var MIN_SIZE = 100; |
| 29 | + |
| 30 | + |
| 31 | +// make artifact folder |
| 32 | +if(!fs.existsSync(constants.pathToTestImages)) { |
| 33 | + fs.mkdirSync(constants.pathToTestImages); |
| 34 | +} |
| 35 | + |
| 36 | +if(!userFileName) runAll(); |
| 37 | +else { |
| 38 | + |
| 39 | + if(path.extname(userFileName) !== '.json') { |
| 40 | + throw new Error('user-specified mock must end with \'.json\''); |
| 41 | + } |
| 42 | + |
| 43 | + runSingle(userFileName); |
| 44 | +} |
| 45 | + |
| 46 | +function runAll() { |
| 47 | + test('testing export formats', function(t) { |
| 48 | + |
| 49 | + // non-exhaustive list of mocks to test |
| 50 | + var fileNames = [ |
| 51 | + '0.json', |
| 52 | + 'geo_first.json', |
| 53 | + 'gl3d_z-range.json' |
| 54 | + ]; |
| 55 | + |
| 56 | + t.plan(fileNames.length * FORMATS.length); |
| 57 | + |
| 58 | + fileNames.forEach(function(fileName) { |
| 59 | + testExports(fileName, t); |
| 60 | + }); |
| 61 | + }); |
| 62 | +} |
| 63 | + |
| 64 | +function runSingle(userFileName) { |
| 65 | + test('testing export for: ' + userFileName, function(t) { |
| 66 | + t.plan(FORMATS.length); |
| 67 | + |
| 68 | + testExports(userFileName, t); |
| 69 | + }); |
| 70 | +} |
| 71 | + |
| 72 | +function testExports(fileName, t) { |
| 73 | + FORMATS.forEach(function(format) { |
| 74 | + testExport(fileName, format, t); |
| 75 | + }); |
| 76 | +} |
| 77 | + |
| 78 | +function testExport(fileName, format, t) { |
| 79 | + var figure = require(path.join(constants.pathToTestImageMocks, fileName)); |
| 80 | + var bodyMock = { |
| 81 | + figure: figure, |
| 82 | + format: format, |
| 83 | + scale: 1 |
| 84 | + }; |
| 85 | + |
| 86 | + var imageFileName = fileName.split('.')[0] + '.' + format; |
| 87 | + var savedImagePath = path.join(constants.pathToTestImages, imageFileName); |
| 88 | + var savedImageStream = fs.createWriteStream(savedImagePath); |
| 89 | + |
| 90 | + var options = getOptions(bodyMock, 'http://localhost:9010/'); |
| 91 | + |
| 92 | + function checkExport(err) { |
| 93 | + if(err) throw err; |
| 94 | + |
| 95 | + fs.stat(savedImagePath, function(err, stats) { |
| 96 | + var didExport = stats.size > MIN_SIZE; |
| 97 | + |
| 98 | + t.ok(didExport, imageFileName + ' should be properly exported'); |
| 99 | + }); |
| 100 | + } |
| 101 | + |
| 102 | + request(options) |
| 103 | + .pipe(savedImageStream) |
| 104 | + .on('close', checkExport); |
| 105 | +} |
0 commit comments