JavaScriptObfuscatorCLI.spec.ts 5.2 KB

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