CLIUtils.spec.ts 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. import { assert } from 'chai';
  2. import { TInputOptions } from '../../../../src/types/options/TInputOptions';
  3. import { CLIUtils } from '../../../../src/cli/utils/CLIUtils';
  4. describe('CLIUtils', () => {
  5. describe('getOutputCodePath (inputPath: string): string', () => {
  6. describe('variant #1: base input path', () => {
  7. let expectedOutputPath: string = 'test/input/test-obfuscated.js',
  8. inputPath: string = 'test/input/test.js';
  9. it('should output path based on `inputPath`', () => {
  10. assert.equal(CLIUtils.getOutputCodePath(inputPath), expectedOutputPath);
  11. });
  12. });
  13. describe('variant #2: relative input path with dot', () => {
  14. let expectedOutputPath: string = 'input-obfuscated.js',
  15. inputPath: string = './input.js';
  16. it('should output path based on `inputPath`', () => {
  17. assert.equal(CLIUtils.getOutputCodePath(inputPath), expectedOutputPath);
  18. });
  19. });
  20. });
  21. describe('getOutputSourceMapPath (outputCodePath: string): string', () => {
  22. let expectedOutputSourceMapPath: string = 'test/output/test.js.map',
  23. outputCodePath: string = 'test/output/test.js';
  24. it('should return output path for source map', () => {
  25. assert.equal(CLIUtils.getOutputSourceMapPath(outputCodePath), expectedOutputSourceMapPath);
  26. });
  27. });
  28. describe('getUserConfig (configPath: string): Object', () => {
  29. describe('variant #1: valid config file path', () => {
  30. describe('variant #1: js file with config', () => {
  31. const configDirName: string = 'test/fixtures';
  32. const configFileName: string = 'config.js';
  33. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  34. const expectedResult: TInputOptions = {
  35. compact: true,
  36. selfDefending: false,
  37. sourceMap: true
  38. };
  39. let result: Object;
  40. before(() => {
  41. result = CLIUtils.getUserConfig(configFilePath);
  42. });
  43. it('should return object with user configuration', () => {
  44. assert.deepEqual(result, expectedResult);
  45. });
  46. });
  47. describe('variant #2: json file with config', () => {
  48. const configDirName: string = 'test/fixtures';
  49. const configFileName: string = 'config.json';
  50. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  51. const expectedResult: TInputOptions = {
  52. compact: true,
  53. selfDefending: false,
  54. sourceMap: true
  55. };
  56. let result: Object;
  57. before(() => {
  58. result = CLIUtils.getUserConfig(configFilePath);
  59. });
  60. it('should return object with user configuration', () => {
  61. assert.deepEqual(result, expectedResult);
  62. });
  63. });
  64. });
  65. describe('variant #2: invalid config file path', () => {
  66. const configDirName: string = 'test/fixtures';
  67. const configFileName: string = 'configs.js';
  68. const configFilePath: string = `../../../${configDirName}/${configFileName}`;
  69. let testFunc: () => void;
  70. before(() => {
  71. testFunc = () => CLIUtils.getUserConfig(configFilePath);
  72. });
  73. it('should throw an error if `configFilePath` is not a valid path', () => {
  74. assert.throws(testFunc, ReferenceError);
  75. });
  76. });
  77. });
  78. });