-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathtest-execution-service.ts
199 lines (173 loc) · 7.52 KB
/
test-execution-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
///<reference path="../.d.ts"/>
"use strict";
import * as constants from "../constants";
import * as path from 'path';
import Future = require('fibers/future');
import * as os from 'os';
import * as fiberBootstrap from "../common/fiber-bootstrap";
interface IKarmaConfigOptions {
debugBrk: boolean;
debugTransport: boolean;
}
class TestExecutionService implements ITestExecutionService {
private static MAIN_APP_NAME = `./tns_modules/${constants.TEST_RUNNER_NAME}/app.js`;
private static CONFIG_FILE_NAME = `node_modules/${constants.TEST_RUNNER_NAME}/config.js`;
private static SOCKETIO_JS_FILE_NAME = `node_modules/${constants.TEST_RUNNER_NAME}/socket.io.js`;
constructor(
private $injector: IInjector,
private $projectData: IProjectData,
private $platformService: IPlatformService,
private $platformsData: IPlatformsData,
private $usbLiveSyncServiceBase: IUsbLiveSyncServiceBase,
private $androidUsbLiveSyncServiceLocator: {factory: Function},
private $iosUsbLiveSyncServiceLocator: {factory: Function},
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $resources: IResourceLoader,
private $httpClient: Server.IHttpClient,
private $config: IConfiguration,
private $logger: ILogger,
private $fs: IFileSystem,
private $options: IOptions,
private $pluginsService: IPluginsService) {
}
public startTestRunner(platform: string) : IFuture<void> {
return (() => {
this.$options.justlaunch = true;
let blockingOperationFuture = new Future<void>();
process.on('message', (launcherConfig: any) => {
fiberBootstrap.run(() => {
try {
let platformData = this.$platformsData.getPlatformData(platform.toLowerCase());
let projectDir = this.$projectData.projectDir;
let projectFilesPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
let configOptions: IKarmaConfigOptions = JSON.parse(launcherConfig);
this.$options.debugBrk = configOptions.debugBrk;
this.$options.debugTransport = configOptions.debugTransport;
let configJs = this.generateConfig(configOptions);
this.$fs.writeFile(path.join(projectDir, TestExecutionService.CONFIG_FILE_NAME), configJs).wait();
let socketIoJsUrl = `http://localhost:${this.$options.port}/socket.io/socket.io.js`;
let socketIoJs = this.$httpClient.httpRequest(socketIoJsUrl).wait().body;
this.$fs.writeFile(path.join(projectDir, TestExecutionService.SOCKETIO_JS_FILE_NAME), socketIoJs).wait();
this.$platformService.preparePlatform(platform).wait();
this.detourEntryPoint(projectFilesPath).wait();
let watchGlob = path.join(projectDir, constants.APP_FOLDER_NAME);
let platformSpecificLiveSyncServices: IDictionary<any> = {
android: (_device: Mobile.IDevice, $injector: IInjector): IPlatformSpecificLiveSyncService => {
return $injector.resolve(this.$androidUsbLiveSyncServiceLocator.factory, {_device: _device});
},
ios: (_device: Mobile.IDevice, $injector: IInjector) => {
return $injector.resolve(this.$iosUsbLiveSyncServiceLocator.factory, {_device: _device});
}
};
let notInstalledAppOnDeviceAction = (device: Mobile.IDevice): IFuture<void> => {
return (() => {
this.$platformService.installOnDevice(platform).wait();
this.detourEntryPoint(projectFilesPath).wait();
}).future<void>()();
};
let notRunningiOSSimulatorAction = (): IFuture<void> => {
return (() => {
this.$platformService.deployOnEmulator(this.$devicePlatformsConstants.iOS.toLowerCase()).wait();
this.detourEntryPoint(projectFilesPath).wait();
}).future<void>()();
};
let beforeBatchLiveSyncAction = (filePath: string): IFuture<string> => {
return (() => {
this.$platformService.preparePlatform(platform).wait();
return path.join(projectFilesPath, path.relative(path.join(this.$projectData.projectDir, constants.APP_FOLDER_NAME), filePath));
}).future<string>()();
};
let localProjectRootPath = platform.toLowerCase() === "ios" ? platformData.appDestinationDirectoryPath : null;
let liveSyncData = {
platform: platform,
appIdentifier: this.$projectData.projectId,
projectFilesPath: projectFilesPath,
excludedProjectDirsAndFiles: constants.LIVESYNC_EXCLUDED_DIRECTORIES,
watchGlob: watchGlob,
platformSpecificLiveSyncServices: platformSpecificLiveSyncServices,
notInstalledAppOnDeviceAction: notInstalledAppOnDeviceAction,
notRunningiOSSimulatorAction: notRunningiOSSimulatorAction,
localProjectRootPath: localProjectRootPath,
beforeBatchLiveSyncAction: beforeBatchLiveSyncAction,
shouldRestartApplication: (localToDevicePaths: Mobile.ILocalToDevicePathData[]) => Future.fromResult(!this.$options.debugBrk),
canExecuteFastLiveSync: (filePath: string) => false,
};
this.$usbLiveSyncServiceBase.sync(liveSyncData).wait();
if (this.$options.debugBrk) {
this.$logger.info('Starting debugger...');
let debugService: IDebugService = this.$injector.resolve(`${platform}DebugService`);
debugService.debugStart().wait();
}
blockingOperationFuture.return();
} catch(err) {
// send the error to the real future
blockingOperationFuture.throw(err);
}
});
});
// Tell the parent that we are ready to receive the data.
process.send("ready");
blockingOperationFuture.wait();
}).future<void>()();
}
public startKarmaServer(platform: string): IFuture<void> {
return (() => {
platform = platform.toLowerCase();
this.$pluginsService.ensureAllDependenciesAreInstalled().wait();
let pathToKarma = path.join(this.$projectData.projectDir, 'node_modules/karma');
let KarmaServer = require(path.join(pathToKarma, 'lib/server'));
if (platform === 'ios' && this.$options.emulator) {
platform = 'ios_simulator';
}
let karmaConfig: any = {
browsers: [platform],
configFile: path.join(this.$projectData.projectDir, 'karma.conf.js'),
_NS: {
log: this.$logger.getLevel(),
path: this.$options.path,
tns: process.argv[1],
node: process.execPath,
options: {
debugTransport: this.$options.debugTransport,
debugBrk: this.$options.debugBrk,
}
},
};
if (this.$config.DEBUG || this.$logger.getLevel() === 'TRACE') {
karmaConfig.logLevel = 'DEBUG';
}
if (!this.$options.watch) {
karmaConfig.singleRun = true;
}
if (this.$options.debugBrk) {
karmaConfig.browserNoActivityTimeout = 1000000000;
}
this.$logger.debug(JSON.stringify(karmaConfig, null, 4));
new KarmaServer(karmaConfig).start();
}).future<void>()();
}
allowedParameters: ICommandParameter[] = [];
private detourEntryPoint(projectFilesPath: string): IFuture<void> {
return (() => {
let packageJsonPath = path.join(projectFilesPath, 'package.json');
let packageJson = this.$fs.readJson(packageJsonPath).wait();
packageJson.main = TestExecutionService.MAIN_APP_NAME;
this.$fs.writeJson(packageJsonPath, packageJson).wait();
}).future<void>()();
}
private generateConfig(options: any): string {
let port = this.$options.port;
let nics = os.networkInterfaces();
let ips = Object.keys(nics)
.map(nicName => nics[nicName].filter((binding: any) => binding.family === 'IPv4' && !binding.internal)[0])
.filter(binding => binding)
.map(binding => binding.address);
let config = {
port,
ips,
options,
};
return 'module.exports = ' + JSON.stringify(config);
}
}
$injector.register('testExecutionService', TestExecutionService);