-
-
Notifications
You must be signed in to change notification settings - Fork 197
/
Copy pathplatform-service.ts
347 lines (299 loc) · 13.5 KB
/
platform-service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/// <reference path=".d.ts" />
"use strict";
import * as yok from "../lib/common/yok";
import * as stubs from "./stubs";
import * as PlatformServiceLib from "../lib/services/platform-service";
import * as StaticConfigLib from "../lib/config";
import * as fsLib from "../lib/common/file-system";
import * as optionsLib from "../lib/options";
import * as hostInfoLib from "../lib/common/host-info";
import * as ProjectFilesManagerLib from "../lib/services/project-files-manager";
import * as path from "path";
import Future = require("fibers/future");
import {assert} from "chai";
require("should");
let temp = require("temp");
temp.track();
function createTestInjector() {
let testInjector = new yok.Yok();
testInjector.register('platformService', PlatformServiceLib.PlatformService);
testInjector.register('errors', stubs.ErrorsStub);
testInjector.register('logger', stubs.LoggerStub);
testInjector.register('npmInstallationManager', stubs.NpmInstallationManagerStub);
testInjector.register('projectData', stubs.ProjectDataStub);
testInjector.register('platformsData', stubs.PlatformsDataStub);
testInjector.register('devicesService', {});
testInjector.register('androidEmulatorServices', {});
testInjector.register('projectDataService', stubs.ProjectDataService);
testInjector.register('prompter', {});
testInjector.register('lockfile', stubs.LockFile);
testInjector.register("commandsService", {
tryExecuteCommand: () => { /* intentionally left blank */ }
});
testInjector.register("options", optionsLib.Options);
testInjector.register("hostInfo", hostInfoLib.HostInfo);
testInjector.register("staticConfig", StaticConfigLib.StaticConfig);
testInjector.register("broccoliBuilder", {
prepareNodeModules: () => {
return Future.fromResult();
}
});
testInjector.register("pluginsService", {
getAllInstalledPlugins: () => {
return (() => {
return <any>[];
}).future<IPluginData[]>()();
},
ensureAllDependenciesAreInstalled: () => {
return Future.fromResult();
}
});
testInjector.register("projectFilesManager", ProjectFilesManagerLib.ProjectFilesManager);
testInjector.register("hooksService", stubs.HooksServiceStub);
return testInjector;
}
describe('Platform Service Tests', () => {
let platformService: IPlatformService, testInjector: IInjector;
beforeEach(() => {
testInjector = createTestInjector();
testInjector.register("fs", stubs.FileSystemStub);
platformService = testInjector.resolve("platformService");
});
describe("add platform unit tests", () => {
describe("#add platform()", () => {
it("should not fail if platform is not normalized", () => {
let fs = testInjector.resolve("fs");
fs.exists = () => Future.fromResult(false);
platformService.addPlatforms(["Android"]).wait();
platformService.addPlatforms(["ANDROID"]).wait();
platformService.addPlatforms(["AnDrOiD"]).wait();
platformService.addPlatforms(["androiD"]).wait();
platformService.addPlatforms(["iOS"]).wait();
platformService.addPlatforms(["IOS"]).wait();
platformService.addPlatforms(["IoS"]).wait();
platformService.addPlatforms(["iOs"]).wait();
});
it("should fail if platform is already installed", () => {
// By default fs.exists returns true, so the platforms directory should exists
(() => platformService.addPlatforms(["android"]).wait()).should.throw();
(() => platformService.addPlatforms(["ios"]).wait()).should.throw();
});
it("should fail if npm is unavalible", () => {
let fs = testInjector.resolve("fs");
fs.exists = () => Future.fromResult(false);
let errorMessage = "Npm is unavalible";
let npmInstallationManager = testInjector.resolve("npmInstallationManager");
npmInstallationManager.install = () => { throw new Error(errorMessage); };
try {
platformService.addPlatforms(["android"]).wait();
} catch(err) {
assert.equal(errorMessage, err.message);
}
});
});
describe("#add platform(ios)", () => {
it("should call validate method", () => {
let fs = testInjector.resolve("fs");
fs.exists = () => Future.fromResult(false);
let errorMessage = "Xcode is not installed or Xcode version is smaller that 5.0";
let platformsData = testInjector.resolve("platformsData");
let platformProjectService = platformsData.getPlatformData().platformProjectService;
platformProjectService.validate = () => {
throw new Error(errorMessage);
};
try {
platformService.addPlatforms(["ios"]).wait();
} catch(err) {
assert.equal(errorMessage, err.message);
}
});
});
describe("#add platform(android)", () => {
it("should fail if java, ant or android are not installed", () => {
let fs = testInjector.resolve("fs");
fs.exists = () => Future.fromResult(false);
let errorMessage = "Java, ant or android are not installed";
let platformsData = testInjector.resolve("platformsData");
let platformProjectService = platformsData.getPlatformData().platformProjectService;
platformProjectService.validate = () => {
throw new Error(errorMessage);
};
try {
platformService.addPlatforms(["android"]).wait();
} catch(err) {
assert.equal(errorMessage, err.message);
}
});
});
});
describe("remove platform unit tests", () => {
it("should fail when platforms are not added", () => {
testInjector.resolve("fs").exists = () => Future.fromResult(false);
(() => platformService.removePlatforms(["android"]).wait()).should.throw();
(() => platformService.removePlatforms(["ios"]).wait()).should.throw();
});
it("shouldn't fail when platforms are added", () => {
testInjector.resolve("fs").exists = () => Future.fromResult(false);
platformService.addPlatforms(["android"]).wait();
testInjector.resolve("fs").exists = () => Future.fromResult(true);
platformService.removePlatforms(["android"]).wait();
});
});
describe("list platform unit tests", () => {
it("fails when platforms are not added", () => {
(() => platformService.getAvailablePlatforms().wait()).should.throw();
});
});
describe("update Platform", () => {
describe("#updatePlatform(platform)", () => {
it("should fail when the versions are the same", () => {
let npmInstallationManager: INpmInstallationManager = testInjector.resolve("npmInstallationManager");
npmInstallationManager.getLatestVersion = () => (() => "0.2.0").future<string>()();
npmInstallationManager.getCacheRootPath = () => "";
(() => platformService.updatePlatforms(["android"]).wait()).should.throw();
});
});
});
describe("prepare platform unit tests", () => {
let fs: IFileSystem;
beforeEach(() => {
testInjector = createTestInjector();
testInjector.register("fs", fsLib.FileSystem);
fs = testInjector.resolve("fs");
});
function prepareDirStructure() {
let tempFolder = temp.mkdirSync("prepare platform");
let appFolderPath = path.join(tempFolder, "app");
fs.createDirectory(appFolderPath).wait();
let app1FolderPath = path.join(tempFolder, "app1");
fs.createDirectory(app1FolderPath).wait();
let appDestFolderPath = path.join(tempFolder, "appDest");
let appResourcesFolderPath = path.join(appDestFolderPath, "App_Resources");
return { tempFolder, appFolderPath, app1FolderPath, appDestFolderPath, appResourcesFolderPath };
}
it("should process only files in app folder when preparing for iOS platform", () => {
let { tempFolder, appFolderPath, app1FolderPath, appDestFolderPath, appResourcesFolderPath } = prepareDirStructure();
// Add platform specific files to app and app1 folders
let platformSpecificFiles = [
"test1.ios.js", "test1-ios-js", "test2.android.js", "test2-android-js"
];
let destinationDirectories = [appFolderPath, app1FolderPath];
_.each(destinationDirectories, directoryPath => {
_.each(platformSpecificFiles, filePath => {
let fileFullPath = path.join(directoryPath, filePath);
fs.writeFile(fileFullPath, "testData").wait();
});
});
let platformsData = testInjector.resolve("platformsData");
platformsData.platformsNames = ["ios", "android"];
platformsData.getPlatformData = (platform: string) => {
return {
appDestinationDirectoryPath: appDestFolderPath,
appResourcesDestinationDirectoryPath: appResourcesFolderPath,
normalizedPlatformName: "iOS",
platformProjectService: {
prepareProject: () => Future.fromResult(),
validate: () => Future.fromResult(),
createProject: (projectRoot: string, frameworkDir: string) => Future.fromResult(),
interpolateData: (projectRoot: string) => Future.fromResult(),
afterCreateProject: (projectRoot: string) => Future.fromResult(),
getAppResourcesDestinationDirectoryPath: () => Future.fromResult(""),
processConfigurationFilesFromAppResources: () => Future.fromResult()
}
};
};
let projectData = testInjector.resolve("projectData");
projectData.projectDir = tempFolder;
platformService = testInjector.resolve("platformService");
platformService.preparePlatform("ios").wait();
// Asserts that the files in app folder are process as platform specific
assert.isTrue(fs.exists(path.join(appDestFolderPath, "app" , "test1.js")).wait());
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test1-js")).wait());
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test2.js")).wait());
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test2-js")).wait());
// Asserts that the files in app1 folder aren't process as platform specific
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app1")).wait());
});
it("should process only files in app folder when preparing for Android platform", () => {
let { tempFolder, appFolderPath, app1FolderPath, appDestFolderPath, appResourcesFolderPath } = prepareDirStructure();
// Add platform specific files to app and app1 folders
let platformSpecificFiles = [
"test1.ios.js", "test1-ios-js", "test2.android.js", "test2-android-js"
];
let destinationDirectories = [appFolderPath, app1FolderPath];
_.each(destinationDirectories, directoryPath => {
_.each(platformSpecificFiles, filePath => {
let fileFullPath = path.join(directoryPath, filePath);
fs.writeFile(fileFullPath, "testData").wait();
});
});
let platformsData = testInjector.resolve("platformsData");
platformsData.platformsNames = ["ios", "android"];
platformsData.getPlatformData = (platform: string) => {
return {
appDestinationDirectoryPath: appDestFolderPath,
appResourcesDestinationDirectoryPath: appResourcesFolderPath,
normalizedPlatformName: "Android",
platformProjectService: {
prepareProject: () => Future.fromResult(),
validate: () => Future.fromResult(),
createProject: (projectRoot: string, frameworkDir: string) => Future.fromResult(),
interpolateData: (projectRoot: string) => Future.fromResult(),
afterCreateProject: (projectRoot: string) => Future.fromResult(),
getAppResourcesDestinationDirectoryPath: () => Future.fromResult(""),
processConfigurationFilesFromAppResources: () => Future.fromResult()
}
};
};
let projectData = testInjector.resolve("projectData");
projectData.projectDir = tempFolder;
platformService = testInjector.resolve("platformService");
platformService.preparePlatform("android").wait();
// Asserts that the files in app folder are process as platform specific
assert.isTrue(fs.exists(path.join(appDestFolderPath, "app" , "test2.js")).wait());
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test2-js")).wait());
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test1.js")).wait());
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app", "test1-js")).wait());
// Asserts that the files in app1 folder aren't process as platform specific
assert.isFalse(fs.exists(path.join(appDestFolderPath, "app1")).wait());
});
it("invalid xml is caught", () => {
require("colors");
let { tempFolder, appFolderPath, appDestFolderPath, appResourcesFolderPath } = prepareDirStructure();
// generate invalid xml
let fileFullPath = path.join(appFolderPath, "file.xml");
fs.writeFile(fileFullPath, "<xml><unclosedTag></xml>").wait();
let platformsData = testInjector.resolve("platformsData");
platformsData.platformsNames = ["android"];
platformsData.getPlatformData = (platform: string) => {
return {
appDestinationDirectoryPath: appDestFolderPath,
appResourcesDestinationDirectoryPath: appResourcesFolderPath,
normalizedPlatformName: "Android",
platformProjectService: {
prepareProject: () => Future.fromResult(),
validate: () => Future.fromResult(),
createProject: (projectRoot: string, frameworkDir: string) => Future.fromResult(),
interpolateData: (projectRoot: string) => Future.fromResult(),
afterCreateProject: (projectRoot: string) => Future.fromResult(),
getAppResourcesDestinationDirectoryPath: () => Future.fromResult(""),
processConfigurationFilesFromAppResources: () => Future.fromResult()
}
};
};
let projectData = testInjector.resolve("projectData");
projectData.projectDir = tempFolder;
platformService = testInjector.resolve("platformService");
let oldLoggerWarner = testInjector.resolve("$logger").warn;
let warnings: string = "";
try {
testInjector.resolve("$logger").warn = (text: string) => warnings += text;
platformService.preparePlatform("android").wait();
} finally {
testInjector.resolve("$logger").warn = oldLoggerWarner;
}
// Asserts that prepare has caught invalid xml
assert.isFalse(warnings.indexOf("has errors") !== -1);
});
});
});