mirror of
https://github.com/balena-io/etcher.git
synced 2025-07-23 03:06:38 +00:00
test: Use mocha with promises instead of callbacks (#1268)
This changes the test cases to just return the Promises, to avoid timing out on failures and to provide better error messages and stack traces. Change-Type: patch
This commit is contained in:
parent
3cdd925c41
commit
f15587807a
@ -34,10 +34,10 @@ describe('ImageStream: Archive hooks: ZIP', function() {
|
|||||||
this.zip = path.join(ZIP_PATH, 'zip-directory-empty.zip');
|
this.zip = path.join(ZIP_PATH, 'zip-directory-empty.zip');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should become an empty array', function(done) {
|
it('should become an empty array', function() {
|
||||||
zipHooks.getEntries(this.zip).then((entries) => {
|
return zipHooks.getEntries(this.zip).then((entries) => {
|
||||||
m.chai.expect(entries).to.deep.equal([]);
|
m.chai.expect(entries).to.deep.equal([]);
|
||||||
}).asCallback(done);
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -48,8 +48,8 @@ describe('ImageStream: Archive hooks: ZIP', function() {
|
|||||||
this.zip = path.join(ZIP_PATH, 'zip-directory-multiple-images.zip');
|
this.zip = path.join(ZIP_PATH, 'zip-directory-multiple-images.zip');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should become all entries', function(done) {
|
it('should become all entries', function() {
|
||||||
zipHooks.getEntries(this.zip).then((entries) => {
|
return zipHooks.getEntries(this.zip).then((entries) => {
|
||||||
m.chai.expect(entries).to.deep.equal([
|
m.chai.expect(entries).to.deep.equal([
|
||||||
{
|
{
|
||||||
name: 'multiple-images/edison-config.img',
|
name: 'multiple-images/edison-config.img',
|
||||||
@ -60,7 +60,7 @@ describe('ImageStream: Archive hooks: ZIP', function() {
|
|||||||
size: 33554432
|
size: 33554432
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
}).asCallback(done);
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -71,8 +71,8 @@ describe('ImageStream: Archive hooks: ZIP', function() {
|
|||||||
this.zip = path.join(ZIP_PATH, 'zip-directory-nested-misc.zip');
|
this.zip = path.join(ZIP_PATH, 'zip-directory-nested-misc.zip');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should become all entries', function(done) {
|
it('should become all entries', function() {
|
||||||
zipHooks.getEntries(this.zip).then((entries) => {
|
return zipHooks.getEntries(this.zip).then((entries) => {
|
||||||
m.chai.expect(entries).to.deep.equal([
|
m.chai.expect(entries).to.deep.equal([
|
||||||
{
|
{
|
||||||
name: 'zip-directory-nested-misc/foo',
|
name: 'zip-directory-nested-misc/foo',
|
||||||
@ -83,7 +83,7 @@ describe('ImageStream: Archive hooks: ZIP', function() {
|
|||||||
size: 4
|
size: 4
|
||||||
}
|
}
|
||||||
]);
|
]);
|
||||||
}).asCallback(done);
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -96,45 +96,41 @@ describe('ImageStream: Archive hooks: ZIP', function() {
|
|||||||
this.zip = path.join(ZIP_PATH, 'zip-directory-nested-misc.zip');
|
this.zip = path.join(ZIP_PATH, 'zip-directory-nested-misc.zip');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to extract a top-level file', function(done) {
|
it('should be able to extract a top-level file', function() {
|
||||||
const fileName = 'zip-directory-nested-misc/foo';
|
const fileName = 'zip-directory-nested-misc/foo';
|
||||||
zipHooks.getEntries(this.zip).then((entries) => {
|
return zipHooks.getEntries(this.zip).then((entries) => {
|
||||||
return zipHooks.extractFile(this.zip, entries, fileName);
|
return zipHooks.extractFile(this.zip, entries, fileName);
|
||||||
}).then(utils.extractStream).then((data) => {
|
}).then(utils.extractStream).then((data) => {
|
||||||
m.chai.expect(data.toString()).to.equal('foo\n');
|
m.chai.expect(data.toString()).to.equal('foo\n');
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be able to extract a nested file', function(done) {
|
it('should be able to extract a nested file', function() {
|
||||||
const fileName = 'zip-directory-nested-misc/hello/there/bar';
|
const fileName = 'zip-directory-nested-misc/hello/there/bar';
|
||||||
zipHooks.getEntries(this.zip).then((entries) => {
|
return zipHooks.getEntries(this.zip).then((entries) => {
|
||||||
return zipHooks.extractFile(this.zip, entries, fileName);
|
return zipHooks.extractFile(this.zip, entries, fileName);
|
||||||
}).then(utils.extractStream).then((data) => {
|
}).then(utils.extractStream).then((data) => {
|
||||||
m.chai.expect(data.toString()).to.equal('bar\n');
|
m.chai.expect(data.toString()).to.equal('bar\n');
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw if the entry does not exist', function(done) {
|
it('should throw if the entry does not exist', function() {
|
||||||
const fileName = 'zip-directory-nested-misc/xxxxxxxxxxxxxxxx';
|
const fileName = 'zip-directory-nested-misc/xxxxxxxxxxxxxxxx';
|
||||||
zipHooks.getEntries(this.zip).then((entries) => {
|
return zipHooks.getEntries(this.zip).then((entries) => {
|
||||||
return zipHooks.extractFile(this.zip, entries, fileName);
|
return zipHooks.extractFile(this.zip, entries, fileName);
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
m.chai.expect(error).to.be.an.instanceof(Error);
|
m.chai.expect(error).to.be.an.instanceof(Error);
|
||||||
m.chai.expect(error.message).to.equal(`Invalid entry: ${fileName}`);
|
m.chai.expect(error.message).to.equal(`Invalid entry: ${fileName}`);
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should throw if the entry is a directory', function(done) {
|
it('should throw if the entry is a directory', function() {
|
||||||
const fileName = 'zip-directory-nested-misc/hello';
|
const fileName = 'zip-directory-nested-misc/hello';
|
||||||
zipHooks.getEntries(this.zip).then((entries) => {
|
return zipHooks.getEntries(this.zip).then((entries) => {
|
||||||
return zipHooks.extractFile(this.zip, entries, fileName);
|
return zipHooks.extractFile(this.zip, entries, fileName);
|
||||||
}).catch((error) => {
|
}).catch((error) => {
|
||||||
m.chai.expect(error).to.be.an.instanceof(Error);
|
m.chai.expect(error).to.be.an.instanceof(Error);
|
||||||
m.chai.expect(error.message).to.equal(`Invalid entry: ${fileName}`);
|
m.chai.expect(error.message).to.equal(`Invalid entry: ${fileName}`);
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -41,11 +41,11 @@ describe('ImageStream: BZ2', function() {
|
|||||||
|
|
||||||
describe('.getImageMetadata()', function() {
|
describe('.getImageMetadata()', function() {
|
||||||
|
|
||||||
it('should return the correct metadata', function(done) {
|
it('should return the correct metadata', function() {
|
||||||
const image = path.join(BZ2_PATH, 'raspberrypi.img.bz2');
|
const image = path.join(BZ2_PATH, 'raspberrypi.img.bz2');
|
||||||
const expectedSize = fs.statSync(image).size;
|
const expectedSize = fs.statSync(image).size;
|
||||||
|
|
||||||
imageStream.getImageMetadata(image).then((metadata) => {
|
return imageStream.getImageMetadata(image).then((metadata) => {
|
||||||
m.chai.expect(metadata).to.deep.equal({
|
m.chai.expect(metadata).to.deep.equal({
|
||||||
path: image,
|
path: image,
|
||||||
size: {
|
size: {
|
||||||
@ -56,7 +56,6 @@ describe('ImageStream: BZ2', function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -41,12 +41,12 @@ describe('ImageStream: GZ', function() {
|
|||||||
|
|
||||||
describe('.getImageMetadata()', function() {
|
describe('.getImageMetadata()', function() {
|
||||||
|
|
||||||
it('should return the correct metadata', function(done) {
|
it('should return the correct metadata', function() {
|
||||||
const image = path.join(GZ_PATH, 'raspberrypi.img.gz');
|
const image = path.join(GZ_PATH, 'raspberrypi.img.gz');
|
||||||
const uncompressedSize = fs.statSync(path.join(IMAGES_PATH, 'raspberrypi.img')).size;
|
const uncompressedSize = fs.statSync(path.join(IMAGES_PATH, 'raspberrypi.img')).size;
|
||||||
const compressedSize = fs.statSync(path.join(GZ_PATH, 'raspberrypi.img.gz')).size;
|
const compressedSize = fs.statSync(path.join(GZ_PATH, 'raspberrypi.img.gz')).size;
|
||||||
|
|
||||||
imageStream.getImageMetadata(image).then((metadata) => {
|
return imageStream.getImageMetadata(image).then((metadata) => {
|
||||||
m.chai.expect(metadata).to.deep.equal({
|
m.chai.expect(metadata).to.deep.equal({
|
||||||
path: image,
|
path: image,
|
||||||
size: {
|
size: {
|
||||||
@ -57,7 +57,6 @@ describe('ImageStream: GZ', function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -40,11 +40,11 @@ describe('ImageStream: IMG', function() {
|
|||||||
|
|
||||||
describe('.getImageMetadata()', function() {
|
describe('.getImageMetadata()', function() {
|
||||||
|
|
||||||
it('should return the correct metadata', function(done) {
|
it('should return the correct metadata', function() {
|
||||||
const image = path.join(IMAGES_PATH, 'raspberrypi.img');
|
const image = path.join(IMAGES_PATH, 'raspberrypi.img');
|
||||||
const expectedSize = fs.statSync(image).size;
|
const expectedSize = fs.statSync(image).size;
|
||||||
|
|
||||||
imageStream.getImageMetadata(image).then((metadata) => {
|
return imageStream.getImageMetadata(image).then((metadata) => {
|
||||||
m.chai.expect(metadata).to.deep.equal({
|
m.chai.expect(metadata).to.deep.equal({
|
||||||
path: image,
|
path: image,
|
||||||
size: {
|
size: {
|
||||||
@ -55,7 +55,6 @@ describe('ImageStream: IMG', function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -68,42 +68,42 @@ describe('ImageStream: Metadata ZIP', function() {
|
|||||||
archive,
|
archive,
|
||||||
path.join(IMAGES_PATH, 'raspberrypi.img'));
|
path.join(IMAGES_PATH, 'raspberrypi.img'));
|
||||||
|
|
||||||
it('should read the manifest name property', function(done) {
|
it('should read the manifest name property', function() {
|
||||||
testMetadataProperty(archive, 'name', 'Raspberry Pi').asCallback(done);
|
return testMetadataProperty(archive, 'name', 'Raspberry Pi');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should read the manifest version property', function(done) {
|
it('should read the manifest version property', function() {
|
||||||
testMetadataProperty(archive, 'version', '1.0.0').asCallback(done);
|
return testMetadataProperty(archive, 'version', '1.0.0');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should read the manifest url property', function(done) {
|
it('should read the manifest url property', function() {
|
||||||
testMetadataProperty(archive, 'url', 'https://www.raspberrypi.org').asCallback(done);
|
return testMetadataProperty(archive, 'url', 'https://www.raspberrypi.org');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should read the manifest supportUrl property', function(done) {
|
it('should read the manifest supportUrl property', function() {
|
||||||
const expectedValue = 'https://www.raspberrypi.org/forums/';
|
const expectedValue = 'https://www.raspberrypi.org/forums/';
|
||||||
testMetadataProperty(archive, 'supportUrl', expectedValue).asCallback(done);
|
return testMetadataProperty(archive, 'supportUrl', expectedValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should read the manifest releaseNotesUrl property', function(done) {
|
it('should read the manifest releaseNotesUrl property', function() {
|
||||||
const expectedValue = 'http://downloads.raspberrypi.org/raspbian/release_notes.txt';
|
const expectedValue = 'http://downloads.raspberrypi.org/raspbian/release_notes.txt';
|
||||||
testMetadataProperty(archive, 'releaseNotesUrl', expectedValue).asCallback(done);
|
return testMetadataProperty(archive, 'releaseNotesUrl', expectedValue);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should read the manifest checksumType property', function(done) {
|
it('should read the manifest checksumType property', function() {
|
||||||
testMetadataProperty(archive, 'checksumType', 'md5').asCallback(done);
|
return testMetadataProperty(archive, 'checksumType', 'md5');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should read the manifest checksum property', function(done) {
|
it('should read the manifest checksum property', function() {
|
||||||
testMetadataProperty(archive, 'checksum', 'add060b285d512f56c175b76b7ef1bee').asCallback(done);
|
return testMetadataProperty(archive, 'checksum', 'add060b285d512f56c175b76b7ef1bee');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should read the manifest bytesToZeroOutFromTheBeginning property', function(done) {
|
it('should read the manifest bytesToZeroOutFromTheBeginning property', function() {
|
||||||
testMetadataProperty(archive, 'bytesToZeroOutFromTheBeginning', 512).asCallback(done);
|
return testMetadataProperty(archive, 'bytesToZeroOutFromTheBeginning', 512);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should read the manifest recommendedDriveSize property', function(done) {
|
it('should read the manifest recommendedDriveSize property', function() {
|
||||||
testMetadataProperty(archive, 'recommendedDriveSize', 4294967296).asCallback(done);
|
return testMetadataProperty(archive, 'recommendedDriveSize', 4294967296);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -119,8 +119,8 @@ describe('ImageStream: Metadata ZIP', function() {
|
|||||||
''
|
''
|
||||||
].join('\n');
|
].join('\n');
|
||||||
|
|
||||||
it('should read the logo contents', function(done) {
|
it('should read the logo contents', function() {
|
||||||
testMetadataProperty(archive, 'logo', logo).asCallback(done);
|
return testMetadataProperty(archive, 'logo', logo);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -145,8 +145,8 @@ describe('ImageStream: Metadata ZIP', function() {
|
|||||||
''
|
''
|
||||||
].join('\n');
|
].join('\n');
|
||||||
|
|
||||||
it('should read the bmap contents', function(done) {
|
it('should read the bmap contents', function() {
|
||||||
testMetadataProperty(archive, 'bmap', bmap).asCallback(done);
|
return testMetadataProperty(archive, 'bmap', bmap);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -162,8 +162,8 @@ describe('ImageStream: Metadata ZIP', function() {
|
|||||||
''
|
''
|
||||||
].join('\n');
|
].join('\n');
|
||||||
|
|
||||||
it('should read the instruction contents', function(done) {
|
it('should read the instruction contents', function() {
|
||||||
testMetadataProperty(archive, 'instructions', instructions).asCallback(done);
|
return testMetadataProperty(archive, 'instructions', instructions);
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -44,22 +44,21 @@ const deleteIfExists = (file) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
exports.expectError = function(file, errorMessage) {
|
exports.expectError = function(file, errorMessage) {
|
||||||
it('should be rejected with an error', function(done) {
|
it('should be rejected with an error', function() {
|
||||||
imageStream.getFromFilePath(file).catch((error) => {
|
return imageStream.getFromFilePath(file).catch((error) => {
|
||||||
m.chai.expect(error).to.be.an.instanceof(Error);
|
m.chai.expect(error).to.be.an.instanceof(Error);
|
||||||
m.chai.expect(error.message).to.equal(errorMessage);
|
m.chai.expect(error.message).to.equal(errorMessage);
|
||||||
m.chai.expect(error.description).to.be.a.string;
|
m.chai.expect(error.description).to.be.a.string;
|
||||||
m.chai.expect(error.description.length > 0).to.be.true;
|
m.chai.expect(error.description.length > 0).to.be.true;
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.extractFromFilePath = function(file, image) {
|
exports.extractFromFilePath = function(file, image) {
|
||||||
it('should be able to extract the image', function(done) {
|
it('should be able to extract the image', function() {
|
||||||
const output = tmp.tmpNameSync();
|
const output = tmp.tmpNameSync();
|
||||||
|
|
||||||
imageStream.getFromFilePath(file).then(function(results) {
|
return imageStream.getFromFilePath(file).then(function(results) {
|
||||||
if (!_.some([
|
if (!_.some([
|
||||||
results.size.original === fs.statSync(file).size,
|
results.size.original === fs.statSync(file).size,
|
||||||
results.size.original === fs.statSync(image).size
|
results.size.original === fs.statSync(image).size
|
||||||
@ -81,6 +80,6 @@ exports.extractFromFilePath = function(file, image) {
|
|||||||
m.chai.expect(areEqual).to.be.true;
|
m.chai.expect(areEqual).to.be.true;
|
||||||
}).finally(function() {
|
}).finally(function() {
|
||||||
return deleteIfExists(output);
|
return deleteIfExists(output);
|
||||||
}).nodeify(done);
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@ -97,11 +97,10 @@ describe('ImageStream: Utils', function() {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should yield the stream data', function(done) {
|
it('should yield the stream data', function() {
|
||||||
utils.extractStream(this.stream).then((data) => {
|
return utils.extractStream(this.stream).then((data) => {
|
||||||
m.chai.expect(data.toString()).to.equal('Hello World');
|
m.chai.expect(data.toString()).to.equal('Hello World');
|
||||||
done();
|
});
|
||||||
}).catch(done);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
@ -121,11 +120,10 @@ describe('ImageStream: Utils', function() {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should be rejected with the error', function(done) {
|
it('should be rejected with the error', function() {
|
||||||
utils.extractStream(this.stream).catch((error) => {
|
return utils.extractStream(this.stream).catch((error) => {
|
||||||
m.chai.expect(error).to.be.an.instanceof(Error);
|
m.chai.expect(error).to.be.an.instanceof(Error);
|
||||||
m.chai.expect(error.message).to.equal('stream error');
|
m.chai.expect(error.message).to.equal('stream error');
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -41,12 +41,12 @@ describe('ImageStream: XZ', function() {
|
|||||||
|
|
||||||
describe('.getImageMetadata()', function() {
|
describe('.getImageMetadata()', function() {
|
||||||
|
|
||||||
it('should return the correct metadata', function(done) {
|
it('should return the correct metadata', function() {
|
||||||
const image = path.join(XZ_PATH, 'raspberrypi.img.xz');
|
const image = path.join(XZ_PATH, 'raspberrypi.img.xz');
|
||||||
const compressedSize = fs.statSync(image).size;
|
const compressedSize = fs.statSync(image).size;
|
||||||
const uncompressedSize = fs.statSync(path.join(IMAGES_PATH, 'raspberrypi.img')).size;
|
const uncompressedSize = fs.statSync(path.join(IMAGES_PATH, 'raspberrypi.img')).size;
|
||||||
|
|
||||||
imageStream.getImageMetadata(image).then((metadata) => {
|
return imageStream.getImageMetadata(image).then((metadata) => {
|
||||||
m.chai.expect(metadata).to.deep.equal({
|
m.chai.expect(metadata).to.deep.equal({
|
||||||
path: image,
|
path: image,
|
||||||
size: {
|
size: {
|
||||||
@ -57,7 +57,6 @@ describe('ImageStream: XZ', function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -65,11 +65,11 @@ describe('ImageStream: ZIP', function() {
|
|||||||
|
|
||||||
describe('.getImageMetadata()', function() {
|
describe('.getImageMetadata()', function() {
|
||||||
|
|
||||||
it('should return the correct metadata', function(done) {
|
it('should return the correct metadata', function() {
|
||||||
const image = path.join(ZIP_PATH, 'zip-directory-rpi-only.zip');
|
const image = path.join(ZIP_PATH, 'zip-directory-rpi-only.zip');
|
||||||
const expectedSize = fs.statSync(path.join(IMAGES_PATH, 'raspberrypi.img')).size;
|
const expectedSize = fs.statSync(path.join(IMAGES_PATH, 'raspberrypi.img')).size;
|
||||||
|
|
||||||
imageStream.getImageMetadata(image).then((metadata) => {
|
return imageStream.getImageMetadata(image).then((metadata) => {
|
||||||
m.chai.expect(metadata).to.deep.equal({
|
m.chai.expect(metadata).to.deep.equal({
|
||||||
path: image,
|
path: image,
|
||||||
size: {
|
size: {
|
||||||
@ -80,7 +80,6 @@ describe('ImageStream: ZIP', function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
done();
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user