CLIUtils.spec.ts 3.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import * as fs from 'fs';
  2. import * as mkdirp from 'mkdirp';
  3. import { assert } from 'chai';
  4. import { CLIUtils } from '../../../../src/cli/utils/CLIUtils';
  5. describe('CLIUtils', () => {
  6. const fileContent: string = 'test';
  7. const tmpDir: string = 'test/tmp';
  8. before(() => {
  9. mkdirp.sync(tmpDir);
  10. });
  11. describe('getOutputCodePath (outputPath: string, inputPath: string): string', () => {
  12. let expectedInputPath: string = 'test/input/test-obfuscated.js',
  13. inputPath: string = 'test/input/test.js',
  14. outputPath: string = 'test/output/test.js';
  15. it('should return `outputPath` if this path is set', () => {
  16. assert.equal(CLIUtils.getOutputCodePath(outputPath, inputPath), outputPath);
  17. });
  18. it('should output path based on `inputPath` if `outputPath` is not set', () => {
  19. assert.equal(CLIUtils.getOutputCodePath('', inputPath), expectedInputPath);
  20. });
  21. });
  22. describe('getOutputSourceMapPath (outputCodePath: string): string', () => {
  23. let expectedOutputSourceMapPath: string = 'test/output/test.js.map',
  24. outputCodePath: string = 'test/output/test.js';
  25. it('should return output path for source map', () => {
  26. assert.equal(CLIUtils.getOutputSourceMapPath(outputCodePath), expectedOutputSourceMapPath);
  27. });
  28. });
  29. describe('getPackageConfig (): IPackageConfig', () => {
  30. it('should return `package.json` `name` field for current CLI program as object', () => {
  31. assert.property(CLIUtils.getPackageConfig(), 'name');
  32. });
  33. it('should return `package.json` `version` field for current CLI program as object', () => {
  34. assert.property(CLIUtils.getPackageConfig(), 'version');
  35. });
  36. });
  37. describe('validateInputPath (inputPath: string): void', () => {
  38. describe('`inputPath` is a valid path', () => {
  39. const tmpFileName: string = 'test.js';
  40. const inputPath: string = `${tmpDir}/${tmpFileName}`;
  41. before(() => {
  42. fs.writeFileSync(inputPath, fileContent);
  43. });
  44. it('shouldn\'t throw an error if `inputPath` is a valid path', () => {
  45. assert.doesNotThrow(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
  46. });
  47. after(() => {
  48. fs.unlinkSync(inputPath);
  49. });
  50. });
  51. describe('`inputPath` is not a valid path', () => {
  52. const tmpFileName: string = 'test.js';
  53. const inputPath: string = `${tmpDir}/${tmpFileName}`;
  54. it('should throw an error if `inputPath` is not a valid path', () => {
  55. assert.throws(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
  56. });
  57. });
  58. describe('`inputPath` is a file name has invalid extension', () => {
  59. const tmpFileName: string = 'test.ts';
  60. const inputPath: string = `${tmpDir}/${tmpFileName}`;
  61. before(() => {
  62. fs.writeFileSync(inputPath, fileContent);
  63. });
  64. it('should throw an error if `inputPath` is a file name has invalid extension', () => {
  65. assert.throws(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
  66. });
  67. after(() => {
  68. fs.unlinkSync(inputPath);
  69. });
  70. });
  71. });
  72. after(() => {
  73. fs.rmdirSync(tmpDir);
  74. });
  75. });