Skip to content

Commit

Permalink
added app output log in verbose mode
Browse files Browse the repository at this point in the history
  • Loading branch information
talkol committed Aug 10, 2016
1 parent 4a1474f commit a02ea22
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
58 changes: 51 additions & 7 deletions detox/src/ios/simulator.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const exec = require('child_process').exec;
const spawn = require('child_process').spawn;
const path = require('path');
const fs = require('fs');
const _ = require('lodash');
Expand All @@ -12,6 +13,7 @@ const websocket = require('../websocket');
let _defaultLaunchArgs = [];
let _currentScheme = {};
let _verbose = false;
let _appLogProcess;

function _waitUntilReady(onReady) {
websocket.waitForNextAction('ready', onReady);
Expand Down Expand Up @@ -64,10 +66,10 @@ function _executeSimulatorCommand(options, onComplete) {
if (err) {
console.error(stdout);
console.error(stderr);
onComplete(err);
onComplete(err, stdout, stderr);
return;
}
onComplete();
onComplete(null, stdout, stderr);
});
}

Expand All @@ -88,10 +90,10 @@ function _executeOrigSimulatorCommand(options, onComplete) {
if (err) {
console.error(stdout);
console.error(stderr);
onComplete(err);
onComplete(err, stdout, stderr);
return;
}
onComplete();
onComplete(null, stdout, stderr);
});
}

Expand Down Expand Up @@ -147,6 +149,40 @@ function _uninstallApp(device, appPath, onComplete) {
});
}

function _getAppLogfile(bundleId, stdout) {
const suffix = `fbsimulatorcontrol/diagnostics/out_err/${bundleId}_err.txt`;
const re = new RegExp('[^\\s]+' + suffix);
const matches = stdout.match(re);
if (matches && matches.length > 0) {
const logfile = matches[0];
console.log(`DETOX app logfile: ${logfile}\n`);
return logfile;
}
return undefined;
}

function _listenOnAppLogfile(logfile) {
if (_appLogProcess) {
_appLogProcess.kill();
_appLogProcess = undefined;
}
if (!logfile) return;
_appLogProcess = spawn('tail', ['-f', logfile]);
_appLogProcess.stdout.on('data', function (buffer) {
const data = buffer.toString('utf8');
if (_verbose) {
console.log('DETOX app: ' + data);
}
});
}

process.on('exit', function () {
if (_appLogProcess) {
_appLogProcess.kill();
_appLogProcess = undefined;
}
});

// ./node_modules/detox-tools/fbsimctl/fbsimctl launch org.reactjs.native.example.example arg1 arg2 arg3
function _launchApp(device, appPath, onComplete) {
const query = _getQueryFromDevice(device);
Expand All @@ -155,8 +191,12 @@ function _launchApp(device, appPath, onComplete) {
onComplete(err);
return;
}
const options = {args: `${query} launch ${bundleId} ${_defaultLaunchArgs.join(' ')}`};
_executeSimulatorCommand(options, function (err2) {
const options = {args: `${query} launch --stderr ${bundleId} ${_defaultLaunchArgs.join(' ')}`};
_executeSimulatorCommand(options, function (err2, stdout, stderr) {
if (_verbose) {
// in the future we'll allow expectations on logs and _listenOnAppLogfile will always run (remove if)
_listenOnAppLogfile(_getAppLogfile(bundleId, stdout));
}
if (err2) {
onComplete(err2);
return;
Expand All @@ -175,7 +215,11 @@ function _relaunchApp(device, appPath, onComplete) {
return;
}
const options = {args: `${query} relaunch ${bundleId} ${_defaultLaunchArgs.join(' ')}`};
_executeSimulatorCommand(options, function (err2) {
_executeSimulatorCommand(options, function (err2, stdout, stderr) {
if (_verbose) {
// in the future we'll allow expectations on logs and _listenOnAppLogfile will always run (remove if)
_listenOnAppLogfile(_getAppLogfile(bundleId, stdout));
}
if (err2) {
onComplete(err2);
return;
Expand Down
2 changes: 2 additions & 0 deletions detox/test/index.ios.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class example extends Component {
this.state = {
greeting: undefined
};
console.log('example React Component constructed (console.log test)');
}
render() {
if (this.state.greeting) return this.renderAfterButton();
Expand Down Expand Up @@ -46,6 +47,7 @@ class example extends Component {
);
}
onButtonPress(greeting) {
console.log('onButtonPress ' + greeting + ' (console.log test)');
this.setState({
greeting: greeting
});
Expand Down

1 comment on commit a02ea22

@talkol
Copy link

@talkol talkol commented on a02ea22 Aug 10, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#14

Please sign in to comment.