Skip to content

Commit be516bb

Browse files
committed
test: Add tests for base Podfile and source code in App_Resources
1 parent 48a552e commit be516bb

File tree

1 file changed

+154
-2
lines changed

1 file changed

+154
-2
lines changed

test/ios-project-service.ts

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,12 @@ function createTestInjector(projectPath: string, projectName: string, xcode?: IX
7575
projectId: "",
7676
projectIdentifiers: { android: "", ios: "" },
7777
projectDir: "",
78-
appDirectoryPath: ""
78+
appDirectoryPath: "",
79+
appResourcesDirectoryPath: ""
7980
});
8081
projectData.projectDir = temp.mkdirSync("projectDir");
8182
projectData.appDirectoryPath = path.join(projectData.projectDir, "app");
83+
projectData.appResourcesDirectoryPath = path.join(projectData.appDirectoryPath, "App_Resources");
8284
testInjector.register("projectData", projectData);
8385
testInjector.register("projectHelper", {});
8486
testInjector.register("xcodeSelectService", {});
@@ -339,6 +341,69 @@ describe("Cocoapods support", () => {
339341
if (require("os").platform() !== "darwin") {
340342
console.log("Skipping Cocoapods tests. They cannot work on windows");
341343
} else {
344+
it("adds а base Podfile", async () => {
345+
const projectName = "projectDirectory";
346+
const projectPath = temp.mkdirSync(projectName);
347+
348+
const testInjector = createTestInjector(projectPath, projectName);
349+
const fs: IFileSystem = testInjector.resolve("fs");
350+
const cocoapodsService = testInjector.resolve("cocoapodsService")
351+
352+
const packageJsonData = {
353+
"name": "myProject",
354+
"version": "0.1.0",
355+
"nativescript": {
356+
"id": "org.nativescript.myProject",
357+
"tns-ios": {
358+
"version": "1.0.0"
359+
}
360+
}
361+
};
362+
fs.writeJson(path.join(projectPath, "package.json"), packageJsonData);
363+
364+
const platformsFolderPath = path.join(projectPath, "platforms", "ios");
365+
fs.createDirectory(platformsFolderPath);
366+
367+
const iOSProjectService = testInjector.resolve("iOSProjectService");
368+
iOSProjectService.createPbxProj = () => {
369+
return {
370+
updateBuildProperty: () => { return {}; },
371+
pbxXCBuildConfigurationSection: () => { return {}; },
372+
};
373+
};
374+
iOSProjectService.savePbxProj = (): Promise<void> => Promise.resolve();
375+
376+
const projectData: IProjectData = testInjector.resolve("projectData");
377+
const basePodfileModuleName = "BasePodfile";
378+
379+
const basePodfilePath = path.join(projectData.appDirectoryPath, "App_Resources", "iOS", "Podfile");
380+
const pluginPodfileContent = ["source 'https://github.com/CocoaPods/Specs.git'", "platform :ios, '8.1'", "pod 'GoogleMaps'"].join("\n");
381+
fs.writeFile(basePodfilePath, pluginPodfileContent);
382+
383+
projectData.podfilePath = basePodfilePath;
384+
385+
cocoapodsService.applyPodfileToProject(basePodfileModuleName, basePodfilePath, projectData, iOSProjectService.getPlatformData(projectData).projectRoot);
386+
387+
const projectPodfilePath = path.join(platformsFolderPath, "Podfile");
388+
assert.isTrue(fs.exists(projectPodfilePath));
389+
390+
let actualProjectPodfileContent = fs.readText(projectPodfilePath);
391+
let expectedProjectPodfileContent = ["use_frameworks!\n",
392+
`target "${projectName}" do`,
393+
`# Begin Podfile - ${basePodfilePath}`,
394+
`${pluginPodfileContent}`,
395+
"# End Podfile",
396+
"end"]
397+
.join("\n");
398+
assert.equal(actualProjectPodfileContent, expectedProjectPodfileContent);
399+
400+
fs.deleteFile(basePodfilePath);
401+
402+
cocoapodsService.applyPodfileToProject(basePodfileModuleName, basePodfilePath, projectData, iOSProjectService.getPlatformData(projectData).projectRoot);
403+
assert.isFalse(fs.exists(projectPodfilePath));
404+
405+
});
406+
342407
it("adds plugin with Podfile", async () => {
343408
const projectName = "projectDirectory";
344409
const projectPath = temp.mkdirSync(projectName);
@@ -486,11 +551,59 @@ describe("Cocoapods support", () => {
486551
}
487552
});
488553

489-
describe("Source code in plugin support", () => {
554+
describe("Source code support", () => {
490555
if (require("os").platform() !== "darwin") {
491556
console.log("Skipping Source code in plugin tests. They cannot work on windows");
492557
} else {
493558

559+
const getProjectWithoutPlugins = async (files: string[]) => {
560+
// Arrange
561+
const projectName = "projectDirectory";
562+
const projectPath = temp.mkdirSync(projectName);
563+
const testInjector = createTestInjector(projectPath, projectName, xcode);
564+
const fs: IFileSystem = testInjector.resolve("fs");
565+
566+
const packageJsonData = {
567+
"name": "myProject",
568+
"version": "0.1.0",
569+
"nativescript": {
570+
"id": "org.nativescript.myProject",
571+
"tns-ios": {
572+
"version": "1.0.0"
573+
}
574+
}
575+
};
576+
fs.writeJson(path.join(projectPath, "package.json"), packageJsonData);
577+
578+
const platformsFolderPath = path.join(projectPath, "platforms", "ios");
579+
fs.createDirectory(platformsFolderPath);
580+
581+
const iOSProjectService = testInjector.resolve("iOSProjectService");
582+
583+
iOSProjectService.getXcodeprojPath = () => {
584+
return path.join(__dirname, "files");
585+
};
586+
let pbxProj: any;
587+
iOSProjectService.savePbxProj = (project: any): Promise<void> => {
588+
pbxProj = project;
589+
return Promise.resolve();
590+
};
591+
592+
const projectData: IProjectData = testInjector.resolve("projectData");
593+
594+
const platformSpecificAppResourcesPath = path.join(projectData.appResourcesDirectoryPath, iOSProjectService.getPlatformData(projectData).normalizedPlatformName);
595+
596+
files.forEach(file => {
597+
const fullPath = path.join(platformSpecificAppResourcesPath, file);
598+
fs.createDirectory(path.dirname(fullPath));
599+
fs.writeFile(fullPath, "");
600+
});
601+
602+
await iOSProjectService.prepareNativeSourceCode("src", platformSpecificAppResourcesPath, projectData);
603+
604+
return pbxProj;
605+
}
606+
494607
const preparePluginWithFiles = async (files: string[], prepareMethodToCall: string) => {
495608
// Arrange
496609
const projectName = "projectDirectory";
@@ -555,6 +668,45 @@ describe("Source code in plugin support", () => {
555668
return pbxProj;
556669
};
557670

671+
it("adds source files as resources", async () => {
672+
const sourceFileNames = [
673+
"src/Header.h", "src/ObjC.m",
674+
"src/nested/Header.hpp", "src/nested/Source.cpp", "src/nested/ObjCpp.mm",
675+
"src/nested/level2/Header2.hxx", "src/nested/level2/Source2.cxx", "src/nested/level2/Source3.c",
676+
"src/SomeOtherExtension.donotadd",
677+
];
678+
679+
const projectName = "projectDirectory";
680+
const projectPath = temp.mkdirSync(projectName);
681+
const testInjector = createTestInjector(projectPath, projectName, xcode);
682+
const fs: IFileSystem = testInjector.resolve("fs");
683+
684+
const platformsFolderPath = path.join(projectPath, "platforms", "ios");
685+
fs.createDirectory(platformsFolderPath);
686+
687+
const pbxProj = await await getProjectWithoutPlugins(sourceFileNames);
688+
689+
const pbxFileReference = pbxProj.hash.project.objects.PBXFileReference;
690+
const pbxFileReferenceValues = Object.keys(pbxFileReference).map(key => pbxFileReference[key]);
691+
const buildPhaseFiles = pbxProj.hash.project.objects.PBXSourcesBuildPhase["858B83F218CA22B800AB12DE"].files;
692+
693+
sourceFileNames.map(file => path.basename(file)).forEach(basename => {
694+
const ext = path.extname(basename);
695+
const shouldBeAdded = ext !== ".donotadd";
696+
if (shouldBeAdded) {
697+
assert.notEqual(pbxFileReferenceValues.indexOf(basename), -1, `${basename} not added to PBXFileRefereces`);
698+
699+
if (shouldBeAdded && !path.extname(basename).startsWith(".h")) {
700+
const buildPhaseFile = buildPhaseFiles.find((fileObject: any) => fileObject.comment.startsWith(basename));
701+
assert.isDefined(buildPhaseFile, `${basename} not added to PBXSourcesBuildPhase`);
702+
assert.include(buildPhaseFile.comment, "in Sources", `${basename} must be added to Sources group`);
703+
}
704+
} else {
705+
assert.equal(pbxFileReferenceValues.indexOf(basename), -1, `${basename} was added to PBXFileRefereces, but it shouldn't have been`);
706+
}
707+
});
708+
});
709+
558710
it("adds plugin with Source files", async () => {
559711
const sourceFileNames = [
560712
"src/Header.h", "src/ObjC.m",

0 commit comments

Comments
 (0)