Explorar el Código

CLI tests refactoring

sanex3339 hace 9 años
padre
commit
599429ddfc
Se han modificado 3 ficheros con 41 adiciones y 10 borrados
  1. 0 1
      dist/index.js
  2. 0 2
      src/cli/JavaScriptObfuscatorCLI.ts
  3. 41 7
      test/JavaScriptObfuscatorCLI.spec.ts

+ 0 - 1
dist/index.js

@@ -1084,7 +1084,6 @@ var JavaScriptObfuscatorCLI = function () {
             this.configureCommands();
             this.configureCommands();
             if (!this.arguments.length || this.arguments.indexOf('--help') >= 0) {
             if (!this.arguments.length || this.arguments.indexOf('--help') >= 0) {
                 this.commands.outputHelp();
                 this.commands.outputHelp();
-                return;
             }
             }
             this.inputPath = this.getInputPath();
             this.inputPath = this.getInputPath();
             this.getData();
             this.getData();

+ 0 - 2
src/cli/JavaScriptObfuscatorCLI.ts

@@ -94,8 +94,6 @@ export class JavaScriptObfuscatorCLI {
 
 
         if (!this.arguments.length || this.arguments.indexOf('--help') >= 0) {
         if (!this.arguments.length || this.arguments.indexOf('--help') >= 0) {
             this.commands.outputHelp();
             this.commands.outputHelp();
-
-            return;
         }
         }
 
 
         this.inputPath = this.getInputPath();
         this.inputPath = this.getInputPath();

+ 41 - 7
test/JavaScriptObfuscatorCLI.spec.ts

@@ -1,4 +1,5 @@
 import * as fs from 'fs';
 import * as fs from 'fs';
+import * as mkdirp from 'mkdirp';
 
 
 import { JavaScriptObfuscator } from "../src/JavaScriptObfuscator";
 import { JavaScriptObfuscator } from "../src/JavaScriptObfuscator";
 
 
@@ -6,14 +7,17 @@ const assert: Chai.AssertStatic = require('chai').assert;
 
 
 describe('JavaScriptObfuscatorCLI', () => {
 describe('JavaScriptObfuscatorCLI', () => {
     let fixturesDirName: string = 'test/fixtures',
     let fixturesDirName: string = 'test/fixtures',
-        tmpDirName: string = 'test/tmp',
         fixtureFileName: string = 'sample.js',
         fixtureFileName: string = 'sample.js',
         fixtureFilePath: string = `${fixturesDirName}/${fixtureFileName}`,
         fixtureFilePath: string = `${fixturesDirName}/${fixtureFileName}`,
+        outputDirName: string = 'test/tmp',
         outputFileName: string = 'sample-obfuscated.js',
         outputFileName: string = 'sample-obfuscated.js',
-        outputFixturesFilePath: string = `${fixturesDirName}/${outputFileName}`,
-        outputFilePath: string = `${tmpDirName}/${outputFileName}`;
+        outputFilePath: string = `${outputDirName}/${outputFileName}`;
 
 
     describe('run (): void', () => {
     describe('run (): void', () => {
+        before(() => {
+            mkdirp.sync(outputDirName);
+        });
+
         describe('--output option is set', () => {
         describe('--output option is set', () => {
             it('should creates file with obfuscated JS code in --output directory', () => {
             it('should creates file with obfuscated JS code in --output directory', () => {
                 JavaScriptObfuscator.runCLI([
                 JavaScriptObfuscator.runCLI([
@@ -21,7 +25,11 @@ describe('JavaScriptObfuscatorCLI', () => {
                     'javascript-obfuscator',
                     'javascript-obfuscator',
                     fixtureFilePath,
                     fixtureFilePath,
                     '--output',
                     '--output',
-                    outputFilePath
+                    outputFilePath,
+                    '--compact',
+                    'true',
+                    '--selfDefending',
+                    '0'
                 ]);
                 ]);
 
 
                 assert.equal(fs.existsSync(outputFilePath), true);
                 assert.equal(fs.existsSync(outputFilePath), true);
@@ -29,12 +37,13 @@ describe('JavaScriptObfuscatorCLI', () => {
 
 
             afterEach(() => {
             afterEach(() => {
                 fs.unlinkSync(outputFilePath);
                 fs.unlinkSync(outputFilePath);
-                fs.rmdirSync(tmpDirName);
             });
             });
         });
         });
 
 
         describe('—output option is not set', () => {
         describe('—output option is not set', () => {
             it(`should creates file called \`${outputFileName}\` with obfuscated JS code in \`${fixturesDirName}\` directory`, () => {
             it(`should creates file called \`${outputFileName}\` with obfuscated JS code in \`${fixturesDirName}\` directory`, () => {
+                let outputFixturesFilePath: string = `${fixturesDirName}/${outputFileName}`;
+
                 JavaScriptObfuscator.runCLI([
                 JavaScriptObfuscator.runCLI([
                     'node',
                     'node',
                     'javascript-obfuscator',
                     'javascript-obfuscator',
@@ -42,11 +51,36 @@ describe('JavaScriptObfuscatorCLI', () => {
                 ]);
                 ]);
 
 
                 assert.equal(fs.existsSync(outputFixturesFilePath), true);
                 assert.equal(fs.existsSync(outputFixturesFilePath), true);
-            });
 
 
-            afterEach(() => {
                 fs.unlinkSync(outputFixturesFilePath);
                 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);
         });
         });
     });
     });
 });
 });