CLIUtils.spec.ts 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. import * as fs from 'fs';
  2. import * as mkdirp from 'mkdirp';
  3. import { assert } from 'chai';
  4. import { TInputOptions } from '../../../../src/types/options/TInputOptions';
  5. import { CLIUtils } from '../../../../src/cli/utils/CLIUtils';
  6. describe('CLIUtils', () => {
  7. const fileContent: string = 'test';
  8. const tmpDir: string = 'test/tmp';
  9. before(() => {
  10. mkdirp.sync(tmpDir);
  11. });
  12. describe('getOutputCodePath (outputPath: string, inputPath: string): string', () => {
  13. let expectedInputPath: string = 'test/input/test-obfuscated.js',
  14. inputPath: string = 'test/input/test.js',
  15. outputPath: string = 'test/output/test.js';
  16. it('should return `outputPath` if this path is set', () => {
  17. assert.equal(CLIUtils.getOutputCodePath(outputPath, inputPath), outputPath);
  18. });
  19. it('should output path based on `inputPath` if `outputPath` is not set', () => {
  20. assert.equal(CLIUtils.getOutputCodePath('', inputPath), expectedInputPath);
  21. });
  22. });
  23. describe('getOutputSourceMapPath (outputCodePath: string): string', () => {
  24. let expectedOutputSourceMapPath: string = 'test/output/test.js.map',
  25. outputCodePath: string = 'test/output/test.js';
  26. it('should return output path for source map', () => {
  27. assert.equal(CLIUtils.getOutputSourceMapPath(outputCodePath), expectedOutputSourceMapPath);
  28. });
  29. });
  30. describe('getPackageConfig (): IPackageConfig', () => {
  31. it('should return `package.json` `name` field for current CLI program as object', () => {
  32. assert.property(CLIUtils.getPackageConfig(), 'name');
  33. });
  34. it('should return `package.json` `version` field for current CLI program as object', () => {
  35. assert.property(CLIUtils.getPackageConfig(), 'version');
  36. });
  37. });
  38. describe('getUserConfig (configPath: string): Object', () => {
  39. describe('variant #1: valid config file path', () => {
  40. const configDirName: string = 'test/fixtures';
  41. const configFileName: string = 'config.js';
  42. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  43. const expectedResult: TInputOptions = {
  44. compact: true,
  45. selfDefending: false,
  46. sourceMap: true
  47. };
  48. let result: Object;
  49. before(() => {
  50. result = CLIUtils.getUserConfig(configFilePath);
  51. });
  52. it('should return object with user configuration', () => {
  53. assert.deepEqual(result, expectedResult);
  54. });
  55. });
  56. describe('variant #2: invalid config file path', () => {
  57. const configDirName: string = 'test/fixtures';
  58. const configFileName: string = 'configs.js';
  59. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  60. let testFunc: () => void;
  61. before(() => {
  62. testFunc = () => CLIUtils.getUserConfig(configFilePath);
  63. });
  64. it('should throw an error if `configFilePath` is not a valid path', () => {
  65. assert.throws(testFunc, ReferenceError);
  66. });
  67. });
  68. });
  69. describe('readSourceCode (inputPath: string): void', () => {
  70. describe('`inputPath` is a valid path', () => {
  71. const tmpFileName: string = 'test.js';
  72. const inputPath: string = `${tmpDir}/${tmpFileName}`;
  73. let result: string;
  74. before(() => {
  75. fs.writeFileSync(inputPath, fileContent);
  76. result = CLIUtils.readSourceCode(inputPath);
  77. });
  78. it('should return content of file', () => {
  79. assert.equal(result, fileContent);
  80. });
  81. after(() => {
  82. fs.unlinkSync(inputPath);
  83. });
  84. });
  85. describe('`inputPath` is not a valid path', () => {
  86. const tmpFileName: string = 'test.js';
  87. const inputPath: string = `${tmpDir}/${tmpFileName}`;
  88. let testFunc: () => void;
  89. before(() => {
  90. testFunc = () => CLIUtils.readSourceCode(inputPath);
  91. });
  92. it('should throw an error if `inputPath` is not a valid path', () => {
  93. assert.throws(testFunc, ReferenceError);
  94. });
  95. });
  96. describe('`inputPath` is a file name has invalid extension', () => {
  97. const tmpFileName: string = 'test.ts';
  98. const inputPath: string = `${tmpDir}/${tmpFileName}`;
  99. let testFunc: () => void;
  100. before(() => {
  101. fs.writeFileSync(inputPath, fileContent);
  102. testFunc = () => CLIUtils.readSourceCode(inputPath);
  103. });
  104. it('should throw an error if `inputPath` is a file name has invalid extension', () => {
  105. assert.throws(testFunc, ReferenceError);
  106. });
  107. after(() => {
  108. fs.unlinkSync(inputPath);
  109. });
  110. });
  111. });
  112. after(() => {
  113. fs.rmdirSync(tmpDir);
  114. });
  115. });