From 7583fa971c3dc2d841c631f736d8067d37314d7d Mon Sep 17 00:00:00 2001 From: fatme Date: Mon, 25 Feb 2019 23:25:10 +0200 Subject: [PATCH] fix: add new line at the end of the platform's section in order to remove it correctly Steps to reproduce: 1. `tns create myApp --js` 2. `tns plugin add nativescript-imagepicker` && `tns plugin add nativescript-facebook` 3. Replace the content of `./node_modules/nativescript-imagepicker/platforms/ios/Podfile` with `platform: ios, '9.0'` 4. Replace the content of `./node_modules/nativescript-facebook/platforms/ios/Podfile` with `platform: ios, '10.0'` 5. Add Podfile in `./app/App_Resources/iOS` with the following content `platform: ios: '8.0'` 6. Execute `tns prepare ios` 7. Remove `./app/App_Resources/iOS/Podfile` 8. Execute `tns prepare ios` Expected behavior: Is should pass correctly and the content of `platforms/ios/Podfile` should be ``` ... # NativeScriptPlatformSection /node_modules/nativescript-facebook/platforms/ios/Podfile with 10.0 platform :ios, '10.0' # End NativeScriptPlatformSection end ``` Actual behavior: It fails with `[!] Invalid `Podfile` file: syntax error, unexpected end-of-input, expecting keyword_end.` error and the the content of `platforms/ios/Podfile` is: ``` ... # NativeScriptPlatformSection /node_modules/nativescript-facebook/platforms/ios/Podfile with 10.0 platform :ios, '10.0' # End NativeScriptPlatformSectionend ``` --- lib/services/cocoapods-platform-manager.ts | 2 +- test/cocoapods-service.ts | 151 +++++++++++++++++---- 2 files changed, 124 insertions(+), 29 deletions(-) diff --git a/lib/services/cocoapods-platform-manager.ts b/lib/services/cocoapods-platform-manager.ts index b271842c85..6b5bfb3771 100644 --- a/lib/services/cocoapods-platform-manager.ts +++ b/lib/services/cocoapods-platform-manager.ts @@ -118,7 +118,7 @@ export class CocoaPodsPlatformManager implements ICocoaPodsPlatformManager { result += ` ${podfilePlatformData.version}`; } - result += `${EOL}${podfilePlatformData.content}${EOL}${this.getPlatformSectionFooter()}`; + result += `${EOL}${podfilePlatformData.content}${EOL}${this.getPlatformSectionFooter()}${EOL}`; return result; } diff --git a/test/cocoapods-service.ts b/test/cocoapods-service.ts index 8f20ed93a5..1fd9105ee0 100644 --- a/test/cocoapods-service.ts +++ b/test/cocoapods-service.ts @@ -913,6 +913,128 @@ end` describe("remove duplicated platfoms from project podfile", () => { const projectRoot = "my/project/platforms/ios"; const projectPodfilePath = path.join(projectRoot, "testProjectPodfilePath"); + let projectPodfileContent = ""; + + beforeEach(() => { + cocoapodsService.getProjectPodfilePath = () => projectPodfilePath; + projectPodfileContent = ""; + }); + + function setupMocks(pods: any[]): { projectData: IProjectData } { + const podsPaths = pods.map(p => p.path); + const projectData = testInjector.resolve("projectData"); + projectData.getAppResourcesDirectoryPath = () => "my/full/path/to/app/App_Resources"; + projectData.projectName = "projectName"; + + const fs = testInjector.resolve("fs"); + fs.exists = (filePath: string) => projectPodfilePath === filePath || _.includes(podsPaths, filePath); + fs.readText = (filePath: string) => { + if (filePath === projectPodfilePath) { + return projectPodfileContent; + } + + const pod = _.find(pods, p => p.path === filePath); + if (pod) { + return pod.content; + } + }; + fs.writeFile = (filePath: string, fileContent: string) => { + if (filePath === projectPodfilePath) { + projectPodfileContent = fileContent; + } + }; + fs.deleteFile = () => ({}); + + return { projectData }; + } + + const testCasesWithApplyAndRemove = [ + { + name: "should select the podfile with highest platform after Podfile from App_Resources has been deleted", + pods: [{ + name: "mySecondPluginWithPlatform", + path: "node_modules/ mypath with spaces/mySecondPluginWithPlatform/Podfile", + content: `platform :ios, '10.0'` + }, { + name: "myPluginWithoutPlatform", + path: "node_modules/myPluginWithoutPlatform/Podfile", + content: `pod 'myPod' ~> 0.3.4` + }, { + name: "myFirstPluginWithPlatform", + path: "node_modules/myFirstPluginWithPlatform/Podfile", + content: `platform :ios, '11.0'` + }, { + name: "App_Resources", + path: "my/full/path/to/app/App_Resources/iOS/Podfile", + content: `platform :ios, '8.0'` + }], + podsToRemove: [{ + name: "NSPodfileBase", + path: "my/full/path/to/app/App_Resources/iOS/Podfile" + }], + expectedProjectPodfileContentAfterApply: `use_frameworks! + +target "projectName" do +# Begin Podfile - my/full/path/to/app/App_Resources/iOS/Podfile +# platform :ios, '8.0' +# End Podfile + +# Begin Podfile - node_modules/myFirstPluginWithPlatform/Podfile +# platform :ios, '11.0' +# End Podfile + +# Begin Podfile - node_modules/myPluginWithoutPlatform/Podfile +pod 'myPod' ~> 0.3.4 +# End Podfile + +# Begin Podfile - node_modules/ mypath with spaces/mySecondPluginWithPlatform/Podfile +# platform :ios, '10.0' +# End Podfile + +# NativeScriptPlatformSection my/full/path/to/app/App_Resources/iOS/Podfile with 8.0 +platform :ios, '8.0' +# End NativeScriptPlatformSection +end`, + expectedProjectPodfileContentAfterRemove: `use_frameworks! + +target "projectName" do + +# Begin Podfile - node_modules/myFirstPluginWithPlatform/Podfile +# platform :ios, '11.0' +# End Podfile + +# Begin Podfile - node_modules/myPluginWithoutPlatform/Podfile +pod 'myPod' ~> 0.3.4 +# End Podfile + +# Begin Podfile - node_modules/ mypath with spaces/mySecondPluginWithPlatform/Podfile +# platform :ios, '10.0' +# End Podfile +# NativeScriptPlatformSection node_modules/myFirstPluginWithPlatform/Podfile with 11.0 +platform :ios, '11.0' +# End NativeScriptPlatformSection +end` + } + ]; + + _.each(testCasesWithApplyAndRemove, testCase => { + it(testCase.name, async () => { + const { projectData } = setupMocks(testCase.pods); + + for (const pod of testCase.pods) { + await cocoapodsService.applyPodfileToProject(pod.name, pod.path, projectData, projectPodfilePath); + } + + assert.deepEqual(projectPodfileContent, testCase.expectedProjectPodfileContentAfterApply); + + for (const pod of testCase.podsToRemove) { + await cocoapodsService.removePodfileFromProject(pod.name, pod.path, projectData, projectPodfilePath); + } + + assert.deepEqual(projectPodfileContent, testCase.expectedProjectPodfileContentAfterRemove); + }); + }); + const testCases = [ { name: "should not change the Podfile when no platform", @@ -1130,36 +1252,9 @@ end` } ]; - beforeEach(() => { - cocoapodsService.getProjectPodfilePath = () => projectPodfilePath; - }); - _.each(testCases, testCase => { it(testCase.name, async () => { - const podsPaths = testCase.pods.map(p => p.path); - let projectPodfileContent = ""; - - const projectData = testInjector.resolve("projectData"); - projectData.getAppResourcesDirectoryPath = () => "my/full/path/to/app/App_Resources"; - projectData.projectName = "projectName"; - - const fs = testInjector.resolve("fs"); - fs.exists = (filePath: string) => projectPodfilePath === filePath || _.includes(podsPaths, filePath); - fs.readText = (filePath: string) => { - if (filePath === projectPodfilePath) { - return projectPodfileContent; - } - - const pod = _.find(testCase.pods, p => p.path === filePath); - if (pod) { - return pod.content; - } - }; - fs.writeFile = (filePath: string, fileContent: string) => { - if (filePath === projectPodfilePath) { - projectPodfileContent = fileContent; - } - }; + const { projectData } = setupMocks(testCase.pods); cocoapodsService.removePodfileFromProject = () => ({}); for (const pod of testCase.pods) {