CLIUtils.spec.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. const configDirName: string = 'test/fixtures';
  40. const configFileName: string = 'config.js';
  41. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  42. const expectedResult: TInputOptions = {
  43. compact: true,
  44. selfDefending: false,
  45. sourceMap: true
  46. };
  47. let result: Object;
  48. before(() => {
  49. result = CLIUtils.getUserConfig(configFilePath);
  50. });
  51. it('should return object with user configuration', () => {
  52. assert.deepEqual(result, expectedResult);
  53. });
  54. });
  55. describe('validateInputPath (inputPath: string): void', () => {
  56. describe('`inputPath` is a valid path', () => {
  57. const tmpFileName: string = 'test.js';
  58. const inputPath: string = `${tmpDir}/${tmpFileName}`;
  59. before(() => {
  60. fs.writeFileSync(inputPath, fileContent);
  61. });
  62. it('shouldn\'t throw an error if `inputPath` is a valid path', () => {
  63. assert.doesNotThrow(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
  64. });
  65. after(() => {
  66. fs.unlinkSync(inputPath);
  67. });
  68. });
  69. describe('`inputPath` is not a valid path', () => {
  70. const tmpFileName: string = 'test.js';
  71. const inputPath: string = `${tmpDir}/${tmpFileName}`;
  72. it('should throw an error if `inputPath` is not a valid path', () => {
  73. assert.throws(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
  74. });
  75. });
  76. describe('`inputPath` is a file name has invalid extension', () => {
  77. const tmpFileName: string = 'test.ts';
  78. const inputPath: string = `${tmpDir}/${tmpFileName}`;
  79. before(() => {
  80. fs.writeFileSync(inputPath, fileContent);
  81. });
  82. it('should throw an error if `inputPath` is a file name has invalid extension', () => {
  83. assert.throws(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
  84. });
  85. after(() => {
  86. fs.unlinkSync(inputPath);
  87. });
  88. });
  89. });
  90. after(() => {
  91. fs.rmdirSync(tmpDir);
  92. });
  93. });