123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150 |
- import * as fs from 'fs';
- import * as mkdirp from 'mkdirp';
- import { assert } from 'chai';
- import { TInputOptions } from '../../../../src/types/options/TInputOptions';
- import { CLIUtils } from '../../../../src/cli/utils/CLIUtils';
- describe('CLIUtils', () => {
- const fileContent: string = 'test';
- const 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` `name` field for current CLI program as object', () => {
- assert.property(CLIUtils.getPackageConfig(), 'name');
- });
- it('should return `package.json` `version` field for current CLI program as object', () => {
- assert.property(CLIUtils.getPackageConfig(), 'version');
- });
- });
- describe('getUserConfig (configPath: string): Object', () => {
- describe('variant #1: valid config file path', () => {
- const configDirName: string = 'test/fixtures';
- const configFileName: string = 'config.js';
- const configFilePath: string = `../../../${configDirName}/${configFileName}`;
- const expectedResult: TInputOptions = {
- compact: true,
- selfDefending: false,
- sourceMap: true
- };
- let result: Object;
- before(() => {
- result = CLIUtils.getUserConfig(configFilePath);
- });
- it('should return object with user configuration', () => {
- assert.deepEqual(result, expectedResult);
- });
- });
- describe('variant #2: invalid config file path', () => {
- const configDirName: string = 'test/fixtures';
- const configFileName: string = 'configs.js';
- const configFilePath: string = `../../../${configDirName}/${configFileName}`;
- let testFunc: () => void;
- before(() => {
- testFunc = () => CLIUtils.getUserConfig(configFilePath);
- });
- it('should throw an error if `configFilePath` is not a valid path', () => {
- assert.throws(testFunc, ReferenceError);
- });
- });
- });
- describe('readSourceCode (inputPath: string): void', () => {
- describe('`inputPath` is a valid path', () => {
- const tmpFileName: string = 'test.js';
- const inputPath: string = `${tmpDir}/${tmpFileName}`;
- let result: string;
- before(() => {
- fs.writeFileSync(inputPath, fileContent);
- result = CLIUtils.readSourceCode(inputPath);
- });
- it('should return content of file', () => {
- assert.equal(result, fileContent);
- });
- after(() => {
- fs.unlinkSync(inputPath);
- });
- });
- describe('`inputPath` is not a valid path', () => {
- const tmpFileName: string = 'test.js';
- const inputPath: string = `${tmpDir}/${tmpFileName}`;
- let testFunc: () => void;
- before(() => {
- testFunc = () => CLIUtils.readSourceCode(inputPath);
- });
- it('should throw an error if `inputPath` is not a valid path', () => {
- assert.throws(testFunc, ReferenceError);
- });
- });
- describe('`inputPath` is a file name has invalid extension', () => {
- const tmpFileName: string = 'test.ts';
- const inputPath: string = `${tmpDir}/${tmpFileName}`;
- let testFunc: () => void;
- before(() => {
- fs.writeFileSync(inputPath, fileContent);
- testFunc = () => CLIUtils.readSourceCode(inputPath);
- });
- it('should throw an error if `inputPath` is a file name has invalid extension', () => {
- assert.throws(testFunc, ReferenceError);
- });
- after(() => {
- fs.unlinkSync(inputPath);
- });
- });
- });
- after(() => {
- fs.rmdirSync(tmpDir);
- });
- });
|