CLIUtils.spec.ts 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import * as fs from 'fs';
  2. import * as mkdirp from 'mkdirp';
  3. import { assert } from 'chai';
  4. import { CLIUtils } from '../../../../src/cli/CLIUtils';
  5. describe('CLIUtils', () => {
  6. const fileContent: string = 'test',
  7. 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` content for current CLI program as object', () => {
  31. assert.property(CLIUtils.getPackageConfig(), 'name');
  32. assert.property(CLIUtils.getPackageConfig(), 'version');
  33. });
  34. });
  35. describe('validateInputPath (inputPath: string): void', () => {
  36. let inputPath: string,
  37. tmpFileName: string;
  38. it('shouldn\'t throw an error if `inputPath` is a valid path', () => {
  39. tmpFileName = 'test.js';
  40. inputPath = `${tmpDir}/${tmpFileName}`;
  41. fs.writeFileSync(inputPath, fileContent);
  42. assert.doesNotThrow(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
  43. fs.unlinkSync(inputPath);
  44. });
  45. it('should throw an error if `inputPath` is not a valid path', () => {
  46. tmpFileName = 'test.js';
  47. inputPath = `${tmpDir}/${tmpFileName}`;
  48. assert.throws(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
  49. });
  50. it('should throw an error if `inputPath` is a file name has invalid extension', () => {
  51. tmpFileName = 'test.ts';
  52. inputPath = `${tmpDir}/${tmpFileName}`;
  53. fs.writeFileSync(inputPath, fileContent);
  54. assert.throws(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
  55. fs.unlinkSync(inputPath);
  56. });
  57. });
  58. after(() => {
  59. fs.rmdirSync(tmpDir);
  60. });
  61. });