-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathtest-execution-service.ts
354 lines (306 loc) · 13.3 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import * as constants from "../constants";
import * as path from 'path';
import * as os from 'os';
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 $platformService: IPlatformService,
private $platformsData: IPlatformsData,
private $liveSyncService: ILiveSyncService,
private $debugDataService: IDebugDataService,
private $httpClient: Server.IHttpClient,
private $config: IConfiguration,
private $logger: ILogger,
private $fs: IFileSystem,
private $options: IOptions,
private $pluginsService: IPluginsService,
private $errors: IErrors,
private $debugService: IDebugService,
private $devicesService: Mobile.IDevicesService,
private $childProcess: IChildProcess) {
}
public platform: string;
public async startTestRunner(platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): Promise<void> {
this.platform = platform;
this.$options.justlaunch = true;
await new Promise<void>((resolve, reject) => {
process.on('message', async (launcherConfig: any) => {
try {
const platformData = this.$platformsData.getPlatformData(platform.toLowerCase(), projectData);
const projectDir = projectData.projectDir;
await this.$devicesService.initialize({
platform: platform,
deviceId: this.$options.device,
emulator: this.$options.emulator
});
const projectFilesPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);
const configOptions: IKarmaConfigOptions = JSON.parse(launcherConfig);
this.$options.debugBrk = configOptions.debugBrk;
this.$options.debugTransport = configOptions.debugTransport;
const configJs = this.generateConfig(this.$options.port.toString(), configOptions);
this.$fs.writeFile(path.join(projectDir, TestExecutionService.CONFIG_FILE_NAME), configJs);
const socketIoJsUrl = `http://localhost:${this.$options.port}/socket.io/socket.io.js`;
const socketIoJs = (await this.$httpClient.httpRequest(socketIoJsUrl)).body;
this.$fs.writeFile(path.join(projectDir, TestExecutionService.SOCKETIO_JS_FILE_NAME), socketIoJs);
const appFilesUpdaterOptions: IAppFilesUpdaterOptions = { bundle: !!this.$options.bundle, release: this.$options.release };
const preparePlatformInfo: IPreparePlatformInfo = {
platform,
appFilesUpdaterOptions,
platformTemplate: this.$options.platformTemplate,
projectData,
config: this.$options,
env: this.$options.env
};
if (!await this.$platformService.preparePlatform(preparePlatformInfo)) {
this.$errors.failWithoutHelp("Verify that listed files are well-formed and try again the operation.");
}
this.detourEntryPoint(projectFilesPath);
const deployOptions: IDeployPlatformOptions = {
clean: this.$options.clean,
device: this.$options.device,
emulator: this.$options.emulator,
projectDir: this.$options.path,
platformTemplate: this.$options.platformTemplate,
release: this.$options.release,
provision: this.$options.provision,
teamId: this.$options.teamId
};
if (this.$options.bundle) {
this.$options.watch = false;
}
const devices = this.$devicesService.getDeviceInstances();
// Now let's take data for each device:
const platformLowerCase = this.platform && this.platform.toLowerCase();
const deviceDescriptors: ILiveSyncDeviceInfo[] = devices.filter(d => !platformLowerCase || d.deviceInfo.platform.toLowerCase() === platformLowerCase)
.map(d => {
const info: ILiveSyncDeviceInfo = {
identifier: d.deviceInfo.identifier,
buildAction: async (): Promise<string> => {
const buildConfig: IBuildConfig = {
buildForDevice: !d.isEmulator, // this.$options.forDevice,
projectDir: this.$options.path,
clean: this.$options.clean,
teamId: this.$options.teamId,
device: this.$options.device,
provision: this.$options.provision,
release: this.$options.release,
keyStoreAlias: this.$options.keyStoreAlias,
keyStorePath: this.$options.keyStorePath,
keyStoreAliasPassword: this.$options.keyStoreAliasPassword,
keyStorePassword: this.$options.keyStorePassword
};
await this.$platformService.buildPlatform(d.deviceInfo.platform, buildConfig, projectData);
const pathToBuildResult = await this.$platformService.lastOutputPath(d.deviceInfo.platform, buildConfig, projectData);
return pathToBuildResult;
},
debugOptions: this.$options
};
return info;
});
const liveSyncInfo: ILiveSyncInfo = {
projectDir: projectData.projectDir,
skipWatcher: !this.$options.watch || this.$options.justlaunch,
watchAllFiles: this.$options.syncAllFiles,
bundle: !!this.$options.bundle,
release: this.$options.release,
env: this.$options.env
};
await this.$liveSyncService.liveSync(deviceDescriptors, liveSyncInfo);
if (this.$options.debugBrk) {
this.$logger.info('Starting debugger...');
const debugService: IPlatformDebugService = this.$injector.resolve(`${platform}DebugService`);
const debugData = this.getDebugData(platform, projectData, deployOptions);
await debugService.debugStart(debugData, this.$options);
}
resolve();
} catch (err) {
reject(err);
}
});
// Tell the parent that we are ready to receive the data.
process.send("ready");
});
}
public async startKarmaServer(platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): Promise<void> {
platform = platform.toLowerCase();
this.platform = platform;
if (this.$options.debugBrk && this.$options.watch) {
this.$errors.failWithoutHelp("You cannot use --watch and --debug-brk simultaneously. Remove one of the flags and try again.");
}
// We need the dependencies installed here, so we can start the Karma server.
await this.$pluginsService.ensureAllDependenciesAreInstalled(projectData);
const projectDir = projectData.projectDir;
await this.$devicesService.initialize({
platform: platform,
deviceId: this.$options.device,
emulator: this.$options.emulator
});
const karmaConfig = this.getKarmaConfiguration(platform, projectData),
karmaRunner = this.$childProcess.fork(path.join(__dirname, "karma-execution.js")),
launchKarmaTests = async (karmaData: any) => {
this.$logger.trace("## Unit-testing: Parent process received message", karmaData);
let port: string;
if (karmaData.url) {
port = karmaData.url.port;
const socketIoJsUrl = `http://${karmaData.url.host}/socket.io/socket.io.js`;
const socketIoJs = (await this.$httpClient.httpRequest(socketIoJsUrl)).body;
this.$fs.writeFile(path.join(projectDir, TestExecutionService.SOCKETIO_JS_FILE_NAME), socketIoJs);
}
if (karmaData.launcherConfig) {
const configOptions: IKarmaConfigOptions = JSON.parse(karmaData.launcherConfig);
const configJs = this.generateConfig(port, configOptions);
this.$fs.writeFile(path.join(projectDir, TestExecutionService.CONFIG_FILE_NAME), configJs);
}
const appFilesUpdaterOptions: IAppFilesUpdaterOptions = { bundle: !!this.$options.bundle, release: this.$options.release };
const preparePlatformInfo: IPreparePlatformInfo = {
platform,
appFilesUpdaterOptions,
platformTemplate: this.$options.platformTemplate,
projectData,
config: this.$options,
env: this.$options.env
};
// Prepare the project AFTER the TestExecutionService.CONFIG_FILE_NAME file is created in node_modules
// so it will be sent to device.
if (!await this.$platformService.preparePlatform(preparePlatformInfo)) {
this.$errors.failWithoutHelp("Verify that listed files are well-formed and try again the operation.");
}
const deployOptions: IDeployPlatformOptions = {
clean: this.$options.clean,
device: this.$options.device,
emulator: this.$options.emulator,
projectDir: this.$options.path,
platformTemplate: this.$options.platformTemplate,
release: this.$options.release,
provision: this.$options.provision,
teamId: this.$options.teamId
};
if (this.$options.debugBrk) {
const debugData = this.getDebugData(platform, projectData, deployOptions);
await this.$debugService.debug(debugData, this.$options);
} else {
const devices = this.$devicesService.getDeviceInstances();
// Now let's take data for each device:
const platformLowerCase = this.platform && this.platform.toLowerCase();
const deviceDescriptors: ILiveSyncDeviceInfo[] = devices.filter(d => !platformLowerCase || d.deviceInfo.platform.toLowerCase() === platformLowerCase)
.map(d => {
const info: ILiveSyncDeviceInfo = {
identifier: d.deviceInfo.identifier,
buildAction: async (): Promise<string> => {
const buildConfig: IBuildConfig = {
buildForDevice: !d.isEmulator,
projectDir: this.$options.path,
clean: this.$options.clean,
teamId: this.$options.teamId,
device: this.$options.device,
provision: this.$options.provision,
release: this.$options.release,
keyStoreAlias: this.$options.keyStoreAlias,
keyStorePath: this.$options.keyStorePath,
keyStoreAliasPassword: this.$options.keyStoreAliasPassword,
keyStorePassword: this.$options.keyStorePassword
};
await this.$platformService.buildPlatform(d.deviceInfo.platform, buildConfig, projectData);
const pathToBuildResult = await this.$platformService.lastOutputPath(d.deviceInfo.platform, buildConfig, projectData);
return pathToBuildResult;
},
debugOptions: this.$options
};
return info;
});
const liveSyncInfo: ILiveSyncInfo = {
projectDir: projectData.projectDir,
skipWatcher: !this.$options.watch || this.$options.justlaunch,
watchAllFiles: this.$options.syncAllFiles,
bundle: !!this.$options.bundle,
release: this.$options.release,
env: this.$options.env
};
await this.$liveSyncService.liveSync(deviceDescriptors, liveSyncInfo);
}
};
karmaRunner.on("message", (karmaData: any) => {
launchKarmaTests(karmaData)
.catch((result) => {
this.$logger.error(result);
process.exit(ErrorCodes.KARMA_FAIL);
});
});
return new Promise<void>((resolve, reject) => {
karmaRunner.on("exit", (exitCode: number) => {
if (exitCode !== 0) {
//End our process with a non-zero exit code
const testError = <any>new Error("Test run failed.");
testError.suppressCommandHelp = true;
reject(testError);
} else {
resolve();
}
});
karmaRunner.send({ karmaConfig: karmaConfig });
});
}
allowedParameters: ICommandParameter[] = [];
private detourEntryPoint(projectFilesPath: string): void {
const packageJsonPath = path.join(projectFilesPath, 'package.json');
const packageJson = this.$fs.readJson(packageJsonPath);
packageJson.main = TestExecutionService.MAIN_APP_NAME;
this.$fs.writeJson(packageJsonPath, packageJson);
}
private generateConfig(port: string, options: any): string {
const nics = os.networkInterfaces();
const ips = Object.keys(nics)
.map(nicName => nics[nicName].filter((binding: any) => binding.family === 'IPv4')[0])
.filter(binding => binding)
.map(binding => binding.address);
const config = {
port,
ips,
options,
};
return 'module.exports = ' + JSON.stringify(config);
}
private getKarmaConfiguration(platform: string, projectData: IProjectData): any {
const karmaConfig: any = {
browsers: [platform],
configFile: path.join(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,
watch: !!this.$options.watch
}
},
};
if (this.$config.DEBUG || this.$logger.getLevel() === 'TRACE') {
karmaConfig.logLevel = 'DEBUG';
}
if (!this.$options.watch) {
// Setting singleRun to true will automatically start the tests when new browser (device in our case) is registered in karma.
karmaConfig.singleRun = true;
}
if (this.$options.debugBrk) {
karmaConfig.browserNoActivityTimeout = 1000000000;
}
karmaConfig.projectDir = projectData.projectDir;
this.$logger.debug(JSON.stringify(karmaConfig, null, 4));
return karmaConfig;
}
private getDebugData(platform: string, projectData: IProjectData, deployOptions: IDeployPlatformOptions): IDebugData {
const buildConfig: IBuildConfig = _.merge({ buildForDevice: this.$options.forDevice }, deployOptions);
const debugData = this.$debugDataService.createDebugData(projectData, this.$options);
debugData.pathToAppPackage = this.$platformService.lastOutputPath(platform, buildConfig, projectData);
return debugData;
}
}
$injector.register('testExecutionService', TestExecutionService);