Skip to content

Commit 6ba3274

Browse files
committed
test: fix unit tests
1 parent 666044d commit 6ba3274

33 files changed

+305
-318
lines changed

.vscode/launch.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"program": "${workspaceRoot}/lib/nativescript-cli.js",
1616

1717
// example commands
18-
"args": [ "run", "ios", "--path", "${workspaceRoot}/scratch/webpackApp", "--hmr"]
18+
"args": [ "test", "ios", "--path", "${workspaceRoot}/scratch/webpackApp", "--bundle"]
1919
// "args": [ "test", "android", "--justlaunch"]
2020
// "args": [ "platform", "add", "android@1.3.0", "--path", "cliapp"]
2121
// "args": [ "platform", "remove", "android", "--path", "cliapp"]

lib/commands/appstore-upload.ts

+23-52
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import * as path from "path";
22
import { StringCommandParameter } from "../common/command-params";
3+
import { BuildPlatformService } from "../services/platform/build-platform-service";
4+
import { WorkflowDataService } from "../services/workflow/workflow-data-service";
5+
import { MainController } from "../controllers/main-controller";
36

47
export class PublishIOS implements ICommand {
58
public allowedParameters: ICommandParameter[] = [new StringCommandParameter(this.$injector), new StringCommandParameter(this.$injector),
@@ -13,29 +16,20 @@ export class PublishIOS implements ICommand {
1316
private $options: IOptions,
1417
private $prompter: IPrompter,
1518
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
19+
private $mainController: MainController,
1620
private $platformValidationService: IPlatformValidationService,
17-
// private $buildPlatformService: BuildPlatformService,
18-
// private $xcodebuildService: IXcodebuildService
19-
) {
21+
private $buildPlatformService: BuildPlatformService,
22+
private $workflowDataService: WorkflowDataService
23+
) {
2024
this.$projectData.initializeProjectData();
2125
}
2226

23-
// private get $platformsData(): IPlatformsData {
24-
// return this.$injector.resolve("platformsData");
25-
// }
26-
27-
// This property was introduced due to the fact that the $platformService dependency
28-
// ultimately tries to resolve the current project's dir and fails if not executed from within a project
29-
// private get $platformService(): IPlatformService {
30-
// return this.$injector.resolve("platformService");
31-
// }
32-
3327
public async execute(args: string[]): Promise<void> {
3428
let username = args[0];
3529
let password = args[1];
3630
const mobileProvisionIdentifier = args[2];
3731
const codeSignIdentity = args[3];
38-
const ipaFilePath = this.$options.ipa ? path.resolve(this.$options.ipa) : null;
32+
let ipaFilePath = this.$options.ipa ? path.resolve(this.$options.ipa) : null;
3933

4034
if (!username) {
4135
username = await this.$prompter.getString("Apple ID", { allowEmpty: false });
@@ -54,47 +48,24 @@ export class PublishIOS implements ICommand {
5448
}
5549

5650
this.$options.release = true;
51+
const platform = this.$devicePlatformsConstants.iOS.toLowerCase();
5752

5853
if (!ipaFilePath) {
59-
// const platform = this.$devicePlatformsConstants.iOS;
6054
// No .ipa path provided, build .ipa on out own.
61-
// const platformWorkflowData = {
62-
// release: this.$options.release,
63-
// useHotModuleReload: false,
64-
// env: this.$options.env,
65-
// platformParam: platform,
66-
// signingOptions: {
67-
// teamId: this.$options.teamId,
68-
// provision: this.$options.provision
69-
// }
70-
// };
71-
// const buildConfig: IBuildConfig = {
72-
// projectDir: this.$options.path,
73-
// release: this.$options.release,
74-
// device: this.$options.device,
75-
// provision: this.$options.provision,
76-
// teamId: this.$options.teamId,
77-
// buildForDevice: true,
78-
// iCloudContainerEnvironment: this.$options.iCloudContainerEnvironment,
79-
// mobileProvisionIdentifier,
80-
// codeSignIdentity
81-
// };
82-
83-
// const platformData = this.$platformsData.getPlatformData(platform, this.$projectData);
84-
85-
// if (mobileProvisionIdentifier || codeSignIdentity) {
86-
// this.$logger.info("Building .ipa with the selected mobile provision and/or certificate.");
87-
// // This is not very correct as if we build multiple targets we will try to sign all of them using the signing identity here.
88-
// await this.$platformService.preparePlatform(platformData, this.$projectData, platformWorkflowData);
89-
// await this.$platformBuildService.buildPlatform(platformData, this.$projectData, buildConfig);
90-
// ipaFilePath = this.$platformService.lastOutputPath(platform, buildConfig, this.$projectData);
91-
// } else {
92-
// this.$logger.info("No .ipa, mobile provision or certificate set. Perfect! Now we'll build .xcarchive and let Xcode pick the distribution certificate and provisioning profile for you when exporting .ipa for AppStore submission.");
93-
// await this.$platformService.preparePlatform(platformData, this.$projectData, platformWorkflowData);
94-
95-
// ipaFilePath = await this.$xcodebuildService.buildForAppStore(platformData, this.$projectData, buildConfig);
96-
// this.$logger.info(`Export at: ${ipaFilePath}`);
97-
// }
55+
if (mobileProvisionIdentifier || codeSignIdentity) {
56+
// This is not very correct as if we build multiple targets we will try to sign all of them using the signing identity here.
57+
this.$logger.info("Building .ipa with the selected mobile provision and/or certificate.");
58+
59+
// As we need to build the package for device
60+
this.$options.forDevice = true;
61+
62+
const { nativePlatformData, buildPlatformData } = this.$workflowDataService.createWorkflowData(platform, this.$projectData.projectDir, this.$options);
63+
ipaFilePath = await this.$buildPlatformService.buildPlatform(nativePlatformData, this.$projectData, buildPlatformData);
64+
} else {
65+
this.$logger.info("No .ipa, mobile provision or certificate set. Perfect! Now we'll build .xcarchive and let Xcode pick the distribution certificate and provisioning profile for you when exporting .ipa for AppStore submission.");
66+
ipaFilePath = await this.$mainController.buildPlatform(platform, this.$projectData.projectDir, { ...this.$options, buildForAppStore: true })
67+
this.$logger.info(`Export at: ${ipaFilePath}`);
68+
}
9869
}
9970

10071
await this.$itmsTransporterService.upload({

lib/commands/test.ts

+34-6
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
import * as helpers from "../common/helpers";
1+
import { LiveSyncCommandHelper } from "../helpers/livesync-command-helper";
22

33
abstract class TestCommandBase {
44
public allowedParameters: ICommandParameter[] = [];
5-
private projectFilesConfig: IProjectFilesConfig;
65
protected abstract platform: string;
76
protected abstract $projectData: IProjectData;
87
protected abstract $testExecutionService: ITestExecutionService;
@@ -11,16 +10,41 @@ abstract class TestCommandBase {
1110
protected abstract $platformEnvironmentRequirements: IPlatformEnvironmentRequirements;
1211
protected abstract $errors: IErrors;
1312
protected abstract $cleanupService: ICleanupService;
13+
protected abstract $liveSyncCommandHelper: LiveSyncCommandHelper;
14+
protected abstract $devicesService: Mobile.IDevicesService;
1415

1516
async execute(args: string[]): Promise<void> {
16-
await this.$testExecutionService.startKarmaServer(this.platform, this.$projectData, this.projectFilesConfig);
17+
let devices = [];
18+
if (this.$options.debugBrk) {
19+
const selectedDeviceForDebug = await this.$devicesService.pickSingleDevice({
20+
onlyEmulators: this.$options.emulator,
21+
onlyDevices: this.$options.forDevice,
22+
deviceId: this.$options.device
23+
});
24+
devices = [selectedDeviceForDebug];
25+
// const debugData = this.getDebugData(platform, projectData, deployOptions, { device: selectedDeviceForDebug.deviceInfo.identifier });
26+
// await this.$debugService.debug(debugData, this.$options);
27+
} else {
28+
devices = await this.$liveSyncCommandHelper.getDeviceInstances(this.platform);
29+
}
30+
31+
if (!this.$options.env) { this.$options.env = { }; }
32+
this.$options.env.unitTesting = true;
33+
34+
const liveSyncInfo = this.$liveSyncCommandHelper.createLiveSyncInfo();
35+
36+
const deviceDebugMap: IDictionary<boolean> = {};
37+
devices.forEach(device => deviceDebugMap[device.deviceInfo.identifier] = this.$options.debugBrk);
38+
39+
const deviceDescriptors = await this.$liveSyncCommandHelper.createDeviceDescriptors(devices, this.platform, <any>{ deviceDebugMap });
40+
41+
await this.$testExecutionService.startKarmaServer(this.platform, liveSyncInfo, deviceDescriptors);
1742
}
1843

1944
async canExecute(args: string[]): Promise<boolean | ICanExecuteCommandOutput> {
2045
this.$projectData.initializeProjectData();
2146
this.$analyticsService.setShouldDispose(this.$options.justlaunch || !this.$options.watch);
2247
this.$cleanupService.setShouldDispose(this.$options.justlaunch || !this.$options.watch);
23-
this.projectFilesConfig = helpers.getProjectFilesConfig({ isReleaseBuild: this.$options.release });
2448

2549
const output = await this.$platformEnvironmentRequirements.checkEnvironmentRequirements({
2650
platform: this.platform,
@@ -54,7 +78,9 @@ class TestAndroidCommand extends TestCommandBase implements ICommand {
5478
protected $options: IOptions,
5579
protected $platformEnvironmentRequirements: IPlatformEnvironmentRequirements,
5680
protected $errors: IErrors,
57-
protected $cleanupService: ICleanupService) {
81+
protected $cleanupService: ICleanupService,
82+
protected $liveSyncCommandHelper: LiveSyncCommandHelper,
83+
protected $devicesService: Mobile.IDevicesService) {
5884
super();
5985
}
6086

@@ -69,7 +95,9 @@ class TestIosCommand extends TestCommandBase implements ICommand {
6995
protected $options: IOptions,
7096
protected $platformEnvironmentRequirements: IPlatformEnvironmentRequirements,
7197
protected $errors: IErrors,
72-
protected $cleanupService: ICleanupService) {
98+
protected $cleanupService: ICleanupService,
99+
protected $liveSyncCommandHelper: LiveSyncCommandHelper,
100+
protected $devicesService: Mobile.IDevicesService) {
73101
super();
74102
}
75103

lib/common/yok.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ import { CommandsDelimiters } from "./constants";
66
let indent = "";
77
function trace(formatStr: string, ...args: any[]) {
88
// uncomment following lines when debugging dependency injection
9-
// var args = [];
10-
// for (var _i = 1; _i < arguments.length; _i++) {
11-
// args[_i - 1] = arguments[_i];
9+
// const items: any[] = [];
10+
// for (let _i = 1; _i < arguments.length; _i++) {
11+
// items[_i - 1] = arguments[_i];
1212
// }
13-
// var util = require("util");
13+
// const util = require("util");
1414
// console.log(util.format.apply(util, [indent + formatStr].concat(args)));
1515
}
1616

lib/controllers/main-controller.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export class MainController extends EventEmitter {
3838
await this.$preparePlatformService.preparePlatform(nativePlatformData, projectData, preparePlatformData);
3939
}
4040

41-
public async buildPlatform(platform: string, projectDir: string, options: IOptions): Promise<string> {
41+
public async buildPlatform(platform: string, projectDir: string, options: IOptions | any): Promise<string> {
4242
const { nativePlatformData, projectData, buildPlatformData } = this.$workflowDataService.createWorkflowData(platform, projectDir, options);
4343

4444
await this.preparePlatform(platform, projectDir, options);

lib/declarations.d.ts

-6
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,6 @@ interface IOptions extends IRelease, IDeviceIdentifier, IJustLaunch, IAvd, IAvai
556556
javascript: boolean;
557557
androidTypings: boolean;
558558
production: boolean; //npm flag
559-
syncAllFiles: boolean;
560559
chrome: boolean;
561560
inspector: boolean; // the counterpart to --chrome
562561
background: string;
@@ -1038,11 +1037,6 @@ interface IPlatformValidationService {
10381037
isPlatformSupportedForOS(platform: string, projectData: IProjectData): boolean;
10391038
}
10401039

1041-
interface IBuildArtefactsService {
1042-
getLastBuiltPackagePath(platformData: IPlatformData, buildConfig: IBuildConfig, outputPath?: string): Promise<string>;
1043-
getAllBuiltApplicationPackages(buildOutputPath: string, validBuildOutputData: IValidBuildOutputData): IApplicationPackage[];
1044-
}
1045-
10461040
interface IPlatformCommandsService {
10471041
addPlatforms(platforms: string[], projectData: IProjectData, frameworkPath: string): Promise<void>;
10481042
cleanPlatforms(platforms: string[], projectData: IProjectData, framworkPath: string): Promise<void>;

lib/definitions/livesync.d.ts

-1
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,6 @@ declare global {
433433
interface IDeviceProjectRootOptions {
434434
appIdentifier: string;
435435
getDirname?: boolean;
436-
syncAllFiles?: boolean;
437436
watch?: boolean;
438437
}
439438

lib/definitions/project.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ interface IValidatePlatformOutput {
358358
}
359359

360360
interface ITestExecutionService {
361-
startKarmaServer(platform: string, projectData: IProjectData, projectFilesConfig: IProjectFilesConfig): Promise<void>;
361+
startKarmaServer(platform: string, liveSyncInfo: ILiveSyncInfo, deviceDescriptors: ILiveSyncDeviceInfo[]): Promise<void>;
362362
canStartKarmaServer(projectData: IProjectData): Promise<boolean>;
363363
}
364364

lib/device-path-provider.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export class DevicePathProvider implements IDevicePathProvider {
2525
projectRoot = `${LiveSyncPaths.ANDROID_TMP_DIR_NAME}/${options.appIdentifier}`;
2626
if (!options.getDirname) {
2727
const hashService = (<Mobile.IAndroidDevice>device).fileSystem.getDeviceHashService(options.appIdentifier);
28-
const hashFile = options.syncAllFiles ? null : await hashService.doesShasumFileExistsOnDevice();
28+
const hashFile = await hashService.doesShasumFileExistsOnDevice();
2929
const syncFolderName = options.watch || hashFile ? LiveSyncPaths.SYNC_DIR_NAME : LiveSyncPaths.FULLSYNC_DIR_NAME;
3030
projectRoot = path.join(projectRoot, syncFolderName);
3131
}

lib/helpers/livesync-command-helper.ts

+40-19
Original file line numberDiff line numberDiff line change
@@ -13,46 +13,51 @@ export class LiveSyncCommandHelper implements ILiveSyncCommandHelper {
1313
private $iosDeviceOperations: IIOSDeviceOperations,
1414
private $mobileHelper: Mobile.IMobileHelper,
1515
private $devicesService: Mobile.IDevicesService,
16-
private $platformsData: IPlatformsData,
16+
private $injector: IInjector,
1717
private $buildPlatformService: BuildPlatformService,
1818
private $analyticsService: IAnalyticsService,
1919
private $bundleValidatorHelper: IBundleValidatorHelper,
2020
private $errors: IErrors,
2121
private $iOSSimulatorLogProvider: Mobile.IiOSSimulatorLogProvider,
22-
private $logger: ILogger,
2322
private $cleanupService: ICleanupService
2423
) { }
2524

26-
public getPlatformsForOperation(platform: string): string[] {
27-
const availablePlatforms = platform ? [platform] : _.values<string>(this.$platformsData.availablePlatforms);
28-
return availablePlatforms;
25+
private get $platformsData(): IPlatformsData {
26+
return this.$injector.resolve("platformsData");
2927
}
3028

31-
public async executeCommandLiveSync(platform?: string, additionalOptions?: ILiveSyncCommandHelperAdditionalOptions) {
32-
if (additionalOptions && additionalOptions.syncToPreviewApp) {
33-
return;
34-
}
35-
36-
if (!this.$options.syncAllFiles) {
37-
this.$logger.info("Skipping node_modules folder! Use the syncAllFiles option to sync files from this folder.");
38-
}
39-
40-
const emulator = this.$options.emulator;
29+
public async getDeviceInstances(platform?: string): Promise<Mobile.IDevice[]> {
4130
await this.$devicesService.initialize({
42-
deviceId: this.$options.device,
4331
platform,
44-
emulator,
32+
deviceId: this.$options.device,
33+
emulator: this.$options.emulator,
4534
skipInferPlatform: !platform,
4635
sdk: this.$options.sdk
4736
});
4837

4938
const devices = this.$devicesService.getDeviceInstances()
5039
.filter(d => !platform || d.deviceInfo.platform.toLowerCase() === platform.toLowerCase());
5140

52-
await this.executeLiveSyncOperation(devices, platform, additionalOptions);
41+
return devices;
5342
}
5443

55-
public async executeLiveSyncOperation(devices: Mobile.IDevice[], platform: string, additionalOptions?: ILiveSyncCommandHelperAdditionalOptions): Promise<void> {
44+
public createLiveSyncInfo(): ILiveSyncInfo {
45+
const liveSyncInfo: ILiveSyncInfo = {
46+
projectDir: this.$projectData.projectDir,
47+
skipWatcher: !this.$options.watch || this.$options.justlaunch,
48+
clean: this.$options.clean,
49+
release: this.$options.release,
50+
env: this.$options.env,
51+
timeout: this.$options.timeout,
52+
useHotModuleReload: this.$options.hmr,
53+
force: this.$options.force,
54+
emulator: this.$options.emulator
55+
};
56+
57+
return liveSyncInfo;
58+
}
59+
60+
public async createDeviceDescriptors(devices: Mobile.IDevice[], platform: string, additionalOptions?: ILiveSyncCommandHelperAdditionalOptions): Promise<ILiveSyncDeviceInfo[]> {
5661
if (!devices || !devices.length) {
5762
if (platform) {
5863
this.$errors.failWithoutHelp("Unable to find applicable devices to execute operation. Ensure connected devices are trusted and try again.");
@@ -113,6 +118,22 @@ export class LiveSyncCommandHelper implements ILiveSyncCommandHelper {
113118
return info;
114119
});
115120

121+
return deviceDescriptors;
122+
}
123+
124+
public getPlatformsForOperation(platform: string): string[] {
125+
const availablePlatforms = platform ? [platform] : _.values<string>(this.$platformsData.availablePlatforms);
126+
return availablePlatforms;
127+
}
128+
129+
public async executeCommandLiveSync(platform?: string, additionalOptions?: ILiveSyncCommandHelperAdditionalOptions) {
130+
const devices = await this.getDeviceInstances(platform);
131+
await this.executeLiveSyncOperation(devices, platform, additionalOptions);
132+
}
133+
134+
public async executeLiveSyncOperation(devices: Mobile.IDevice[], platform: string, additionalOptions?: ILiveSyncCommandHelperAdditionalOptions): Promise<void> {
135+
const deviceDescriptors = await this.createDeviceDescriptors(devices, platform, additionalOptions);
136+
116137
const liveSyncInfo: ILiveSyncInfo = {
117138
projectDir: this.$projectData.projectDir,
118139
skipWatcher: !this.$options.watch || this.$options.justlaunch,

lib/options.ts

-1
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,6 @@ export class Options {
105105
bundle: { type: OptionType.String, hasSensitiveValue: false },
106106
all: { type: OptionType.Boolean, hasSensitiveValue: false },
107107
teamId: { type: OptionType.Object, hasSensitiveValue: true },
108-
syncAllFiles: { type: OptionType.Boolean, default: false, hasSensitiveValue: false },
109108
chrome: { type: OptionType.Boolean, hasSensitiveValue: false },
110109
inspector: { type: OptionType.Boolean, hasSensitiveValue: false },
111110
clean: { type: OptionType.Boolean, hasSensitiveValue: false },

0 commit comments

Comments
 (0)