diff --git a/lib/commands/platform-clean.ts b/lib/commands/platform-clean.ts index d2ff1106dc..2918d9c396 100644 --- a/lib/commands/platform-clean.ts +++ b/lib/commands/platform-clean.ts @@ -18,7 +18,7 @@ export class CleanCommand implements ICommand { this.$errors.fail("No platform specified. Please specify a platform to clean"); } - _.each(args, arg => this.$platformService.validatePlatform(arg, this.$projectData)); + _.each(args, arg => this.$platformService.validatePlatformInstalled(arg, this.$projectData)); return true; } diff --git a/lib/definitions/platform.d.ts b/lib/definitions/platform.d.ts index 974d2e7067..9268d5cca8 100644 --- a/lib/definitions/platform.d.ts +++ b/lib/definitions/platform.d.ts @@ -130,6 +130,11 @@ interface IPlatformService extends NodeJS.EventEmitter { cleanDestinationApp(platform: string, appFilesUpdaterOptions: IAppFilesUpdaterOptions, platformTemplate: string, projectData: IProjectData, platformSpecificData: IPlatformSpecificData): Promise; validatePlatformInstalled(platform: string, projectData: IProjectData): void; + + /** + * Ensures the passed platform is a valid one (from the supported ones) + * and that it can be built on the current OS + */ validatePlatform(platform: string, projectData: IProjectData): void; /** diff --git a/test/platform-commands.ts b/test/platform-commands.ts index 429168da5e..8791bfec88 100644 --- a/test/platform-commands.ts +++ b/test/platform-commands.ts @@ -3,6 +3,7 @@ import * as stubs from "./stubs"; import * as PlatformAddCommandLib from "../lib/commands/add-platform"; import * as PlatformRemoveCommandLib from "../lib/commands/remove-platform"; import * as PlatformUpdateCommandLib from "../lib/commands/update-platform"; +import * as PlatformCleanCommandLib from "../lib/commands/platform-clean"; import * as PlatformServiceLib from '../lib/services/platform-service'; import * as StaticConfigLib from "../lib/config"; import * as CommandsServiceLib from "../lib/common/services/commands-service"; @@ -19,7 +20,6 @@ import { MobilePlatformsCapabilities } from "../lib/mobile-platforms-capabilitie import { DevicePlatformsConstants } from "../lib/common/mobile/device-platforms-constants"; import { XmlValidator } from "../lib/xml-validator"; import * as ChildProcessLib from "../lib/common/child-process"; -import { CleanCommand } from "../lib/commands/platform-clean"; import ProjectChangesLib = require("../lib/services/project-changes-service"); let isCommandExecuted = true; @@ -105,6 +105,7 @@ function createTestInjector() { testInjector.registerCommand("platform|add", PlatformAddCommandLib.AddPlatformCommand); testInjector.registerCommand("platform|remove", PlatformRemoveCommandLib.RemovePlatformCommand); testInjector.registerCommand("platform|update", PlatformUpdateCommandLib.UpdatePlatformCommand); + testInjector.registerCommand("platform|clean", PlatformCleanCommandLib.CleanCommand); testInjector.register("lockfile", {}); testInjector.register("resources", {}); testInjector.register("commandsServiceProvider", { @@ -146,11 +147,13 @@ function createTestInjector() { describe('Platform Service Tests', () => { let platformService: IPlatformService, testInjector: IInjector; let commandsService: ICommandsService; + let fs: IFileSystem; beforeEach(() => { testInjector = createTestInjector(); testInjector.register("fs", stubs.FileSystemStub); commandsService = testInjector.resolve("commands-service"); platformService = testInjector.resolve("platformService"); + fs = testInjector.resolve("fs"); }); describe("platform commands tests", () => { @@ -301,6 +304,12 @@ describe('Platform Service Tests', () => { }); describe("#CleanPlatformCommand", () => { + beforeEach(() => { + fs.exists = (platform: string) => { + return false; + }; + }); + it("is not executed when platform is not passed", async () => { isCommandExecuted = false; commandsService.executeCommandUnchecked = async (commandName: string): Promise => { @@ -330,18 +339,25 @@ describe('Platform Service Tests', () => { }); it("is executed when platform is valid", async () => { + let commandsExecutedCount = 0; isCommandExecuted = false; commandsService.executeCommandUnchecked = async (commandName: string): Promise => { if (commandName !== "help") { isCommandExecuted = true; + commandsExecutedCount++; } return false; }; + fs.exists = (platform: string) => { + return platform === "android"; + }; + await commandsService.tryExecuteCommand("platform|add", ["android"]); await commandsService.tryExecuteCommand("platform|clean", ["android"]); assert.isTrue(isCommandExecuted); + assert.isTrue(commandsExecutedCount === 2); }); it("is not executed when platform is not added", async () => { @@ -359,23 +375,30 @@ describe('Platform Service Tests', () => { }); it("is executed when all platforms are valid", async () => { + let commandsExecutedCount = 0; isCommandExecuted = false; commandsService.executeCommandUnchecked = async (commandName: string): Promise => { if (commandName !== "help") { isCommandExecuted = true; + commandsExecutedCount++; } return false; }; + fs.exists = (platform: string) => { + return ["android", "ios"].indexOf(platform) !== -1; + }; + await commandsService.tryExecuteCommand("platform|add", ["android"]); await commandsService.tryExecuteCommand("platform|add", ["ios"]); await commandsService.tryExecuteCommand("platform|clean", ["android", "ios"]); assert.isTrue(isCommandExecuted); + assert.isTrue(commandsExecutedCount === 3); }); - it("is not executed when at least on platform is not added", async () => { + it("is not executed when at least one platform is not added", async () => { isCommandExecuted = false; commandsService.executeCommandUnchecked = async (commandName: string): Promise => { if (commandName !== "help") { @@ -385,6 +408,10 @@ describe('Platform Service Tests', () => { return false; }; + fs.exists = (platform: string) => { + return platform === "android"; + }; + await commandsService.tryExecuteCommand("platform|clean", ["android", "ios"]); assert.isFalse(isCommandExecuted); }); @@ -405,7 +432,6 @@ describe('Platform Service Tests', () => { it("will call removePlatform and addPlatform on the platformService passing the provided platforms", async () => { let platformActions: { action: string, platforms: string[] }[] = []; - testInjector.registerCommand("platform|clean", CleanCommand); let cleanCommand = testInjector.resolveCommand("platform|clean"); platformService.removePlatforms = async (platforms: string[]) => {