JavaScriptObfuscatorCLI.spec.ts 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. import * as fs from 'fs';
  2. import * as mkdirp from 'mkdirp';
  3. import * as sinon from 'sinon';
  4. import { JavaScriptObfuscator } from "../../src/JavaScriptObfuscator";
  5. const assert: Chai.AssertStatic = require('chai').assert;
  6. describe('JavaScriptObfuscatorCLI', function (): void {
  7. let fixturesDirName: string = 'test/fixtures',
  8. fixtureFileName: string = 'sample.js',
  9. fixtureFilePath: string = `${fixturesDirName}/${fixtureFileName}`,
  10. outputDirName: string = 'test/tmp',
  11. outputFileName: string = 'sample-obfuscated.js',
  12. outputFilePath: string = `${outputDirName}/${outputFileName}`;
  13. this.timeout(5000);
  14. describe('run (): void', () => {
  15. before(() => {
  16. mkdirp.sync(outputDirName);
  17. });
  18. describe('--output option is set', () => {
  19. it('should creates file with obfuscated JS code in --output directory', () => {
  20. JavaScriptObfuscator.runCLI([
  21. 'node',
  22. 'javascript-obfuscator',
  23. fixtureFilePath,
  24. '--output',
  25. outputFilePath,
  26. '--compact',
  27. 'true',
  28. '--selfDefending',
  29. '0'
  30. ]);
  31. assert.equal(fs.existsSync(outputFilePath), true);
  32. });
  33. afterEach(() => {
  34. fs.unlinkSync(outputFilePath);
  35. });
  36. });
  37. describe('--output option is not set', () => {
  38. it(`should creates file called \`${outputFileName}\` with obfuscated JS code in \`${fixturesDirName}\` directory`, () => {
  39. let outputFixturesFilePath: string = `${fixturesDirName}/${outputFileName}`;
  40. JavaScriptObfuscator.runCLI([
  41. 'node',
  42. 'javascript-obfuscator',
  43. fixtureFilePath
  44. ]);
  45. assert.equal(fs.existsSync(outputFixturesFilePath), true);
  46. fs.unlinkSync(outputFixturesFilePath);
  47. });
  48. it(`should throw an error if input path is not a valid file path`, () => {
  49. assert.throws(() => JavaScriptObfuscator.runCLI([
  50. 'node',
  51. 'javascript-obfuscator',
  52. 'wrong/file/path'
  53. ]), ReferenceError);
  54. });
  55. it(`should throw an error if input file extension is not a .js extension`, () => {
  56. let outputWrongExtensionFileName: string = 'sample-obfuscated.ts',
  57. outputWrongExtensionFilePath: string = `${outputDirName}/${outputWrongExtensionFileName}`;
  58. fs.writeFileSync(outputWrongExtensionFilePath, 'data');
  59. assert.throws(() => JavaScriptObfuscator.runCLI([
  60. 'node',
  61. 'javascript-obfuscator',
  62. outputWrongExtensionFilePath
  63. ]), ReferenceError);
  64. fs.unlinkSync(outputWrongExtensionFilePath);
  65. });
  66. });
  67. describe('--sourceMap option is set', () => {
  68. let outputSourceMapPath: string = `${outputFilePath}.map`;
  69. it('should creates file with source map in the same directory as output file', () => {
  70. JavaScriptObfuscator.runCLI([
  71. 'node',
  72. 'javascript-obfuscator',
  73. fixtureFilePath,
  74. '--output',
  75. outputFilePath,
  76. '--compact',
  77. 'true',
  78. '--selfDefending',
  79. '0',
  80. '--sourceMap',
  81. 'true'
  82. ]);
  83. assert.equal(fs.existsSync(outputSourceMapPath), true);
  84. const content: string = fs.readFileSync(outputSourceMapPath, { encoding: 'utf8' }),
  85. sourceMap: any = JSON.parse(content);
  86. assert.property(sourceMap, 'version');
  87. assert.property(sourceMap, 'sources');
  88. assert.property(sourceMap, 'names');
  89. });
  90. afterEach(() => {
  91. fs.unlinkSync(outputFilePath);
  92. fs.unlinkSync(outputSourceMapPath);
  93. });
  94. });
  95. describe('help output', () => {
  96. let callback: sinon.SinonSpy;
  97. beforeEach(() => {
  98. callback = sinon.spy(console, 'log');
  99. });
  100. it('should print `console.log` help if `--help` option is set', () => {
  101. JavaScriptObfuscator.runCLI([
  102. 'node',
  103. 'javascript-obfuscator',
  104. fixtureFilePath,
  105. '--help'
  106. ]);
  107. assert.equal(callback.called, true);
  108. });
  109. it('should print `console.log` help if no options is passed', () => {
  110. JavaScriptObfuscator.runCLI([
  111. 'node',
  112. 'javascript-obfuscator'
  113. ]);
  114. assert.equal(callback.called, true);
  115. });
  116. afterEach(() => {
  117. callback.restore();
  118. });
  119. });
  120. after(() => {
  121. fs.rmdirSync(outputDirName);
  122. });
  123. });
  124. });