JavaScriptObfuscatorCLI.spec.ts 4.4 KB

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