Properly test extraction methods
This commit is contained in:
parent
fab6f685cc
commit
d0544aa518
3
.gitignore
vendored
3
.gitignore
vendored
@ -106,4 +106,5 @@ dist
|
|||||||
.DS_Store
|
.DS_Store
|
||||||
gerber/
|
gerber/
|
||||||
hello.txt
|
hello.txt
|
||||||
test/tmp/*
|
test/tmp/*
|
||||||
|
test/archiveTest
|
30
index.js
30
index.js
@ -37,7 +37,7 @@ class ImageGenerator {
|
|||||||
* Extracts the passed in zip file
|
* Extracts the passed in zip file
|
||||||
* @param {string} fileName Name of the file to be extracted
|
* @param {string} fileName Name of the file to be extracted
|
||||||
* @param {string} tmpDir Temporary directory to extract to
|
* @param {string} tmpDir Temporary directory to extract to
|
||||||
* @returns {Promise} Promise object represents number of files extracted
|
* @returns {number} Number of objects contained in the archive
|
||||||
*/
|
*/
|
||||||
static extractArchive(fileName, tmpDir) {
|
static extractArchive(fileName, tmpDir) {
|
||||||
// Check archive exists
|
// Check archive exists
|
||||||
@ -54,6 +54,34 @@ class ImageGenerator {
|
|||||||
|
|
||||||
const zip = new AdmZip(fileName);
|
const zip = new AdmZip(fileName);
|
||||||
zip.extractAllTo(path.join(tmpDir, 'archive'));
|
zip.extractAllTo(path.join(tmpDir, 'archive'));
|
||||||
|
|
||||||
|
return zip.getEntries().length;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Temporary test method zip file
|
||||||
|
* @param {string} fileName Name of the file to be extracted
|
||||||
|
* @param {string} tmpDir Temporary directory to extract to
|
||||||
|
* @returns {number} Number of objects contained in the archive
|
||||||
|
*/
|
||||||
|
static testArchive(fileName, tmpDir) {
|
||||||
|
// Check archive exists
|
||||||
|
try {
|
||||||
|
if (!existsSync(fileName)) {
|
||||||
|
throw Error('Archive does not exist.');
|
||||||
|
}
|
||||||
|
if (!existsSync(tmpDir)) {
|
||||||
|
throw Error('Temporary folder does not exist.');
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
throw new Error(e);
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
const zip = new AdmZip(fileName);
|
||||||
|
return zip.getEntries().length;
|
||||||
|
} catch (error) {
|
||||||
|
throw new Error(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
/* eslint-disable */
|
/* eslint-disable */
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const fs = require('fs-extra');
|
const fs = require('fs-extra');
|
||||||
|
const { readdirSync, Dirent } = require('node:fs');
|
||||||
const Readable = require('stream').Readable;
|
const Readable = require('stream').Readable;
|
||||||
const { ImageGenerator } = require('../index.js');
|
const { ImageGenerator } = require('../index.js');
|
||||||
require('../index.js');
|
require('../index.js');
|
||||||
@ -9,6 +10,7 @@ const testGerber = path.join(__dirname, 'Arduino-Pro-Mini.zip');
|
|||||||
const incompleteGerber = path.join(__dirname, 'incomplete.zip');
|
const incompleteGerber = path.join(__dirname, 'incomplete.zip');
|
||||||
const testLayers = path.join(__dirname, 'layers');
|
const testLayers = path.join(__dirname, 'layers');
|
||||||
const emptyFolder = path.join(__dirname, 'layers', 'Empty');
|
const emptyFolder = path.join(__dirname, 'layers', 'Empty');
|
||||||
|
const archiveTestFolder = path.join(__dirname, 'archiveTest');
|
||||||
const folderConfig = {
|
const folderConfig = {
|
||||||
tmpDir: path.join(__dirname, 'tmp'),
|
tmpDir: path.join(__dirname, 'tmp'),
|
||||||
imgDir: path.join(__dirname, 'tmp'),
|
imgDir: path.join(__dirname, 'tmp'),
|
||||||
@ -110,6 +112,7 @@ describe('Passing in', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Testing static methods
|
// Testing static methods
|
||||||
|
//Layer methods
|
||||||
describe('Getting layers', () => {
|
describe('Getting layers', () => {
|
||||||
test('should return a promise of array layers', () => {
|
test('should return a promise of array layers', () => {
|
||||||
expect.assertions(1);
|
expect.assertions(1);
|
||||||
@ -138,28 +141,41 @@ describe('Getting layers', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Archive methods
|
||||||
describe('When extracting an archive', () => {
|
describe('When extracting an archive', () => {
|
||||||
test('a non-existent archive should throw an error', () => {
|
test('a non-existent archive should throw an error', () => {
|
||||||
expect(() =>
|
expect(() =>
|
||||||
ImageGenerator.extractArchive(
|
ImageGenerator.extractArchive('invalid.zip', folderConfig.tmpDir),
|
||||||
'invalid.zip',
|
).toThrow();
|
||||||
folderConfig.tmpDir,
|
|
||||||
).toThrow(),
|
|
||||||
);
|
|
||||||
});
|
});
|
||||||
test('if the temp dir does not exist it should throw an error', () => {
|
test('if the temp dir does not exist it should throw an error', () => {
|
||||||
expect(() =>
|
expect(() =>
|
||||||
ImageGenerator.extractArchive(testGerber, './invalid_dir').toThrow(Error),
|
ImageGenerator.extractArchive(testGerber, './invalid_dir'),
|
||||||
|
).toThrow(Error);
|
||||||
|
});
|
||||||
|
test('it should load the archive and return the number of files extracted', () => {
|
||||||
|
expect(() => {
|
||||||
|
ImageGenerator.testArchive(testGerber, archiveTestFolder);
|
||||||
|
}).not.toThrow();
|
||||||
|
expect(ImageGenerator.testArchive(testGerber, archiveTestFolder)).toEqual(
|
||||||
|
12,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
test('it should extract archive and resolve with the number of files extracted', () => {
|
test('it should extract archive and all files should be present', () => {
|
||||||
expect(() =>
|
expect(ImageGenerator.testArchive(testGerber, archiveTestFolder)).toEqual(
|
||||||
ImageGenerator.extractArchive(testGerber, folderConfig.tmpDir).toBe(12),
|
12,
|
||||||
);
|
);
|
||||||
|
ImageGenerator.extractArchive(testGerber, archiveTestFolder);
|
||||||
|
const dirents = readdirSync(archiveTestFolder, {
|
||||||
|
recursive: true,
|
||||||
|
withFileTypes: true,
|
||||||
|
});
|
||||||
|
const numOutputFiles = dirents.filter((dirent) => dirent.isFile());
|
||||||
|
expect(numOutputFiles).toHaveLength(12);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Gerber methods
|
//Gerber methods
|
||||||
describe('Converting a gerber to an image', () => {
|
describe('Converting a gerber to an image', () => {
|
||||||
test('temp dir not existing should throw an error', () => {
|
test('temp dir not existing should throw an error', () => {
|
||||||
expect(() =>
|
expect(() =>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user