SourceCodeReader.spec.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. import * as fs from 'fs';
  2. import * as mkdirp from 'mkdirp';
  3. import * as rimraf from 'rimraf';
  4. import { assert } from 'chai';
  5. import * as sinon from 'sinon';
  6. import { IFileData } from '../../../../src/interfaces/cli/IFileData';
  7. import { SourceCodeReader } from '../../../../src/cli/utils/SourceCodeReader';
  8. describe('SourceCodeReader', () => {
  9. const expectedError: RegExp = /Given input path must be a valid/;
  10. const fileContent: string = 'test';
  11. const tmpDirectoryPath: string = 'test/tmp';
  12. before(() => {
  13. mkdirp.sync(tmpDirectoryPath);
  14. });
  15. describe('readSourceCode', () => {
  16. describe('Variant #1: input path is a file path', () => {
  17. describe('Variant #1: `inputPath` is a valid path', () => {
  18. const tmpFileName: string = 'test.js';
  19. const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
  20. let result: string | IFileData[];
  21. before(() => {
  22. fs.writeFileSync(inputPath, fileContent);
  23. result = new SourceCodeReader({}).readSourceCode(inputPath);
  24. });
  25. it('should return content of file', () => {
  26. assert.equal(result, fileContent);
  27. });
  28. after(() => {
  29. fs.unlinkSync(inputPath);
  30. });
  31. });
  32. describe('Variant #2: `inputPath` is not a valid path', () => {
  33. const tmpFileName: string = 'test.js';
  34. const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
  35. let testFunc: () => void;
  36. before(() => {
  37. testFunc = () => new SourceCodeReader({}).readSourceCode(inputPath);
  38. });
  39. it('should throw an error if `inputPath` is not a valid path', () => {
  40. assert.throws(testFunc, expectedError);
  41. });
  42. });
  43. describe('Variant #3: `inputPath` has invalid extension', () => {
  44. const tmpFileName: string = 'test.ts';
  45. const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
  46. let testFunc: () => void;
  47. before(() => {
  48. fs.writeFileSync(inputPath, fileContent);
  49. testFunc = () => new SourceCodeReader({}).readSourceCode(inputPath);
  50. });
  51. it('should throw an error if `inputPath` has invalid extension', () => {
  52. assert.throws(testFunc, expectedError);
  53. });
  54. after(() => {
  55. fs.unlinkSync(inputPath);
  56. });
  57. });
  58. describe('Variant #4: `exclude` option', () => {
  59. describe('Variant #1: `inputPath` isn\'t excluded path', () => {
  60. const tmpFileName: string = 'test.js';
  61. const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
  62. let result: string | IFileData[];
  63. before(() => {
  64. fs.writeFileSync(inputPath, fileContent);
  65. result = new SourceCodeReader({
  66. exclude: ['**/foo.js']
  67. }).readSourceCode(inputPath); });
  68. it('should return content of file', () => {
  69. assert.equal(result, fileContent);
  70. });
  71. after(() => {
  72. fs.unlinkSync(inputPath);
  73. });
  74. });
  75. describe('Variant #2: `inputPath` is excluded path', () => {
  76. describe('Variant #1: exclude by `glob` pattern', () => {
  77. const tmpFileName: string = 'test.js';
  78. const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
  79. let testFunc: () => void;
  80. before(() => {
  81. fs.writeFileSync(inputPath, fileContent);
  82. testFunc = () => new SourceCodeReader({
  83. exclude: [`**/${tmpFileName}`]
  84. }).readSourceCode(inputPath);
  85. });
  86. it('should throw an error if `inputPath` is the excluded file path', () => {
  87. assert.throws(testFunc, expectedError);
  88. });
  89. after(() => {
  90. fs.unlinkSync(inputPath);
  91. });
  92. });
  93. describe('Variant #2: exclude by file name', () => {
  94. const tmpFileName: string = 'test.js';
  95. const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
  96. let testFunc: () => void;
  97. before(() => {
  98. fs.writeFileSync(inputPath, fileContent);
  99. testFunc = () => new SourceCodeReader({
  100. exclude: [tmpFileName]
  101. }).readSourceCode(inputPath);
  102. });
  103. it('should throw an error if `inputPath` is the excluded file path', () => {
  104. assert.throws(testFunc, expectedError);
  105. });
  106. after(() => {
  107. fs.unlinkSync(inputPath);
  108. });
  109. });
  110. describe('Variant #3: exclude by file path', () => {
  111. const tmpFileName: string = 'test.js';
  112. const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
  113. let testFunc: () => void;
  114. before(() => {
  115. fs.writeFileSync(inputPath, fileContent);
  116. testFunc = () => new SourceCodeReader({
  117. exclude: [inputPath]
  118. }).readSourceCode(inputPath);
  119. });
  120. it('should throw an error if `inputPath` is the excluded file path', () => {
  121. assert.throws(testFunc, expectedError);
  122. });
  123. after(() => {
  124. fs.unlinkSync(inputPath);
  125. });
  126. });
  127. });
  128. });
  129. });
  130. describe('Variant #2: input path is a directory path', () => {
  131. describe('Variant #1: `inputPath` is a valid path', () => {
  132. const tmpFileName1: string = 'foo.js';
  133. const tmpFileName2: string = 'bar.js';
  134. const tmpFileName3: string = 'baz.png';
  135. const tmpFileName4: string = 'bark-obfuscated.js';
  136. const filePath1: string = `${tmpDirectoryPath}/${tmpFileName1}`;
  137. const filePath2: string = `${tmpDirectoryPath}/${tmpFileName2}`;
  138. const filePath3: string = `${tmpDirectoryPath}/${tmpFileName3}`;
  139. const filePath4: string = `${tmpDirectoryPath}/${tmpFileName4}`;
  140. const expectedResult: IFileData[] = [
  141. {
  142. filePath: filePath2,
  143. content: fileContent
  144. },
  145. {
  146. filePath: filePath1,
  147. content: fileContent
  148. }
  149. ];
  150. let result: string | IFileData[];
  151. before(() => {
  152. fs.writeFileSync(filePath1, fileContent);
  153. fs.writeFileSync(filePath2, fileContent);
  154. fs.writeFileSync(filePath3, fileContent);
  155. fs.writeFileSync(filePath4, fileContent);
  156. result = new SourceCodeReader({}).readSourceCode(tmpDirectoryPath);
  157. });
  158. it('should return files data', () => {
  159. assert.deepEqual(result, expectedResult);
  160. });
  161. after(() => {
  162. fs.unlinkSync(filePath1);
  163. fs.unlinkSync(filePath2);
  164. fs.unlinkSync(filePath3);
  165. fs.unlinkSync(filePath4);
  166. });
  167. });
  168. describe('Variant #2: `inputPath` is not a valid path', () => {
  169. const inputPath: string = 'abc';
  170. let testFunc: () => void;
  171. before(() => {
  172. testFunc = () => new SourceCodeReader({}).readSourceCode(inputPath);
  173. });
  174. it('should throw an error if `inputPath` is not a valid path', () => {
  175. assert.throws(testFunc, expectedError);
  176. });
  177. });
  178. describe('Variant #3: `inputPath` is a directory with sub-directories', () => {
  179. const parentDirectoryName1: string = 'parent1';
  180. const parentDirectoryName2: string = 'parent';
  181. const parentDirectoryPath1: string = `${tmpDirectoryPath}/${parentDirectoryName1}`;
  182. const parentDirectoryPath2: string = `${tmpDirectoryPath}/${parentDirectoryName2}`;
  183. const tmpFileName1: string = 'foo.js';
  184. const tmpFileName2: string = 'bar.js';
  185. const tmpFileName3: string = 'baz.js';
  186. const tmpFileName4: string = 'bark.js';
  187. const filePath1: string = `${tmpDirectoryPath}/${tmpFileName1}`;
  188. const filePath2: string = `${tmpDirectoryPath}/${tmpFileName2}`;
  189. const filePath3: string = `${parentDirectoryPath1}/${tmpFileName3}`;
  190. const filePath4: string = `${parentDirectoryPath2}/${tmpFileName4}`;
  191. const expectedResult: IFileData[] = [
  192. {
  193. filePath: filePath2,
  194. content: fileContent
  195. },
  196. {
  197. filePath: filePath1,
  198. content: fileContent
  199. },
  200. {
  201. filePath: filePath4,
  202. content: fileContent
  203. },
  204. {
  205. filePath: filePath3,
  206. content: fileContent
  207. }
  208. ];
  209. let result: string | IFileData[];
  210. before(() => {
  211. mkdirp.sync(parentDirectoryPath1);
  212. mkdirp.sync(parentDirectoryPath2);
  213. fs.writeFileSync(filePath1, fileContent);
  214. fs.writeFileSync(filePath2, fileContent);
  215. fs.writeFileSync(filePath3, fileContent);
  216. fs.writeFileSync(filePath4, fileContent);
  217. result = new SourceCodeReader({}).readSourceCode(tmpDirectoryPath);
  218. });
  219. it('should return files data', () => {
  220. assert.deepEqual(result, expectedResult);
  221. });
  222. after(() => {
  223. fs.unlinkSync(filePath1);
  224. fs.unlinkSync(filePath2);
  225. fs.unlinkSync(filePath3);
  226. fs.unlinkSync(filePath4);
  227. rimraf.sync(parentDirectoryPath1);
  228. rimraf.sync(parentDirectoryPath2);
  229. });
  230. });
  231. describe('Variant #4: `exclude` option', () => {
  232. describe('Variant #1: `inputPath` isn\'t excluded path', () => {
  233. const tmpFileName1: string = 'foo.js';
  234. const tmpFileName2: string = 'bar.js';
  235. const tmpFileName3: string = 'baz.png';
  236. const tmpFileName4: string = 'bark-obfuscated.js';
  237. const filePath1: string = `${tmpDirectoryPath}/${tmpFileName1}`;
  238. const filePath2: string = `${tmpDirectoryPath}/${tmpFileName2}`;
  239. const filePath3: string = `${tmpDirectoryPath}/${tmpFileName3}`;
  240. const filePath4: string = `${tmpDirectoryPath}/${tmpFileName4}`;
  241. const expectedResult: IFileData[] = [
  242. {
  243. filePath: filePath2,
  244. content: fileContent
  245. },
  246. {
  247. filePath: filePath1,
  248. content: fileContent
  249. }
  250. ];
  251. let result: string | IFileData[];
  252. before(() => {
  253. fs.writeFileSync(filePath1, fileContent);
  254. fs.writeFileSync(filePath2, fileContent);
  255. fs.writeFileSync(filePath3, fileContent);
  256. fs.writeFileSync(filePath4, fileContent);
  257. result = new SourceCodeReader({
  258. exclude: ['**/hawk.js']
  259. }).readSourceCode(tmpDirectoryPath);
  260. });
  261. it('should return files data', () => {
  262. assert.deepEqual(result, expectedResult);
  263. });
  264. after(() => {
  265. fs.unlinkSync(filePath1);
  266. fs.unlinkSync(filePath2);
  267. fs.unlinkSync(filePath3);
  268. fs.unlinkSync(filePath4);
  269. });
  270. });
  271. describe('Variant #2: `inputPath` is excluded path', () => {
  272. describe('Variant #1: exclude by `glob` pattern', () => {
  273. const tmpFileName1: string = 'foo.js';
  274. const tmpFileName2: string = 'bar.js';
  275. const tmpFileName3: string = 'baz.js';
  276. const tmpFileName4: string = 'bark.js';
  277. const filePath1: string = `${tmpDirectoryPath}/${tmpFileName1}`;
  278. const filePath2: string = `${tmpDirectoryPath}/${tmpFileName2}`;
  279. const filePath3: string = `${tmpDirectoryPath}/${tmpFileName3}`;
  280. const filePath4: string = `${tmpDirectoryPath}/${tmpFileName4}`;
  281. const expectedResult: IFileData[] = [
  282. {
  283. filePath: filePath3,
  284. content: fileContent
  285. },
  286. {
  287. filePath: filePath1,
  288. content: fileContent
  289. }
  290. ];
  291. let result: string | IFileData[];
  292. before(() => {
  293. fs.writeFileSync(filePath1, fileContent);
  294. fs.writeFileSync(filePath2, fileContent);
  295. fs.writeFileSync(filePath3, fileContent);
  296. fs.writeFileSync(filePath4, fileContent);
  297. result = new SourceCodeReader({
  298. exclude: [
  299. `**/${tmpFileName2}`,
  300. `**/${tmpFileName4}`
  301. ]
  302. }).readSourceCode(tmpDirectoryPath);
  303. });
  304. it('should return files data', () => {
  305. assert.deepEqual(result, expectedResult);
  306. });
  307. after(() => {
  308. fs.unlinkSync(filePath1);
  309. fs.unlinkSync(filePath2);
  310. fs.unlinkSync(filePath3);
  311. fs.unlinkSync(filePath4);
  312. });
  313. });
  314. describe('Variant #2: exclude by file name', () => {
  315. const tmpFileName1: string = 'foo.js';
  316. const tmpFileName2: string = 'bar.js';
  317. const tmpFileName3: string = 'baz.js';
  318. const tmpFileName4: string = 'bark.js';
  319. const filePath1: string = `${tmpDirectoryPath}/${tmpFileName1}`;
  320. const filePath2: string = `${tmpDirectoryPath}/${tmpFileName2}`;
  321. const filePath3: string = `${tmpDirectoryPath}/${tmpFileName3}`;
  322. const filePath4: string = `${tmpDirectoryPath}/${tmpFileName4}`;
  323. const expectedResult: IFileData[] = [
  324. {
  325. filePath: filePath3,
  326. content: fileContent
  327. },
  328. {
  329. filePath: filePath1,
  330. content: fileContent
  331. }
  332. ];
  333. let result: string | IFileData[];
  334. before(() => {
  335. fs.writeFileSync(filePath1, fileContent);
  336. fs.writeFileSync(filePath2, fileContent);
  337. fs.writeFileSync(filePath3, fileContent);
  338. fs.writeFileSync(filePath4, fileContent);
  339. result = new SourceCodeReader({
  340. exclude: [
  341. tmpFileName2,
  342. tmpFileName4
  343. ]
  344. }).readSourceCode(tmpDirectoryPath);
  345. });
  346. it('should return files data', () => {
  347. assert.deepEqual(result, expectedResult);
  348. });
  349. after(() => {
  350. fs.unlinkSync(filePath1);
  351. fs.unlinkSync(filePath2);
  352. fs.unlinkSync(filePath3);
  353. fs.unlinkSync(filePath4);
  354. });
  355. });
  356. describe('Variant #3: exclude by file path', () => {
  357. const tmpFileName1: string = 'foo.js';
  358. const tmpFileName2: string = 'bar.js';
  359. const tmpFileName3: string = 'baz.js';
  360. const tmpFileName4: string = 'bark.js';
  361. const filePath1: string = `${tmpDirectoryPath}/${tmpFileName1}`;
  362. const filePath2: string = `${tmpDirectoryPath}/${tmpFileName2}`;
  363. const filePath3: string = `${tmpDirectoryPath}/${tmpFileName3}`;
  364. const filePath4: string = `${tmpDirectoryPath}/${tmpFileName4}`;
  365. const expectedResult: IFileData[] = [
  366. {
  367. filePath: filePath3,
  368. content: fileContent
  369. },
  370. {
  371. filePath: filePath1,
  372. content: fileContent
  373. }
  374. ];
  375. let result: string | IFileData[];
  376. before(() => {
  377. fs.writeFileSync(filePath1, fileContent);
  378. fs.writeFileSync(filePath2, fileContent);
  379. fs.writeFileSync(filePath3, fileContent);
  380. fs.writeFileSync(filePath4, fileContent);
  381. result = new SourceCodeReader({
  382. exclude: [
  383. filePath2,
  384. filePath4
  385. ]
  386. }).readSourceCode(tmpDirectoryPath);
  387. });
  388. it('should return files data', () => {
  389. assert.deepEqual(result, expectedResult);
  390. });
  391. after(() => {
  392. fs.unlinkSync(filePath1);
  393. fs.unlinkSync(filePath2);
  394. fs.unlinkSync(filePath3);
  395. fs.unlinkSync(filePath4);
  396. });
  397. });
  398. describe('Variant #4: exclude whole directory', () => {
  399. const tmpFileName1: string = 'foo.js';
  400. const tmpFileName2: string = 'bar.js';
  401. const tmpFileName3: string = 'baz.js';
  402. const tmpFileName4: string = 'bark.js';
  403. const filePath1: string = `${tmpDirectoryPath}/${tmpFileName1}`;
  404. const filePath2: string = `${tmpDirectoryPath}/${tmpFileName2}`;
  405. const filePath3: string = `${tmpDirectoryPath}/${tmpFileName3}`;
  406. const filePath4: string = `${tmpDirectoryPath}/${tmpFileName4}`;
  407. let testFunc: () => void;
  408. before(() => {
  409. fs.writeFileSync(filePath1, fileContent);
  410. fs.writeFileSync(filePath2, fileContent);
  411. fs.writeFileSync(filePath3, fileContent);
  412. fs.writeFileSync(filePath4, fileContent);
  413. testFunc = () => new SourceCodeReader({
  414. exclude: [tmpDirectoryPath]
  415. }).readSourceCode(tmpDirectoryPath);
  416. });
  417. it('should return files data', () => {
  418. assert.throws(testFunc, expectedError);
  419. });
  420. after(() => {
  421. fs.unlinkSync(filePath1);
  422. fs.unlinkSync(filePath2);
  423. fs.unlinkSync(filePath3);
  424. fs.unlinkSync(filePath4);
  425. });
  426. });
  427. });
  428. });
  429. });
  430. describe('Variant #3: logging', () => {
  431. const tmpFileName: string = 'test.js';
  432. const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
  433. const expectedConsoleLogCallResult: boolean = true;
  434. const expectedLoggingMessage: string = `[javascript-obfuscator-cli] Obfuscating file: ${inputPath}...`;
  435. let consoleLogSpy: sinon.SinonSpy<any, void>,
  436. consoleLogCallResult: boolean,
  437. loggingMessageResult: string;
  438. before(() => {
  439. consoleLogSpy = sinon.spy(console, 'log');
  440. fs.writeFileSync(inputPath, fileContent);
  441. new SourceCodeReader({}).readSourceCode(inputPath);
  442. consoleLogCallResult = consoleLogSpy.called;
  443. loggingMessageResult = consoleLogSpy.getCall(0).args[0];
  444. });
  445. it('should call `console.log`', () => {
  446. assert.equal(consoleLogCallResult, expectedConsoleLogCallResult);
  447. });
  448. it('should log file name to the console', () => {
  449. assert.include(loggingMessageResult, expectedLoggingMessage);
  450. });
  451. after(() => {
  452. consoleLogSpy.restore();
  453. fs.unlinkSync(inputPath);
  454. });
  455. });
  456. });
  457. after(() => {
  458. rimraf.sync(tmpDirectoryPath);
  459. });
  460. });