Skip to content

Check XML files for well-formedness #1271

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 27, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion lib/commands/prepare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ export class PrepareCommand implements ICommand {
private $platformCommandParameter: ICommandParameter) { }

execute(args: string[]): IFuture<void> {
return this.$platformService.preparePlatform(args[0]);
return (() => {
this.$platformService.preparePlatform(args[0]);
}).future<void>()();
}

allowedParameters = [this.$platformCommandParameter];
Expand Down
2 changes: 1 addition & 1 deletion lib/definitions/platform.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ interface IPlatformService {
removePlatforms(platforms: string[]): IFuture<void>;
updatePlatforms(platforms: string[]): IFuture<void>;
runPlatform(platform: string, buildConfig?: IBuildConfig): IFuture<void>;
preparePlatform(platform: string): IFuture<void>;
preparePlatform(platform: string): IFuture<boolean>;
buildPlatform(platform: string, buildConfig?: IBuildConfig): IFuture<void>;
installOnDevice(platform: string, buildConfig?: IBuildConfig): IFuture<void>;
deployOnDevice(platform: string, buildConfig?: IBuildConfig): IFuture<void>;
Expand Down
45 changes: 39 additions & 6 deletions lib/services/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ export class PlatformService implements IPlatformService {
}).future<string[]>()();
}

public preparePlatform(platform: string): IFuture<void> {
public preparePlatform(platform: string): IFuture<boolean> {
return (() => {
this.validatePlatform(platform);

Expand All @@ -166,12 +166,36 @@ export class PlatformService implements IPlatformService {
this.$errors.failWithoutHelp(`Unable to install dependencies. Make sure your package.json is valid and all dependencies are correct. Error is: ${err.message}`);
}

this.preparePlatformCore(platform).wait();
}).future<void>()();
return this.preparePlatformCore(platform).wait();
}).future<boolean>()();
}

private checkXmlFiles(sourceFiles: string[]): IFuture<boolean> {
return (() => {
let xmlHasErrors = false;
let DomParser = require("xmldom").DOMParser;
sourceFiles
.filter(file => _.endsWith(file, ".xml"))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe path.ext

.forEach(file => {
let fileContents = this.$fs.readText(file).wait();
let hasErrors = false;
let domErrorHandler = (level:any, msg:string) => hasErrors = true;
let parser = new DomParser({
locator:{},
errorHandler: domErrorHandler
});
parser.parseFromString(fileContents, "text/xml");
xmlHasErrors = xmlHasErrors || hasErrors;
if (xmlHasErrors) {
this.$logger.out("Error: ".red.bold + `${file} has syntax errors.`);
}
});
return !xmlHasErrors;
}).future<boolean>()();
}

@helpers.hook('prepare')
private preparePlatformCore(platform: string): IFuture<void> {
private preparePlatformCore(platform: string): IFuture<boolean> {
return (() => {
platform = platform.toLowerCase();
this.ensurePlatformInstalled(platform).wait();
Expand Down Expand Up @@ -206,6 +230,12 @@ export class PlatformService implements IPlatformService {
sourceFiles = sourceFiles.filter(source => !minimatch(source, `**/${constants.TNS_MODULES_FOLDER_NAME}/**`, {nocase: true}));
}

// verify .xml files are well-formed
let validXmlFiles = this.checkXmlFiles(sourceFiles).wait();
if (!validXmlFiles) {
return false;
}

// Remove .ts and .js.map files
PlatformService.EXCLUDE_FILES_PATTERN.forEach(pattern => sourceFiles = sourceFiles.filter(file => !minimatch(file, pattern, {nocase: true})));
let copyFileFutures = sourceFiles.map(source => {
Expand Down Expand Up @@ -245,13 +275,16 @@ export class PlatformService implements IPlatformService {
platformData.platformProjectService.processConfigurationFilesFromAppResources().wait();

this.$logger.out("Project successfully prepared");
}).future<void>()();
return true;
}).future<boolean>()();
}

public buildPlatform(platform: string, buildConfig?: IBuildConfig): IFuture<void> {
return (() => {
platform = platform.toLowerCase();
this.preparePlatform(platform).wait();
if (!this.preparePlatform(platform).wait()) {
this.$errors.failWithoutHelp("Verify that listed files are well-formed and try again the operation.");
}

let platformData = this.$platformsData.getPlatformData(platform);
platformData.platformProjectService.buildProject(platformData.projectRoot, buildConfig).wait();
Expand Down
12 changes: 9 additions & 3 deletions lib/services/test-execution-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class TestExecutionService implements ITestExecutionService {
private $logger: ILogger,
private $fs: IFileSystem,
private $options: IOptions,
private $pluginsService: IPluginsService) {
private $pluginsService: IPluginsService,
private $errors: IErrors) {
}

public startTestRunner(platform: string) : IFuture<void> {
Expand All @@ -58,7 +59,9 @@ class TestExecutionService implements ITestExecutionService {
let socketIoJs = this.$httpClient.httpRequest(socketIoJsUrl).wait().body;
this.$fs.writeFile(path.join(projectDir, TestExecutionService.SOCKETIO_JS_FILE_NAME), socketIoJs).wait();

this.$platformService.preparePlatform(platform).wait();
if (!this.$platformService.preparePlatform(platform).wait()) {
this.$errors.failWithoutHelp("Verify that listed files are well-formed and try again the operation.");
}
this.detourEntryPoint(projectFilesPath).wait();

let watchGlob = path.join(projectDir, constants.APP_FOLDER_NAME);
Expand Down Expand Up @@ -88,7 +91,10 @@ class TestExecutionService implements ITestExecutionService {

let beforeBatchLiveSyncAction = (filePath: string): IFuture<string> => {
return (() => {
this.$platformService.preparePlatform(platform).wait();
if (!this.$platformService.preparePlatform(platform).wait()) {
this.$logger.out("Verify that listed files are well-formed and try again the operation.");
return;
}
return path.join(projectFilesPath, path.relative(path.join(this.$projectData.projectDir, constants.APP_FOLDER_NAME), filePath));
}).future<string>()();
};
Expand Down
15 changes: 12 additions & 3 deletions lib/services/usb-livesync-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class UsbLiveSyncService extends usbLivesyncServiceBaseLib.UsbLiveSyncSer
private $devicePlatformsConstants: Mobile.IDevicePlatformsConstants,
private $projectDataService: IProjectDataService,
private $prompter: IPrompter,
private $errors: IErrors,
$hostInfo: IHostInfo) {
super($devicesService, $mobileHelper, $localToDevicePathDataFactory, $logger, $options,
$deviceAppDataFactory, $fs, $dispatcher, $injector, $childProcess, $iOSEmulatorServices,
Expand Down Expand Up @@ -65,7 +66,9 @@ export class UsbLiveSyncService extends usbLivesyncServiceBaseLib.UsbLiveSyncSer
}
}

this.$platformService.preparePlatform(platform).wait();
if (!this.$platformService.preparePlatform(platform).wait()) {
this.$errors.failWithoutHelp("Verify that listed files are well-formed and try again the operation.");
}

let projectFilesPath = path.join(platformData.appDestinationDirectoryPath, constants.APP_FOLDER_NAME);

Expand Down Expand Up @@ -121,7 +124,10 @@ export class UsbLiveSyncService extends usbLivesyncServiceBaseLib.UsbLiveSyncSer
let fastLiveSync = (filePath: string) => {
this.$dispatcher.dispatch(() => {
return (() => {
this.$platformService.preparePlatform(platform).wait();
if (!this.$platformService.preparePlatform(platform).wait()) {
this.$logger.out("Verify that listed files are well-formed and try again the operation.");
return;
}
let mappedFilePath = beforeBatchLiveSyncAction(filePath).wait();

if (this.shouldSynciOSSimulator(platform).wait()) {
Expand Down Expand Up @@ -177,7 +183,10 @@ export class UsbLiveSyncService extends usbLivesyncServiceBaseLib.UsbLiveSyncSer
}

protected preparePlatformForSync(platform: string) {
this.$platformService.preparePlatform(platform).wait();
if (!this.$platformService.preparePlatform(platform).wait()) {
this.$logger.out("Verify that listed files are well-formed and try again the operation.");
return;
}
}

private resolveUsbLiveSyncService(platform: string, device: Mobile.IDevice): IPlatformSpecificUsbLiveSyncService {
Expand Down
57 changes: 46 additions & 11 deletions test/platform-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ describe('Platform Service Tests', () => {
testInjector.register("fs", fsLib.FileSystem);
fs = testInjector.resolve("fs");
});
it("should process only files in app folder when preparing for iOS platform", () => {

function prepareDirStructure() {
let tempFolder = temp.mkdirSync("prepare platform");

let appFolderPath = path.join(tempFolder, "app");
Expand All @@ -193,6 +194,12 @@ describe('Platform Service Tests', () => {
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"
Expand Down Expand Up @@ -242,16 +249,7 @@ describe('Platform Service Tests', () => {
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");
let { tempFolder, appFolderPath, app1FolderPath, appDestFolderPath, appResourcesFolderPath } = prepareDirStructure();

// Add platform specific files to app and app1 folders
let platformSpecificFiles = [
Expand Down Expand Up @@ -301,5 +299,42 @@ describe('Platform Service Tests', () => {
// 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 result = platformService.preparePlatform("android").wait();

// Asserts that prepare has caught invalid xml
assert.isFalse(result);
});
});
});