Skip to content

Commit

Permalink
breaking: replace more shell.cp w/ fs.copySync
Browse files Browse the repository at this point in the history
* Added missing defaults.xml test fixture file
  • Loading branch information
erisu committed May 16, 2020
1 parent be21ddc commit a55f605
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 43 deletions.
6 changes: 1 addition & 5 deletions bin/templates/scripts/cordova/lib/plugman/pluginHandlers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
'use strict';
const fs = require('fs-extra');
const path = require('path');
const shell = require('shelljs');
const util = require('util');
const events = require('cordova-common').events;
const CordovaError = require('cordova-common').CordovaError;
Expand Down Expand Up @@ -323,11 +322,8 @@ function copyFile (plugin_dir, src, project_dir, dest, link) {

if (link) {
linkFileOrDirTree(src, dest);
} else if (fs.statSync(src).isDirectory()) {
// XXX shelljs decides to create a directory when -R|-r is used which sucks. http://goo.gl/nbsjq
shell.cp('-Rf', path.join(src, '/*'), dest);
} else {
shell.cp('-f', src, dest);
fs.copySync(src, dest);
}
}

Expand Down
3 changes: 1 addition & 2 deletions bin/templates/scripts/cordova/lib/prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
const Q = require('q');
const fs = require('fs-extra');
const path = require('path');
const shell = require('shelljs');
const unorm = require('unorm');
const plist = require('plist');
const URL = require('url');
Expand Down Expand Up @@ -109,7 +108,7 @@ function updateConfigFile (sourceConfig, configMunger, locations) {

// First cleanup current config and merge project's one into own
// Overwrite platform config.xml with defaults.xml.
shell.cp('-f', locations.defaultConfigXml, locations.configXml);
fs.copySync(locations.defaultConfigXml, locations.configXml);

// Then apply config changes from global munge to all config files
// in project (including project's config)
Expand Down
8 changes: 3 additions & 5 deletions tests/spec/unit/Plugman/common.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
const fs = require('fs-extra');
const path = require('path');
const osenv = require('os');
const shell = require('shelljs');
const rewire = require('rewire');

const common = rewire('../../../../bin/templates/scripts/cordova/lib/plugman/pluginHandlers');
Expand Down Expand Up @@ -107,13 +106,13 @@ describe('common handler routines', () => {
fs.ensureDirSync(srcDirTree);
fs.writeFileSync(srcFile, 'contents', 'utf-8');

const s = spyOn(shell, 'cp').and.callThrough();
spyOn(fs, 'copySync').and.callThrough();
const resolvedDest = path.resolve(project_dir, dest);

copyFile(test_dir, srcFile, project_dir, dest);

expect(s).toHaveBeenCalled();
expect(s).toHaveBeenCalledWith('-f', srcFile, resolvedDest);
expect(fs.copySync).toHaveBeenCalled();
expect(fs.copySync).toHaveBeenCalledWith(srcFile, resolvedDest);

fs.removeSync(project_dir);
});
Expand All @@ -129,7 +128,6 @@ describe('common handler routines', () => {
});

describe('deleteJava', () => {
// This is testing that shelljs.rm is removing the source file.
it('Test 009 : source file should have been removed', () => {
fs.ensureDirSync(srcDirTree);
fs.writeFileSync(srcFile, 'contents', 'utf-8');
Expand Down
39 changes: 19 additions & 20 deletions tests/spec/unit/Plugman/pluginHandler.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ const os = require('os');
const fs = require('fs-extra');
const path = require('path');
const rewire = require('rewire');
const shell = require('shelljs');
const EventEmitter = require('events');

const PluginInfo = require('cordova-common').PluginInfo;
Expand All @@ -32,7 +31,7 @@ const pluginHandlers = rewire('../../../../bin/templates/scripts/cordova/lib/plu
const temp = path.join(os.tmpdir(), 'plugman');

const FIXTURES = path.join(__dirname, '../fixtures');
const iosProject = path.join(FIXTURES, 'ios-config-xml', '*');
const iosProject = path.join(FIXTURES, 'ios-config-xml');
const faultyplugin = path.join(FIXTURES, 'org.test.plugins.faultyplugin');
const dummyplugin = path.join(FIXTURES, 'org.test.plugins.dummyplugin');
const weblessplugin = path.join(FIXTURES, 'org.test.plugins.weblessplugin');
Expand Down Expand Up @@ -68,7 +67,7 @@ describe('ios plugin handler', () => {
let dummyProject;

beforeEach(() => {
shell.cp('-rf', iosProject, temp);
fs.copySync(iosProject, temp);
projectFile.purgeProjectFileCache(temp);

dummyProject = projectFile.parse({
Expand Down Expand Up @@ -118,15 +117,15 @@ describe('ios plugin handler', () => {
});
it('Test 005 : should cp the file to the right target location when element has no target-dir', () => {
const source = copyArray(valid_source).filter(s => s.targetDir === undefined);
const spy = spyOn(shell, 'cp');
spyOn(fs, 'copySync');
install(source[0], dummyPluginInfo, dummyProject);
expect(spy).toHaveBeenCalledWith('-f', path.join(dummyplugin, 'src', 'ios', 'DummyPluginCommand.m'), path.join(temp, 'SampleApp', 'Plugins', dummy_id, 'DummyPluginCommand.m'));
expect(fs.copySync).toHaveBeenCalledWith(path.join(dummyplugin, 'src', 'ios', 'DummyPluginCommand.m'), path.join(temp, 'SampleApp', 'Plugins', dummy_id, 'DummyPluginCommand.m'));
});
it('Test 006 : should cp the file to the right target location when element has a target-dir', () => {
const source = copyArray(valid_source).filter(s => s.targetDir !== undefined);
const spy = spyOn(shell, 'cp');
spyOn(fs, 'copySync');
install(source[0], dummyPluginInfo, dummyProject);
expect(spy).toHaveBeenCalledWith('-f', path.join(dummyplugin, 'src', 'ios', 'TargetDirTest.m'), path.join(temp, 'SampleApp', 'Plugins', dummy_id, 'targetDir', 'TargetDirTest.m'));
expect(fs.copySync).toHaveBeenCalledWith(path.join(dummyplugin, 'src', 'ios', 'TargetDirTest.m'), path.join(temp, 'SampleApp', 'Plugins', dummy_id, 'targetDir', 'TargetDirTest.m'));
});
it('Test 007 : should call into xcodeproj\'s addFramework appropriately when element has framework=true set', () => {
const source = copyArray(valid_source).filter(s => s.framework);
Expand Down Expand Up @@ -173,15 +172,15 @@ describe('ios plugin handler', () => {
});
it('Test 012 : should cp the file to the right target location when element has no target-dir', () => {
const headers = copyArray(valid_headers).filter(s => s.targetDir === undefined);
const spy = spyOn(shell, 'cp');
spyOn(fs, 'copySync');
install(headers[0], dummyPluginInfo, dummyProject);
expect(spy).toHaveBeenCalledWith('-f', path.join(dummyplugin, 'src', 'ios', 'DummyPluginCommand.h'), path.join(temp, 'SampleApp', 'Plugins', dummy_id, 'DummyPluginCommand.h'));
expect(fs.copySync).toHaveBeenCalledWith(path.join(dummyplugin, 'src', 'ios', 'DummyPluginCommand.h'), path.join(temp, 'SampleApp', 'Plugins', dummy_id, 'DummyPluginCommand.h'));
});
it('Test 013 : should cp the file to the right target location when element has a target-dir', () => {
const headers = copyArray(valid_headers).filter(s => s.targetDir !== undefined);
const spy = spyOn(shell, 'cp');
spyOn(fs, 'copySync');
install(headers[0], dummyPluginInfo, dummyProject);
expect(spy).toHaveBeenCalledWith('-f', path.join(dummyplugin, 'src', 'ios', 'TargetDirTest.h'), path.join(temp, 'SampleApp', 'Plugins', dummy_id, 'targetDir', 'TargetDirTest.h'));
expect(fs.copySync).toHaveBeenCalledWith(path.join(dummyplugin, 'src', 'ios', 'TargetDirTest.h'), path.join(temp, 'SampleApp', 'Plugins', dummy_id, 'targetDir', 'TargetDirTest.h'));
});
});

Expand Down Expand Up @@ -215,9 +214,9 @@ describe('ios plugin handler', () => {
});
it('Test 017 : should cp the file to the right target location', () => {
const resources = copyArray(valid_resources);
const spy = spyOn(shell, 'cp');
spyOn(fs, 'copySync');
install(resources[0], dummyPluginInfo, dummyProject);
expect(spy).toHaveBeenCalledWith('-f', path.join(dummyplugin, 'src', 'ios', 'DummyPlugin.bundle'), path.join(temp, 'SampleApp', 'Resources', 'DummyPlugin.bundle'));
expect(fs.copySync).toHaveBeenCalledWith(path.join(dummyplugin, 'src', 'ios', 'DummyPlugin.bundle'), path.join(temp, 'SampleApp', 'Resources', 'DummyPlugin.bundle'));
});

it('Test 018 : should link files to the right target location', () => {
Expand Down Expand Up @@ -274,9 +273,9 @@ describe('ios plugin handler', () => {
});
it('Test 022 : should cp the file to the right target location', () => {
const frameworks = copyArray(valid_custom_frameworks);
const spy = spyOn(shell, 'cp');
spyOn(fs, 'copySync');
install(frameworks[0], dummyPluginInfo, dummyProject);
expect(spy).toHaveBeenCalledWith('-Rf', path.join(dummyplugin, 'src', 'ios', 'Custom.framework', '*'),
expect(fs.copySync).toHaveBeenCalledWith(path.join(dummyplugin, 'src', 'ios', 'Custom.framework'),
path.join(temp, 'SampleApp/Plugins/org.test.plugins.dummyplugin/Custom.framework'));
});

Expand Down Expand Up @@ -325,21 +324,21 @@ describe('ios plugin handler', () => {
/* eslint-enable no-unused-vars */

beforeEach(() => {
spyOn(shell, 'cp');
spyOn(fs, 'copySync');
wwwDest = path.resolve(dummyProject.www, asset.target);
platformWwwDest = path.resolve(dummyProject.platformWww, asset.target);
});

it('Test 026 : should put asset to both www and platform_www when options.usePlatformWww flag is specified', () => {
install(asset, dummyPluginInfo, dummyProject, { usePlatformWww: true });
expect(shell.cp).toHaveBeenCalledWith('-f', path.resolve(dummyPluginInfo.dir, asset.src), path.resolve(dummyProject.www, asset.target));
expect(shell.cp).toHaveBeenCalledWith('-f', path.resolve(dummyPluginInfo.dir, asset.src), path.resolve(dummyProject.platformWww, asset.target));
expect(fs.copySync).toHaveBeenCalledWith(path.resolve(dummyPluginInfo.dir, asset.src), path.resolve(dummyProject.www, asset.target));
expect(fs.copySync).toHaveBeenCalledWith(path.resolve(dummyPluginInfo.dir, asset.src), path.resolve(dummyProject.platformWww, asset.target));
});

it('Test 027 : should put asset to www only when options.usePlatformWww flag is not specified', () => {
install(asset, dummyPluginInfo, dummyProject);
expect(shell.cp).toHaveBeenCalledWith('-f', path.resolve(dummyPluginInfo.dir, asset.src), path.resolve(dummyProject.www, asset.target));
expect(shell.cp).not.toHaveBeenCalledWith(path.resolve(dummyPluginInfo.dir, asset.src), path.resolve(dummyProject.platformWww, asset.target));
expect(fs.copySync).toHaveBeenCalledWith(path.resolve(dummyPluginInfo.dir, asset.src), path.resolve(dummyProject.www, asset.target));
expect(fs.copySync).not.toHaveBeenCalledWith(path.resolve(dummyPluginInfo.dir, asset.src), path.resolve(dummyProject.platformWww, asset.target));
});
});

Expand Down
62 changes: 62 additions & 0 deletions tests/spec/unit/fixtures/ios-config-xml/cordova/defaults.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<widget xmlns = "http://www.w3.org/ns/widgets"
id = "io.cordova.helloCordova"
version = "2.0.0">

<!-- Preferences for iOS -->
<preference name="AllowInlineMediaPlayback" value="false" />
<preference name="BackupWebStorage" value="cloud" />
<preference name="DisallowOverscroll" value="false" />
<preference name="EnableViewportScale" value="false" />
<preference name="KeyboardDisplayRequiresUserAction" value="true" />
<preference name="MediaTypesRequiringUserActionForPlayback" value="none" />
<preference name="SuppressesIncrementalRendering" value="false" />
<preference name="SuppressesLongPressGesture" value="false" />
<preference name="Suppresses3DTouchGesture" value="false" />
<preference name="GapBetweenPages" value="0" />
<preference name="PageLength" value="0" />
<preference name="PaginationBreakingMode" value="page" /> <!-- page, column -->
<preference name="PaginationMode" value="unpaginated" /> <!-- unpaginated, leftToRight, topToBottom, bottomToTop, rightToLeft -->

<feature name="CDVWebViewEngine">
<param name="ios-package" value="CDVWebViewEngine" />
</feature>
<feature name="LocalStorage">
<param name="ios-package" value="CDVLocalStorage"/>
</feature>
<feature name="Console">
<param name="ios-package" value="CDVLogger"/>
<param name="onload" value="true"/>
</feature>
<feature name="HandleOpenUrl">
<param name="ios-package" value="CDVHandleOpenURL"/>
<param name="onload" value="true"/>
</feature>
<feature name="IntentAndNavigationFilter">
<param name="ios-package" value="CDVIntentAndNavigationFilter"/>
<param name="onload" value="true"/>
</feature>
<feature name="GestureHandler">
<param name="ios-package" value="CDVGestureHandler"/>
<param name="onload" value="true"/>
</feature>

</widget>
9 changes: 4 additions & 5 deletions tests/spec/unit/prepare.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const fs = require('fs-extra');
const EventEmitter = require('events');
const os = require('os');
const path = require('path');
const shell = require('shelljs');
const plist = require('plist');
const xcode = require('xcode');
const rewire = require('rewire');
Expand All @@ -46,12 +45,12 @@ describe('prepare', () => {
Api = rewire('../../../bin/templates/scripts/cordova/Api');

fs.ensureDirSync(iosPlatform);
shell.cp('-rf', `${iosProjectFixture}/*`, iosPlatform);
fs.copySync(iosProjectFixture, iosPlatform);
p = new Api('ios', iosPlatform, new EventEmitter());
});

afterEach(() => {
fs.removeSync(path.join(__dirname, 'some'));
fs.removeSync(iosPlatform);
});

describe('launch storyboard feature (CB-9762)', () => {
Expand Down Expand Up @@ -364,7 +363,7 @@ describe('prepare', () => {
};

// copy the splash screen fixtures to the iOS project
shell.cp('-rf', path.join(FIXTURES, 'launch-storyboard-support', 'res'), iosProject);
fs.copySync(path.join(FIXTURES, 'launch-storyboard-support', 'res'), path.join(iosProject, 'res'));

// copy splash screens and update Contents.json
updateLaunchStoryboardImages(project, p.locations);
Expand Down Expand Up @@ -411,7 +410,7 @@ describe('prepare', () => {
projectConfig: new ConfigParser(path.join(FIXTURES, 'launch-storyboard-support', 'configs', 'modern-only.xml'))
};

shell.cp('-rf', path.join(FIXTURES, 'launch-storyboard-support', 'res'), iosProject);
fs.copySync(path.join(FIXTURES, 'launch-storyboard-support', 'res'), path.join(iosProject, 'res'));
updateLaunchStoryboardImages(project, p.locations);

// now, clean the images
Expand Down
3 changes: 1 addition & 2 deletions tests/spec/unit/preparePlatform.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

const path = require('path');
const fs = require('fs-extra');
const shell = require('shelljs');
const EventEmitter = require('events').EventEmitter;
const ConfigParser = require('cordova-common').ConfigParser;
const PluginInfo = require('cordova-common').PluginInfo;
Expand All @@ -37,7 +36,7 @@ describe('prepare after plugin add', () => {
let api;
beforeEach(() => {
fs.ensureDirSync(iosPlatform);
shell.cp('-rf', `${iosProjectFixture}/*`, iosPlatform);
fs.copySync(iosProjectFixture, iosPlatform);
api = new Api('ios', iosPlatform, new EventEmitter());

jasmine.addMatchers({
Expand Down
7 changes: 3 additions & 4 deletions tests/spec/unit/projectFile.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
const os = require('os');
const path = require('path');
const fs = require('fs-extra');
const shell = require('shelljs');
const projectFile = require('../../../bin/templates/scripts/cordova/lib/projectFile');

const iosProject = path.join(os.tmpdir(), 'plugman/projectFile');
const iosProjectFixture = path.join(__dirname, 'fixtures/ios-config-xml/*');
const iosProjectFixture = path.join(__dirname, 'fixtures/ios-config-xml');

const locations = {
root: iosProject,
Expand All @@ -33,7 +32,7 @@ const locations = {

describe('projectFile', () => {
beforeEach(() => {
shell.cp('-rf', iosProjectFixture, iosProject);
fs.copySync(iosProjectFixture, iosProject);
});

afterEach(() => {
Expand All @@ -59,7 +58,7 @@ describe('projectFile', () => {
// Create a folder named A with config.xml and .plist files in it
const pathToFolderA = path.join(iosProject, 'A');
fs.ensureDirSync(pathToFolderA);
shell.cp('-rf', path.join(iosProject, 'SampleApp/*'), pathToFolderA);
fs.copySync(path.join(iosProject, 'SampleApp'), pathToFolderA);

const parsedProjectFile = projectFile.parse(locations);
const pluginsDir = parsedProjectFile.plugins_dir;
Expand Down

0 comments on commit a55f605

Please sign in to comment.