Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[+coverage] unit tests for ArtifactsManager.js and ArtifactPlugin.js #794

Merged
merged 9 commits into from
Jul 2, 2018
2 changes: 0 additions & 2 deletions detox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@
"src/artifacts/log",
"src/artifacts/screenshot",
"src/artifacts/video",
"src/artifacts/templates/plugin",
"src/artifacts/ArtifactsManager.js",
".*Driver.js",
"EmulatorTelnet.js",
"Emulator.js",
Expand Down
56 changes: 34 additions & 22 deletions detox/src/artifacts/ArtifactsManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,35 +3,32 @@ const fs = require('fs-extra');
const path = require('path');
const log = require('npmlog');
const argparse = require('../utils/argparse');
const environment = require('../utils/environment');
const logError = require('../utils/logError');
const DetoxRuntimeError = require('../errors/DetoxRuntimeError');
const ArtifactPathBuilder = require('./utils/ArtifactPathBuilder');

class ArtifactsManager {
constructor() {
constructor(pathBuilder) {
this.onBeforeResetDevice = this.onBeforeResetDevice.bind(this);
this.onResetDevice = this.onResetDevice.bind(this);
this.onBeforeLaunchApp = this.onBeforeLaunchApp.bind(this);
this.onLaunchApp = this.onLaunchApp.bind(this);
this._executeIdleCallback = this._executeIdleCallback.bind(this);

this.onTerminate = _.once(this.onTerminate.bind(this));
this._executeIdleCallback = this._executeIdleCallback.bind(this);

this._idlePromise = Promise.resolve();
this._onIdleCallbacks = [];
this._activeArtifacts = [];
this._artifactPluginsFactories = [];
this._artifactPlugins = [];
this._pathBuilder = pathBuilder || new ArtifactPathBuilder({
artifactsRootDir: argparse.getArgValue('artifacts-location') || 'artifacts',
});

this._deviceId = '';
this._bundleId = '';
this._pid = NaN;

const pathBuilder = new ArtifactPathBuilder({
artifactsRootDir: argparse.getArgValue('artifacts-location') || 'artifacts',
});

this.artifactsApi = {
getDeviceId: () => {
if (!this._deviceId) {
Expand All @@ -54,17 +51,17 @@ class ArtifactsManager {
},

getPid: () => {
if (!this._pid) {
if (isNaN(this._pid)) {
throw new DetoxRuntimeError({
message: 'Detox Artifacts API had no app pid at the time of calling',
message: 'Detox Artifacts API had no app PID at the time of calling',
});
}

return this._pid;
},

preparePathForArtifact: async (artifactName, testSummary) => {
const artifactPath = pathBuilder.buildPathForTestArtifact(artifactName, testSummary);
const artifactPath = this._pathBuilder.buildPathForTestArtifact(artifactName, testSummary);
const artifactDir = path.dirname(artifactPath);
await fs.ensureDir(artifactDir);

Expand All @@ -75,12 +72,15 @@ class ArtifactsManager {
this._activeArtifacts.push(artifact);
},

untrackArtifact(artifact) {
untrackArtifact: (artifact) => {
_.pull(this._activeArtifacts, artifact);
},

requestIdleCallback: (callback, caller) => {
callback._from = caller.name;
if (caller) {
callback._from = caller.name;
}

this._onIdleCallbacks.push(callback);

this._idlePromise = this._idlePromise.then(() => {
Expand Down Expand Up @@ -123,14 +123,26 @@ class ArtifactsManager {
this._deviceId = deviceId;
this._bundleId = bundleId;

if (isFirstTime) {
this._artifactPlugins = this._artifactPluginsFactories.map((factory) => {
return factory(this.artifactsApi);
});
} else {
// TODO: implement this lifecycle event
// await this._emit('onBeforeRelaunchApp', [{ deviceId, bundleId }]);
}
return isFirstTime
? this._onBeforeLaunchAppFirstTime()
: this._onBeforeRelaunchApp({ deviceId, bundleId });
}

async _onBeforeLaunchAppFirstTime() {
this._artifactPlugins = this._instantiateArtifactPlugins();
}

_instantiateArtifactPlugins() {
return this._artifactPluginsFactories.map((factory) => {
return factory(this.artifactsApi);
});
}

async _onBeforeRelaunchApp() {
await this._emit('onBeforeRelaunchApp', [{
deviceId: this._deviceId,
bundleId: this._bundleId,
}]);
}

async onLaunchApp({ deviceId, bundleId, pid }) {
Expand Down Expand Up @@ -177,7 +189,7 @@ class ArtifactsManager {
}

log.info('ArtifactsManager', 'finalizing all artifacts, this can take some time');
await Promise.all(this._artifactPlugins.map(plugin => plugin.onTerminate()));
await this._emit('onTerminate', []);
await Promise.all(this._onIdleCallbacks.splice(0).map(this._executeIdleCallback));
await this._idlePromise;

Expand Down
Loading