Skip to content

Commit

Permalink
Move to logger from appium-support
Browse files Browse the repository at this point in the history
  • Loading branch information
imurchie committed Feb 28, 2017
1 parent 30ba87a commit 90d62dd
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 49 deletions.
2 changes: 1 addition & 1 deletion docs/en/contributing-to-appium/appium-packages.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
- contains all available and supported CLI arguments
- check for deprecation and mutual exclusion
- put logging together
- mixture out of npmlog, winston and appium-logger
- mixture out of npmlog, winston and a custom logger
- initiates AppiumDriver (extends Basedriver)
- assigns iOS/Android/Selendroid/Fake driver to session
- creates/deletes Appium session
Expand Down
7 changes: 4 additions & 3 deletions lib/logger.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { getLogger } from 'appium-logger';
import { logger } from 'appium-support';

let logger = getLogger('Appium');

export default logger;
let log = logger.getLogger('Appium');

export default log;
25 changes: 12 additions & 13 deletions lib/logsink.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import npmlog from 'npmlog';
import { patchLogger } from 'appium-logger';
import winston from 'winston';
import { fs } from 'appium-support';
import { fs, logger } from 'appium-support';
import 'date-utils';
import _ from 'lodash';


// set up distributed logging before everything else
patchLogger(npmlog);
logger.patchLogger(npmlog);
global._global_npmlog = npmlog;

// npmlog is used only for emitting, we use winston for output
Expand Down Expand Up @@ -36,7 +35,7 @@ const npmToWinstonLevels = {
error: 'error',
};

let logger = null;
let log = null;
let timeZone = null;

function timestamp () {
Expand Down Expand Up @@ -184,7 +183,7 @@ async function init (args) {
// object
clear();

logger = new (winston.Logger)({
log = new (winston.Logger)({
transports: await _createTransports(args)
});

Expand All @@ -196,29 +195,29 @@ async function init (args) {
let prefix = `[${logObj.prefix}]`;
msg = `${prefix.magenta} ${msg}`;
}
logger[winstonLevel](msg);
log[winstonLevel](msg);
if (args.logHandler && typeof args.logHandler === "function") {
args.logHandler(logObj.level, msg);
}

});


logger.setLevels(levels);
log.setLevels(levels);

// 8/19/14 this is a hack to force Winston to print debug messages to stdout rather than stderr.
// TODO: remove this if winston provides an API for directing streams.
if (levels[logger.transports.console.level] === levels.debug) {
logger.debug = function (msg) {
logger.info('[debug] ' + msg);
if (levels[log.transports.console.level] === levels.debug) {
log.debug = function (msg) {
log.info('[debug] ' + msg);
};
}
}

function clear () {
if (logger) {
for (let transport of _.keys(logger.transports)) {
logger.remove(transport);
if (log) {
for (let transport of _.keys(log.transports)) {
log.remove(transport);
}
}
npmlog.removeAllListeners('log');
Expand Down
3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"appium-base-driver": "2.x",
"appium-fake-driver": "0.x",
"appium-ios-driver": "1.x",
"appium-logger": "2.x",
"appium-selendroid-driver": "1.x",
"appium-support": "2.x",
"appium-uiautomator2-driver": "0.x",
Expand Down Expand Up @@ -85,7 +84,7 @@
"mocha": "2.x",
"pre-commit": "1.x",
"sinon": "1.x",
"wd": "0.x"
"wd": "1.x"
},
"optionalDependencies": {
"fsevents": "1.x"
Expand Down
59 changes: 35 additions & 24 deletions test/driver-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ chai.should();
chai.use(chaiAsPromised);

const BASE_CAPS = {platformName: 'Fake', deviceName: 'Fake', app: TEST_FAKE_APP};
const SESSION_ID = 1;

describe('AppiumDriver', () => {
describe('getAppiumRouter', () => {
Expand All @@ -37,20 +38,20 @@ describe('AppiumDriver', () => {
return [appium, mockFakeDriver];
}
describe('createSession', () => {
let appium
, mockFakeDriver;
let appium;
let mockFakeDriver;
beforeEach(() => {
[appium, mockFakeDriver] = getDriverAndFakeDriver();
});
afterEach(() => {
afterEach(async () => {
mockFakeDriver.restore();
appium.args.defaultCapabilities = {};
await appium.deleteSession(SESSION_ID);
});

it('should call inner driver\'s createSession with desired capabilities', async () => {
mockFakeDriver.expects("createSession")
.once().withExactArgs(BASE_CAPS, undefined, [])
.returns([1, BASE_CAPS]);
.returns([SESSION_ID, BASE_CAPS]);
await appium.createSession(BASE_CAPS);
mockFakeDriver.verify();
});
Expand All @@ -60,7 +61,7 @@ describe('AppiumDriver', () => {
appium.args.defaultCapabilities = defaultCaps;
mockFakeDriver.expects("createSession")
.once().withArgs(allCaps)
.returns([1, allCaps]);
.returns([SESSION_ID, allCaps]);
await appium.createSession(BASE_CAPS);
mockFakeDriver.verify();
});
Expand All @@ -71,7 +72,7 @@ describe('AppiumDriver', () => {
appium.args.defaultCapabilities = defaultCaps;
mockFakeDriver.expects("createSession")
.once().withArgs(BASE_CAPS)
.returns([1, BASE_CAPS]);
.returns([SESSION_ID, BASE_CAPS]);
await appium.createSession(BASE_CAPS);
mockFakeDriver.verify();
});
Expand Down Expand Up @@ -99,7 +100,7 @@ describe('AppiumDriver', () => {

mockFakeDriver.expects("createSession")
.once().withExactArgs(BASE_CAPS, undefined, [])
.returns([1, BASE_CAPS]);
.returns([SESSION_ID, BASE_CAPS]);
await appium.createSession(BASE_CAPS);

sessions = await appium.getSessions();
Expand All @@ -112,14 +113,13 @@ describe('AppiumDriver', () => {
});
});
describe('deleteSession', () => {
let appium
, mockFakeDriver;
let appium;
let mockFakeDriver;
beforeEach(() => {
[appium, mockFakeDriver] = getDriverAndFakeDriver();
});
afterEach(() => {
mockFakeDriver.restore();
appium.args.defaultCapabilities = {};
});
it('should remove the session if it is found', async () => {
let [sessionId] = await appium.createSession(BASE_CAPS);
Expand All @@ -136,39 +136,49 @@ describe('AppiumDriver', () => {
.returns();
await appium.deleteSession(sessionId);
mockFakeDriver.verify();

// cleanup, since we faked the delete session call
await mockFakeDriver.object.deleteSession();
});
});
describe('getSessions', () => {
let appium;
let sessions;
before(() => {
appium = new AppiumDriver({});
});
afterEach(async () => {
for (let session of sessions) {
await appium.deleteSession(session.id);
}
});
it('should return an empty array of sessions', async () => {
let sessions = await appium.getSessions();
sessions = await appium.getSessions();
sessions.should.be.an.array;
sessions.should.be.empty;
});
it('should return sessions created', async () => {
let session1 = await appium.createSession(_.extend(_.clone(BASE_CAPS), {cap: 'value'}));
let session2 = await appium.createSession(_.extend(_.clone(BASE_CAPS), {cap: 'other value'}));
let sessions = await appium.getSessions();

sessions = await appium.getSessions();
sessions.should.be.an.array;
sessions.should.have.length(2);
sessions[0].id.should.equal(session1[0]);
sessions[0].capabilities.should.eql(session1[1]);
sessions[1].id.should.equal(session2[0]);
sessions[1].capabilities.should.eql(session2[1]);
});
describe('getStatus', () => {
let appium;
before(() => {
appium = new AppiumDriver({});
});
it('should return a status', async () => {
let status = await appium.getStatus();
status.build.should.exist;
status.build.version.should.exist;
});
});
describe('getStatus', () => {
let appium;
before(() => {
appium = new AppiumDriver({});
});
it('should return a status', async () => {
let status = await appium.getStatus();
status.build.should.exist;
status.build.version.should.exist;
});
});
describe('sessionExists', () => {
Expand All @@ -179,7 +189,8 @@ describe('AppiumDriver', () => {
beforeEach(() => {
[appium, mockFakeDriver] = getDriverAndFakeDriver();
});
afterEach(() => {
afterEach(async () => {
await mockFakeDriver.object.deleteSession();
mockFakeDriver.restore();
appium.args.defaultCapabilities = {};
});
Expand Down
12 changes: 6 additions & 6 deletions test/logger-specs.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@

import { init as logsinkInit, clear as logsinkClear } from '../lib/logsink';
import sinon from 'sinon';
import { getLogger } from 'appium-logger';
import { logger } from 'appium-support';


// temporarily turn on logging to stdio, so we can catch and query
let forceLogs = process.env._FORCE_LOGS;
process.env._FORCE_LOGS = 1;
let logger = getLogger('Appium');
let log = logger.getLogger('Appium');

describe('Logger', () => {
describe('logging', () => {
let stderrSpy;
let stdoutSpy;
beforeEach(() => {
Expand All @@ -31,9 +31,9 @@ describe('Logger', () => {
const debugMsg = 'some debug';

function doLogging () {
logger.error(errorMsg);
logger.warn(warnMsg);
logger.debug(debugMsg);
log.error(errorMsg);
log.warn(warnMsg);
log.debug(debugMsg);
}

it('should send error, info and debug when loglevel is debug', async () => {
Expand Down

0 comments on commit 90d62dd

Please sign in to comment.