SourceCodeReader.spec.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 fileContent: string = 'test';
  10. const tmpDirectoryPath: string = 'test/tmp';
  11. before(() => {
  12. mkdirp.sync(tmpDirectoryPath);
  13. });
  14. describe('readSourceCode (inputPath: string): void', () => {
  15. describe('Variant #1: input path is a file path', () => {
  16. describe('`inputPath` is a valid path', () => {
  17. const tmpFileName: string = 'test.js';
  18. const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
  19. let result: string | IFileData[];
  20. before(() => {
  21. fs.writeFileSync(inputPath, fileContent);
  22. result = SourceCodeReader.readSourceCode(inputPath);
  23. });
  24. it('should return content of file', () => {
  25. assert.equal(result, fileContent);
  26. });
  27. after(() => {
  28. fs.unlinkSync(inputPath);
  29. });
  30. });
  31. describe('`inputPath` is not a valid path', () => {
  32. const tmpFileName: string = 'test.js';
  33. const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
  34. let testFunc: () => void;
  35. before(() => {
  36. testFunc = () => SourceCodeReader.readSourceCode(inputPath);
  37. });
  38. it('should throw an error if `inputPath` is not a valid path', () => {
  39. assert.throws(testFunc, ReferenceError);
  40. });
  41. });
  42. describe('`inputPath` has invalid extension', () => {
  43. const tmpFileName: string = 'test.ts';
  44. const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
  45. let testFunc: () => void;
  46. before(() => {
  47. fs.writeFileSync(inputPath, fileContent);
  48. testFunc = () => SourceCodeReader.readSourceCode(inputPath);
  49. });
  50. it('should throw an error if `inputPath` has invalid extension', () => {
  51. assert.throws(testFunc, ReferenceError);
  52. });
  53. after(() => {
  54. fs.unlinkSync(inputPath);
  55. });
  56. });
  57. });
  58. describe('Variant #2: input path is a directory path', () => {
  59. describe('`inputPath` is a valid path', () => {
  60. const tmpFileName1: string = 'foo.js';
  61. const tmpFileName2: string = 'bar.js';
  62. const tmpFileName3: string = 'baz.png';
  63. const tmpFileName4: string = 'bark-obfuscated.js';
  64. const filePath1: string = `${tmpDirectoryPath}/${tmpFileName1}`;
  65. const filePath2: string = `${tmpDirectoryPath}/${tmpFileName2}`;
  66. const filePath3: string = `${tmpDirectoryPath}/${tmpFileName3}`;
  67. const filePath4: string = `${tmpDirectoryPath}/${tmpFileName4}`;
  68. const expectedResult: IFileData[] = [
  69. {
  70. filePath: filePath2,
  71. content: fileContent
  72. },
  73. {
  74. filePath: filePath1,
  75. content: fileContent
  76. }
  77. ];
  78. let result: string | IFileData[];
  79. before(() => {
  80. fs.writeFileSync(filePath1, fileContent);
  81. fs.writeFileSync(filePath2, fileContent);
  82. fs.writeFileSync(filePath3, fileContent);
  83. fs.writeFileSync(filePath4, fileContent);
  84. result = SourceCodeReader.readSourceCode(tmpDirectoryPath);
  85. });
  86. it('should return files data', () => {
  87. assert.deepEqual(result, expectedResult);
  88. });
  89. after(() => {
  90. fs.unlinkSync(filePath1);
  91. fs.unlinkSync(filePath2);
  92. fs.unlinkSync(filePath3);
  93. fs.unlinkSync(filePath4);
  94. });
  95. });
  96. describe('`inputPath` is not a valid path', () => {
  97. const inputPath: string = 'abc';
  98. let testFunc: () => void;
  99. before(() => {
  100. testFunc = () => SourceCodeReader.readSourceCode(inputPath);
  101. });
  102. it('should throw an error if `inputPath` is not a valid path', () => {
  103. assert.throws(testFunc, ReferenceError);
  104. });
  105. });
  106. describe('`inputPath` is a directory with sub-directories', () => {
  107. const parentDirectoryName1: string = 'parent1';
  108. const parentDirectoryName2: string = 'parent';
  109. const parentDirectoryPath1: string = `${tmpDirectoryPath}/${parentDirectoryName1}`;
  110. const parentDirectoryPath2: string = `${tmpDirectoryPath}/${parentDirectoryName2}`;
  111. const tmpFileName1: string = 'foo.js';
  112. const tmpFileName2: string = 'bar.js';
  113. const tmpFileName3: string = 'baz.js';
  114. const tmpFileName4: string = 'bark.js';
  115. const filePath1: string = `${tmpDirectoryPath}/${tmpFileName1}`;
  116. const filePath2: string = `${tmpDirectoryPath}/${tmpFileName2}`;
  117. const filePath3: string = `${parentDirectoryPath1}/${tmpFileName3}`;
  118. const filePath4: string = `${parentDirectoryPath2}/${tmpFileName4}`;
  119. const expectedResult: IFileData[] = [
  120. {
  121. filePath: filePath2,
  122. content: fileContent
  123. },
  124. {
  125. filePath: filePath1,
  126. content: fileContent
  127. },
  128. {
  129. filePath: filePath4,
  130. content: fileContent
  131. },
  132. {
  133. filePath: filePath3,
  134. content: fileContent
  135. }
  136. ];
  137. let result: string | IFileData[];
  138. before(() => {
  139. mkdirp.sync(parentDirectoryPath1);
  140. mkdirp.sync(parentDirectoryPath2);
  141. fs.writeFileSync(filePath1, fileContent);
  142. fs.writeFileSync(filePath2, fileContent);
  143. fs.writeFileSync(filePath3, fileContent);
  144. fs.writeFileSync(filePath4, fileContent);
  145. result = SourceCodeReader.readSourceCode(tmpDirectoryPath);
  146. });
  147. it('should return files data', () => {
  148. assert.deepEqual(result, expectedResult);
  149. });
  150. after(() => {
  151. fs.unlinkSync(filePath1);
  152. fs.unlinkSync(filePath2);
  153. fs.unlinkSync(filePath3);
  154. fs.unlinkSync(filePath4);
  155. rimraf.sync(parentDirectoryPath1);
  156. rimraf.sync(parentDirectoryPath2);
  157. });
  158. });
  159. });
  160. describe('Variant #3: logging', () => {
  161. const tmpFileName: string = 'test.js';
  162. const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
  163. const expectedConsoleLogCallResult: boolean = true;
  164. const expectedLoggingMessage: string = `[javascript-obfuscator-cli] Obfuscating file: ${inputPath}...`;
  165. let consoleLogSpy: sinon.SinonSpy,
  166. consoleLogCallResult: boolean,
  167. loggingMessageResult: string;
  168. before(() => {
  169. consoleLogSpy = sinon.spy(console, 'log');
  170. fs.writeFileSync(inputPath, fileContent);
  171. SourceCodeReader.readSourceCode(inputPath);
  172. consoleLogCallResult = consoleLogSpy.called;
  173. loggingMessageResult = consoleLogSpy.getCall(0).args[0];
  174. });
  175. it('should call `console.log`', () => {
  176. assert.equal(consoleLogCallResult, expectedConsoleLogCallResult);
  177. });
  178. it('should log file name to the console', () => {
  179. assert.include(loggingMessageResult, expectedLoggingMessage);
  180. });
  181. after(() => {
  182. consoleLogSpy.restore();
  183. fs.unlinkSync(inputPath);
  184. });
  185. });
  186. });
  187. after(() => {
  188. rimraf.sync(tmpDirectoryPath);
  189. });
  190. });