forked from NativeScript/nativescript-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.ts
208 lines (185 loc) · 6.58 KB
/
test.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
import { hasValidAndroidSigning } from "../common/helpers";
import {
ANDROID_RELEASE_BUILD_ERROR_MESSAGE,
ANDROID_APP_BUNDLE_SIGNING_ERROR_MESSAGE,
} from "../constants";
import { IProjectData, ITestExecutionService } from "../definitions/project";
import { IOptions } from "../declarations";
import { IPlatformEnvironmentRequirements } from "../definitions/platform";
import { IMigrateController } from "../definitions/migrate";
import { ICommandParameter, ICommand } from "../common/definitions/commands";
import {
OptionType,
IAnalyticsService,
IErrors,
IDictionary,
ErrorCodes,
} from "../common/declarations";
import { ICleanupService } from "../definitions/cleanup-service";
import { injector } from "../common/yok";
abstract class TestCommandBase {
public allowedParameters: ICommandParameter[] = [];
public dashedOptions = {
hmr: { type: OptionType.Boolean, default: false, hasSensitiveValue: false },
};
protected abstract platform: string;
protected abstract $projectData: IProjectData;
protected abstract $testExecutionService: ITestExecutionService;
protected abstract $analyticsService: IAnalyticsService;
protected abstract $options: IOptions;
protected abstract $platformEnvironmentRequirements: IPlatformEnvironmentRequirements;
protected abstract $errors: IErrors;
protected abstract $cleanupService: ICleanupService;
protected abstract $liveSyncCommandHelper: ILiveSyncCommandHelper;
protected abstract $devicesService: Mobile.IDevicesService;
protected abstract $migrateController: IMigrateController;
public async execute(args: string[]): Promise<void> {
let devices = [];
if (this.$options.debugBrk) {
await this.$devicesService.initialize({
platform: this.platform,
deviceId: this.$options.device,
emulator: this.$options.emulator,
skipInferPlatform: !this.platform,
sdk: this.$options.sdk,
});
const selectedDeviceForDebug = await this.$devicesService.pickSingleDevice(
{
onlyEmulators: this.$options.emulator,
onlyDevices: this.$options.forDevice,
deviceId: this.$options.device,
}
);
devices = [selectedDeviceForDebug];
// const debugData = this.getDebugData(platform, projectData, deployOptions, { device: selectedDeviceForDebug.deviceInfo.identifier });
// await this.$debugService.debug(debugData, this.$options);
} else {
devices = await this.$liveSyncCommandHelper.getDeviceInstances(
this.platform
);
}
if (!this.$options.env) {
this.$options.env = {};
}
this.$options.env.unitTesting = true;
const liveSyncInfo = this.$liveSyncCommandHelper.getLiveSyncData(
this.$projectData.projectDir
);
const deviceDebugMap: IDictionary<boolean> = {};
devices.forEach(
(device) =>
(deviceDebugMap[device.deviceInfo.identifier] = this.$options.debugBrk)
);
const deviceDescriptors = await this.$liveSyncCommandHelper.createDeviceDescriptors(
devices,
this.platform,
<any>{ deviceDebugMap }
);
await this.$testExecutionService.startKarmaServer(
this.platform,
liveSyncInfo,
deviceDescriptors
);
// if we got here, it means karma exited with exit code 0 (success)
process.exit(0);
}
async canExecute(args: string[]): Promise<boolean> {
if (!this.$options.force) {
if (this.$options.hmr) {
// With HMR we are not restarting after LiveSync which is causing a 30 seconds app start on Android
// because the Runtime does not watch for the `/data/local/tmp<appId>-livesync-in-progress` file deletion.
// The App is closing itself after each test execution and the bug will be reproducible on each LiveSync.
this.$errors.fail(
"The `--hmr` option is not supported for this command."
);
}
await this.$migrateController.validate({
projectDir: this.$projectData.projectDir,
platforms: [this.platform],
});
}
this.$projectData.initializeProjectData();
this.$analyticsService.setShouldDispose(
this.$options.justlaunch || !this.$options.watch
);
this.$cleanupService.setShouldDispose(
this.$options.justlaunch || !this.$options.watch
);
const output = await this.$platformEnvironmentRequirements.checkEnvironmentRequirements(
{
platform: this.platform,
projectDir: this.$projectData.projectDir,
options: this.$options,
notConfiguredEnvOptions: {
hideSyncToPreviewAppOption: true,
},
}
);
const canStartKarmaServer = await this.$testExecutionService.canStartKarmaServer(
this.$projectData
);
if (!canStartKarmaServer) {
this.$errors.fail({
formatStr:
"Error: In order to run unit tests, your project must already be configured by running $ ns test init.",
errorCode: ErrorCodes.TESTS_INIT_REQUIRED,
});
}
return output.canExecute && canStartKarmaServer;
}
}
class TestAndroidCommand extends TestCommandBase implements ICommand {
protected platform = "android";
constructor(
protected $projectData: IProjectData,
protected $testExecutionService: ITestExecutionService,
protected $analyticsService: IAnalyticsService,
protected $options: IOptions,
protected $platformEnvironmentRequirements: IPlatformEnvironmentRequirements,
protected $errors: IErrors,
protected $cleanupService: ICleanupService,
protected $liveSyncCommandHelper: ILiveSyncCommandHelper,
protected $devicesService: Mobile.IDevicesService,
protected $migrateController: IMigrateController
) {
super();
}
public async execute(args: string[]): Promise<void> {
await super.execute(args);
}
async canExecute(args: string[]): Promise<boolean> {
const canExecuteBase = await super.canExecute(args);
if (canExecuteBase) {
if (
(this.$options.release || this.$options.aab) &&
!hasValidAndroidSigning(this.$options)
) {
if (this.$options.release) {
this.$errors.failWithHelp(ANDROID_RELEASE_BUILD_ERROR_MESSAGE);
} else {
this.$errors.failWithHelp(ANDROID_APP_BUNDLE_SIGNING_ERROR_MESSAGE);
}
}
}
return canExecuteBase;
}
}
class TestIosCommand extends TestCommandBase implements ICommand {
protected platform = "iOS";
constructor(
protected $projectData: IProjectData,
protected $testExecutionService: ITestExecutionService,
protected $analyticsService: IAnalyticsService,
protected $options: IOptions,
protected $platformEnvironmentRequirements: IPlatformEnvironmentRequirements,
protected $errors: IErrors,
protected $cleanupService: ICleanupService,
protected $liveSyncCommandHelper: ILiveSyncCommandHelper,
protected $devicesService: Mobile.IDevicesService,
protected $migrateController: IMigrateController
) {
super();
}
}
injector.registerCommand("test|android", TestAndroidCommand);
injector.registerCommand("test|ios", TestIosCommand);