Skip to content

Commit

Permalink
Asset Install Fix from Mobilespec Report (#30)
Browse files Browse the repository at this point in the history
* Asset Install Fix from Mobilespec Report
* handler.js cleanup and test refactor
  • Loading branch information
erisu authored Feb 25, 2019
1 parent ff232ca commit 6548e75
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 19 deletions.
8 changes: 3 additions & 5 deletions bin/templates/cordova/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,12 +121,10 @@ module.exports = {
install: (asset, plugin_dir, wwwDest) => {
const src = path.join(plugin_dir, asset.src);
const dest = path.join(wwwDest, asset.target);
const destDir = path.parse(dest).dir;

if (fs.statSync(src).isDirectory()) {
fs.copySync(src + '/*', dest);
} else {
fs.copySync(src, dest);
}
if (destDir) fs.ensureDirSync(destDir);
fs.copySync(src, dest);
},
uninstall: (asset, wwwDest, plugin_id) => {
fs.removeSync(path.join(wwwDest, asset.target));
Expand Down
37 changes: 23 additions & 14 deletions tests/spec/unit/handler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -330,37 +330,46 @@ describe('Handler export', () => {
const wwwDest = 'dest';

describe('Install', () => {
it('if src is a directory, should be called with cp, -Rf', () => {
const asset = { itemType: 'asset', src: 'someSrc/ServiceWorker.js', target: 'ServiceWorker.js' };
it('should copySync with a directory path.', () => {
const asset = {
itemType: 'asset',
src: 'someSrc/ServiceWorker.js',
target: 'ServiceWorker.js'
};

// Spies
const copySyncSpy = jasmine.createSpy('copySync').and.returnValue('-Rf');
const fsstatMock = { isDirectory: () => true };
const statSyncSpy = jasmine.createSpy('statSync').and.returnValue(fsstatMock);
const copySyncSpy = jasmine.createSpy('copySync');
const ensureDirSyncSpy = jasmine.createSpy('ensureDirSync').and.returnValue(true);

handler.__set__('fs', {
copySync: copySyncSpy,
statSync: statSyncSpy
ensureDirSync: ensureDirSyncSpy
});

handler.asset.install(asset, plugin_dir, wwwDest);
expect(ensureDirSyncSpy).toHaveBeenCalled();
expect(copySyncSpy).toHaveBeenCalledWith(jasmine.any(String), path.join('dest', asset.target));
});

it('if src is not a directory, should be called with cp, -f', () => {
const asset = { itemType: 'asset', src: 'someSrc', target: 'ServiceWorker.js' };
const cpPath = path.join(plugin_dir, asset.src);
it('should call copySync with a file path.', () => {
const asset = {
itemType: 'asset',
src: 'someSrc/ServiceWorker.js',
target: 'ServiceWorker.js'
};

// Spies
const copySyncSpy = jasmine.createSpy('copySync').and.returnValue('-f');
const fsstatMock = { isDirectory: () => false };
const statSyncSpy = jasmine.createSpy('statSync').and.returnValue(fsstatMock);
const copySyncSpy = jasmine.createSpy('copySync');
const ensureDirSyncSpy = jasmine.createSpy('ensureDirSync');

handler.__set__('fs', {
copySync: copySyncSpy,
statSync: statSyncSpy
ensureDirSync: ensureDirSyncSpy
});

handler.asset.install(asset, plugin_dir, wwwDest);
expect(copySyncSpy).toHaveBeenCalledWith(cpPath, path.join('dest', asset.target));
expect(ensureDirSyncSpy).toHaveBeenCalled();
expect(copySyncSpy).toHaveBeenCalledWith(jasmine.any(String), path.join('dest', asset.target));
});
});

Expand Down

0 comments on commit 6548e75

Please sign in to comment.