From e7e2f02581416c85ea3520b810ddb3a75c88b808 Mon Sep 17 00:00:00 2001 From: Nick Playfair Date: Mon, 1 Feb 2021 23:01:33 +0000 Subject: [PATCH] Test getLayers --- index.js | 21 +++++++++++++++++++++ test/index.test.js | 5 +++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 4dd4a05..d6e6437 100644 --- a/index.js +++ b/index.js @@ -99,6 +99,27 @@ function getLayers(fileName, tmpDir) { }); } +function getLayers2(dir) { + return new Promise((resolve, reject) => { + // Make sure the directory exists + if (!fs.existsSync(dir)) { + return reject(new Error('Layers folder does not exist.')); + } + // Check that the required layer files exist in source dir + let layersValid = true; + gerberFiles.forEach((layer) => { + if (!fs.existsSync(path.join(dir, layer))) layersValid = false; + }); + if (!layersValid) return reject(new Error('Layer not found.')); + // Construct array of layers that match the supplied filenames array + const layers = gerberFiles.map((layerName) => ({ + filename: layerName, + gerber: fs.createReadStream(path.join(dir, layerName)), + })); + return resolve(layers); + }); +} + /** * Clean up the archive folder in the specified directory * @param {string} dir Path to a directory to clean up diff --git a/test/index.test.js b/test/index.test.js index 09725e3..9b428a7 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -6,6 +6,7 @@ const fileProc = require('../index.js'); const testGerber = path.join(__dirname, 'Arduino-Pro-Mini.zip'); const testLayers = path.join(__dirname, 'layers'); const emptyFolder = path.join(__dirname, 'layers', 'Empty'); +const tmpDir = path.join(__dirname, 'tmp'); // getLayers test('Promise of an array of layers from a given folder', () => { @@ -22,9 +23,9 @@ test('Promise of an array of layers from a given folder', () => { }); test('Non-existent folder should reject promise with error', () => { - return expect(fileProc.getLayers2('./invalid_folder')).rejects.toThrow('Layers folder does not exist'); + return expect(fileProc.getLayers2('./invalid_folder')).rejects.toThrow('Layers folder does not exist.'); }); test('Folder with incorrect number of layers should reject promise with error', () => { - return expect(fileProc.getLayers2(emptyFolder)).rejects.toThrow('Layer not found'); + return expect(fileProc.getLayers2(emptyFolder)).rejects.toThrow('Layer not found.'); });