|
| 1 | +import * as yok from "../lib/common/yok"; |
| 2 | +import {assert} from "chai"; |
| 3 | +import {CocoaPodsService} from "../lib/services/cocoapods-service"; |
| 4 | +import {EOL} from "os"; |
| 5 | +import Future = require("fibers/future"); |
| 6 | + |
| 7 | +interface IMergePodfileHooksTestCase { |
| 8 | + input: string; |
| 9 | + output: string; |
| 10 | + testCaseDescription: string; |
| 11 | +} |
| 12 | + |
| 13 | +function createTestInjector(): IInjector { |
| 14 | + let testInjector: IInjector = new yok.Yok(); |
| 15 | + |
| 16 | + testInjector.register("fs", {}); |
| 17 | + testInjector.register("cocoapodsService", CocoaPodsService); |
| 18 | + |
| 19 | + return testInjector; |
| 20 | +} |
| 21 | + |
| 22 | +// The newline characters should be replaced with EOL because on Windows the EOL is \r\n |
| 23 | +// but the character which is placed in `` for newline is only \n |
| 24 | +// if we do not replace the newline characters the tests will pass only on linux and mac. |
| 25 | +function changeNewLineCharacter(input: string): string { |
| 26 | + return input ? input.replace(/\r?\n/g, EOL) : input; |
| 27 | +} |
| 28 | + |
| 29 | +describe("Cocoapods service", () => { |
| 30 | + describe("merge Podfile hooks", () => { |
| 31 | + let testInjector: IInjector; |
| 32 | + let cocoapodsService: ICocoaPodsService; |
| 33 | + let newPodfileContent: string; |
| 34 | + |
| 35 | + let mockFileSystem = (injector: IInjector, podfileContent: string): void => { |
| 36 | + let fs: IFileSystem = injector.resolve("fs"); |
| 37 | + |
| 38 | + fs.exists = () => Future.fromResult(true); |
| 39 | + fs.readText = () => Future.fromResult(podfileContent); |
| 40 | + fs.writeFile = (pathToFile: string, content: any) => { |
| 41 | + newPodfileContent = content; |
| 42 | + return Future.fromResult(); |
| 43 | + }; |
| 44 | + }; |
| 45 | + |
| 46 | + let testCaces: IMergePodfileHooksTestCase[] = [ |
| 47 | + { |
| 48 | + input: ` |
| 49 | +target 'MyApp' do |
| 50 | + pod 'GoogleAnalytics', '~> 3.1' |
| 51 | + target 'MyAppTests' do |
| 52 | + inherit! :search_paths |
| 53 | + pod 'OCMock', '~> 2.0.1' |
| 54 | + end |
| 55 | +end |
| 56 | +
|
| 57 | +post_install do |installer| |
| 58 | + installer.pods_project.targets.each do |target| |
| 59 | + puts target.name |
| 60 | + end |
| 61 | +end |
| 62 | +post_install do |installer| |
| 63 | + installer.pods_project.targets.each do |target| |
| 64 | + puts target.name |
| 65 | + end |
| 66 | +end |
| 67 | +post_install do |installer| |
| 68 | + installer.pods_project.targets.each do |target| |
| 69 | + puts target.name |
| 70 | + end |
| 71 | +end`, |
| 72 | + output: ` |
| 73 | +target 'MyApp' do |
| 74 | + pod 'GoogleAnalytics', '~> 3.1' |
| 75 | + target 'MyAppTests' do |
| 76 | + inherit! :search_paths |
| 77 | + pod 'OCMock', '~> 2.0.1' |
| 78 | + end |
| 79 | +end |
| 80 | +
|
| 81 | +def post_install1 (installer) |
| 82 | + installer.pods_project.targets.each do |target| |
| 83 | + puts target.name |
| 84 | + end |
| 85 | +end |
| 86 | +def post_install2 (installer) |
| 87 | + installer.pods_project.targets.each do |target| |
| 88 | + puts target.name |
| 89 | + end |
| 90 | +end |
| 91 | +def post_install3 (installer) |
| 92 | + installer.pods_project.targets.each do |target| |
| 93 | + puts target.name |
| 94 | + end |
| 95 | +end |
| 96 | +post_install do |installer| |
| 97 | + post_install1 installer |
| 98 | + post_install2 installer |
| 99 | + post_install3 installer |
| 100 | +end`, |
| 101 | + testCaseDescription: "should merge more than one hooks with block parameter correctly." |
| 102 | + }, { |
| 103 | + input: ` |
| 104 | +target 'MyApp' do |
| 105 | + pod 'GoogleAnalytics', '~> 3.1' |
| 106 | + target 'MyAppTests' do |
| 107 | + inherit! :search_paths |
| 108 | + pod 'OCMock', '~> 2.0.1' |
| 109 | + end |
| 110 | +
|
| 111 | + post_install do |installer_representation| |
| 112 | + installer_representation.pods_project.targets.each do |target| |
| 113 | + puts target.name |
| 114 | + end |
| 115 | + end |
| 116 | + post_install do |
| 117 | + puts "Hello World!" |
| 118 | + end |
| 119 | +end`, |
| 120 | + output: ` |
| 121 | +target 'MyApp' do |
| 122 | + pod 'GoogleAnalytics', '~> 3.1' |
| 123 | + target 'MyAppTests' do |
| 124 | + inherit! :search_paths |
| 125 | + pod 'OCMock', '~> 2.0.1' |
| 126 | + end |
| 127 | +
|
| 128 | + def post_install1 (installer_representation) |
| 129 | + installer_representation.pods_project.targets.each do |target| |
| 130 | + puts target.name |
| 131 | + end |
| 132 | + end |
| 133 | + def post_install2 |
| 134 | + puts "Hello World!" |
| 135 | + end |
| 136 | +end |
| 137 | +post_install do |installer| |
| 138 | + post_install1 installer |
| 139 | + post_install2 |
| 140 | +end`, |
| 141 | + testCaseDescription: "should merge more than one hooks with and without block parameter correctly." |
| 142 | + }, { |
| 143 | + input: ` |
| 144 | +target 'MyApp' do |
| 145 | + pod 'GoogleAnalytics', '~> 3.1' |
| 146 | + target 'MyAppTests' do |
| 147 | + inherit! :search_paths |
| 148 | + pod 'OCMock', '~> 2.0.1' |
| 149 | + end |
| 150 | +end |
| 151 | +
|
| 152 | +post_install do |installer| |
| 153 | + installer.pods_project.targets.each do |target| |
| 154 | + puts target.name |
| 155 | + end |
| 156 | +end`, |
| 157 | + output: null, |
| 158 | + testCaseDescription: "should not change the Podfile when there is only one hook." |
| 159 | + } |
| 160 | + ]; |
| 161 | + |
| 162 | + beforeEach(() => { |
| 163 | + testInjector = createTestInjector(); |
| 164 | + cocoapodsService = testInjector.resolve("cocoapodsService"); |
| 165 | + newPodfileContent = null; |
| 166 | + }); |
| 167 | + |
| 168 | + _.each(testCaces, (testCase: IMergePodfileHooksTestCase) => { |
| 169 | + it(testCase.testCaseDescription, () => { |
| 170 | + mockFileSystem(testInjector, testCase.input); |
| 171 | + |
| 172 | + cocoapodsService.mergePodfileHookContent("post_install", "").wait(); |
| 173 | + |
| 174 | + assert.deepEqual(changeNewLineCharacter(newPodfileContent), changeNewLineCharacter(testCase.output)); |
| 175 | + }); |
| 176 | + }); |
| 177 | + }); |
| 178 | +}); |
0 commit comments