|
1 | 1 | import * as helpers from "../common/helpers";
|
2 | 2 |
|
3 |
| -function RunKarmaTestCommandFactory(platform: string) { |
4 |
| - return function RunKarmaTestCommand($options: IOptions, $testExecutionService: ITestExecutionService, $projectData: IProjectData, $analyticsService: IAnalyticsService, $platformEnvironmentRequirements: IPlatformEnvironmentRequirements) { |
5 |
| - $projectData.initializeProjectData(); |
6 |
| - $analyticsService.setShouldDispose($options.justlaunch || !$options.watch); |
7 |
| - const projectFilesConfig = helpers.getProjectFilesConfig({ isReleaseBuild: $options.release }); |
8 |
| - this.execute = (args: string[]): Promise<void> => $testExecutionService.startKarmaServer(platform, $projectData, projectFilesConfig); |
9 |
| - this.canExecute = (args: string[]): Promise<boolean> => canExecute({ $platformEnvironmentRequirements, $projectData, $options, platform }); |
10 |
| - this.allowedParameters = []; |
11 |
| - }; |
12 |
| -} |
| 3 | +abstract class TestCommandBase { |
| 4 | + public allowedParameters: ICommandParameter[] = []; |
| 5 | + private projectFilesConfig: IProjectFilesConfig; |
| 6 | + protected abstract platform: string; |
| 7 | + protected abstract $projectData: IProjectData; |
| 8 | + protected abstract $testExecutionService: ITestExecutionService; |
| 9 | + protected abstract $analyticsService: IAnalyticsService; |
| 10 | + protected abstract $options: IOptions; |
| 11 | + protected abstract $platformEnvironmentRequirements: IPlatformEnvironmentRequirements; |
| 12 | + protected abstract $errors: IErrors; |
| 13 | + |
| 14 | + async execute(args: string[]): Promise<void> { |
| 15 | + await this.$testExecutionService.startKarmaServer(this.platform, this.$projectData, this.projectFilesConfig); |
| 16 | + } |
| 17 | + |
| 18 | + async canExecute(args: string[]): Promise<boolean | ICanExecuteCommandOutput> { |
| 19 | + this.$projectData.initializeProjectData(); |
| 20 | + this.$analyticsService.setShouldDispose(this.$options.justlaunch || !this.$options.watch); |
| 21 | + this.projectFilesConfig = helpers.getProjectFilesConfig({ isReleaseBuild: this.$options.release }); |
13 | 22 |
|
14 |
| -async function canExecute(input: { $platformEnvironmentRequirements: IPlatformEnvironmentRequirements, $projectData: IProjectData, $options: IOptions, platform: string }): Promise<boolean> { |
15 |
| - const { $platformEnvironmentRequirements, $projectData, $options, platform } = input; |
16 |
| - const output = await $platformEnvironmentRequirements.checkEnvironmentRequirements({ |
17 |
| - platform, |
18 |
| - projectDir: $projectData.projectDir, |
19 |
| - options: $options, |
20 |
| - notConfiguredEnvOptions: { |
21 |
| - hideSyncToPreviewAppOption: true, |
22 |
| - hideCloudBuildOption: true |
| 23 | + const output = await this.$platformEnvironmentRequirements.checkEnvironmentRequirements({ |
| 24 | + platform: this.platform, |
| 25 | + projectDir: this.$projectData.projectDir, |
| 26 | + options: this.$options, |
| 27 | + notConfiguredEnvOptions: { |
| 28 | + hideSyncToPreviewAppOption: true, |
| 29 | + hideCloudBuildOption: true |
| 30 | + } |
| 31 | + }); |
| 32 | + |
| 33 | + const canStartKarmaServer = await this.$testExecutionService.canStartKarmaServer(this.$projectData); |
| 34 | + if (!canStartKarmaServer) { |
| 35 | + this.$errors.fail({ |
| 36 | + formatStr: "Error: In order to run unit tests, your project must already be configured by running $ tns test init.", |
| 37 | + suppressCommandHelp: true, |
| 38 | + errorCode: ErrorCodes.TESTS_INIT_REQUIRED |
| 39 | + }); |
23 | 40 | }
|
24 |
| - }); |
25 | 41 |
|
26 |
| - return output.canExecute; |
| 42 | + return output.canExecute && canStartKarmaServer; |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +class TestAndroidCommand extends TestCommandBase implements ICommand { |
| 47 | + protected platform = "android"; |
| 48 | + |
| 49 | + constructor(protected $projectData: IProjectData, |
| 50 | + protected $testExecutionService: ITestExecutionService, |
| 51 | + protected $analyticsService: IAnalyticsService, |
| 52 | + protected $options: IOptions, |
| 53 | + protected $platformEnvironmentRequirements: IPlatformEnvironmentRequirements, |
| 54 | + protected $errors: IErrors) { |
| 55 | + super(); |
| 56 | + } |
| 57 | + |
| 58 | +} |
| 59 | + |
| 60 | +class TestIosCommand extends TestCommandBase implements ICommand { |
| 61 | + protected platform = "iOS"; |
| 62 | + |
| 63 | + constructor(protected $projectData: IProjectData, |
| 64 | + protected $testExecutionService: ITestExecutionService, |
| 65 | + protected $analyticsService: IAnalyticsService, |
| 66 | + protected $options: IOptions, |
| 67 | + protected $platformEnvironmentRequirements: IPlatformEnvironmentRequirements, |
| 68 | + protected $errors: IErrors) { |
| 69 | + super(); |
| 70 | + } |
| 71 | + |
27 | 72 | }
|
28 | 73 |
|
29 |
| -$injector.registerCommand("test|android", RunKarmaTestCommandFactory('android')); |
30 |
| -$injector.registerCommand("test|ios", RunKarmaTestCommandFactory('iOS')); |
| 74 | +$injector.registerCommand("test|android", TestAndroidCommand); |
| 75 | +$injector.registerCommand("test|ios", TestIosCommand); |
0 commit comments