SourceCodeReader.spec.ts 7.4 KB

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