diff --git a/lib/services/platform-service.ts b/lib/services/platform-service.ts index 4fda8de1ae..63583e769f 100644 --- a/lib/services/platform-service.ts +++ b/lib/services/platform-service.ts @@ -157,15 +157,16 @@ export class PlatformService implements IPlatformService { } // Process platform specific files - var contents = this.$fs.readDirectory(platformData.appDestinationDirectoryPath).wait(); + var contents = this.$fs.readDirectory(path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME)).wait(); var files: string[] = []; _.each(contents, d => { - var fsStat = this.$fs.getFsStats(path.join(platformData.appDestinationDirectoryPath, d)).wait(); + let filePath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME, d); + let fsStat = this.$fs.getFsStats(filePath).wait(); if(fsStat.isDirectory() && d !== constants.APP_RESOURCES_FOLDER_NAME) { - this.processPlatformSpecificFiles(platform, this.$fs.enumerateFilesInDirectorySync(path.join(platformData.appDestinationDirectoryPath, d))).wait(); + this.processPlatformSpecificFiles(platform, this.$fs.enumerateFilesInDirectorySync(filePath)).wait(); } else if(fsStat.isFile()) { - files.push(path.join(platformData.appDestinationDirectoryPath, d)); + files.push(filePath); } }); diff --git a/test/platform-service.ts b/test/platform-service.ts index 0ed596c258..bd5a72f326 100644 --- a/test/platform-service.ts +++ b/test/platform-service.ts @@ -25,6 +25,9 @@ import Future = require("fibers/future"); var assert = require("chai").assert; require('should'); +let temp = require("temp"); +temp.track(); + function createTestInjector() { var testInjector = new yok.Yok(); @@ -165,4 +168,115 @@ describe('Platform Service Tests', () => { }); }); }); + + describe("prepare platform unit tests", () => { + let testInjector: IInjector, fs: IFileSystem; + beforeEach(() => { + testInjector = createTestInjector(); + testInjector.register("fs", fsLib.FileSystem); + fs = testInjector.resolve("fs"); + }); + it("should process only files in app folder when preparing for iOS platform", () => { + 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"); + + // 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" + } + }; + + let projectData = testInjector.resolve("projectData"); + projectData.projectDir = tempFolder; + + let 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.isTrue(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 = 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"); + + // 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" + } + }; + + let projectData = testInjector.resolve("projectData"); + projectData.projectDir = tempFolder; + + let 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.isTrue(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()); + }); + }); }); diff --git a/test/stubs.ts b/test/stubs.ts index 9fb439a3b0..7909889b0a 100644 --- a/test/stubs.ts +++ b/test/stubs.ts @@ -227,9 +227,7 @@ export class ProjectDataStub implements IProjectData { } export class PlatformsDataStub implements IPlatformsData { - public get platformsNames(): string[] { - return undefined; - } + public platformsNames: string[]; public getPlatformData(platform: string): IPlatformData { return {