Skip to content

Commit

Permalink
update ios pullFile to account for the fact that the sim root might n…
Browse files Browse the repository at this point in the history
…ot have the exact version in it

@DylanLacey have a look at this, it's a gotcha of working with sim paths. My iOS SDK version might be '7.0', but the directory might be '7.0.3', or '7.0.3-64', so I changed this to use the directory finder from settings.js, and to throw a warning if there are multiple dirs. Unfortunately this change makes the unit tests rather impractical since it really depends on getting the status of the filesystem, so I added a new functional test and removed the unit tests.
Also verified tests pass now on 6.1, 7.0, and 7.1 with these changes
  • Loading branch information
jlipps committed Apr 3, 2014
1 parent 30b7c8b commit d1553cc
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 111 deletions.
18 changes: 15 additions & 3 deletions lib/devices/ios/ios-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ var uuid = require('uuid-js')
, xpath = require("xpath")
, XMLDom = require("xmldom")
, settings = require('./settings.js')
, getSimRoot = settings.getSimRoot
, getSimRootsWithVersion = settings.getSimRootsWithVersion
, errors = require('../../server/errors.js')
, NotImplementedError = errors.NotImplementedError
, NotYetImplementedError = errors.NotYetImplementedError;
Expand Down Expand Up @@ -440,22 +440,32 @@ iOSController.pushFile = function (base64Data, remotePath, cb) {
};

iOSController.pullFile = function (remotePath, cb) {
logger.info("Pulling " + remotePath + " from sim");
if (this.realDevice) {
return cb(new NotYetImplementedError(), null);
}
var fullPath = "";
var v = (this.args.platformVersion || this.iOSSDKVersion);
var basePath = path.resolve(getSimRoot(), v);
var simRoots = getSimRootsWithVersion(v);
if (simRoots.length < 1) {
return cb(new Error("Could not find simulator root for SDK version " +
v + "; have you launched this sim before?"));
} else if (simRoots.length > 1) {
logger.warn("There were multiple simulator roots for version " + v + ". " +
"We may be pulling the file from the one you're not using!");
}
var basePath = simRoots[0];

var readAndReturnFile = function () {
logger.info("Attempting to read " + fullPath);
fs.readFile(fullPath, {encoding: 'base64'}, function (err, data) {
if (err) return cb(err);
cb(null, {status: status.codes.Success.code, value: data});
});
};

var appName = null;

if (this.args.app) {
var appNameRegex = new RegExp("\\" + path.sep + "(\\w+\\.app)");
var appNameMatches = appNameRegex.exec(this.args.app);
Expand All @@ -465,6 +475,7 @@ iOSController.pullFile = function (remotePath, cb) {
}

if (remotePath.indexOf(appName) === 1) {
logger.info("We want an app-relative file");
var findPath = basePath.replace(/\s/g, '\\ ');
var findCmd = 'find ' + findPath + ' -name "' + appName + '"';
exec(findCmd, function (err, stdout) {
Expand All @@ -475,6 +486,7 @@ iOSController.pullFile = function (remotePath, cb) {
readAndReturnFile();
}.bind(this));
} else {
logger.info("We want a sim-relative file");
fullPath = path.resolve(basePath, remotePath);
readAndReturnFile();
}
Expand Down
4 changes: 3 additions & 1 deletion lib/devices/ios/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ settings.getSimRoot = function () {
"iPhone Simulator");
};

var getSimDirs = function (sdk) {
settings.getSimRootsWithVersion = function (sdk) {
sdk = sdk.toString();
var base = settings.getSimRoot();
var list = [];
Expand All @@ -110,6 +110,8 @@ var getSimDirs = function (sdk) {
return dirs;
};

var getSimDirs = settings.getSimRootsWithVersion;

var getPrefsDirs = function (sdk) {
return multiResolve(getSimDirs(sdk), "Library", "Preferences");
};
Expand Down
23 changes: 15 additions & 8 deletions test/functional/ios/file-movement-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ var setup = require("../common/setup-base")
, getAppPath = require('../../helpers/app').getAppPath
, fs = require('fs')
, path = require('path')
, iOSSettings = require('../../../lib/devices/ios/settings.js')
, exec = require('child_process').exec;

describe('pullFile', function () {
var driver;
var desired = {
app: getAppPath('testapp')
, platformName: 'iPhone Simulator'
};
app: getAppPath('testapp')
};
setup(this, desired).then(function (d) { driver = d; });

it('should be able to fetch the Address book', function (done) {
Expand All @@ -24,15 +24,22 @@ describe('pullFile', function () {
})
.nodeify(done);
});
it('should not be able to fetch something that does not exist', function (done) {
var args = {path: 'Library/AddressBook/nothere.txt'};
driver
.execute('mobile: pullFile', [args])
.should.eventually.be.rejectedWith(/13/)
.nodeify(done);
});
describe('for a .app', function () {
var fileContent = "IAmTheVeryModelOfAModernMajorTestingTool";
var fileName = "someFile.tmp";
var fullPath = "";
before(function (done) {
var u = process.env.USER;
var pv = env.CAPS.version || '7.1';
var basePath = path.resolve('/Users', u, 'Library/Application\\ Support',
'iPhone\\ Simulator', pv, 'Applications');
var pv = env.CAPS.platformVersion || '7.1';
var simRoots = iOSSettings.getSimRootsWithVersion(pv);
var basePath = path.resolve(simRoots[0], 'Applications')
.replace(/\s/g, '\\ ');

var findCmd = 'find ' + basePath + ' -name "testapp.app"';
exec(findCmd, function (err, stdout) {
Expand Down Expand Up @@ -63,4 +70,4 @@ describe('pullFile', function () {
.nodeify(done);
});
});
});
});
100 changes: 1 addition & 99 deletions test/unit/ios-controller-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,102 +83,4 @@ describe('ios-controller', function () {
});
});
});
describe('#pullFile', function () {
describe('on a real device', function () {
before(function () {
controller.realDevice = true;
});
after(function () {
controller.realDevice = false;
});
it('returns a NotYetImplementedError', function () {
var cb = sinon.spy();
controller.pullFile("somefile", cb);
cb.args[0][0].should.be.an.instanceOf(NotYetImplementedError);
});
});
describe('on the simulator', function () {
before(function () {
controller.args = {platformVersion: '7.1'};
});
describe('with an invalid file', function () {
beforeEach(function () {
var stubbedFs = sinon.stub(fs, 'readFile');
stubbedFs.yields("someErr");
});
afterEach(function () {
fs.readFile.restore();
});
it('should return an error', function () {
var cb = sinon.spy();
controller.pullFile("", cb);
cb.args[0][0].should.equal("someErr");
});
});
describe('with a valid file', function () {
beforeEach(function () {
var stubbedFs = sinon.stub(fs, 'readFile');
var data = 'somedata';
stubbedFs.yields(null, data);
});
afterEach(function () {
fs.readFile.restore();
});
it('fetches data from the full path', function () {
var cb = sinon.spy();
var user = process.env.USER;
var expected = path.resolve("/Users", user, "Library", "Application Support",
"iPhone Simulator", "7.1", "Documents", "somefile");
controller.pullFile('Documents/somefile', cb);
fs.readFile.args[0][0].should.equal(expected);
});
it('returns the data and a success Code', function () {
var cb = sinon.spy();
controller.pullFile('somefile', cb);
cb.args[0][1].should.have.property('status', status.codes.Success.code);
cb.args[0][1].should.have.property('value', 'somedata');
});
});
describe('inside an application directory', function () {
var guid = "234234234445795432";
var user = process.env.USER;
var rootPath = path.resolve("/Users", user, "Library",
"Application Support", "iPhone Simulator",
"7.1", "Applications");
var appPath = path.resolve(rootPath, guid, 'greatApp.app');
var data = 'somedata';

beforeEach(function () {
var stubbedFs = sinon.stub(fs, 'readFile');

stubbedFs.yieldsAsync(null, data);
var stubbed_child_process = sinon.stub(child_process,
'exec').yieldsAsync(null, appPath);

this.controller = SandboxedModule.require(controller_path, {
locals: {
"exec": stubbed_child_process.exec
}
});

this.controller.args = {platformVersion: '7.1'};

return this.controller;
});
afterEach(function () {
fs.readFile.restore();
child_process.exec.restore();
});
it('pulls from that directory, adding a GUID', function (done) {
var expectedPath = path.resolve(appPath, 'somefile');
this.controller.args.app = '/some/location/to/a/greatApp.app';
this.controller.pullFile('/greatApp.app/somefile', function (err, data) {
data.value.should.equal('somedata');
fs.readFile.args[0][0].should.equal(expectedPath);
done();
});
});
});
});
});
});
});

0 comments on commit d1553cc

Please sign in to comment.