|
@@ -1,4 +1,5 @@
|
|
|
import * as fs from 'fs';
|
|
|
+import * as mkdirp from 'mkdirp';
|
|
|
|
|
|
import { JavaScriptObfuscator } from "../src/JavaScriptObfuscator";
|
|
|
|
|
@@ -6,14 +7,17 @@ const assert: Chai.AssertStatic = require('chai').assert;
|
|
|
|
|
|
describe('JavaScriptObfuscatorCLI', () => {
|
|
|
let fixturesDirName: string = 'test/fixtures',
|
|
|
- tmpDirName: string = 'test/tmp',
|
|
|
fixtureFileName: string = 'sample.js',
|
|
|
fixtureFilePath: string = `${fixturesDirName}/${fixtureFileName}`,
|
|
|
+ outputDirName: string = 'test/tmp',
|
|
|
outputFileName: string = 'sample-obfuscated.js',
|
|
|
- outputFixturesFilePath: string = `${fixturesDirName}/${outputFileName}`,
|
|
|
- outputFilePath: string = `${tmpDirName}/${outputFileName}`;
|
|
|
+ outputFilePath: string = `${outputDirName}/${outputFileName}`;
|
|
|
|
|
|
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([
|
|
@@ -21,7 +25,11 @@ describe('JavaScriptObfuscatorCLI', () => {
|
|
|
'javascript-obfuscator',
|
|
|
fixtureFilePath,
|
|
|
'--output',
|
|
|
- outputFilePath
|
|
|
+ outputFilePath,
|
|
|
+ '--compact',
|
|
|
+ 'true',
|
|
|
+ '--selfDefending',
|
|
|
+ '0'
|
|
|
]);
|
|
|
|
|
|
assert.equal(fs.existsSync(outputFilePath), true);
|
|
@@ -29,12 +37,13 @@ describe('JavaScriptObfuscatorCLI', () => {
|
|
|
|
|
|
afterEach(() => {
|
|
|
fs.unlinkSync(outputFilePath);
|
|
|
- fs.rmdirSync(tmpDirName);
|
|
|
});
|
|
|
});
|
|
|
|
|
|
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',
|
|
@@ -42,11 +51,36 @@ describe('JavaScriptObfuscatorCLI', () => {
|
|
|
]);
|
|
|
|
|
|
assert.equal(fs.existsSync(outputFixturesFilePath), true);
|
|
|
- });
|
|
|
|
|
|
- afterEach(() => {
|
|
|
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);
|
|
|
+ });
|
|
|
+ });
|
|
|
+
|
|
|
+ after(() => {
|
|
|
+ fs.rmdirSync(outputDirName);
|
|
|
});
|
|
|
});
|
|
|
});
|