Ver código fonte

CLIUtils tests

sanex3339 8 anos atrás
pai
commit
f53b307fb0

+ 14 - 16
dist/index.js

@@ -1145,21 +1145,8 @@ var CLIUtils = function () {
     }
 
     _createClass(CLIUtils, null, [{
-        key: 'getInputPath',
-        value: function getInputPath(argv) {
-            var inputPath = argv[0];
-            if (!CLIUtils.isFilePath(inputPath)) {
-                throw new ReferenceError('First argument must be a valid file path');
-            }
-            if (!Utils_1.Utils.arrayContains(CLIUtils.availableInputExtensions, path.extname(inputPath))) {
-                throw new ReferenceError('Input file must have .js extension');
-            }
-            return inputPath;
-        }
-    }, {
         key: 'getOutputCodePath',
-        value: function getOutputCodePath(commands, inputPath) {
-            var outputPath = commands.output;
+        value: function getOutputCodePath(outputPath, inputPath) {
             if (outputPath) {
                 return outputPath;
             }
@@ -1193,6 +1180,16 @@ var CLIUtils = function () {
         value: function readFile(inputPath) {
             return fs.readFileSync(inputPath, CLIUtils.encoding);
         }
+    }, {
+        key: 'validateInputPath',
+        value: function validateInputPath(inputPath) {
+            if (!CLIUtils.isFilePath(inputPath)) {
+                throw new ReferenceError('Given input path must be a valid file path');
+            }
+            if (!Utils_1.Utils.arrayContains(CLIUtils.availableInputExtensions, path.extname(inputPath))) {
+                throw new ReferenceError('Input file must have .js extension');
+            }
+        }
     }, {
         key: 'writeFile',
         value: function writeFile(outputPath, data) {
@@ -1247,7 +1244,8 @@ var JavaScriptObfuscatorCLI = function () {
                 this.commands.outputHelp();
                 return;
             }
-            this.inputPath = CLIUtils_1.CLIUtils.getInputPath(this.arguments);
+            this.inputPath = this.arguments[0];
+            CLIUtils_1.CLIUtils.validateInputPath(this.inputPath);
             this.getData();
             this.processData();
         }
@@ -1289,7 +1287,7 @@ var JavaScriptObfuscatorCLI = function () {
         key: 'processData',
         value: function processData() {
             var options = this.buildOptions(),
-                outputCodePath = CLIUtils_1.CLIUtils.getOutputCodePath(this.commands, this.inputPath);
+                outputCodePath = CLIUtils_1.CLIUtils.getOutputCodePath(this.commands.output, this.inputPath);
             if (options.sourceMap) {
                 this.processDataWithSourceMap(outputCodePath, options);
             } else {

+ 15 - 23
src/cli/CLIUtils.ts

@@ -1,4 +1,3 @@
-import * as commander from 'commander';
 import * as fs from 'fs';
 import * as mkdirp from 'mkdirp';
 import * as path from 'path';
@@ -21,31 +20,11 @@ export class CLIUtils {
     private static encoding: BufferEncoding = 'utf8';
 
     /**
-     * @param argv
-     * @returns {string}
-     */
-    public static getInputPath (argv: string[]): string {
-        let inputPath: string = argv[0];
-
-        if (!CLIUtils.isFilePath(inputPath)) {
-            throw new ReferenceError(`First argument must be a valid file path`);
-        }
-
-        if (!Utils.arrayContains(CLIUtils.availableInputExtensions, path.extname(inputPath))) {
-            throw new ReferenceError(`Input file must have .js extension`);
-        }
-
-        return inputPath;
-    }
-
-    /**
-     * @param commands
+     * @param outputPath
      * @param inputPath
      * @returns {string}
      */
-    public static getOutputCodePath (commands: commander.ICommand, inputPath: string): string {
-        let outputPath: string = (<any>commands).output;
-
+    public static getOutputCodePath (outputPath: string, inputPath: string): string {
         if (outputPath) {
             return outputPath;
         }
@@ -106,6 +85,19 @@ export class CLIUtils {
         return fs.readFileSync(inputPath, CLIUtils.encoding);
     }
 
+    /**
+     * @param inputPath
+     */
+    public static validateInputPath (inputPath: string): void {
+        if (!CLIUtils.isFilePath(inputPath)) {
+            throw new ReferenceError(`Given input path must be a valid file path`);
+        }
+
+        if (!Utils.arrayContains(CLIUtils.availableInputExtensions, path.extname(inputPath))) {
+            throw new ReferenceError(`Input file must have .js extension`);
+        }
+    }
+
     /**
      * @param outputPath
      * @param data

+ 3 - 2
src/cli/JavaScriptObfuscatorCLI.ts

@@ -90,7 +90,8 @@ export class JavaScriptObfuscatorCLI {
             return;
         }
 
-        this.inputPath = CLIUtils.getInputPath(this.arguments);
+        this.inputPath = this.arguments[0];
+        CLIUtils.validateInputPath(this.inputPath);
 
         this.getData();
         this.processData();
@@ -156,7 +157,7 @@ export class JavaScriptObfuscatorCLI {
 
     private processData (): void {
         let options: IObfuscatorOptions = this.buildOptions(),
-            outputCodePath: string = CLIUtils.getOutputCodePath(this.commands, this.inputPath);
+            outputCodePath: string = CLIUtils.getOutputCodePath((<any>this.commands).output, this.inputPath);
 
         if (options.sourceMap) {
             this.processDataWithSourceMap(outputCodePath, options);

+ 0 - 0
test/unit-tests/ObfuscationResultWrapper.spec.ts → test/unit-tests/ObfuscationResult.spec.ts


+ 81 - 0
test/unit-tests/cli/CLIUtils.spec.ts

@@ -0,0 +1,81 @@
+import * as fs from 'fs';
+import * as mkdirp from 'mkdirp';
+
+import { CLIUtils } from '../../../src/cli/CLIUtils';
+
+const assert: Chai.AssertStatic = require('chai').assert;
+
+describe('CLIUtils', () => {
+    const fileContent: string = 'test',
+        tmpDir: string = 'test/tmp';
+
+    before(() => {
+        mkdirp.sync(tmpDir);
+    });
+
+    describe('getOutputCodePath (outputPath: string, inputPath: string): string', () => {
+        let expectedInputPath: string = 'test/input/test-obfuscated.js',
+            inputPath: string = 'test/input/test.js',
+            outputPath: string = 'test/output/test.js';
+
+        it('should return `outputPath` if this path is set', () => {
+            assert.equal(CLIUtils.getOutputCodePath(outputPath, inputPath), outputPath);
+        });
+
+        it('should output path based on `inputPath` if `outputPath` is not set', () => {
+            assert.equal(CLIUtils.getOutputCodePath('', inputPath), expectedInputPath);
+        });
+    });
+
+    describe('getOutputSourceMapPath (outputCodePath: string): string', () => {
+        let expectedOutputSourceMapPath: string = 'test/output/test.js.map',
+            outputCodePath: string = 'test/output/test.js';
+
+        it('should return output path for source map', () => {
+            assert.equal(CLIUtils.getOutputSourceMapPath(outputCodePath), expectedOutputSourceMapPath);
+        });
+    });
+
+    describe('getPackageConfig (): IPackageConfig', () => {
+        it('should return `package.json` content for current CLI program as object', () => {
+            assert.property(CLIUtils.getPackageConfig(), 'name');
+            assert.property(CLIUtils.getPackageConfig(), 'version');
+        });
+    });
+
+    describe('validateInputPath (inputPath: string): void', () => {
+        let inputPath: string,
+            tmpFileName: string;
+
+        it('shouldn\'t throw an error if `inputPath` is a valid path', () => {
+            tmpFileName = 'test.js';
+            inputPath = `${tmpDir}/${tmpFileName}`;
+            fs.writeFileSync(inputPath, fileContent);
+
+            assert.doesNotThrow(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
+
+            fs.unlinkSync(inputPath);
+        });
+
+        it('should throw an error if `inputPath` is not a valid path', () => {
+            tmpFileName = 'test.js';
+            inputPath = `${tmpDir}/${tmpFileName}`;
+
+            assert.throws(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
+        });
+
+        it('should throw an error if `inputPath` is a file name has invalid extension', () => {
+            tmpFileName = 'test.ts';
+            inputPath = `${tmpDir}/${tmpFileName}`;
+            fs.writeFileSync(inputPath, fileContent);
+
+            assert.throws(() => CLIUtils.validateInputPath(inputPath), ReferenceError);
+
+            fs.unlinkSync(inputPath);
+        });
+    });
+
+    after(() => {
+        fs.rmdirSync(tmpDir);
+    });
+});