123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133 |
- import * as fs from 'fs';
- import * as mkdirp from 'mkdirp';
- import * as sinon from 'sinon';
- import { StdoutWriteMock } from "../test/mocks/StdoutWriteMock";
- import { JavaScriptObfuscator } from "../src/JavaScriptObfuscator";
- const assert: Chai.AssertStatic = require('chai').assert;
- describe('JavaScriptObfuscatorCLI', function (): void {
- let fixturesDirName: string = 'test/fixtures',
- fixtureFileName: string = 'sample.js',
- fixtureFilePath: string = `${fixturesDirName}/${fixtureFileName}`,
- outputDirName: string = 'test/tmp',
- outputFileName: string = 'sample-obfuscated.js',
- outputFilePath: string = `${outputDirName}/${outputFileName}`;
- this.timeout(5000);
- describe('run (): void', () => {
- before(() => {
- mkdirp.sync(outputDirName);
- });
- describe('--output option is set', () => {
- it('should creates file with obfuscated JS code in --output directory', () => {
- JavaScriptObfuscator.runCLI([
- 'node',
- 'javascript-obfuscator',
- fixtureFilePath,
- '--output',
- outputFilePath,
- '--compact',
- 'true',
- '--selfDefending',
- '0'
- ]);
- assert.equal(fs.existsSync(outputFilePath), true);
- });
- afterEach(() => {
- fs.unlinkSync(outputFilePath);
- });
- });
- describe('--output option is not set', () => {
- it(`should creates file called \`${outputFileName}\` with obfuscated JS code in \`${fixturesDirName}\` directory`, () => {
- let outputFixturesFilePath: string = `${fixturesDirName}/${outputFileName}`;
- JavaScriptObfuscator.runCLI([
- 'node',
- 'javascript-obfuscator',
- fixtureFilePath
- ]);
- assert.equal(fs.existsSync(outputFixturesFilePath), true);
- fs.unlinkSync(outputFixturesFilePath);
- });
- it(`should throw an error if input path is not a valid file path`, () => {
- assert.throws(() => JavaScriptObfuscator.runCLI([
- 'node',
- 'javascript-obfuscator',
- 'wrong/file/path'
- ]), ReferenceError);
- });
- it(`should throw an error if input file extension is not a .js extension`, () => {
- let outputWrongExtensionFileName: string = 'sample-obfuscated.ts',
- outputWrongExtensionFilePath: string = `${outputDirName}/${outputWrongExtensionFileName}`;
- fs.writeFileSync(outputWrongExtensionFilePath, 'data');
- assert.throws(() => JavaScriptObfuscator.runCLI([
- 'node',
- 'javascript-obfuscator',
- outputWrongExtensionFilePath
- ]), ReferenceError);
- fs.unlinkSync(outputWrongExtensionFilePath);
- });
- });
- describe('help output', () => {
- let callback: Sinon.SinonSpy,
- stdoutWriteMock: StdoutWriteMock;
- beforeEach(() => {
- callback = sinon.spy(console, 'log');
- stdoutWriteMock = new StdoutWriteMock(process.stdout.write);
- });
- it('should print `console.log` help if `--help` option is set', () => {
- stdoutWriteMock.mute();
- JavaScriptObfuscator.runCLI([
- 'node',
- 'javascript-obfuscator',
- fixtureFilePath,
- '--help'
- ]);
- stdoutWriteMock.restore();
- assert.equal(callback.called, true);
- });
- it('should print `console.log` help if no options is passed', () => {
- stdoutWriteMock.mute();
- JavaScriptObfuscator.runCLI([
- 'node',
- 'javascript-obfuscator'
- ]);
- stdoutWriteMock.restore();
- assert.equal(callback.called, true);
- });
- afterEach(() => {
- callback.restore();
- });
- });
- after(() => {
- fs.rmdirSync(outputDirName);
- });
- });
- });
|