ObfuscatedCodeFileUtils.spec.ts 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. import { assert } from 'chai';
  2. import * as fs from 'fs';
  3. import * as mkdirp from 'mkdirp';
  4. import * as path from 'path';
  5. import * as rimraf from 'rimraf';
  6. import { ObfuscatedCodeFileUtils } from '../../../../src/cli/utils/ObfuscatedCodeFileUtils';
  7. describe('obfuscatedCodeFileUtils', () => {
  8. const tmpDirectoryPath: string = 'test/tmp';
  9. describe('getOutputCodePath', () => {
  10. before(() => {
  11. mkdirp.sync(path.join(tmpDirectoryPath, 'input', 'nested',));
  12. fs.writeFileSync(
  13. path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js'),
  14. 'var foo = 1;'
  15. );
  16. });
  17. describe('Variant #1: raw input path is a file path, raw output path is a file path', () => {
  18. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  19. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  20. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  21. const expectedOutputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  22. let outputCodePath: string;
  23. before(() => {
  24. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  25. rawInputPath,
  26. {
  27. output: rawOutputPath
  28. }
  29. );
  30. outputCodePath = obfuscatedCodeFileUtils.getOutputCodePath(inputPath);
  31. });
  32. it('should return output path that equals to passed output file path', () => {
  33. assert.equal(outputCodePath, expectedOutputCodePath);
  34. });
  35. });
  36. describe('Variant #2: raw input path is a file path, raw output path is a directory path', () => {
  37. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  38. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  39. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output');
  40. const expectedOutputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-input.js');
  41. let outputCodePath: string;
  42. before(() => {
  43. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  44. rawInputPath,
  45. {
  46. output: rawOutputPath
  47. }
  48. );
  49. outputCodePath = obfuscatedCodeFileUtils.getOutputCodePath(inputPath);
  50. });
  51. it('should return output path that equals to passed output directory with file name from actual file path', () => {
  52. assert.equal(outputCodePath, expectedOutputCodePath);
  53. });
  54. });
  55. describe('Variant #3: raw input path is a directory path, raw output path is a file path', () => {
  56. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  57. const rawInputPath: string = path.join(tmpDirectoryPath, 'input');
  58. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  59. let testFunc: () => string;
  60. before(() => {
  61. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  62. rawInputPath,
  63. {
  64. output: rawOutputPath
  65. }
  66. );
  67. testFunc = () => obfuscatedCodeFileUtils.getOutputCodePath(inputPath);
  68. });
  69. it('should throw an error if output path is a file path', () => {
  70. assert.throws(testFunc, Error);
  71. });
  72. });
  73. describe('Variant #4: raw input path is a directory path, raw output path is a directory path', () => {
  74. describe('Variant #1: base directory name', () => {
  75. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  76. const rawInputPath: string = path.join(tmpDirectoryPath, 'input');
  77. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output');
  78. const expectedOutputCodePath: string = path.join(
  79. tmpDirectoryPath,
  80. 'output',
  81. 'test-input.js'
  82. );
  83. let outputCodePath: string;
  84. before(() => {
  85. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  86. rawInputPath,
  87. {
  88. output: rawOutputPath
  89. }
  90. );
  91. outputCodePath = obfuscatedCodeFileUtils.getOutputCodePath(inputPath);
  92. });
  93. it('should return output path that contains raw output path and actual file input path', () => {
  94. assert.equal(outputCodePath, expectedOutputCodePath);
  95. });
  96. });
  97. describe('Variant #2: base directory name with leading dot in output path', () => {
  98. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  99. const rawInputPath: string = path.join(tmpDirectoryPath, 'input');
  100. const rawOutputPath: string = path.join('.', tmpDirectoryPath, 'output');
  101. const expectedOutputCodePath: string = path.join(
  102. tmpDirectoryPath,
  103. 'output',
  104. 'test-input.js'
  105. );
  106. let outputCodePath: string;
  107. before(() => {
  108. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  109. rawInputPath,
  110. {
  111. output: rawOutputPath
  112. }
  113. );
  114. outputCodePath = obfuscatedCodeFileUtils.getOutputCodePath(inputPath);
  115. });
  116. it('should return output path that contains raw output path and actual file input path', () => {
  117. assert.equal(outputCodePath, expectedOutputCodePath);
  118. });
  119. });
  120. describe('Variant #3: base nested directory name', () => {
  121. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  122. const rawInputPath: string = path.join(tmpDirectoryPath, 'input');
  123. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output');
  124. const expectedOutputCodePath: string = path.join(
  125. tmpDirectoryPath,
  126. 'output',
  127. 'nested',
  128. 'test-input.js'
  129. );
  130. let outputCodePath: string;
  131. before(() => {
  132. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  133. rawInputPath,
  134. {
  135. output: rawOutputPath
  136. }
  137. );
  138. outputCodePath = obfuscatedCodeFileUtils.getOutputCodePath(inputPath);
  139. });
  140. it('should return output path that contains raw output path and actual file input path', () => {
  141. assert.equal(outputCodePath, expectedOutputCodePath);
  142. });
  143. });
  144. describe('Variant #4: directory name with dot', () => {
  145. const inputPath: string = path.join(tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  146. const rawInputPath: string = path.join(tmpDirectoryPath, 'input');
  147. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'foo.bar');
  148. const expectedOutputCodePath: string = path.join(
  149. tmpDirectoryPath,
  150. 'output',
  151. 'foo.bar',
  152. 'nested',
  153. 'test-input.js'
  154. );
  155. let outputCodePath: string;
  156. before(() => {
  157. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  158. rawInputPath,
  159. {
  160. output: rawOutputPath
  161. }
  162. );
  163. outputCodePath = obfuscatedCodeFileUtils.getOutputCodePath(inputPath);
  164. });
  165. it('should return output path that contains raw output path and actual file input path', () => {
  166. assert.equal(outputCodePath, expectedOutputCodePath);
  167. });
  168. });
  169. describe('Variant #5: input directory name with dot only', () => {
  170. const inputPath: string = path.join('test-input.js');
  171. const rawInputPath: string = path.join('.');
  172. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output');
  173. const expectedOutputCodePath: string = path.join(
  174. tmpDirectoryPath,
  175. 'output',
  176. 'test-input.js'
  177. );
  178. let outputCodePath: string;
  179. before(() => {
  180. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  181. rawInputPath,
  182. {
  183. output: rawOutputPath
  184. }
  185. );
  186. outputCodePath = obfuscatedCodeFileUtils.getOutputCodePath(inputPath);
  187. });
  188. it('should return output path that contains raw output path and actual file input path', () => {
  189. assert.equal(outputCodePath, expectedOutputCodePath);
  190. });
  191. });
  192. describe('Variant #6: input directory name with dot and slash only', () => {
  193. const inputPath: string = path.join('test-input.js');
  194. const rawInputPath: string = path.join('./');
  195. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output');
  196. const expectedOutputCodePath: string = path.join(
  197. tmpDirectoryPath,
  198. 'output',
  199. 'test-input.js'
  200. );
  201. let outputCodePath: string;
  202. before(() => {
  203. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  204. rawInputPath,
  205. {
  206. output: rawOutputPath
  207. }
  208. );
  209. outputCodePath = obfuscatedCodeFileUtils.getOutputCodePath(inputPath);
  210. });
  211. it('should return output path that contains raw output path and actual file input path', () => {
  212. assert.equal(outputCodePath, expectedOutputCodePath);
  213. });
  214. });
  215. });
  216. describe('Variant #5: Win32 environment', () => {
  217. const baseDirnamePath: string = __dirname;
  218. before(() => {
  219. mkdirp.sync(path.join(baseDirnamePath, tmpDirectoryPath, 'input', 'nested'));
  220. fs.writeFileSync(
  221. path.join(baseDirnamePath, tmpDirectoryPath, 'input', 'nested', 'test-input.js'),
  222. 'var foo = 1;'
  223. );
  224. });
  225. describe('Variant #1: raw input absolute path is a directory path, raw output absolute path is a directory path', () => {
  226. describe('Variant #1: base directory name', () => {
  227. const inputPath: string = path.join(baseDirnamePath, tmpDirectoryPath, 'input', 'nested', 'test-input.js');
  228. const rawInputPath: string = path.join(baseDirnamePath, tmpDirectoryPath, 'input');
  229. const rawOutputPath: string = path.join(baseDirnamePath, tmpDirectoryPath, 'output');
  230. const expectedOutputCodePath: string = path.join(
  231. baseDirnamePath,
  232. tmpDirectoryPath,
  233. 'output',
  234. 'nested',
  235. 'test-input.js'
  236. );
  237. let outputCodePath: string;
  238. before(() => {
  239. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  240. rawInputPath,
  241. {
  242. output: rawOutputPath
  243. }
  244. );
  245. outputCodePath = obfuscatedCodeFileUtils.getOutputCodePath(inputPath);
  246. });
  247. it('should return output path that contains raw output path and actual file input path', () => {
  248. assert.equal(outputCodePath, expectedOutputCodePath);
  249. });
  250. });
  251. });
  252. after(() => {
  253. rimraf.sync(path.join(baseDirnamePath, tmpDirectoryPath));
  254. });
  255. });
  256. after(() => {
  257. rimraf.sync(tmpDirectoryPath);
  258. });
  259. });
  260. describe('getOutputSourceMapPath', () => {
  261. describe('Variant #1: output code path is a file path', () => {
  262. describe('Variant #1: file path with directory', () => {
  263. describe('Variant #1: source map file name is not set', () => {
  264. describe('Variant #1: base output code path', () => {
  265. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  266. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  267. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  268. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js.map');
  269. let outputSourceMapPath: string;
  270. before(() => {
  271. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  272. rawInputPath,
  273. {
  274. output: rawOutputPath
  275. }
  276. );
  277. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath);
  278. });
  279. it('should return output path for source map', () => {
  280. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  281. });
  282. });
  283. describe('Variant #2: output code path with dot', () => {
  284. const rawInputPath: string = path.join(tmpDirectoryPath, 'input.with.dot', 'test-input.js');
  285. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output.with.dot', 'test-output.js');
  286. const outputCodePath: string = path.join(tmpDirectoryPath, 'output.with.dot', 'test-output.js');
  287. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output.with.dot', 'test-output.js.map');
  288. let outputSourceMapPath: string;
  289. before(() => {
  290. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  291. rawInputPath,
  292. {
  293. output: rawOutputPath
  294. }
  295. );
  296. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath);
  297. });
  298. it('should return output path for source map', () => {
  299. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  300. });
  301. });
  302. });
  303. describe('Variant #2: source map file name is set', () => {
  304. describe('Variant #1: source map file name without extension is set', () => {
  305. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  306. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  307. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  308. const sourceMapFileName: string = 'foo';
  309. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  310. let outputSourceMapPath: string;
  311. before(() => {
  312. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  313. rawInputPath,
  314. {
  315. output: rawOutputPath
  316. }
  317. );
  318. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  319. });
  320. it('should return output path for source map', () => {
  321. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  322. });
  323. });
  324. describe('Variant #2: source map file name with wrong extension is set', () => {
  325. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  326. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  327. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  328. const sourceMapFileName: string = 'foo.js';
  329. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  330. let outputSourceMapPath: string;
  331. before(() => {
  332. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  333. rawInputPath,
  334. {
  335. output: rawOutputPath
  336. }
  337. );
  338. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  339. });
  340. it('should return output path for source map', () => {
  341. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  342. });
  343. });
  344. describe('Variant #3: source map file name with valid extension is set', () => {
  345. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  346. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  347. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  348. const sourceMapFileName: string = 'foo.js.map';
  349. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  350. let outputSourceMapPath: string;
  351. before(() => {
  352. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  353. rawInputPath,
  354. {
  355. output: rawOutputPath
  356. }
  357. );
  358. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  359. });
  360. it('should return output path for source map', () => {
  361. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  362. });
  363. });
  364. describe('Variant #4: source map file name contains directories', () => {
  365. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  366. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  367. const outputCodePath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  368. const sourceMapFileName: string = path.join('parent', 'foo.js.map');
  369. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'parent', 'foo.js.map');
  370. let outputSourceMapPath: string;
  371. before(() => {
  372. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  373. rawInputPath,
  374. {
  375. output: rawOutputPath
  376. }
  377. );
  378. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  379. });
  380. it('should return output path for source map', () => {
  381. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  382. });
  383. });
  384. describe('Variant #5: output code path with dot', () => {
  385. const rawInputPath: string = path.join(tmpDirectoryPath, 'input.with.dot', 'test-input.js');
  386. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output.with.dot', 'test-output.js');
  387. const outputCodePath: string = path.join(tmpDirectoryPath, 'output.with.dot', 'test-output.js');
  388. const sourceMapFileName: string = path.join('parent', 'foo.js.map');
  389. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output.with.dot', 'parent', 'foo.js.map');
  390. let outputSourceMapPath: string;
  391. before(() => {
  392. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  393. rawInputPath,
  394. {
  395. output: rawOutputPath
  396. }
  397. );
  398. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  399. });
  400. it('should return output path for source map', () => {
  401. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  402. });
  403. });
  404. });
  405. });
  406. describe('Variant #2: file path without directory', () => {
  407. describe('Variant #1: source map file name is not set', () => {
  408. describe('Variant #1: base output code path', () => {
  409. const rawInputPath: string = 'test-input.js';
  410. const rawOutputPath: string = 'test-output.js';
  411. const outputCodePath: string = 'test-output.js';
  412. const expectedOutputSourceMapPath: string = 'test-output.js.map';
  413. let outputSourceMapPath: string;
  414. before(() => {
  415. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  416. rawInputPath,
  417. {
  418. output: rawOutputPath
  419. }
  420. );
  421. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath);
  422. });
  423. it('should return output path for source map', () => {
  424. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  425. });
  426. });
  427. });
  428. describe('Variant #2: source map file name is set', () => {
  429. describe('Variant #1: base output code path', () => {
  430. const rawInputPath: string = 'test-input.js';
  431. const rawOutputPath: string = 'test-output.js';
  432. const outputCodePath: string = 'test-output.js';
  433. const outputSourceMapFileName: string = 'test-output-source-map';
  434. const expectedOutputSourceMapPath: string = 'test-output-source-map.js.map';
  435. let outputSourceMapPath: string;
  436. before(() => {
  437. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  438. rawInputPath,
  439. {
  440. output: rawOutputPath
  441. }
  442. );
  443. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(
  444. outputCodePath,
  445. outputSourceMapFileName
  446. );
  447. });
  448. it('should return output path for source map', () => {
  449. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  450. });
  451. });
  452. });
  453. });
  454. describe('Variant #10: Win32 environment', () => {
  455. describe('Variant #1: source map file name is a file name without extension', () => {
  456. const rawInputPath: string = path.join('C:\\', tmpDirectoryPath, 'input', 'test-input.js');
  457. const rawOutputPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  458. const outputCodePath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  459. const sourceMapFileName: string = path.join('foo');
  460. const expectedOutputSourceMapPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'foo.js.map');
  461. let outputSourceMapPath: string;
  462. before(() => {
  463. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  464. rawInputPath,
  465. {
  466. output: rawOutputPath
  467. }
  468. );
  469. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  470. });
  471. it('should return output path for source map', () => {
  472. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  473. });
  474. });
  475. describe('Variant #2: source map file name is a file name with an extension', () => {
  476. const rawInputPath: string = path.join('C:\\', tmpDirectoryPath, 'input', 'test-input.js');
  477. const rawOutputPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  478. const outputCodePath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  479. const sourceMapFileName: string = path.join('foo.js.map');
  480. const expectedOutputSourceMapPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'foo.js.map');
  481. let outputSourceMapPath: string;
  482. before(() => {
  483. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  484. rawInputPath,
  485. {
  486. output: rawOutputPath
  487. }
  488. );
  489. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  490. });
  491. it('should return output path for source map', () => {
  492. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  493. });
  494. });
  495. describe('Variant #3: output path and win32 path in source map file name', () => {
  496. const rawInputPath: string = path.join('C:\\', tmpDirectoryPath, 'input', 'test-input.js');
  497. const rawOutputPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  498. const outputCodePath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  499. const sourceMapFileName: string = path.join('C:\\', 'parent', 'foo.js.map');
  500. const expectedOutputSourceMapPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'parent', 'foo.js.map');
  501. let outputSourceMapPath: string;
  502. before(() => {
  503. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  504. rawInputPath,
  505. {
  506. output: rawOutputPath
  507. }
  508. );
  509. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  510. });
  511. it('should return output path for source map', () => {
  512. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  513. });
  514. });
  515. });
  516. });
  517. describe(`Variant #2: output code path is a directory path`, () => {
  518. describe('Variant #1: source map file name is not set', () => {
  519. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  520. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  521. const outputCodePath: string = path.join(tmpDirectoryPath, 'output');
  522. let testFunc: () => string;
  523. before(() => {
  524. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  525. rawInputPath,
  526. {
  527. output: rawOutputPath
  528. }
  529. );
  530. testFunc = () => obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath);
  531. });
  532. it('should throw an error if output code path is a directory path and source map file name is not set', () => {
  533. assert.throws(testFunc, Error);
  534. });
  535. });
  536. describe('Variant #2: source map file name without extension is set', () => {
  537. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  538. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  539. const outputCodePath: string = path.join(tmpDirectoryPath, 'output');
  540. const sourceMapFileName: string = 'foo';
  541. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  542. let outputSourceMapPath: string;
  543. before(() => {
  544. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  545. rawInputPath,
  546. {
  547. output: rawOutputPath
  548. }
  549. );
  550. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  551. });
  552. it('should return output path for source map', () => {
  553. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  554. });
  555. });
  556. describe('Variant #2: source map file name with extension is set', () => {
  557. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  558. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  559. const outputCodePath: string = path.join(tmpDirectoryPath, 'output');
  560. const sourceMapFileName: string = 'foo.js.map';
  561. const expectedOutputSourceMapPath: string = path.join(tmpDirectoryPath, 'output', 'foo.js.map');
  562. let outputSourceMapPath: string;
  563. before(() => {
  564. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  565. rawInputPath,
  566. {
  567. output: rawOutputPath
  568. }
  569. );
  570. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  571. });
  572. it('should return output path for source map', () => {
  573. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  574. });
  575. });
  576. describe('Variant #3: Win32 environment', () => {
  577. const rawInputPath: string = path.join('C:\\', tmpDirectoryPath, 'input', 'test-input.js');
  578. const rawOutputPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'test-output.js');
  579. const outputCodePath: string = path.join('C:\\', tmpDirectoryPath, 'output');
  580. const sourceMapFileName: string = path.join('foo');
  581. const expectedOutputSourceMapPath: string = path.join('C:\\', tmpDirectoryPath, 'output', 'foo.js.map');
  582. let outputSourceMapPath: string;
  583. before(() => {
  584. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  585. rawInputPath,
  586. {
  587. output: rawOutputPath
  588. }
  589. );
  590. outputSourceMapPath = obfuscatedCodeFileUtils.getOutputSourceMapPath(outputCodePath, sourceMapFileName);
  591. });
  592. it('should return output path for source map', () => {
  593. assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
  594. });
  595. });
  596. });
  597. describe('Variant #3: empty paths', () => {
  598. const rawInputPath: string = path.join(tmpDirectoryPath, 'input', 'test-input.js');
  599. const rawOutputPath: string = path.join(tmpDirectoryPath, 'output', 'test-output.js');
  600. let testFunc: () => string;
  601. before(() => {
  602. const obfuscatedCodeFileUtils: ObfuscatedCodeFileUtils = new ObfuscatedCodeFileUtils(
  603. rawInputPath,
  604. {
  605. output: rawOutputPath
  606. }
  607. );
  608. testFunc = () => obfuscatedCodeFileUtils.getOutputSourceMapPath('', '');
  609. });
  610. it('should throw an error if output code path is empty', () => {
  611. assert.throws(testFunc, Error);
  612. });
  613. });
  614. });
  615. });