From eacb0deea2914c9bed4390c7ba4a75591a02b09d Mon Sep 17 00:00:00 2001 From: JoshuaStorm Date: Wed, 29 Jun 2016 11:34:54 -0400 Subject: [PATCH] Refactor throwing exceptions. Added writeGltfSpec --- bin/gltf-pipeline.js | 26 +++++++------------- lib/writeBinaryGltf.js | 10 ++++++-- lib/writeGltf.js | 10 ++++++-- specs/lib/writeBinaryGltfSpec.js | 13 ++++++++++ specs/lib/writeGltfSpec.js | 41 ++++++++++++++++++++++++++++++++ 5 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 specs/lib/writeGltfSpec.js diff --git a/bin/gltf-pipeline.js b/bin/gltf-pipeline.js index 1cd5819c..c8ecbc1d 100644 --- a/bin/gltf-pipeline.js +++ b/bin/gltf-pipeline.js @@ -4,7 +4,6 @@ var path = require('path'); var Cesium = require('cesium'); var defaultValue = Cesium.defaultValue; var defined = Cesium.defined; -var DeveloperError = Cesium.DeveloperError; var gltfPipeline = require('../lib/gltfPipeline'); var processFileToDisk = gltfPipeline.processFileToDisk; @@ -22,30 +21,21 @@ if (process.argv.length < 3 || defined(argv.h) || defined(argv.help)) { } var gltfPath = defaultValue(argv._[0], defaultValue(argv.i, argv.input)); -var fileExtension = path.extname(gltfPath); -var fileName = path.basename(gltfPath, fileExtension); -var filePath = path.dirname(gltfPath); - var outputPath = defaultValue(argv._[1], defaultValue(argv.o, argv.output)); -var outputExtension = path.extname(outputPath); var binary = defaultValue(defaultValue(argv.b, argv.binary), false); var separate = defaultValue(defaultValue(argv.s, argv.separate), false); var separateImage = defaultValue(defaultValue(argv.t, argv.separateImage), false); var quantize = defaultValue(defaultValue(argv.q, argv.quantize), false); -if (!defined(gltfPath)) { - throw new DeveloperError('Input path is undefined.'); -} - -if (fileExtension !== '.glb' && fileExtension !== '.gltf') { - throw new DeveloperError('Invalid glTF file.'); -} - -if (outputExtension !== '.gltf' && outputExtension !== '.glb' || binary && outputExtension !== '.glb') { - throw new DeveloperError('Invalid output path extension.'); -} - if (!defined(outputPath)) { + var fileExtension; + if (binary) { + fileExtension = '.glb'; + } else { + fileExtension = path.extname(gltfPath); + } + var fileName = path.basename(gltfPath, fileExtension); + var filePath = path.dirname(gltfPath); // Default output. For example, path/asset.gltf becomes path/asset-optimized.gltf outputPath = path.join(filePath, fileName + '-optimized' + fileExtension); } diff --git a/lib/writeBinaryGltf.js b/lib/writeBinaryGltf.js index 3784ab37..51d699f6 100644 --- a/lib/writeBinaryGltf.js +++ b/lib/writeBinaryGltf.js @@ -3,14 +3,20 @@ var fs = require('fs'); var path = require('path'); var mkdirp = require('mkdirp'); var getBinaryGltf = require('./getBinaryGltf'); +var Cesium = require('cesium'); +var defined = Cesium.defined; +var DeveloperError = Cesium.DeveloperError; module.exports = writeBinaryGltf; function writeBinaryGltf(gltf, outputPath, createDirectory, callback) { - // Correct output path extension if necessary + if (!defined(outputPath)) { + throw new DeveloperError('Output path is undefined.'); + } + var outputExtension = path.extname(outputPath); if (outputExtension !== '.glb') { - outputPath = path.join(path.dirname(outputPath), path.basename(outputPath, outputExtension) + '.glb'); + throw new DeveloperError('Invalid output path extension.'); } // Create the output directory if specified if (createDirectory) { diff --git a/lib/writeGltf.js b/lib/writeGltf.js index 7c60bb62..097381c7 100755 --- a/lib/writeGltf.js +++ b/lib/writeGltf.js @@ -5,14 +5,20 @@ var async = require('async'); var mkdirp = require('mkdirp'); var writeSource = require('./writeSource'); var removePipelineExtras = require('./removePipelineExtras'); +var Cesium = require('cesium'); +var defined = Cesium.defined; +var DeveloperError = Cesium.DeveloperError; module.exports = writeGltf; function writeGltf(gltf, outputPath, embed, embedImage, createDirectory, callback) { - // Correct output path extension if necessary + if (!defined(outputPath)) { + throw new DeveloperError('Output path is undefined.'); + } + var outputExtension = path.extname(outputPath); if (outputExtension !== '.gltf') { - outputPath = path.join(path.dirname(outputPath), path.basename(outputPath, outputExtension) + '.gltf'); + throw new DeveloperError('Invalid output path extension.'); } // Create the output directory if specified if (createDirectory) { diff --git a/specs/lib/writeBinaryGltfSpec.js b/specs/lib/writeBinaryGltfSpec.js index 2086c7df..4e8daf52 100644 --- a/specs/lib/writeBinaryGltfSpec.js +++ b/specs/lib/writeBinaryGltfSpec.js @@ -14,6 +14,7 @@ var bufferPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest.bin' var imagePath = './specs/data/boxTexturedUnoptimized/Cesium_Logo_Flat_Binary.png'; var fragmentShaderPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest0FS_Binary.glsl'; var vertexShaderPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest0VS_Binary.glsl'; +var invalidPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest.gltf'; describe('writeBinaryGltf', function() { var testData = { @@ -90,4 +91,16 @@ describe('writeBinaryGltf', function() { expect(bufferEqual(binaryBody, body)).toBe(true); }) }); + + it('throws an invalid output path error', function() { + expect(function() { + writeBinaryGltf(clone(testData.gltf), undefined, true); + }).toThrowError('Output path is undefined.'); + }); + + it('throws an invalid output extension error', function() { + expect(function() { + writeBinaryGltf(clone(testData.gltf), invalidPath, true); + }).toThrowError('Invalid output path extension.'); + }); }); \ No newline at end of file diff --git a/specs/lib/writeGltfSpec.js b/specs/lib/writeGltfSpec.js new file mode 100644 index 00000000..5e94f23e --- /dev/null +++ b/specs/lib/writeGltfSpec.js @@ -0,0 +1,41 @@ +'use strict'; +var fs = require('fs'); +var path = require('path'); +var writeGltf = require('../../lib/writeGltf'); +var readGltf = require('../../lib/readGltf'); + +var gltfPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest_BinaryInput.gltf'; +var outputGltfPath = './output/CesiumTexturedBoxTest.gltf'; +var invalidPath = './specs/data/boxTexturedUnoptimized/CesiumTexturedBoxTest.exe'; + +describe('writeGltf', function() { + it('will write a file to the correct directory', function(done) { + var spy = spyOn(fs, 'writeFile').and.callFake(function(file, data, callback) { + callback(); + }); + + writeGltf(gltfPath, outputGltfPath, true, true, false, function() { + expect(path.normalize(spy.calls.first().args[0])).toEqual(path.normalize(outputGltfPath)); + }); + done(); + }); + + it('throws an invalid output path error', function() { + var options = {}; + readGltf(gltfPath, options, function(gltf) { + expect(function() { + writeGltf(gltf, undefined, true, true, true); + }).toThrowError('Output path is undefined.'); + }); + }); + + it('throws an invalid output extension error', function() { + var options = {}; + readGltf(gltfPath, options, function(gltf) { + expect(function() { + writeGltf(gltf, invalidPath, true, true, true); + }).toThrowError('Invalid output path extension.'); + }); + }); + +});