OptionsNormalizer.spec.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. import 'reflect-metadata';
  2. import { ServiceIdentifiers } from '../../../src/container/ServiceIdentifiers';
  3. import { assert } from 'chai';
  4. import { TInputOptions } from '../../../src/types/options/TInputOptions';
  5. import { IInversifyContainerFacade } from '../../../src/interfaces/container/IInversifyContainerFacade';
  6. import { IOptions } from '../../../src/interfaces/options/IOptions';
  7. import { IOptionsNormalizer } from '../../../src/interfaces/options/IOptionsNormalizer';
  8. import { StringArrayEncoding } from '../../../src/enums/StringArrayEncoding';
  9. import { DEFAULT_PRESET } from '../../../src/options/presets/Default';
  10. import { InversifyContainerFacade } from '../../../src/container/InversifyContainerFacade';
  11. /**
  12. * @param optionsPreset
  13. * @returns {IOptions}
  14. */
  15. function getNormalizedOptions (optionsPreset: TInputOptions): TInputOptions {
  16. const inversifyContainerFacade: IInversifyContainerFacade = new InversifyContainerFacade();
  17. inversifyContainerFacade.load('', '', optionsPreset);
  18. const options: IOptions = inversifyContainerFacade
  19. .get<IOptions>(ServiceIdentifiers.IOptions);
  20. const optionsNormalizer: IOptionsNormalizer = inversifyContainerFacade
  21. .get<IOptionsNormalizer>(ServiceIdentifiers.IOptionsNormalizer);
  22. return optionsNormalizer.normalize(options);
  23. }
  24. describe('OptionsNormalizer', () => {
  25. describe('normalize', () => {
  26. let optionsPreset: TInputOptions,
  27. expectedOptionsPreset: TInputOptions;
  28. describe('controlFlowFlatteningThresholdRule', () => {
  29. before(() => {
  30. optionsPreset = getNormalizedOptions({
  31. ...DEFAULT_PRESET,
  32. controlFlowFlattening: true,
  33. controlFlowFlatteningThreshold: 0
  34. });
  35. expectedOptionsPreset = {
  36. ...DEFAULT_PRESET,
  37. controlFlowFlattening: false,
  38. controlFlowFlatteningThreshold: 0
  39. };
  40. });
  41. it('should normalize options preset', () => {
  42. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  43. });
  44. });
  45. describe('deadCodeInjectionRule', () => {
  46. before(() => {
  47. optionsPreset = getNormalizedOptions({
  48. ...DEFAULT_PRESET,
  49. deadCodeInjection: true,
  50. deadCodeInjectionThreshold: 0.4,
  51. stringArray: false,
  52. stringArrayThreshold: 0
  53. });
  54. expectedOptionsPreset = {
  55. ...DEFAULT_PRESET,
  56. deadCodeInjection: true,
  57. deadCodeInjectionThreshold: 0.4,
  58. stringArray: true,
  59. stringArrayThreshold: 0.75
  60. };
  61. });
  62. it('should normalize options preset', () => {
  63. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  64. });
  65. });
  66. describe('deadCodeInjectionRule', () => {
  67. describe('`stringArrayThreshold` option is empty', () => {
  68. before(() => {
  69. optionsPreset = getNormalizedOptions({
  70. ...DEFAULT_PRESET,
  71. deadCodeInjection: true,
  72. deadCodeInjectionThreshold: 0.4,
  73. stringArray: false,
  74. stringArrayThreshold: 0
  75. });
  76. expectedOptionsPreset = {
  77. ...DEFAULT_PRESET,
  78. deadCodeInjection: true,
  79. deadCodeInjectionThreshold: 0.4,
  80. stringArray: true,
  81. stringArrayThreshold: 0.75
  82. };
  83. });
  84. it('should normalize options preset', () => {
  85. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  86. });
  87. });
  88. describe('`stringArrayThreshold` option is not empty', () => {
  89. before(() => {
  90. optionsPreset = getNormalizedOptions({
  91. ...DEFAULT_PRESET,
  92. deadCodeInjection: true,
  93. deadCodeInjectionThreshold: 0.4,
  94. stringArray: false,
  95. stringArrayThreshold: 0.5
  96. });
  97. expectedOptionsPreset = {
  98. ...DEFAULT_PRESET,
  99. deadCodeInjection: true,
  100. deadCodeInjectionThreshold: 0.4,
  101. stringArray: true,
  102. stringArrayThreshold: 0.5
  103. };
  104. });
  105. it('should normalize options preset', () => {
  106. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  107. });
  108. });
  109. });
  110. describe('deadCodeInjectionThresholdRule', () => {
  111. before(() => {
  112. optionsPreset = getNormalizedOptions({
  113. ...DEFAULT_PRESET,
  114. deadCodeInjection: true,
  115. deadCodeInjectionThreshold: 0
  116. });
  117. expectedOptionsPreset = {
  118. ...DEFAULT_PRESET,
  119. deadCodeInjection: false,
  120. deadCodeInjectionThreshold: 0
  121. };
  122. });
  123. it('should normalize options preset', () => {
  124. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  125. });
  126. });
  127. describe('domainLockRule', () => {
  128. before(() => {
  129. optionsPreset = getNormalizedOptions({
  130. ...DEFAULT_PRESET,
  131. domainLock: [
  132. '//localhost:9000',
  133. 'https://google.ru/abc?cde=fgh'
  134. ]
  135. });
  136. expectedOptionsPreset = {
  137. ...DEFAULT_PRESET,
  138. domainLock: [
  139. 'localhost',
  140. 'google.ru'
  141. ]
  142. };
  143. });
  144. it('should normalize options preset', () => {
  145. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  146. });
  147. });
  148. describe('inputFileNameRule', () => {
  149. describe('Variant #1: extension isn\'t set', () => {
  150. before(() => {
  151. optionsPreset = getNormalizedOptions({
  152. ...DEFAULT_PRESET,
  153. inputFileName: 'foo'
  154. });
  155. expectedOptionsPreset = {
  156. ...DEFAULT_PRESET,
  157. inputFileName: 'foo.js'
  158. };
  159. });
  160. it('should normalize options preset', () => {
  161. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  162. });
  163. });
  164. describe('Variant #2: extension is set', () => {
  165. before(() => {
  166. optionsPreset = getNormalizedOptions({
  167. ...DEFAULT_PRESET,
  168. inputFileName: 'foo.js'
  169. });
  170. expectedOptionsPreset = {
  171. ...DEFAULT_PRESET,
  172. inputFileName: 'foo.js'
  173. };
  174. });
  175. it('should normalize options preset', () => {
  176. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  177. });
  178. });
  179. describe('Variant #3: extension in set with `.map` postfix', () => {
  180. before(() => {
  181. optionsPreset = getNormalizedOptions({
  182. ...DEFAULT_PRESET,
  183. inputFileName: 'foo.map.js'
  184. });
  185. expectedOptionsPreset = {
  186. ...DEFAULT_PRESET,
  187. inputFileName: 'foo.map.js'
  188. };
  189. });
  190. it('should normalize options preset', () => {
  191. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  192. });
  193. });
  194. describe('Variant #4: no file name', () => {
  195. before(() => {
  196. optionsPreset = getNormalizedOptions({
  197. ...DEFAULT_PRESET,
  198. inputFileName: ''
  199. });
  200. expectedOptionsPreset = {
  201. ...DEFAULT_PRESET,
  202. inputFileName: ''
  203. };
  204. });
  205. it('should normalize options preset', () => {
  206. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  207. });
  208. });
  209. });
  210. describe('selfDefendingRule', () => {
  211. before(() => {
  212. optionsPreset = getNormalizedOptions({
  213. ...DEFAULT_PRESET,
  214. selfDefending: true,
  215. compact: false
  216. });
  217. expectedOptionsPreset = {
  218. ...DEFAULT_PRESET,
  219. selfDefending: true,
  220. compact: true
  221. };
  222. });
  223. it('should normalize options preset', () => {
  224. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  225. });
  226. });
  227. describe('sourceMapBaseUrlRule', () => {
  228. describe('Variant #1: only source map base url', () => {
  229. before(() => {
  230. optionsPreset = getNormalizedOptions({
  231. ...DEFAULT_PRESET,
  232. sourceMapBaseUrl: 'http://localhost:9000',
  233. });
  234. expectedOptionsPreset = {
  235. ...DEFAULT_PRESET,
  236. sourceMapBaseUrl: ''
  237. };
  238. });
  239. it('should normalize options preset', () => {
  240. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  241. });
  242. });
  243. describe('Variant #2: source map base url with source map file name', () => {
  244. before(() => {
  245. optionsPreset = getNormalizedOptions({
  246. ...DEFAULT_PRESET,
  247. sourceMapBaseUrl: 'http://localhost:9000',
  248. sourceMapFileName: '/outputSourceMapName.map'
  249. });
  250. expectedOptionsPreset = {
  251. ...DEFAULT_PRESET,
  252. sourceMapBaseUrl: 'http://localhost:9000/',
  253. sourceMapFileName: 'outputSourceMapName.js.map'
  254. };
  255. });
  256. it('should normalize options preset', () => {
  257. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  258. });
  259. });
  260. });
  261. describe('sourceMapFileNameRule', () => {
  262. before(() => {
  263. optionsPreset = getNormalizedOptions({
  264. ...DEFAULT_PRESET,
  265. sourceMapBaseUrl: 'http://localhost:9000',
  266. sourceMapFileName: '//outputSourceMapName'
  267. });
  268. expectedOptionsPreset = {
  269. ...DEFAULT_PRESET,
  270. sourceMapBaseUrl: 'http://localhost:9000/',
  271. sourceMapFileName: 'outputSourceMapName.js.map'
  272. };
  273. });
  274. it('should normalize options preset', () => {
  275. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  276. });
  277. });
  278. describe('stringArrayRule', () => {
  279. before(() => {
  280. optionsPreset = getNormalizedOptions({
  281. ...DEFAULT_PRESET,
  282. stringArray: false,
  283. stringArrayEncoding: StringArrayEncoding.Rc4,
  284. stringArrayThreshold: 0.5,
  285. rotateStringArray: true
  286. });
  287. expectedOptionsPreset = {
  288. ...DEFAULT_PRESET,
  289. stringArray: false,
  290. stringArrayEncoding: false,
  291. stringArrayThreshold: 0,
  292. rotateStringArray: false
  293. };
  294. });
  295. it('should normalize options preset', () => {
  296. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  297. });
  298. });
  299. describe('stringArrayEncodingRule', () => {
  300. before(() => {
  301. optionsPreset = getNormalizedOptions({
  302. ...DEFAULT_PRESET,
  303. stringArrayEncoding: true
  304. });
  305. expectedOptionsPreset = {
  306. ...DEFAULT_PRESET,
  307. stringArrayEncoding: StringArrayEncoding.Base64
  308. };
  309. });
  310. it('should normalize options preset', () => {
  311. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  312. });
  313. });
  314. describe('stringArrayThresholdRule', () => {
  315. before(() => {
  316. optionsPreset = getNormalizedOptions({
  317. ...DEFAULT_PRESET,
  318. rotateStringArray: true,
  319. stringArray: true,
  320. stringArrayThreshold: 0
  321. });
  322. expectedOptionsPreset = {
  323. ...DEFAULT_PRESET,
  324. rotateStringArray: false,
  325. stringArray: false,
  326. stringArrayThreshold: 0
  327. };
  328. });
  329. it('should normalize options preset', () => {
  330. assert.deepEqual(optionsPreset, expectedOptionsPreset);
  331. });
  332. });
  333. });
  334. });