SourceCodeReader.spec.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 filePath1: string = `${tmpDir}/${tmpFileName1}`;
  62. const filePath2: string = `${tmpDir}/${tmpFileName2}`;
  63. const filePath3: string = `${tmpDir}/${tmpFileName3}`;
  64. const expectedResult: IFileData[] = [
  65. {
  66. filePath: filePath2,
  67. content: fileContent
  68. },
  69. {
  70. filePath: filePath1,
  71. content: fileContent
  72. }
  73. ];
  74. let result: string | IFileData[];
  75. before(() => {
  76. fs.writeFileSync(filePath1, fileContent);
  77. fs.writeFileSync(filePath2, fileContent);
  78. fs.writeFileSync(filePath3, fileContent);
  79. result = SourceCodeReader.readSourceCode(tmpDir);
  80. });
  81. it('should return files data', () => {
  82. assert.deepEqual(result, expectedResult);
  83. });
  84. after(() => {
  85. fs.unlinkSync(filePath1);
  86. fs.unlinkSync(filePath2);
  87. fs.unlinkSync(filePath3);
  88. });
  89. });
  90. describe('`inputPath` is not a valid path', () => {
  91. const inputPath: string = 'abc';
  92. let testFunc: () => void;
  93. before(() => {
  94. testFunc = () => SourceCodeReader.readSourceCode(inputPath);
  95. });
  96. it('should throw an error if `inputPath` is not a valid path', () => {
  97. assert.throws(testFunc, ReferenceError);
  98. });
  99. });
  100. });
  101. });
  102. after(() => {
  103. fs.rmdirSync(tmpDir);
  104. });
  105. });