-
-
Notifications
You must be signed in to change notification settings - Fork 197
Polish xml merge #553
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
Polish xml merge #553
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ import ErrorsLib = require("../lib/common/errors"); | |
import ProjectHelperLib = require("../lib/common/project-helper"); | ||
import PlatformsDataLib = require("../lib/platforms-data"); | ||
import ProjectDataServiceLib = require("../lib/services/project-data-service"); | ||
import helpers = require("../lib/common/helpers"); | ||
import os = require("os"); | ||
|
||
import PluginsServiceLib = require("../lib/services/plugins-service"); | ||
import AddPluginCommandLib = require("../lib/commands/plugin/add-plugin"); | ||
|
@@ -124,6 +126,30 @@ function addPluginWhenExpectingToFail(testInjector: IInjector, plugin: string, e | |
assert.isTrue(isErrorThrown); | ||
} | ||
|
||
function createAndroidManifestFile(projectFolder: string, fs:IFileSystem): void { | ||
let manifest = '<?xml version="1.0" encoding="UTF-8"?>' + | ||
'<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.basiccontactables" android:versionCode="1" android:versionName="1.0" >' + | ||
'<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>' + | ||
'<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>' + | ||
'<uses-permission android:name="android.permission.INTERNET"/>' + | ||
'<application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/Theme.Sample" >' + | ||
'<activity android:name="com.example.android.basiccontactables.MainActivity" android:label="@string/app_name" android:launchMode="singleTop">' + | ||
'<meta-data android:name="android.app.searchable" android:resource="@xml/searchable" />' + | ||
'<intent-filter>' + | ||
'<action android:name="android.intent.action.SEARCH" />' + | ||
'</intent-filter>' + | ||
'<intent-filter>' + | ||
'<action android:name="android.intent.action.MAIN" />' + | ||
'</intent-filter>' + | ||
'</activity>' + | ||
'</application>' + | ||
'</manifest>'; | ||
|
||
fs.createDirectory(path.join(projectFolder, "platforms")).wait(); | ||
fs.createDirectory(path.join(projectFolder, "platforms", "android")).wait(); | ||
fs.writeFile(path.join(projectFolder, "platforms", "android", "AndroidManifest.xml"), manifest).wait(); | ||
} | ||
|
||
describe("Plugins service", () => { | ||
let testInjector: IInjector; | ||
beforeEach(() => { | ||
|
@@ -256,7 +282,7 @@ describe("Plugins service", () => { | |
let packageJsonContent = fs.readJson(path.join(projectFolder, "package.json")).wait(); | ||
let actualDependencies = packageJsonContent.dependencies; | ||
let expectedDependencies = { | ||
"plugin1": "^1.0.0" | ||
"plugin1": "^1.0.3" | ||
}; | ||
assert.deepEqual(actualDependencies, expectedDependencies); | ||
}); | ||
|
@@ -419,4 +445,141 @@ describe("Plugins service", () => { | |
commandsService.tryExecuteCommand("plugin|add", [pluginFolderPath]).wait(); | ||
}); | ||
}); | ||
|
||
describe("merge xmls tests", () => { | ||
let testInjector: IInjector; | ||
beforeEach(() => { | ||
testInjector = createTestInjector(); | ||
testInjector.registerCommand("plugin|add", AddPluginCommandLib.AddPluginCommand); | ||
}); | ||
it("fails if the plugin contains incorrect xml", () => { | ||
let pluginName = "mySamplePlugin"; | ||
let projectFolder = createProjectFile(testInjector); | ||
let pluginFolderPath = path.join(projectFolder, pluginName); | ||
let pluginJsonData = { | ||
"name": pluginName, | ||
"version": "0.0.1", | ||
"nativescript": { | ||
"platforms": { | ||
"android": "0.10.0" | ||
} | ||
} | ||
}; | ||
let fs = testInjector.resolve("fs"); | ||
fs.writeJson(path.join(pluginFolderPath, "package.json"), pluginJsonData).wait(); | ||
|
||
// Adds AndroidManifest.xml file in platforms/android folder | ||
createAndroidManifestFile(projectFolder, fs); | ||
|
||
// Mock plugins service | ||
let pluginsService = testInjector.resolve("pluginsService"); | ||
pluginsService.getAllInstalledPlugins = () => { | ||
return (() => { | ||
return [{ | ||
name: "" | ||
}]; | ||
}).future<IPluginData[]>()(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a merge stopper, but you may use future.fromResult to cut some code |
||
} | ||
|
||
let appDestinationDirectoryPath = path.join(projectFolder, "platforms", "android"); | ||
|
||
// Mock platformsData | ||
let platformsData = testInjector.resolve("platformsData"); | ||
platformsData.getPlatformData = (platform: string) => { | ||
return { | ||
appDestinationDirectoryPath: appDestinationDirectoryPath, | ||
frameworkPackageName: "tns-android", | ||
configurationFileName: "AndroidManifest.xml" | ||
} | ||
} | ||
|
||
// Ensure the pluginDestinationPath folder exists | ||
let pluginPlatformsDirPath = path.join(appDestinationDirectoryPath, "app", "tns_modules", pluginName, "platforms", "android"); | ||
fs.ensureDirectoryExists(pluginPlatformsDirPath).wait(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there a need to use the actual FS? Can we mock the FS instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to use the actual, real |
||
|
||
// Creates invalid plugin's AndroidManifest.xml file | ||
let xml = '<?xml version="1.0" encoding="UTF-8"?>' + | ||
'<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.basiccontactables" android:versionCode="1" android:versionName="1.0" >' + | ||
'<uses-permission android:name="android.permission.READ_CONTACTS"/>'; | ||
let pluginConfigurationFilePath = path.join(pluginPlatformsDirPath, "AndroidManifest.xml"); | ||
fs.writeFile(pluginConfigurationFilePath, xml).wait(); | ||
|
||
// Expected error message. The assertion happens in mockBeginCommand | ||
let expectedErrorMessage = `Exception: Invalid xml file ${pluginConfigurationFilePath}. Additional technical information: element parse error: Exception: Invalid xml file ` + | ||
`${pluginConfigurationFilePath}. Additional technical information: unclosed xml attribute` + | ||
`\n@#[line:1,col:39].` + | ||
`\n@#[line:1,col:39].`; | ||
mockBeginCommand(testInjector, expectedErrorMessage); | ||
|
||
let commandsService = testInjector.resolve(CommandsServiceLib.CommandsService); | ||
commandsService.tryExecuteCommand("plugin|add", [pluginFolderPath]).wait(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we executing the command itself? Can't we test a service instead? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Actually because I have already created mocks and want to reuse them 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please reconsider leaving the test as-is. Currently you are testing the entire command infrastructure and the plugins service at the same time, which is not the correct way of writing unit tests. |
||
}); | ||
it("merges AndroidManifest.xml and produces correct xml", () => { | ||
let pluginName = "mySamplePlugin"; | ||
let projectFolder = createProjectFile(testInjector); | ||
let pluginFolderPath = path.join(projectFolder, pluginName); | ||
let pluginJsonData = { | ||
"name": pluginName, | ||
"version": "0.0.1", | ||
"nativescript": { | ||
"platforms": { | ||
"android": "0.10.0" | ||
} | ||
} | ||
}; | ||
let fs = testInjector.resolve("fs"); | ||
fs.writeJson(path.join(pluginFolderPath, "package.json"), pluginJsonData).wait(); | ||
|
||
// Adds AndroidManifest.xml file in platforms/android folder | ||
createAndroidManifestFile(projectFolder, fs); | ||
|
||
// Mock plugins service | ||
let pluginsService = testInjector.resolve("pluginsService"); | ||
pluginsService.getAllInstalledPlugins = () => { | ||
return (() => { | ||
return [{ | ||
name: "" | ||
}]; | ||
}).future<IPluginData[]>()(); | ||
} | ||
|
||
let appDestinationDirectoryPath = path.join(projectFolder, "platforms", "android"); | ||
|
||
// Mock platformsData | ||
let platformsData = testInjector.resolve("platformsData"); | ||
platformsData.getPlatformData = (platform: string) => { | ||
return { | ||
appDestinationDirectoryPath: appDestinationDirectoryPath, | ||
frameworkPackageName: "tns-android", | ||
configurationFileName: "AndroidManifest.xml", | ||
configurationFilePath: path.join(appDestinationDirectoryPath, "AndroidManifest.xml"), | ||
mergeXmlConfig: [{ "nodename": "manifest", "attrname": "*" }] | ||
} | ||
} | ||
|
||
// Ensure the pluginDestinationPath folder exists | ||
let pluginPlatformsDirPath = path.join(appDestinationDirectoryPath, "app", "tns_modules", pluginName, "platforms", "android"); | ||
fs.ensureDirectoryExists(pluginPlatformsDirPath).wait(); | ||
|
||
// Creates valid plugin's AndroidManifest.xml file | ||
let xml = '<?xml version="1.0" encoding="UTF-8"?>' + | ||
'<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.android.basiccontactables" android:versionCode="1" android:versionName="1.0" >' + | ||
'<uses-permission android:name="android.permission.READ_CONTACTS"/>' + | ||
'</manifest>'; | ||
let pluginConfigurationFilePath = path.join(pluginPlatformsDirPath, "AndroidManifest.xml"); | ||
fs.writeFile(pluginConfigurationFilePath, xml).wait(); | ||
|
||
pluginsService.add(pluginFolderPath).wait(); | ||
|
||
let expectedXml = '<?xmlversion="1.0"encoding="UTF-8"?><manifestxmlns:android="http://schemas.android.com/apk/res/android"package="com.example.android.basiccontactables"android:versionCode="1"android:versionName="1.0"><uses-permissionandroid:name="android.permission.READ_EXTERNAL_STORAGE"/><uses-permissionandroid:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permissionandroid:name="android.permission.INTERNET"/><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/Theme.Sample"><activityandroid:name="com.example.android.basiccontactables.MainActivity"android:label="@string/app_name"android:launchMode="singleTop"><meta-dataandroid:name="android.app.searchable"android:resource="@xml/searchable"/><intent-filter><actionandroid:name="android.intent.action.SEARCH"/></intent-filter><intent-filter><actionandroid:name="android.intent.action.MAIN"/></intent-filter></activity></application><uses-permissionandroid:name="android.permission.READ_CONTACTS"/></manifest>'; | ||
expectedXml = helpers.stringReplaceAll(expectedXml, os.EOL, ""); | ||
expectedXml = helpers.stringReplaceAll(expectedXml, " ", ""); | ||
|
||
let actualXml = fs.readText(path.join(appDestinationDirectoryPath, "AndroidManifest.xml")).wait(); | ||
actualXml = helpers.stringReplaceAll(actualXml, "\n", ""); | ||
actualXml = helpers.stringReplaceAll(actualXml, " ", ""); | ||
|
||
assert.equal(expectedXml, actualXml); | ||
}); | ||
}); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't you verify the resultXml as well? This way we will not save incorrect xml, which the user will have to modify manually after that.