mirror of
https://github.com/javascript-obfuscator/javascript-obfuscator
synced 2026-08-09 12:42:29 +00:00
Refactoring of CLI utils
This commit is contained in:
@@ -5,35 +5,6 @@ import { TInputOptions } from '../../../../src/types/options/TInputOptions';
|
||||
import { CLIUtils } from '../../../../src/cli/utils/CLIUtils';
|
||||
|
||||
describe('CLIUtils', () => {
|
||||
describe('getOutputCodePath', () => {
|
||||
describe('Variant #1: base input path', () => {
|
||||
let expectedOutputPath: string = 'test/input/test-obfuscated.js',
|
||||
inputPath: string = 'test/input/test.js';
|
||||
|
||||
it('should output path based on `inputPath`', () => {
|
||||
assert.equal(CLIUtils.getOutputCodePath(inputPath), expectedOutputPath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #2: relative input path with dot', () => {
|
||||
let expectedOutputPath: string = 'input-obfuscated.js',
|
||||
inputPath: string = './input.js';
|
||||
|
||||
it('should output path based on `inputPath`', () => {
|
||||
assert.equal(CLIUtils.getOutputCodePath(inputPath), expectedOutputPath);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('getOutputSourceMapPath', () => {
|
||||
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('getUserConfig', () => {
|
||||
describe('Variant #1: valid config file path', () => {
|
||||
describe('Variant #1: js file with config', () => {
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
import { assert } from 'chai';
|
||||
import * as mkdirp from 'mkdirp';
|
||||
import * as rimraf from 'rimraf';
|
||||
import * as fs from "fs";
|
||||
|
||||
import { ObfuscatedCodeWriter } from '../../../../src/cli/utils/ObfuscatedCodeWriter';
|
||||
|
||||
describe('ObfuscatedCodeWriter', () => {
|
||||
const tmpDirectoryPath: string = 'test/tmp';
|
||||
|
||||
describe('getOutputCodePath', () => {
|
||||
before(() => {
|
||||
mkdirp.sync(`${tmpDirectoryPath}/input/`);
|
||||
fs.writeFileSync(
|
||||
`${tmpDirectoryPath}/input/test-input.js`,
|
||||
'var foo = 1;'
|
||||
);
|
||||
});
|
||||
|
||||
describe('Variant #1: raw input path is a file path, raw output path is a file path', () => {
|
||||
const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
|
||||
const rawInputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
|
||||
const rawOutputPath: string = `${tmpDirectoryPath}/output/test-output.js`;
|
||||
const expectedOutputCodePath: string = `${tmpDirectoryPath}/output/test-output.js`;
|
||||
|
||||
let outputCodePath: string;
|
||||
|
||||
before(() => {
|
||||
const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
|
||||
rawInputPath,
|
||||
{
|
||||
output: rawOutputPath
|
||||
}
|
||||
);
|
||||
outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
|
||||
});
|
||||
|
||||
it('should return output path that equals to passed output file path', () => {
|
||||
assert.equal(outputCodePath, expectedOutputCodePath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #2: raw input path is a file path, raw output path is a directory path', () => {
|
||||
const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
|
||||
const rawInputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
|
||||
const rawOutputPath: string = `${tmpDirectoryPath}/output`;
|
||||
const expectedOutputCodePath: string = `${tmpDirectoryPath}/output/test-input.js`;
|
||||
|
||||
let outputCodePath: string;
|
||||
|
||||
before(() => {
|
||||
const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
|
||||
rawInputPath,
|
||||
{
|
||||
output: rawOutputPath
|
||||
}
|
||||
);
|
||||
outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
|
||||
});
|
||||
|
||||
it('should return output path that equals to passed output directory with file name from actual file path', () => {
|
||||
assert.equal(outputCodePath, expectedOutputCodePath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #3: raw input path is a directory path, raw output path is a file path', () => {
|
||||
const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
|
||||
const rawInputPath: string = `${tmpDirectoryPath}/input`;
|
||||
const rawOutputPath: string = `${tmpDirectoryPath}/output/test-output.js`;
|
||||
|
||||
let testFunc: () => string;
|
||||
|
||||
before(() => {
|
||||
const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
|
||||
rawInputPath,
|
||||
{
|
||||
output: rawOutputPath
|
||||
}
|
||||
);
|
||||
testFunc = () => obfuscatedCodeWriter.getOutputCodePath(inputPath);
|
||||
});
|
||||
|
||||
it('should throw an error if output path is a file path', () => {
|
||||
assert.throws(testFunc, Error);
|
||||
});
|
||||
});
|
||||
|
||||
describe('Variant #4: raw input path is a directory path, raw output path is a directory path', () => {
|
||||
const inputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
|
||||
const rawInputPath: string = `${tmpDirectoryPath}/input`;
|
||||
const rawOutputPath: string = `${tmpDirectoryPath}/output`;
|
||||
const expectedOutputCodePath: string = `${tmpDirectoryPath}/output/${tmpDirectoryPath}/input/test-input.js`;
|
||||
|
||||
let outputCodePath: string;
|
||||
|
||||
before(() => {
|
||||
const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
|
||||
rawInputPath,
|
||||
{
|
||||
output: rawOutputPath
|
||||
}
|
||||
);
|
||||
outputCodePath = obfuscatedCodeWriter.getOutputCodePath(inputPath);
|
||||
});
|
||||
|
||||
it('should return output path that contains raw output path and actual file input path', () => {
|
||||
assert.equal(outputCodePath, expectedOutputCodePath);
|
||||
});
|
||||
});
|
||||
|
||||
after(() => {
|
||||
rimraf.sync(tmpDirectoryPath);
|
||||
});
|
||||
});
|
||||
|
||||
describe('getOutputSourceMapPath', () => {
|
||||
const rawInputPath: string = `${tmpDirectoryPath}/input/test-input.js`;
|
||||
const rawOutputPath: string = `${tmpDirectoryPath}/output/test-output.js`;
|
||||
const outputCodePath: string = `${tmpDirectoryPath}/output/test-output.js`;
|
||||
const expectedOutputSourceMapPath: string = `${tmpDirectoryPath}/output/test-output.js.map`;
|
||||
|
||||
let outputSourceMapPath: string;
|
||||
|
||||
before(() => {
|
||||
const obfuscatedCodeWriter: ObfuscatedCodeWriter = new ObfuscatedCodeWriter(
|
||||
rawInputPath,
|
||||
{
|
||||
output: rawOutputPath
|
||||
}
|
||||
);
|
||||
outputSourceMapPath = obfuscatedCodeWriter.getOutputSourceMapPath(outputCodePath);
|
||||
});
|
||||
|
||||
it('should return output path for source map', () => {
|
||||
assert.equal(outputSourceMapPath, expectedOutputSourceMapPath);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -32,7 +32,7 @@ describe('SourceCodeReader', () => {
|
||||
|
||||
before(() => {
|
||||
fs.writeFileSync(inputPath, fileContent);
|
||||
filesData = new SourceCodeReader({}).readSourceCode(inputPath);
|
||||
filesData = new SourceCodeReader(inputPath, {}).readSourceCode();
|
||||
});
|
||||
|
||||
it('should return valid files data', () => {
|
||||
@@ -51,7 +51,7 @@ describe('SourceCodeReader', () => {
|
||||
let testFunc: () => void;
|
||||
|
||||
before(() => {
|
||||
testFunc = () => new SourceCodeReader({}).readSourceCode(inputPath);
|
||||
testFunc = () => new SourceCodeReader(inputPath, {}).readSourceCode();
|
||||
});
|
||||
|
||||
it('should throw an error if `inputPath` is not a valid path', () => {
|
||||
@@ -67,7 +67,7 @@ describe('SourceCodeReader', () => {
|
||||
|
||||
before(() => {
|
||||
fs.writeFileSync(inputPath, fileContent);
|
||||
testFunc = () => new SourceCodeReader({}).readSourceCode(inputPath);
|
||||
testFunc = () => new SourceCodeReader(inputPath, {}).readSourceCode();
|
||||
});
|
||||
|
||||
it('should throw an error if `inputPath` has invalid extension', () => {
|
||||
@@ -92,9 +92,12 @@ describe('SourceCodeReader', () => {
|
||||
|
||||
before(() => {
|
||||
fs.writeFileSync(inputPath, fileContent);
|
||||
filesData = new SourceCodeReader({
|
||||
exclude: ['**/foo.js']
|
||||
}).readSourceCode(inputPath);
|
||||
filesData = new SourceCodeReader(
|
||||
inputPath,
|
||||
{
|
||||
exclude: ['**/foo.js']
|
||||
}
|
||||
).readSourceCode();
|
||||
});
|
||||
|
||||
it('should return valid files data', () => {
|
||||
@@ -115,9 +118,12 @@ describe('SourceCodeReader', () => {
|
||||
|
||||
before(() => {
|
||||
fs.writeFileSync(inputPath, fileContent);
|
||||
testFunc = () => new SourceCodeReader({
|
||||
exclude: [`**/${tmpFileName}`]
|
||||
}).readSourceCode(inputPath);
|
||||
testFunc = () => new SourceCodeReader(
|
||||
inputPath,
|
||||
{
|
||||
exclude: [`**/${tmpFileName}`]
|
||||
}
|
||||
).readSourceCode();
|
||||
});
|
||||
|
||||
it('should throw an error if `inputPath` is the excluded file path', () => {
|
||||
@@ -137,9 +143,12 @@ describe('SourceCodeReader', () => {
|
||||
|
||||
before(() => {
|
||||
fs.writeFileSync(inputPath, fileContent);
|
||||
testFunc = () => new SourceCodeReader({
|
||||
exclude: [tmpFileName]
|
||||
}).readSourceCode(inputPath);
|
||||
testFunc = () => new SourceCodeReader(
|
||||
inputPath,
|
||||
{
|
||||
exclude: [tmpFileName]
|
||||
}
|
||||
).readSourceCode();
|
||||
});
|
||||
|
||||
it('should throw an error if `inputPath` is the excluded file path', () => {
|
||||
@@ -159,9 +168,12 @@ describe('SourceCodeReader', () => {
|
||||
|
||||
before(() => {
|
||||
fs.writeFileSync(inputPath, fileContent);
|
||||
testFunc = () => new SourceCodeReader({
|
||||
exclude: [inputPath]
|
||||
}).readSourceCode(inputPath);
|
||||
testFunc = () => new SourceCodeReader(
|
||||
inputPath,
|
||||
{
|
||||
exclude: [inputPath]
|
||||
}
|
||||
).readSourceCode();
|
||||
});
|
||||
|
||||
it('should throw an error if `inputPath` is the excluded file path', () => {
|
||||
@@ -205,7 +217,7 @@ describe('SourceCodeReader', () => {
|
||||
fs.writeFileSync(filePath2, fileContent);
|
||||
fs.writeFileSync(filePath3, fileContent);
|
||||
fs.writeFileSync(filePath4, fileContent);
|
||||
result = new SourceCodeReader({}).readSourceCode(tmpDirectoryPath);
|
||||
result = new SourceCodeReader(tmpDirectoryPath, {}).readSourceCode();
|
||||
});
|
||||
|
||||
it('should return files data', () => {
|
||||
@@ -226,7 +238,7 @@ describe('SourceCodeReader', () => {
|
||||
let testFunc: () => void;
|
||||
|
||||
before(() => {
|
||||
testFunc = () => new SourceCodeReader({}).readSourceCode(inputPath);
|
||||
testFunc = () => new SourceCodeReader(inputPath, {}).readSourceCode();
|
||||
});
|
||||
|
||||
it('should throw an error if `inputPath` is not a valid path', () => {
|
||||
@@ -276,7 +288,7 @@ describe('SourceCodeReader', () => {
|
||||
fs.writeFileSync(filePath2, fileContent);
|
||||
fs.writeFileSync(filePath3, fileContent);
|
||||
fs.writeFileSync(filePath4, fileContent);
|
||||
result = new SourceCodeReader({}).readSourceCode(tmpDirectoryPath);
|
||||
result = new SourceCodeReader(tmpDirectoryPath, {}).readSourceCode();
|
||||
});
|
||||
|
||||
it('should return files data', () => {
|
||||
@@ -322,9 +334,12 @@ describe('SourceCodeReader', () => {
|
||||
fs.writeFileSync(filePath2, fileContent);
|
||||
fs.writeFileSync(filePath3, fileContent);
|
||||
fs.writeFileSync(filePath4, fileContent);
|
||||
result = new SourceCodeReader({
|
||||
exclude: ['**/hawk.js']
|
||||
}).readSourceCode(tmpDirectoryPath);
|
||||
result = new SourceCodeReader(
|
||||
tmpDirectoryPath,
|
||||
{
|
||||
exclude: ['**/hawk.js']
|
||||
}
|
||||
).readSourceCode();
|
||||
});
|
||||
|
||||
it('should return files data', () => {
|
||||
@@ -368,12 +383,15 @@ describe('SourceCodeReader', () => {
|
||||
fs.writeFileSync(filePath2, fileContent);
|
||||
fs.writeFileSync(filePath3, fileContent);
|
||||
fs.writeFileSync(filePath4, fileContent);
|
||||
result = new SourceCodeReader({
|
||||
exclude: [
|
||||
`**/${tmpFileName2}`,
|
||||
`**/${tmpFileName4}`
|
||||
]
|
||||
}).readSourceCode(tmpDirectoryPath);
|
||||
result = new SourceCodeReader(
|
||||
tmpDirectoryPath,
|
||||
{
|
||||
exclude: [
|
||||
`**/${tmpFileName2}`,
|
||||
`**/${tmpFileName4}`
|
||||
]
|
||||
}
|
||||
).readSourceCode();
|
||||
});
|
||||
|
||||
it('should return files data', () => {
|
||||
@@ -416,12 +434,15 @@ describe('SourceCodeReader', () => {
|
||||
fs.writeFileSync(filePath2, fileContent);
|
||||
fs.writeFileSync(filePath3, fileContent);
|
||||
fs.writeFileSync(filePath4, fileContent);
|
||||
result = new SourceCodeReader({
|
||||
exclude: [
|
||||
tmpFileName2,
|
||||
tmpFileName4
|
||||
]
|
||||
}).readSourceCode(tmpDirectoryPath);
|
||||
result = new SourceCodeReader(
|
||||
tmpDirectoryPath,
|
||||
{
|
||||
exclude: [
|
||||
tmpFileName2,
|
||||
tmpFileName4
|
||||
]
|
||||
}
|
||||
).readSourceCode();
|
||||
});
|
||||
|
||||
it('should return files data', () => {
|
||||
@@ -464,12 +485,15 @@ describe('SourceCodeReader', () => {
|
||||
fs.writeFileSync(filePath2, fileContent);
|
||||
fs.writeFileSync(filePath3, fileContent);
|
||||
fs.writeFileSync(filePath4, fileContent);
|
||||
result = new SourceCodeReader({
|
||||
exclude: [
|
||||
filePath2,
|
||||
filePath4
|
||||
]
|
||||
}).readSourceCode(tmpDirectoryPath);
|
||||
result = new SourceCodeReader(
|
||||
tmpDirectoryPath,
|
||||
{
|
||||
exclude: [
|
||||
filePath2,
|
||||
filePath4
|
||||
]
|
||||
}
|
||||
).readSourceCode();
|
||||
});
|
||||
|
||||
it('should return files data', () => {
|
||||
@@ -501,9 +525,12 @@ describe('SourceCodeReader', () => {
|
||||
fs.writeFileSync(filePath2, fileContent);
|
||||
fs.writeFileSync(filePath3, fileContent);
|
||||
fs.writeFileSync(filePath4, fileContent);
|
||||
testFunc = () => new SourceCodeReader({
|
||||
exclude: [tmpDirectoryPath]
|
||||
}).readSourceCode(tmpDirectoryPath);
|
||||
testFunc = () => new SourceCodeReader(
|
||||
tmpDirectoryPath,
|
||||
{
|
||||
exclude: [tmpDirectoryPath]
|
||||
}
|
||||
).readSourceCode();
|
||||
});
|
||||
|
||||
it('should return files data', () => {
|
||||
@@ -535,7 +562,7 @@ describe('SourceCodeReader', () => {
|
||||
consoleLogSpy = sinon.spy(console, 'log');
|
||||
|
||||
fs.writeFileSync(inputPath, fileContent);
|
||||
new SourceCodeReader({}).readSourceCode(inputPath);
|
||||
new SourceCodeReader(inputPath, {}).readSourceCode();
|
||||
|
||||
consoleLogCallResult = consoleLogSpy.called;
|
||||
loggingMessageResult = consoleLogSpy.getCall(0).args[0];
|
||||
|
||||
Reference in New Issue
Block a user