-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathdebug.ts
373 lines (326 loc) · 14.4 KB
/
debug.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
import * as stubs from "./stubs";
import * as yok from "../lib/common/yok";
import { DebugAndroidCommand, DebugPlatformCommand } from "../lib/commands/debug";
import { assert } from "chai";
import { Configuration, StaticConfig } from "../lib/config";
import { Options } from "../lib/options";
import { DevicePlatformsConstants } from "../lib/common/mobile/device-platforms-constants";
import { FileSystem } from "../lib/common/file-system";
import { AndroidProjectService } from "../lib/services/android-project-service";
import { AndroidDebugBridge } from "../lib/common/mobile/android/android-debug-bridge";
import { AndroidDebugBridgeResultHandler } from "../lib/common/mobile/android/android-debug-bridge-result-handler";
import { DebugCommandErrors } from "../lib/constants";
import { CONNECTED_STATUS, UNREACHABLE_STATUS } from "../lib/common/constants";
import { SettingsService } from "../lib/common/test/unit-tests/stubs";
const helpers = require("../lib/common/helpers");
const originalIsInteracive = helpers.isInteractive;
function createTestInjector(): IInjector {
const testInjector: IInjector = new yok.Yok();
testInjector.register("debug|android", DebugAndroidCommand);
testInjector.register("config", Configuration);
testInjector.register("staticConfig", StaticConfig);
testInjector.register("logger", stubs.LoggerStub);
testInjector.register("options", Options);
testInjector.register("devicePlatformsConstants", DevicePlatformsConstants);
testInjector.register('childProcess', stubs.ChildProcessStub);
testInjector.register('fs', FileSystem);
testInjector.register('errors', stubs.ErrorsStub);
testInjector.register('hostInfo', {});
testInjector.register("analyticsService", {
trackException: async (): Promise<void> => undefined,
checkConsent: async (): Promise<void> => undefined,
trackFeature: async (): Promise<void> => undefined
});
testInjector.register('devicesService', {
initialize: async () => { /* Intentionally left blank */ },
getDeviceInstances: (): any[] => { return []; },
execute: async (): Promise<any> => ({})
});
testInjector.register("liveSyncService", stubs.LiveSyncServiceStub);
testInjector.register("androidProjectService", AndroidProjectService);
testInjector.register("androidToolsInfo", stubs.AndroidToolsInfoStub);
testInjector.register("hostInfo", {});
testInjector.register("projectData", { platformsDir: "test", initializeProjectData: () => { /* empty */ } });
testInjector.register("projectDataService", {});
testInjector.register("sysInfo", {});
testInjector.register("mobileHelper", {});
testInjector.register("pluginVariablesService", {});
testInjector.register("deviceAppDataFactory", {});
testInjector.register("projectTemplatesService", {});
testInjector.register("debugService", {});
testInjector.register("xmlValidator", {});
testInjector.register("npm", {});
testInjector.register("debugDataService", {
createDebugData: () => ({})
});
testInjector.register("androidEmulatorServices", {});
testInjector.register("adb", AndroidDebugBridge);
testInjector.register("androidDebugBridgeResultHandler", AndroidDebugBridgeResultHandler);
testInjector.register("platformService", stubs.PlatformServiceStub);
testInjector.register("platformsData", {
availablePlatforms: {
Android: "Android",
iOS: "iOS"
}
});
testInjector.register("prompter", {});
testInjector.registerCommand("debug|android", DebugAndroidCommand);
testInjector.register("liveSyncCommandHelper", {
executeLiveSyncOperation: async (): Promise<void> => {
return null;
}
});
testInjector.register("settingsService", SettingsService);
testInjector.register("androidPluginBuildService", stubs.AndroidPluginBuildServiceStub);
testInjector.register("platformEnvironmentRequirements", {});
testInjector.register("androidResourcesMigrationService", stubs.AndroidResourcesMigrationServiceStub);
return testInjector;
}
describe("debug command tests", () => {
describe("getDeviceForDebug", () => {
it("throws error when both --for-device and --emulator are passed", async () => {
const testInjector = createTestInjector();
const options = testInjector.resolve<IOptions>("options");
options.forDevice = options.emulator = true;
const debugCommand = testInjector.resolve<DebugPlatformCommand>(DebugPlatformCommand, { debugService: {}, platform: "android" });
await assert.isRejected(debugCommand.getDeviceForDebug(), DebugCommandErrors.UNABLE_TO_USE_FOR_DEVICE_AND_EMULATOR);
});
it("returns selected device, when --device is passed", async () => {
const testInjector = createTestInjector();
const devicesService = testInjector.resolve<Mobile.IDevicesService>("devicesService");
const deviceInstance = <Mobile.IDevice>{};
const specifiedDeviceOption = "device1";
devicesService.getDevice = async (deviceOption: string): Promise<Mobile.IDevice> => {
if (deviceOption === specifiedDeviceOption) {
return deviceInstance;
}
};
const options = testInjector.resolve<IOptions>("options");
options.device = specifiedDeviceOption;
const debugCommand = testInjector.resolve<DebugPlatformCommand>(DebugPlatformCommand, { debugService: {}, platform: "android" });
const selectedDeviceInstance = await debugCommand.getDeviceForDebug();
assert.deepEqual(selectedDeviceInstance, deviceInstance);
});
const assertErrorIsThrown = async (getDeviceInstancesResult: Mobile.IDevice[], passedOptions?: { forDevice: boolean, emulator: boolean }) => {
const testInjector = createTestInjector();
if (passedOptions) {
const options = testInjector.resolve<IOptions>("options");
options.forDevice = passedOptions.forDevice;
options.emulator = passedOptions.emulator;
}
const devicesService = testInjector.resolve<Mobile.IDevicesService>("devicesService");
devicesService.getDeviceInstances = (): Mobile.IDevice[] => getDeviceInstancesResult;
const debugCommand = testInjector.resolve<DebugPlatformCommand>(DebugPlatformCommand, { debugService: {}, platform: "android" });
await assert.isRejected(debugCommand.getDeviceForDebug(), DebugCommandErrors.NO_DEVICES_EMULATORS_FOUND_FOR_OPTIONS);
};
it("throws error when there are no devices/emulators available", () => {
return assertErrorIsThrown([]);
});
it("throws error when there are no devices/emulators available for selected platform", () => {
return assertErrorIsThrown([
<Mobile.IDevice>{
deviceInfo: {
platform: "ios",
status: CONNECTED_STATUS
}
}
]);
});
it("throws error when there are only not-trusted devices/emulators available for selected platform", () => {
return assertErrorIsThrown([
<Mobile.IDevice>{
deviceInfo: {
platform: "android",
status: UNREACHABLE_STATUS
}
}
]);
});
it("throws error when there are only devices and --emulator is passed", () => {
return assertErrorIsThrown([
<Mobile.IDevice>{
deviceInfo: {
platform: "android",
status: CONNECTED_STATUS
},
isEmulator: false
}
], {
forDevice: false,
emulator: true
});
});
it("throws error when there are only emulators and --forDevice is passed", () => {
return assertErrorIsThrown([
<Mobile.IDevice>{
deviceInfo: {
platform: "android",
status: CONNECTED_STATUS
},
isEmulator: true
}
], {
forDevice: true,
emulator: false
});
});
it("returns the only available device/emulator when it matches passed -- options", async () => {
const testInjector = createTestInjector();
const deviceInstance = <Mobile.IDevice>{
deviceInfo: {
platform: "android",
status: CONNECTED_STATUS
},
isEmulator: true
};
const devicesService = testInjector.resolve<Mobile.IDevicesService>("devicesService");
devicesService.getDeviceInstances = (): Mobile.IDevice[] => [deviceInstance];
const debugCommand = testInjector.resolve<DebugPlatformCommand>(DebugPlatformCommand, { debugService: {}, platform: "android" });
const actualDeviceInstance = await debugCommand.getDeviceForDebug();
assert.deepEqual(actualDeviceInstance, deviceInstance);
});
describe("when multiple devices are detected", () => {
beforeEach(() => {
helpers.isInteractive = originalIsInteracive;
});
after(() => {
helpers.isInteractive = originalIsInteracive;
});
describe("when terminal is interactive", () => {
it("prompts the user with information about available devices for specified platform only and returns the selected device instance", async () => {
helpers.isInteractive = () => true;
const testInjector = createTestInjector();
const deviceInstance1 = <Mobile.IDevice>{
deviceInfo: {
platform: "android",
status: CONNECTED_STATUS,
identifier: "deviceInstance1",
displayName: "displayName1"
},
isEmulator: true
};
const deviceInstance2 = <Mobile.IDevice>{
deviceInfo: {
platform: "android",
status: CONNECTED_STATUS,
identifier: "deviceInstance2",
displayName: "displayName2"
},
isEmulator: true
};
const iOSDeviceInstance = <Mobile.IDevice>{
deviceInfo: {
platform: "ios",
status: CONNECTED_STATUS,
identifier: "iosDevice",
displayName: "iPhone"
},
isEmulator: true
};
const devicesService = testInjector.resolve<Mobile.IDevicesService>("devicesService");
devicesService.getDeviceInstances = (): Mobile.IDevice[] => [deviceInstance1, deviceInstance2, iOSDeviceInstance];
let choicesPassedToPrompter: string[];
const prompter = testInjector.resolve<IPrompter>("prompter");
prompter.promptForChoice = async (promptMessage: string, choices: any[]): Promise<string> => {
choicesPassedToPrompter = choices;
return choices[1];
};
const debugCommand = testInjector.resolve<DebugPlatformCommand>(DebugPlatformCommand, { debugService: {}, platform: "android" });
const actualDeviceInstance = await debugCommand.getDeviceForDebug();
const expectedChoicesPassedToPrompter = [deviceInstance1, deviceInstance2].map(d => `${d.deviceInfo.identifier} - ${d.deviceInfo.displayName}`);
assert.deepEqual(choicesPassedToPrompter, expectedChoicesPassedToPrompter);
assert.deepEqual(actualDeviceInstance, deviceInstance2);
});
});
describe("when terminal is not interactive", () => {
beforeEach(() => {
helpers.isInteractive = () => false;
});
const assertCorrectInstanceIsUsed = async (opts: { forDevice: boolean, emulator: boolean, isEmulatorTest: boolean, excludeLastDevice?: boolean }) => {
const testInjector = createTestInjector();
const deviceInstance1 = <Mobile.IDevice>{
deviceInfo: {
platform: "android",
status: CONNECTED_STATUS,
identifier: "deviceInstance1",
displayName: "displayName1",
version: "5.1"
},
isEmulator: opts.isEmulatorTest
};
const deviceInstance2 = <Mobile.IDevice>{
deviceInfo: {
platform: "android",
status: CONNECTED_STATUS,
identifier: "deviceInstance2",
displayName: "displayName2",
version: "6.0"
},
isEmulator: opts.isEmulatorTest
};
const deviceInstance3 = <Mobile.IDevice>{
deviceInfo: {
platform: "android",
status: CONNECTED_STATUS,
identifier: "deviceInstance3",
displayName: "displayName3",
version: "7.1"
},
isEmulator: !opts.isEmulatorTest
};
const options = testInjector.resolve<IOptions>("options");
options.forDevice = opts.forDevice;
options.emulator = opts.emulator;
const devicesService = testInjector.resolve<Mobile.IDevicesService>("devicesService");
const deviceInstances = [deviceInstance1, deviceInstance2];
if (!opts.excludeLastDevice) {
deviceInstances.push(deviceInstance3);
}
devicesService.getDeviceInstances = (): Mobile.IDevice[] => deviceInstances;
const debugCommand = testInjector.resolve<DebugPlatformCommand>(DebugPlatformCommand, { debugService: {}, platform: "android" });
const actualDeviceInstance = await debugCommand.getDeviceForDebug();
assert.deepEqual(actualDeviceInstance, deviceInstance2);
};
it("returns the emulator with highest API level when --emulator is passed", () => {
return assertCorrectInstanceIsUsed({ forDevice: false, emulator: true, isEmulatorTest: true });
});
it("returns the device with highest API level when --forDevice is passed", () => {
return assertCorrectInstanceIsUsed({ forDevice: true, emulator: false, isEmulatorTest: false });
});
it("returns the emulator with highest API level when neither --emulator and --forDevice are passed", () => {
return assertCorrectInstanceIsUsed({ forDevice: false, emulator: false, isEmulatorTest: true });
});
it("returns the device with highest API level when neither --emulator and --forDevice are passed and emulators are not available", async () => {
return assertCorrectInstanceIsUsed({ forDevice: false, emulator: false, isEmulatorTest: false, excludeLastDevice: true });
});
});
});
});
describe("Debugger tests", () => {
let testInjector: IInjector;
beforeEach(() => {
testInjector = createTestInjector();
});
it("Ensures that beforePrepareAllPlugins will call gradle with clean option when *NOT* livesyncing", async () => {
const platformData = testInjector.resolve<IPlatformData>("platformsData");
platformData.frameworkPackageName = "tns-android";
// only test that 'clean' is performed on android <=3.2. See https://github.com/NativeScript/nativescript-cli/pull/3032
const projectDataService: IProjectDataService = testInjector.resolve("projectDataService");
projectDataService.getNSValue = (projectDir: string, propertyName: string) => {
return { version: "3.2.0" };
};
const childProcess: stubs.ChildProcessStub = testInjector.resolve("childProcess");
const androidProjectService: IPlatformProjectService = testInjector.resolve("androidProjectService");
androidProjectService.getPlatformData = (projectData: IProjectData): IPlatformData => {
return platformData;
};
const projectData: IProjectData = testInjector.resolve("projectData");
const spawnFromEventCount = childProcess.spawnFromEventCount;
await androidProjectService.beforePrepareAllPlugins(projectData);
assert.isTrue(childProcess.lastCommand.indexOf("gradle") !== -1);
assert.isTrue(childProcess.lastCommandArgs[0] === "clean");
assert.isTrue(spawnFromEventCount === 0);
assert.isTrue(spawnFromEventCount + 1 === childProcess.spawnFromEventCount);
});
});
});