metro: switch Assets-test to the new memory-fs

Reviewed By: mjesun

Differential Revision: D7135767

fbshipit-source-id: dd7998b8aca87391ec2081770645ba73d11cf859
This commit is contained in:
Jean Lauliac 2018-03-05 07:33:11 -08:00 committed by Facebook Github Bot
parent e565f10a30
commit 08283058ec
2 changed files with 87 additions and 117 deletions

View File

@ -88,6 +88,7 @@ const ASYNC_FUNC_NAMES = [
'readdir', 'readdir',
'readFile', 'readFile',
'realpath', 'realpath',
'stat',
'write', 'write',
'writeFile', 'writeFile',
]; ];

View File

@ -10,12 +10,14 @@
'use strict'; 'use strict';
jest.mock('fs'); jest.mock('fs', () => new (require('metro-memory-fs'))());
jest.mock('image-size'); jest.mock('image-size');
const {getAssetData, getAsset} = require('../'); const {getAssetData, getAsset} = require('../');
const crypto = require('crypto'); const crypto = require('crypto');
const fs = require('fs'); const fs = require('fs');
const mkdirp = require('mkdirp');
const path = require('path');
const mockImageWidth = 300; const mockImageWidth = 300;
const mockImageHeight = 200; const mockImageHeight = 200;
@ -26,41 +28,35 @@ require('image-size').mockReturnValue({
}); });
describe('getAsset', () => { describe('getAsset', () => {
beforeEach(() => {
fs.reset();
mkdirp.sync('/root/imgs');
});
it('should work for the simple case', () => { it('should work for the simple case', () => {
fs.__setMockFilesystem({ writeImages({'b.png': 'b image', 'b@2x.png': 'b2 image'});
root: {
imgs: {
'b.png': 'b image',
'b@2x.png': 'b2 image',
},
},
});
return Promise.all([ return Promise.all([
getAsset('imgs/b.png', ['/root']), getAssetStr('imgs/b.png', ['/root']),
getAsset('imgs/b@1x.png', ['/root']), getAssetStr('imgs/b@1x.png', ['/root']),
]).then(resp => resp.forEach(data => expect(data).toBe('b image'))); ]).then(resp => resp.forEach(data => expect(data).toBe('b image')));
}); });
it('should work for the simple case with platform ext', async () => { it('should work for the simple case with platform ext', async () => {
fs.__setMockFilesystem({ writeImages({
root: { 'b.ios.png': 'b ios image',
imgs: { 'b.android.png': 'b android image',
'b.ios.png': 'b ios image', 'c.png': 'c general image',
'b.android.png': 'b android image', 'c.android.png': 'c android image',
'c.png': 'c general image',
'c.android.png': 'c android image',
},
},
}); });
expect( expect(
await Promise.all([ await Promise.all([
getAsset('imgs/b.png', ['/root'], 'ios'), getAssetStr('imgs/b.png', ['/root'], 'ios'),
getAsset('imgs/b.png', ['/root'], 'android'), getAssetStr('imgs/b.png', ['/root'], 'android'),
getAsset('imgs/c.png', ['/root'], 'android'), getAssetStr('imgs/c.png', ['/root'], 'android'),
getAsset('imgs/c.png', ['/root'], 'ios'), getAssetStr('imgs/c.png', ['/root'], 'ios'),
getAsset('imgs/c.png', ['/root']), getAssetStr('imgs/c.png', ['/root']),
]), ]),
).toEqual([ ).toEqual([
'b ios image', 'b ios image',
@ -72,93 +68,71 @@ describe('getAsset', () => {
}); });
it('should work for the simple case with jpg', () => { it('should work for the simple case with jpg', () => {
fs.__setMockFilesystem({ writeImages({
root: { 'b.png': 'png image',
imgs: { 'b.jpg': 'jpeg image',
'b.png': 'png image',
'b.jpg': 'jpeg image',
},
},
}); });
return Promise.all([ return Promise.all([
getAsset('imgs/b.jpg', ['/root']), getAssetStr('imgs/b.jpg', ['/root']),
getAsset('imgs/b.png', ['/root']), getAssetStr('imgs/b.png', ['/root']),
]).then(data => expect(data).toEqual(['jpeg image', 'png image'])); ]).then(data => expect(data).toEqual(['jpeg image', 'png image']));
}); });
it('should pick the bigger one', async () => { it('should pick the bigger one', async () => {
fs.__setMockFilesystem({ writeImages({
root: { 'b@1x.png': 'b1 image',
imgs: { 'b@2x.png': 'b2 image',
'b@1x.png': 'b1 image', 'b@4x.png': 'b4 image',
'b@2x.png': 'b2 image', 'b@4.5x.png': 'b4.5 image',
'b@4x.png': 'b4 image',
'b@4.5x.png': 'b4.5 image',
},
},
}); });
expect(await getAsset('imgs/b@3x.png', ['/root'])).toBe('b4 image'); expect(await getAssetStr('imgs/b@3x.png', ['/root'])).toBe('b4 image');
}); });
it('should pick the bigger one with platform ext', async () => { it('should pick the bigger one with platform ext', async () => {
fs.__setMockFilesystem({ writeImages({
root: { 'b@1x.png': 'b1 image',
imgs: { 'b@2x.png': 'b2 image',
'b@1x.png': 'b1 image', 'b@4x.png': 'b4 image',
'b@2x.png': 'b2 image', 'b@4.5x.png': 'b4.5 image',
'b@4x.png': 'b4 image', 'b@1x.ios.png': 'b1 ios image',
'b@4.5x.png': 'b4.5 image', 'b@2x.ios.png': 'b2 ios image',
'b@1x.ios.png': 'b1 ios image', 'b@4x.ios.png': 'b4 ios image',
'b@2x.ios.png': 'b2 ios image', 'b@4.5x.ios.png': 'b4.5 ios image',
'b@4x.ios.png': 'b4 ios image',
'b@4.5x.ios.png': 'b4.5 ios image',
},
},
}); });
expect( expect(
await Promise.all([ await Promise.all([
getAsset('imgs/b@3x.png', ['/root']), getAssetStr('imgs/b@3x.png', ['/root']),
getAsset('imgs/b@3x.png', ['/root'], 'ios'), getAssetStr('imgs/b@3x.png', ['/root'], 'ios'),
]), ]),
).toEqual(['b4 image', 'b4 ios image']); ).toEqual(['b4 image', 'b4 ios image']);
}); });
it('should support multiple project roots', async () => { it('should support multiple project roots', async () => {
fs.__setMockFilesystem({ writeImages({'b.png': 'b image'});
root: { mkdirp.sync('/root2/newImages/imgs');
imgs: { fs.writeFileSync('/root2/newImages/imgs/b@1x.png', 'b1 image');
'b.png': 'b image',
},
},
root2: {
newImages: {
imgs: {
'b@1x.png': 'b1 image',
},
},
},
});
expect(await getAsset('newImages/imgs/b.png', ['/root', '/root2'])).toBe( expect(await getAssetStr('newImages/imgs/b.png', ['/root', '/root2'])).toBe(
'b1 image', 'b1 image',
); );
}); });
}); });
describe('getAssetData', () => { describe('getAssetData', () => {
beforeEach(() => {
fs.reset();
mkdirp.sync('/root/imgs');
});
it('should get assetData', () => { it('should get assetData', () => {
fs.__setMockFilesystem({ writeImages({
root: { 'b@1x.png': 'b1 image',
imgs: { 'b@2x.png': 'b2 image',
'b@1x.png': 'b1 image', 'b@4x.png': 'b4 image',
'b@2x.png': 'b2 image', 'b@4.5x.png': 'b4.5 image',
'b@4x.png': 'b4 image',
'b@4.5x.png': 'b4.5 image',
},
},
}); });
return getAssetData('/root/imgs/b.png', 'imgs/b.png', []).then(data => { return getAssetData('/root/imgs/b.png', 'imgs/b.png', []).then(data => {
@ -182,15 +156,11 @@ describe('getAssetData', () => {
}); });
it('should get assetData for non-png images', async () => { it('should get assetData for non-png images', async () => {
fs.__setMockFilesystem({ writeImages({
root: { 'b@1x.jpg': 'b1 image',
imgs: { 'b@2x.jpg': 'b2 image',
'b@1x.jpg': 'b1 image', 'b@4x.jpg': 'b4 image',
'b@2x.jpg': 'b2 image', 'b@4.5x.jpg': 'b4.5 image',
'b@4x.jpg': 'b4 image',
'b@4.5x.jpg': 'b4.5 image',
},
},
}); });
const data = await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg', []); const data = await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg', []);
@ -240,14 +210,10 @@ describe('getAssetData', () => {
{virtual: true}, {virtual: true},
); );
fs.__setMockFilesystem({ writeImages({
root: { 'b@1x.png': 'b1 image',
imgs: { 'b@2x.png': 'b2 image',
'b@1x.png': 'b1 image', 'b@3x.png': 'b3 image',
'b@2x.png': 'b2 image',
'b@3x.png': 'b3 image',
},
},
}); });
const data = await getAssetData('/root/imgs/b.png', 'imgs/b.png', [ const data = await getAssetData('/root/imgs/b.png', 'imgs/b.png', [
@ -275,28 +241,20 @@ describe('getAssetData', () => {
}); });
describe('hash:', () => { describe('hash:', () => {
let mockFS;
beforeEach(() => { beforeEach(() => {
mockFS = { writeImages({
root: { 'b@1x.jpg': 'b1 image',
imgs: { 'b@2x.jpg': 'b2 image',
'b@1x.jpg': 'b1 image', 'b@4x.jpg': 'b4 image',
'b@2x.jpg': 'b2 image', 'b@4.5x.jpg': 'b4.5 image',
'b@4x.jpg': 'b4 image', });
'b@4.5x.jpg': 'b4.5 image',
},
},
};
fs.__setMockFilesystem(mockFS);
}); });
it('uses the file contents to build the hash', async () => { it('uses the file contents to build the hash', async () => {
const hash = crypto.createHash('md5'); const hash = crypto.createHash('md5');
for (const name in mockFS.root.imgs) { for (const name of fs.readdirSync('/root/imgs')) {
hash.update(mockFS.root.imgs[name]); hash.update(fs.readFileSync(path.join('/root/imgs', name), 'utf8'));
} }
expect(await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg', [])).toEqual( expect(await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg', [])).toEqual(
@ -311,10 +269,21 @@ describe('getAssetData', () => {
[], [],
); );
mockFS.root.imgs['b@4x.jpg'] = 'updated data'; fs.writeFileSync('/root/imgs/b@4x.jpg', 'updated data');
const data = await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg', []); const data = await getAssetData('/root/imgs/b.jpg', 'imgs/b.jpg', []);
expect(data.hash).not.toEqual(initialData.hash); expect(data.hash).not.toEqual(initialData.hash);
}); });
}); });
}); });
function writeImages(imgMap) {
for (const fileName in imgMap) {
fs.writeFileSync(path.join('/root/imgs', fileName), imgMap[fileName]);
}
}
async function getAssetStr(...args) {
const buffer = await getAsset(...args);
return buffer.toString('utf8');
}