|
| 1 | +import { MetadataFilteringService } from "../../lib/services/metadata-filtering-service"; |
| 2 | +import { Yok } from "../../lib/common/yok"; |
| 3 | +import { LoggerStub, FileSystemStub } from "../stubs"; |
| 4 | +import { assert } from "chai"; |
| 5 | +import * as path from "path"; |
| 6 | +import { MetadataFilteringConstants } from "../../lib/constants"; |
| 7 | +import { EOL } from "os"; |
| 8 | + |
| 9 | +describe("metadataFilteringService", () => { |
| 10 | + const platform = "platform"; |
| 11 | + const projectDir = "projectDir"; |
| 12 | + const projectRoot = path.join(projectDir, "platforms", platform); |
| 13 | + const projectData: any = { |
| 14 | + appResourcesDirectoryPath: path.join(projectDir, "App_Resources") |
| 15 | + }; |
| 16 | + const blacklistArray: string[] = ["blacklisted1", "blacklisted2"]; |
| 17 | + const whitelistArray: string[] = ["whitelisted1", "whitelisted2"]; |
| 18 | + const appResourcesNativeApiUsageFilePath = path.join(projectData.appResourcesDirectoryPath, platform, MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME); |
| 19 | + const pluginPlatformsDir = path.join("pluginDir", platform); |
| 20 | + const pluginNativeApiUsageFilePath = path.join(pluginPlatformsDir, MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME); |
| 21 | + const pluginsUses: string[] = ["whitelisted1", "pluginUses1", "pluginUses2"]; |
| 22 | + |
| 23 | + const createTestInjector = (input?: { hasPlugins: boolean }): IInjector => { |
| 24 | + const testInjector = new Yok(); |
| 25 | + testInjector.register("logger", LoggerStub); |
| 26 | + testInjector.register("fs", FileSystemStub); |
| 27 | + testInjector.register("pluginsService", { |
| 28 | + getAllProductionPlugins: (prjData: IProjectData, dependencies?: IDependencyData[]): IPluginData[] => { |
| 29 | + const plugins = !!(input && input.hasPlugins) ? [ |
| 30 | + <any>{ |
| 31 | + pluginPlatformsFolderPath: (pl: string) => pluginPlatformsDir |
| 32 | + } |
| 33 | + ] : []; |
| 34 | + |
| 35 | + return plugins; |
| 36 | + } |
| 37 | + }); |
| 38 | + testInjector.register("mobileHelper", { |
| 39 | + normalizePlatformName: (pl: string) => pl |
| 40 | + }); |
| 41 | + testInjector.register("platformsDataService", { |
| 42 | + getPlatformData: (pl: string, prjData: IProjectData): IPlatformData => (<any>{ projectRoot }) |
| 43 | + }); |
| 44 | + return testInjector; |
| 45 | + }; |
| 46 | + |
| 47 | + describe("generateMetadataFilters", () => { |
| 48 | + const mockFs = (input: { testInjector: IInjector, readJsonData?: any, writeFileAction?: (filePath: string, data: string) => void, existingFiles?: any[] }): { fs: FileSystemStub, dataWritten: IDictionary<any> } => { |
| 49 | + const fs = input.testInjector.resolve<FileSystemStub>("fs"); |
| 50 | + const dataWritten: IDictionary<any> = {}; |
| 51 | + |
| 52 | + if (input.writeFileAction) { |
| 53 | + fs.writeFile = (filePath: string, data: string) => input.writeFileAction(filePath, data); |
| 54 | + } else { |
| 55 | + fs.writeFile = (filePath: string, data: string) => dataWritten[filePath] = data; |
| 56 | + } |
| 57 | + |
| 58 | + if (input.readJsonData) { |
| 59 | + fs.readJson = (filePath: string) => input.readJsonData[filePath]; |
| 60 | + } |
| 61 | + |
| 62 | + if (input.existingFiles) { |
| 63 | + fs.exists = (filePath: string) => input.existingFiles.indexOf(filePath) !== -1; |
| 64 | + } |
| 65 | + |
| 66 | + return { fs, dataWritten }; |
| 67 | + }; |
| 68 | + |
| 69 | + it("deletes previously generated files for metadata filtering", () => { |
| 70 | + const testInjector = createTestInjector(); |
| 71 | + const metadataFilteringService: IMetadataFilteringService = testInjector.resolve(MetadataFilteringService); |
| 72 | + const { fs } = mockFs({ |
| 73 | + testInjector, writeFileAction: (filePath: string, data: string) => { |
| 74 | + throw new Error(`No data should be written when the ${MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME} does not exist in App_Resource/<platform>`); |
| 75 | + } |
| 76 | + }); |
| 77 | + |
| 78 | + metadataFilteringService.generateMetadataFilters(projectData, platform); |
| 79 | + |
| 80 | + const expectedDeletedFiles = [ |
| 81 | + path.join(projectRoot, MetadataFilteringConstants.WHITELIST_FILE_NAME), |
| 82 | + path.join(projectRoot, MetadataFilteringConstants.BLACKLIST_FILE_NAME) |
| 83 | + ]; |
| 84 | + assert.deepEqual(fs.deletedFiles, expectedDeletedFiles); |
| 85 | + }); |
| 86 | + |
| 87 | + it(`generates ${MetadataFilteringConstants.BLACKLIST_FILE_NAME} when the file ${MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME} exists in App_Resources/<platform>`, () => { |
| 88 | + const testInjector = createTestInjector(); |
| 89 | + const metadataFilteringService: IMetadataFilteringService = testInjector.resolve(MetadataFilteringService); |
| 90 | + const { dataWritten } = mockFs({ |
| 91 | + testInjector, |
| 92 | + existingFiles: [appResourcesNativeApiUsageFilePath], |
| 93 | + readJsonData: { [`${appResourcesNativeApiUsageFilePath}`]: { blacklist: blacklistArray } } |
| 94 | + }); |
| 95 | + |
| 96 | + metadataFilteringService.generateMetadataFilters(projectData, platform); |
| 97 | + |
| 98 | + assert.deepEqual(dataWritten, { [path.join(projectRoot, MetadataFilteringConstants.BLACKLIST_FILE_NAME)]: blacklistArray.join(EOL) }); |
| 99 | + }); |
| 100 | + |
| 101 | + it(`generates ${MetadataFilteringConstants.WHITELIST_FILE_NAME} when the file ${MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME} exists in App_Resources/<platform>`, () => { |
| 102 | + const testInjector = createTestInjector(); |
| 103 | + const metadataFilteringService: IMetadataFilteringService = testInjector.resolve(MetadataFilteringService); |
| 104 | + const { dataWritten } = mockFs({ |
| 105 | + testInjector, |
| 106 | + existingFiles: [appResourcesNativeApiUsageFilePath], |
| 107 | + readJsonData: { [`${appResourcesNativeApiUsageFilePath}`]: { whitelist: whitelistArray } }, |
| 108 | + }); |
| 109 | + |
| 110 | + metadataFilteringService.generateMetadataFilters(projectData, platform); |
| 111 | + assert.deepEqual(dataWritten, { [path.join(projectRoot, MetadataFilteringConstants.WHITELIST_FILE_NAME)]: whitelistArray.join(EOL) }); |
| 112 | + }); |
| 113 | + |
| 114 | + it(`generates ${MetadataFilteringConstants.WHITELIST_FILE_NAME} with content from plugins when the file ${MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME} exists in App_Resources/<platform> and whitelist-plugins-usages is true`, () => { |
| 115 | + const testInjector = createTestInjector({ hasPlugins: true }); |
| 116 | + const metadataFilteringService: IMetadataFilteringService = testInjector.resolve(MetadataFilteringService); |
| 117 | + const { dataWritten } = mockFs({ |
| 118 | + testInjector, |
| 119 | + existingFiles: [appResourcesNativeApiUsageFilePath, pluginNativeApiUsageFilePath], |
| 120 | + readJsonData: { |
| 121 | + [`${appResourcesNativeApiUsageFilePath}`]: { ["whitelist-plugins-usages"]: true }, |
| 122 | + [`${pluginNativeApiUsageFilePath}`]: { uses: whitelistArray } |
| 123 | + }, |
| 124 | + }); |
| 125 | + |
| 126 | + metadataFilteringService.generateMetadataFilters(projectData, platform); |
| 127 | + assert.deepEqual(dataWritten, { [path.join(projectRoot, MetadataFilteringConstants.WHITELIST_FILE_NAME)]: whitelistArray.join(EOL) }); |
| 128 | + }); |
| 129 | + |
| 130 | + it(`generates all files when both plugins and applications filters are included`, () => { |
| 131 | + const testInjector = createTestInjector({ hasPlugins: true }); |
| 132 | + const metadataFilteringService: IMetadataFilteringService = testInjector.resolve(MetadataFilteringService); |
| 133 | + const { dataWritten } = mockFs({ |
| 134 | + testInjector, |
| 135 | + existingFiles: [appResourcesNativeApiUsageFilePath, pluginNativeApiUsageFilePath], |
| 136 | + readJsonData: { |
| 137 | + [`${appResourcesNativeApiUsageFilePath}`]: { |
| 138 | + whitelist: whitelistArray, |
| 139 | + blacklist: blacklistArray, |
| 140 | + ["whitelist-plugins-usages"]: true |
| 141 | + }, |
| 142 | + [`${pluginNativeApiUsageFilePath}`]: { uses: pluginsUses } |
| 143 | + }, |
| 144 | + }); |
| 145 | + |
| 146 | + metadataFilteringService.generateMetadataFilters(projectData, platform); |
| 147 | + const expectedWhitelist = [ |
| 148 | + "pluginUses1", |
| 149 | + "pluginUses2", |
| 150 | + ...whitelistArray |
| 151 | + ].join(EOL); |
| 152 | + |
| 153 | + assert.deepEqual(dataWritten, { |
| 154 | + [path.join(projectRoot, MetadataFilteringConstants.WHITELIST_FILE_NAME)]: expectedWhitelist, |
| 155 | + [path.join(projectRoot, MetadataFilteringConstants.BLACKLIST_FILE_NAME)]: blacklistArray.join(EOL) |
| 156 | + }); |
| 157 | + }); |
| 158 | + |
| 159 | + it(`skips plugins ${MetadataFilteringConstants.NATIVE_API_USAGE_FILE_NAME} files when whitelist-plugins-usages in App_Resources is false`, () => { |
| 160 | + const testInjector = createTestInjector({ hasPlugins: true }); |
| 161 | + const metadataFilteringService: IMetadataFilteringService = testInjector.resolve(MetadataFilteringService); |
| 162 | + const { dataWritten } = mockFs({ |
| 163 | + testInjector, |
| 164 | + existingFiles: [appResourcesNativeApiUsageFilePath, pluginNativeApiUsageFilePath], |
| 165 | + readJsonData: { |
| 166 | + [`${appResourcesNativeApiUsageFilePath}`]: { |
| 167 | + whitelist: whitelistArray, |
| 168 | + blacklist: blacklistArray, |
| 169 | + ["whitelist-plugins-usages"]: false |
| 170 | + }, |
| 171 | + [`${pluginNativeApiUsageFilePath}`]: { uses: pluginsUses } |
| 172 | + }, |
| 173 | + }); |
| 174 | + |
| 175 | + metadataFilteringService.generateMetadataFilters(projectData, "platform"); |
| 176 | + const expectedWhitelist = [ |
| 177 | + ...whitelistArray |
| 178 | + ].join(EOL); |
| 179 | + |
| 180 | + assert.deepEqual(dataWritten, { |
| 181 | + [path.join(projectRoot, MetadataFilteringConstants.WHITELIST_FILE_NAME)]: expectedWhitelist, |
| 182 | + [path.join(projectRoot, MetadataFilteringConstants.BLACKLIST_FILE_NAME)]: blacklistArray.join(EOL) |
| 183 | + }); |
| 184 | + }); |
| 185 | + }); |
| 186 | +}); |
0 commit comments