From f53b307fb0768a89a93d6588d556dca6374918e9 Mon Sep 17 00:00:00 2001 From: sanex3339 Date: Thu, 21 Jul 2016 13:59:55 +0300 Subject: [PATCH] CLIUtils tests --- dist/index.js | 30 ++++--- src/cli/CLIUtils.ts | 38 ++++----- src/cli/JavaScriptObfuscatorCLI.ts | 5 +- ...pper.spec.ts => ObfuscationResult.spec.ts} | 0 test/unit-tests/cli/CLIUtils.spec.ts | 81 +++++++++++++++++++ 5 files changed, 113 insertions(+), 41 deletions(-) rename test/unit-tests/{ObfuscationResultWrapper.spec.ts => ObfuscationResult.spec.ts} (100%) create mode 100644 test/unit-tests/cli/CLIUtils.spec.ts diff --git a/dist/index.js b/dist/index.js index 48a3fc43..4fb8d1a2 100644 --- a/dist/index.js +++ b/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 { diff --git a/src/cli/CLIUtils.ts b/src/cli/CLIUtils.ts index dcbd6367..6b0bbc75 100644 --- a/src/cli/CLIUtils.ts +++ b/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 = (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 diff --git a/src/cli/JavaScriptObfuscatorCLI.ts b/src/cli/JavaScriptObfuscatorCLI.ts index 33d506a2..5eaf4abe 100644 --- a/src/cli/JavaScriptObfuscatorCLI.ts +++ b/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((this.commands).output, this.inputPath); if (options.sourceMap) { this.processDataWithSourceMap(outputCodePath, options); diff --git a/test/unit-tests/ObfuscationResultWrapper.spec.ts b/test/unit-tests/ObfuscationResult.spec.ts similarity index 100% rename from test/unit-tests/ObfuscationResultWrapper.spec.ts rename to test/unit-tests/ObfuscationResult.spec.ts diff --git a/test/unit-tests/cli/CLIUtils.spec.ts b/test/unit-tests/cli/CLIUtils.spec.ts new file mode 100644 index 00000000..7a6f56e6 --- /dev/null +++ b/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); + }); +});