SourceCodeReader.spec.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. import * as fs from 'fs';
  2. import * as mkdirp from 'mkdirp';
  3. import { assert } from 'chai';
  4. import { SourceCodeReader } from '../../../../src/cli/utils/SourceCodeReader';
  5. import { IFileData } from '../../../../src/interfaces/cli/IFileData';
  6. describe('SourceCodeReader', () => {
  7. const fileContent: string = 'test';
  8. const tmpDir: string = 'test/tmp';
  9. before(() => {
  10. mkdirp.sync(tmpDir);
  11. });
  12. describe('readSourceCode (inputPath: string): void', () => {
  13. describe('Variant #1: input path is a file path', () => {
  14. describe('`inputPath` is a valid path', () => {
  15. const tmpFileName: string = 'test.js';
  16. const inputPath: string = `${tmpDir}/${tmpFileName}`;
  17. let result: string | IFileData[];
  18. before(() => {
  19. fs.writeFileSync(inputPath, fileContent);
  20. result = SourceCodeReader.readSourceCode(inputPath);
  21. });
  22. it('should return content of file', () => {
  23. assert.equal(result, fileContent);
  24. });
  25. after(() => {
  26. fs.unlinkSync(inputPath);
  27. });
  28. });
  29. describe('`inputPath` is not a valid path', () => {
  30. const tmpFileName: string = 'test.js';
  31. const inputPath: string = `${tmpDir}/${tmpFileName}`;
  32. let testFunc: () => void;
  33. before(() => {
  34. testFunc = () => SourceCodeReader.readSourceCode(inputPath);
  35. });
  36. it('should throw an error if `inputPath` is not a valid path', () => {
  37. assert.throws(testFunc, ReferenceError);
  38. });
  39. });
  40. describe('`inputPath` has invalid extension', () => {
  41. const tmpFileName: string = 'test.ts';
  42. const inputPath: string = `${tmpDir}/${tmpFileName}`;
  43. let testFunc: () => void;
  44. before(() => {
  45. fs.writeFileSync(inputPath, fileContent);
  46. testFunc = () => SourceCodeReader.readSourceCode(inputPath);
  47. });
  48. it('should throw an error if `inputPath` has invalid extension', () => {
  49. assert.throws(testFunc, ReferenceError);
  50. });
  51. after(() => {
  52. fs.unlinkSync(inputPath);
  53. });
  54. });
  55. });
  56. describe('Variant #2: input path is a directory path', () => {
  57. describe('`inputPath` is a valid path', () => {
  58. const tmpFileName1: string = 'foo.js';
  59. const tmpFileName2: string = 'bar.js';
  60. const tmpFileName3: string = 'baz.png';
  61. const tmpFileName4: string = 'bark-obfuscated.js';
  62. const filePath1: string = `${tmpDir}/${tmpFileName1}`;
  63. const filePath2: string = `${tmpDir}/${tmpFileName2}`;
  64. const filePath3: string = `${tmpDir}/${tmpFileName3}`;
  65. const filePath4: string = `${tmpDir}/${tmpFileName4}`;
  66. const expectedResult: IFileData[] = [
  67. {
  68. filePath: filePath2,
  69. content: fileContent
  70. },
  71. {
  72. filePath: filePath1,
  73. content: fileContent
  74. }
  75. ];
  76. let result: string | IFileData[];
  77. before(() => {
  78. fs.writeFileSync(filePath1, fileContent);
  79. fs.writeFileSync(filePath2, fileContent);
  80. fs.writeFileSync(filePath3, fileContent);
  81. fs.writeFileSync(filePath4, fileContent);
  82. result = SourceCodeReader.readSourceCode(tmpDir);
  83. });
  84. it('should return files data', () => {
  85. assert.deepEqual(result, expectedResult);
  86. });
  87. after(() => {
  88. fs.unlinkSync(filePath1);
  89. fs.unlinkSync(filePath2);
  90. fs.unlinkSync(filePath3);
  91. fs.unlinkSync(filePath4);
  92. });
  93. });
  94. describe('`inputPath` is not a valid path', () => {
  95. const inputPath: string = 'abc';
  96. let testFunc: () => void;
  97. before(() => {
  98. testFunc = () => SourceCodeReader.readSourceCode(inputPath);
  99. });
  100. it('should throw an error if `inputPath` is not a valid path', () => {
  101. assert.throws(testFunc, ReferenceError);
  102. });
  103. });
  104. });
  105. });
  106. after(() => {
  107. fs.rmdirSync(tmpDir);
  108. });
  109. });