Skip to content

Commit 666044d

Browse files
committed
refactor: refactor the prepare-platform tests
1 parent 2410493 commit 666044d

File tree

5 files changed

+225
-261
lines changed

5 files changed

+225
-261
lines changed

lib/services/platform/platform-commands-service.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,18 @@ import * as semver from "semver";
33
import * as temp from "temp";
44
import * as constants from "../../constants";
55
import { AddPlatformService } from "./add-platform-service";
6+
import { PlatformValidationService } from "./platform-validation-service";
67

78
export class PlatformCommandsService implements IPlatformCommandsService {
89
constructor(
10+
private $addPlatformService: AddPlatformService,
911
private $fs: IFileSystem,
1012
private $errors: IErrors,
1113
private $logger: ILogger,
1214
private $packageInstallationManager: IPackageInstallationManager,
1315
private $pacoteService: IPacoteService,
14-
private $addPlatformService: AddPlatformService,
1516
private $platformsData: IPlatformsData,
16-
private $platformValidationService: IPlatformValidationService,
17+
private $platformValidationService: PlatformValidationService,
1718
private $projectChangesService: IProjectChangesService,
1819
private $projectDataService: IProjectDataService
1920
) { }

test/platform-service.ts

Lines changed: 0 additions & 247 deletions
Original file line numberDiff line numberDiff line change
@@ -142,237 +142,6 @@
142142
// console.log("============ PLATFORM SERVICE ========== ", platformService);
143143
// });
144144

145-
// describe("add platform unit tests", () => {
146-
// describe("#add platform()", () => {
147-
// it("should not fail if platform is not normalized", async () => {
148-
// const fs = testInjector.resolve("fs");
149-
// fs.exists = () => false;
150-
// const projectData: IProjectData = testInjector.resolve("projectData");
151-
// await platformCommandsService.addPlatforms(["Android"], projectData, "");
152-
// await platformCommandsService.addPlatforms(["ANDROID"], projectData, "");
153-
// await platformCommandsService.addPlatforms(["AnDrOiD"], projectData, "");
154-
// await platformCommandsService.addPlatforms(["androiD"], projectData, "");
155-
156-
// await platformCommandsService.addPlatforms(["iOS"], projectData, "");
157-
// await platformCommandsService.addPlatforms(["IOS"], projectData, "");
158-
// await platformCommandsService.addPlatforms(["IoS"], projectData, "");
159-
// await platformCommandsService.addPlatforms(["iOs"], projectData, "");
160-
// });
161-
162-
// it("should fail if platform is already installed", async () => {
163-
// const projectData: IProjectData = testInjector.resolve("projectData");
164-
// // By default fs.exists returns true, so the platforms directory should exists
165-
// await assert.isRejected(platformCommandsService.addPlatforms(["android"], projectData, ""), "Platform android already added");
166-
// await assert.isRejected(platformCommandsService.addPlatforms(["ios"], projectData, ""), "Platform ios already added");
167-
// });
168-
169-
// it("should fail if unable to extract runtime package", async () => {
170-
// const fs = testInjector.resolve("fs");
171-
// fs.exists = () => false;
172-
173-
// const pacoteService = testInjector.resolve<IPacoteService>("pacoteService");
174-
// const errorMessage = "Pacote service unable to extract package";
175-
// pacoteService.extractPackage = async (packageName: string, destinationDirectory: string, options?: IPacoteExtractOptions): Promise<void> => {
176-
// throw new Error(errorMessage);
177-
// };
178-
179-
// const projectData: IProjectData = testInjector.resolve("projectData");
180-
// await assert.isRejected(platformCommandsService.addPlatforms(["android"], projectData, ""), errorMessage);
181-
// });
182-
183-
// it("fails when path passed to frameworkPath does not exist", async () => {
184-
// const fs = testInjector.resolve("fs");
185-
// fs.exists = () => false;
186-
187-
// const projectData: IProjectData = testInjector.resolve("projectData");
188-
// const frameworkPath = "invalidPath";
189-
// const errorMessage = format(AddPlaformErrors.InvalidFrameworkPathStringFormat, frameworkPath);
190-
// await assert.isRejected(platformCommandsService.addPlatforms(["android"], projectData, frameworkPath), errorMessage);
191-
// });
192-
193-
// const assertCorrectDataIsPassedToPacoteService = async (versionString: string): Promise<void> => {
194-
// const fs = testInjector.resolve("fs");
195-
// fs.exists = () => false;
196-
197-
// const pacoteService = testInjector.resolve<IPacoteService>("pacoteService");
198-
// let packageNamePassedToPacoteService = "";
199-
// pacoteService.extractPackage = async (name: string, destinationDirectory: string, options?: IPacoteExtractOptions): Promise<void> => {
200-
// packageNamePassedToPacoteService = name;
201-
// };
202-
203-
// const platformsData = testInjector.resolve<IPlatformsData>("platformsData");
204-
// const packageName = "packageName";
205-
// platformsData.getPlatformData = (platform: string, pData: IProjectData): IPlatformData => {
206-
// return {
207-
// frameworkPackageName: packageName,
208-
// platformNameLowerCase: "",
209-
// platformProjectService: new stubs.PlatformProjectServiceStub(),
210-
// projectRoot: "",
211-
// normalizedPlatformName: "",
212-
// appDestinationDirectoryPath: "",
213-
// getBuildOutputPath: () => "",
214-
// getValidBuildOutputData: (buildOptions: IBuildOutputOptions) => ({ packageNames: [] }),
215-
// frameworkFilesExtensions: [],
216-
// relativeToFrameworkConfigurationFilePath: "",
217-
// fastLivesyncFileExtensions: []
218-
// };
219-
// };
220-
// const projectData: IProjectData = testInjector.resolve("projectData");
221-
222-
// await platformCommandsService.addPlatforms(["android"], projectData, "");
223-
// assert.equal(packageNamePassedToPacoteService, `${packageName}@${versionString}`);
224-
// await platformCommandsService.addPlatforms(["ios"], projectData, "");
225-
// assert.equal(packageNamePassedToPacoteService, `${packageName}@${versionString}`);
226-
// };
227-
// it("should respect platform version in package.json's nativescript key", async () => {
228-
// const versionString = "2.5.0";
229-
// const nsValueObject: any = {
230-
// [VERSION_STRING]: versionString
231-
// };
232-
// const projectDataService = testInjector.resolve("projectDataService");
233-
// projectDataService.getNSValue = () => nsValueObject;
234-
235-
// await assertCorrectDataIsPassedToPacoteService(versionString);
236-
// });
237-
238-
// it("should install latest platform if no information found in package.json's nativescript key", async () => {
239-
240-
// const projectDataService = testInjector.resolve("projectDataService");
241-
// projectDataService.getNSValue = (): any => null;
242-
243-
// const latestCompatibleVersion = "1.0.0";
244-
// const packageInstallationManager = testInjector.resolve<IPackageInstallationManager>("packageInstallationManager");
245-
// packageInstallationManager.getLatestCompatibleVersion = async (packageName: string, referenceVersion?: string): Promise<string> => {
246-
// return latestCompatibleVersion;
247-
// };
248-
249-
// await assertCorrectDataIsPassedToPacoteService(latestCompatibleVersion);
250-
// });
251-
252-
// // Workflow: tns preview; tns platform add
253-
// it(`should add platform when only .js part of the platform has already been added (nativePlatformStatus is ${constants.NativePlatformStatus.requiresPlatformAdd})`, async () => {
254-
// const fs = testInjector.resolve("fs");
255-
// fs.exists = () => true;
256-
// const projectChangesService = testInjector.resolve("projectChangesService");
257-
// projectChangesService.getPrepareInfo = () => ({ nativePlatformStatus: constants.NativePlatformStatus.requiresPlatformAdd });
258-
// const projectData = testInjector.resolve("projectData");
259-
// let isJsPlatformAdded = false;
260-
// const preparePlatformJSService = testInjector.resolve("preparePlatformJSService");
261-
// preparePlatformJSService.addPlatform = async () => isJsPlatformAdded = true;
262-
// let isNativePlatformAdded = false;
263-
// const preparePlatformService = testInjector.resolve("preparePlatformService");
264-
// preparePlatformService.addNativePlatform = async () => isNativePlatformAdded = true;
265-
266-
// await platformCommandsService.addPlatforms(["android"], projectData, "");
267-
268-
// assert.isTrue(isJsPlatformAdded);
269-
// assert.isTrue(isNativePlatformAdded);
270-
// });
271-
272-
// // Workflow: tns platform add; tns platform add
273-
// it("shouldn't add platform when platforms folder exist and no .nsprepare file", async () => {
274-
// const fs = testInjector.resolve("fs");
275-
// fs.exists = () => true;
276-
// const projectChangesService = testInjector.resolve("projectChangesService");
277-
// projectChangesService.getPrepareInfo = () => <any>null;
278-
// const projectData = testInjector.resolve("projectData");
279-
280-
// await assert.isRejected(platformCommandsService.addPlatforms(["android"], projectData, ""), "Platform android already added");
281-
// });
282-
283-
// // Workflow: tns run; tns platform add
284-
// it(`shouldn't add platform when both native and .js parts of the platform have already been added (nativePlatformStatus is ${constants.NativePlatformStatus.alreadyPrepared})`, async () => {
285-
// const fs = testInjector.resolve("fs");
286-
// fs.exists = () => true;
287-
// const projectChangesService = testInjector.resolve("projectChangesService");
288-
// projectChangesService.getPrepareInfo = () => ({ nativePlatformStatus: constants.NativePlatformStatus.alreadyPrepared });
289-
// const projectData = testInjector.resolve("projectData");
290-
291-
// await assert.isRejected(platformCommandsService.addPlatforms(["android"], projectData, ""), "Platform android already added");
292-
// });
293-
// });
294-
// });
295-
296-
// describe("remove platform unit tests", () => {
297-
// it("should fail when platforms are not added", async () => {
298-
// const ExpectedErrorsCaught = 2;
299-
// let errorsCaught = 0;
300-
// const projectData: IProjectData = testInjector.resolve("projectData");
301-
// testInjector.resolve("fs").exists = () => false;
302-
303-
// try {
304-
// await platformCommandsService.removePlatforms(["android"], projectData);
305-
// } catch (e) {
306-
// errorsCaught++;
307-
// }
308-
309-
// try {
310-
// await platformCommandsService.removePlatforms(["ios"], projectData);
311-
// } catch (e) {
312-
// errorsCaught++;
313-
// }
314-
315-
// assert.isTrue(errorsCaught === ExpectedErrorsCaught);
316-
// });
317-
// it("shouldn't fail when platforms are added", async () => {
318-
// const projectData: IProjectData = testInjector.resolve("projectData");
319-
// testInjector.resolve("fs").exists = () => false;
320-
// await platformCommandsService.addPlatforms(["android"], projectData, "");
321-
322-
// testInjector.resolve("fs").exists = () => true;
323-
// await platformCommandsService.removePlatforms(["android"], projectData);
324-
// });
325-
// });
326-
327-
// describe("clean platform unit tests", () => {
328-
// it("should preserve the specified in the project nativescript version", async () => {
329-
// const versionString = "2.4.1";
330-
// const fs = testInjector.resolve("fs");
331-
// fs.exists = () => false;
332-
333-
// const nsValueObject: any = {};
334-
// nsValueObject[VERSION_STRING] = versionString;
335-
// const projectDataService = testInjector.resolve("projectDataService");
336-
// projectDataService.getNSValue = () => nsValueObject;
337-
338-
// const packageInstallationManager = testInjector.resolve("packageInstallationManager");
339-
// packageInstallationManager.install = (packageName: string, packageDir: string, options: INpmInstallOptions) => {
340-
// assert.deepEqual(options.version, versionString);
341-
// return "";
342-
// };
343-
344-
// const projectData: IProjectData = testInjector.resolve("projectData");
345-
// platformCommandsService.removePlatforms = (platforms: string[], prjctData: IProjectData): Promise<void> => {
346-
// nsValueObject[VERSION_STRING] = undefined;
347-
// return Promise.resolve();
348-
// };
349-
350-
// await platformCommandsService.cleanPlatforms(["android"], projectData, "");
351-
352-
// nsValueObject[VERSION_STRING] = versionString;
353-
// await platformCommandsService.cleanPlatforms(["ios"], projectData, "");
354-
// });
355-
// });
356-
357-
// // TODO: Commented as it doesn't seem correct. Check what's the case and why it's been expected to fail.
358-
// // describe("list platform unit tests", () => {
359-
// // it("fails when platforms are not added", () => {
360-
// // assert.throws(async () => await platformService.getAvailablePlatforms());
361-
// // });
362-
// // });
363-
364-
// describe("update Platform", () => {
365-
// describe("#updatePlatform(platform)", () => {
366-
// it("should fail when the versions are the same", async () => {
367-
// const packageInstallationManager: IPackageInstallationManager = testInjector.resolve("packageInstallationManager");
368-
// packageInstallationManager.getLatestVersion = async () => "0.2.0";
369-
// const projectData: IProjectData = testInjector.resolve("projectData");
370-
371-
// await assert.isRejected(platformCommandsService.updatePlatforms(["android"], projectData));
372-
// });
373-
// });
374-
// });
375-
376145
// // TODO: check this tests with QAs
377146
// // describe("prepare platform unit tests", () => {
378147
// // let fs: IFileSystem;
@@ -532,22 +301,6 @@
532301
// // fs.writeFile(fileToUpdate, content);
533302
// // }
534303

535-
// // it("should process only files in app folder when preparing for iOS platform", async () => {
536-
// // await testPreparePlatform("iOS");
537-
// // });
538-
539-
// // it("should process only files in app folder when preparing for Android platform", async () => {
540-
// // await testPreparePlatform("Android");
541-
// // });
542-
543-
// // it("should process only files in app folder when preparing for iOS platform", async () => {
544-
// // await testPreparePlatform("iOS", true);
545-
// // });
546-
547-
// // it("should process only files in app folder when preparing for Android platform", async () => {
548-
// // await testPreparePlatform("Android", true);
549-
// // });
550-
551304
// // function getDefaultFolderVerificationData(platform: string, appDestFolderPath: string) {
552305
// // const data: any = {};
553306
// // if (platform.toLowerCase() === "ios") {
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
import { InjectorStub } from "../../stubs";
2+
import { AddPlatformService } from "../../../lib/services/platform/add-platform-service";
3+
import { PacoteService } from "../../../lib/services/pacote-service";
4+
import { assert } from "chai";
5+
import { format } from "util";
6+
import { AddPlaformErrors } from "../../../lib/constants";
7+
8+
let extractedPackageFromPacote: string = null;
9+
10+
function createTestInjector() {
11+
const injector = new InjectorStub();
12+
injector.register("pacoteService", {
13+
extractPackage: async (name: string): Promise<void> => { extractedPackageFromPacote = name; }
14+
});
15+
injector.register("terminalSpinnerService", {
16+
createSpinner: () => {
17+
return {
18+
start: () => ({}),
19+
stop: () => ({})
20+
};
21+
}
22+
});
23+
injector.register("addPlatformService", AddPlatformService);
24+
25+
const fs = injector.resolve("fs");
26+
fs.exists = () => false;
27+
28+
return injector;
29+
}
30+
31+
describe("AddPlatformService", () => {
32+
describe("addPlatform", () => {
33+
let injector: IInjector, addPlatformService: AddPlatformService, projectData: IProjectData;
34+
beforeEach(() => {
35+
injector = createTestInjector();
36+
addPlatformService = injector.resolve("addPlatformService");
37+
projectData = injector.resolve("projectData");
38+
});
39+
40+
_.each(["ios", "android"], platform => {
41+
it(`should fail if unable to extract runtime package for ${platform}`, async () => {
42+
const errorMessage = "Pacote service unable to extract package";
43+
44+
const pacoteService: PacoteService = injector.resolve("pacoteService");
45+
pacoteService.extractPackage = async (): Promise<void> => { throw new Error(errorMessage); };
46+
47+
await assert.isRejected(addPlatformService.addPlatform(projectData, { platformParam: platform }), errorMessage);
48+
});
49+
it(`should fail when path passed to frameworkPath does not exist for ${platform}`, async () => {
50+
const frameworkPath = "invalidPath";
51+
const errorMessage = format(AddPlaformErrors.InvalidFrameworkPathStringFormat, frameworkPath);
52+
53+
await assert.isRejected(addPlatformService.addPlatform(projectData, { platformParam: platform, frameworkPath }), errorMessage);
54+
});
55+
it(`should respect platform version in package.json's nativescript key for ${platform}`, async () => {
56+
const version = "2.5.0";
57+
58+
const projectDataService = injector.resolve("projectDataService");
59+
projectDataService.getNSValue = () => ({ version });
60+
61+
await addPlatformService.addPlatform(projectData, { platformParam: platform });
62+
63+
const expectedPackageToAdd = `tns-${platform}@${version}`;
64+
assert.deepEqual(extractedPackageFromPacote, expectedPackageToAdd);
65+
});
66+
it(`should install latest platform if no information found in package.json's nativescript key for ${platform}`, async () => {
67+
const latestCompatibleVersion = "5.0.0";
68+
69+
const packageInstallationManager = injector.resolve<IPackageInstallationManager>("packageInstallationManager");
70+
packageInstallationManager.getLatestCompatibleVersion = async () => latestCompatibleVersion;
71+
const projectDataService = injector.resolve("projectDataService");
72+
projectDataService.getNSValue = () => <any>null;
73+
74+
await addPlatformService.addPlatform(projectData, { platformParam: platform });
75+
76+
const expectedPackageToAdd = `tns-${platform}@${latestCompatibleVersion}`;
77+
assert.deepEqual(extractedPackageFromPacote, expectedPackageToAdd);
78+
});
79+
it(`shouldn't add native platform when skipNativePrepare is provided for ${platform}`, async () => {
80+
const projectDataService = injector.resolve("projectDataService");
81+
projectDataService.getNSValue = () => ({ version: "4.2.0" });
82+
83+
let isCreateNativeProjectCalled = false;
84+
const platformsData = injector.resolve("platformsData");
85+
const platformData = platformsData.getPlatformData(platform, injector.resolve("projectData"));
86+
platformData.platformProjectService.createProject = () => isCreateNativeProjectCalled = true;
87+
platformsData.getPlatformData = () => platformData;
88+
89+
await addPlatformService.addPlatform(projectData, { platformParam: platform, nativePrepare: { skipNativePrepare: true } });
90+
assert.isFalse(isCreateNativeProjectCalled);
91+
});
92+
it(`should add native platform when skipNativePrepare is not provided for ${platform}`, async () => {
93+
const projectDataService = injector.resolve("projectDataService");
94+
projectDataService.getNSValue = () => ({ version: "4.2.0" });
95+
96+
let isCreateNativeProjectCalled = false;
97+
const platformsData = injector.resolve("platformsData");
98+
const platformData = platformsData.getPlatformData(platform, injector.resolve("projectData"));
99+
platformData.platformProjectService.createProject = () => isCreateNativeProjectCalled = true;
100+
platformsData.getPlatformData = () => platformData;
101+
102+
await addPlatformService.addPlatform(projectData, { platformParam: platform });
103+
assert.isTrue(isCreateNativeProjectCalled);
104+
});
105+
});
106+
});
107+
});

0 commit comments

Comments
 (0)