-
-
Notifications
You must be signed in to change notification settings - Fork 197
Fix unit test runner #1545
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
Fix unit test runner #1545
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
///<reference path="../.d.ts"/> | ||
|
||
"use strict"; | ||
|
||
import * as path from "path"; | ||
|
||
process.on("message", (data: any) => { | ||
if(data.karmaConfig) { | ||
let pathToKarma = path.join(data.karmaConfig.projectDir, 'node_modules/karma'), | ||
KarmaServer = require(path.join(pathToKarma, 'lib/server')), | ||
karma = new KarmaServer(data.karmaConfig); | ||
|
||
karma.start(); | ||
} | ||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,11 +32,17 @@ class TestExecutionService implements ITestExecutionService { | |
private $options: IOptions, | ||
private $pluginsService: IPluginsService, | ||
private $errors: IErrors, | ||
private $devicesService: Mobile.IDevicesService) { | ||
private $androidDebugService:IDebugService, | ||
private $iOSDebugService: IDebugService, | ||
private $devicesService: Mobile.IDevicesService, | ||
private $childProcess: IChildProcess) { | ||
} | ||
|
||
public platform: string; | ||
|
||
public startTestRunner(platform: string) : IFuture<void> { | ||
return (() => { | ||
this.platform = platform; | ||
this.$options.justlaunch = true; | ||
let blockingOperationFuture = new Future<void>(); | ||
process.on('message', (launcherConfig: any) => { | ||
|
@@ -50,7 +56,7 @@ class TestExecutionService implements ITestExecutionService { | |
let configOptions: IKarmaConfigOptions = JSON.parse(launcherConfig); | ||
this.$options.debugBrk = configOptions.debugBrk; | ||
this.$options.debugTransport = configOptions.debugTransport; | ||
let configJs = this.generateConfig(configOptions); | ||
let configJs = this.generateConfig(this.$options.port.toString(), 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`; | ||
|
@@ -93,37 +99,47 @@ class TestExecutionService implements ITestExecutionService { | |
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; | ||
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."); | ||
} | ||
if (this.$options.debugBrk) { | ||
karmaConfig.browserNoActivityTimeout = 1000000000; | ||
|
||
if (!this.$platformService.preparePlatform(platform).wait()) { | ||
this.$errors.failWithoutHelp("Verify that listed files are well-formed and try again the operation."); | ||
} | ||
this.$logger.debug(JSON.stringify(karmaConfig, null, 4)); | ||
new KarmaServer(karmaConfig).start(); | ||
|
||
let projectDir = this.$projectData.projectDir; | ||
this.$devicesService.initialize({ platform: platform, deviceId: this.$options.device }).wait(); | ||
|
||
let karmaConfig = this.getKarmaConfiguration(platform), | ||
karmaRunner = this.$childProcess.fork(path.join(__dirname, "karma-execution.js")); | ||
|
||
karmaRunner.send({karmaConfig: karmaConfig}); | ||
karmaRunner.on("message", (karmaData: any) => { | ||
fiberBootstrap.run(() => { | ||
this.$logger.trace("## Unit-testing: Parent process received message", karmaData); | ||
let port: string; | ||
if(karmaData.url) { | ||
port = karmaData.url.port; | ||
let socketIoJsUrl = `http://${karmaData.url.host}/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(); | ||
} | ||
|
||
if(karmaData.launcherConfig) { | ||
let configOptions: IKarmaConfigOptions = JSON.parse(karmaData.launcherConfig); | ||
let configJs = this.generateConfig(port, configOptions); | ||
this.$fs.writeFile(path.join(projectDir, TestExecutionService.CONFIG_FILE_NAME), configJs).wait(); | ||
} | ||
|
||
if(this.$options.debugBrk) { | ||
this.getDebugService(platform).debug().wait(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why we do not need There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I've understood your point here, but I've moved the generation of LiveSync data and calling LiveSync service in a separate method that is called only in the |
||
} else { | ||
this.liveSyncProject(platform).wait(); | ||
} | ||
}); | ||
}); | ||
}).future<void>()(); | ||
} | ||
|
||
|
@@ -138,8 +154,7 @@ class TestExecutionService implements ITestExecutionService { | |
}).future<void>()(); | ||
} | ||
|
||
private generateConfig(options: any): string { | ||
let port = this.$options.port; | ||
private generateConfig(port: string, options: any): string { | ||
let nics = os.networkInterfaces(); | ||
let ips = Object.keys(nics) | ||
.map(nicName => nics[nicName].filter((binding: any) => binding.family === 'IPv4' && !binding.internal)[0]) | ||
|
@@ -154,5 +169,65 @@ class TestExecutionService implements ITestExecutionService { | |
|
||
return 'module.exports = ' + JSON.stringify(config); | ||
} | ||
|
||
private getDebugService(platform: string): IDebugService { | ||
let lowerCasedPlatform = platform.toLowerCase(); | ||
if(lowerCasedPlatform === this.$devicePlatformsConstants.iOS.toLowerCase()) { | ||
return this.$iOSDebugService; | ||
} else if(lowerCasedPlatform === this.$devicePlatformsConstants.Android.toLowerCase()) { | ||
return this.$androidDebugService; | ||
} | ||
|
||
throw new Error(`Invalid platform ${platform}. Valid platforms are ${this.$devicePlatformsConstants.iOS} and ${this.$devicePlatformsConstants.Android}`); | ||
} | ||
|
||
private getKarmaConfiguration(platform: string): any { | ||
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; | ||
} | ||
|
||
karmaConfig.projectDir = this.$projectData.projectDir; | ||
this.$logger.debug(JSON.stringify(karmaConfig, null, 4)); | ||
|
||
return karmaConfig; | ||
} | ||
|
||
private liveSyncProject(platform: string): IFuture<void> { | ||
return (() => { | ||
let platformData = this.$platformsData.getPlatformData(platform.toLowerCase()), | ||
projectFilesPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME); | ||
|
||
let liveSyncData: ILiveSyncData = { | ||
platform: platform, | ||
appIdentifier: this.$projectData.projectId, | ||
projectFilesPath: projectFilesPath, | ||
syncWorkingDirectory: path.join(this.$projectData.projectDir, constants.APP_FOLDER_NAME), | ||
canExecuteFastSync: false, // Always restart the application when change is detected, so tests will be rerun. | ||
excludedProjectDirsAndFiles: constants.LIVESYNC_EXCLUDED_FILE_PATTERNS | ||
}; | ||
|
||
this.$liveSyncServiceBase.sync(liveSyncData).wait(); | ||
}).future<void>()(); | ||
} | ||
} | ||
$injector.register('testExecutionService', TestExecutionService); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using
hostname
here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I need the port for configuration below (next
if
). However, I've replaced the hard-codedlocalhost:${port}
withhost
which contains bothhostname
andport
(check next line)