Changed file log messages

This commit is contained in:
sanex3339
2020-02-20 23:33:13 +03:00
parent ae111ead95
commit ed09424df5
18 changed files with 141 additions and 153 deletions
+8 -4
View File
@@ -7,13 +7,17 @@ import { NO_ADDITIONAL_NODES_PRESET } from '../../src/options/presets/NoCustomNo
let obfuscatedCode: string = JavaScriptObfuscator.obfuscate(
`
(function () {
return 1;
})();
var s = function () {};
class Foo {
bar (baz, bark) {
return s;
}
}
`,
{
...NO_ADDITIONAL_NODES_PRESET,
selfDefending: true
renameGlobals: true,
identifierNamesGenerator: 'mangled'
}
).getObfuscatedCode();
+1
View File
@@ -0,0 +1 @@
error error
@@ -852,6 +852,94 @@ describe('JavaScriptObfuscatorCLI', function (): void {
});
});
describe('Logging', () => {
describe('Obfuscating file message', () => {
const directoryPath: string = `${fixturesDirName}/directory-obfuscation`;
const inputFileName1: string = 'foo.js';
const inputFileName2: string = 'bar.js';
const inputFilePath1: string = `${directoryPath}/${inputFileName1}`;
const inputFilePath2: string = `${directoryPath}/${inputFileName2}`;
const outputFileName1: string = 'foo-obfuscated.js';
const outputFileName2: string = 'bar-obfuscated.js';
const outputFilePath1: string = `${directoryPath}/${outputFileName1}`;
const outputFilePath2: string = `${directoryPath}/${outputFileName2}`;
const expectedLoggingMessage1: string = `[javascript-obfuscator-cli] Obfuscating file: ${inputFilePath1}...`;
const expectedLoggingMessage2: string = `[javascript-obfuscator-cli] Obfuscating file: ${inputFilePath2}...`;
let consoleLogSpy: sinon.SinonSpy<any, void>,
loggingMessageResult1: string,
loggingMessageResult2: string;
before(() => {
consoleLogSpy = sinon.spy(console, 'log');
JavaScriptObfuscatorCLI.obfuscate([
'node',
'javascript-obfuscator',
directoryPath,
'--rename-globals',
'true'
]);
loggingMessageResult1 = consoleLogSpy.getCall(1).args[0];
loggingMessageResult2 = consoleLogSpy.getCall(0).args[0];
});
it('Variant #1: should log file name to the console', () => {
assert.include(loggingMessageResult1, expectedLoggingMessage1);
});
it('Variant #2: should log file name to the console', () => {
assert.include(loggingMessageResult2, expectedLoggingMessage2);
});
after(() => {
rimraf.sync(outputFilePath1);
rimraf.sync(outputFilePath2);
consoleLogSpy.restore();
});
});
describe('Error message', () => {
const directoryPath: string = `${fixturesDirName}/directory-obfuscation-error`;
const inputFileName: string = 'foo.js';
const inputFilePath: string = `${directoryPath}/${inputFileName}`;
const expectedLoggingMessage1: string = `[javascript-obfuscator-cli] Error in file: ${inputFilePath}...`;
let consoleLogSpy: sinon.SinonSpy<any, void>,
loggingMessageResult: string
before(() => {
consoleLogSpy = sinon.spy(console, 'log');
try {
JavaScriptObfuscatorCLI.obfuscate([
'node',
'javascript-obfuscator',
directoryPath,
'--rename-globals',
'true'
]);
} catch {}
loggingMessageResult = consoleLogSpy.getCall(1).args[0];
});
it('Should log file name to the console', () => {
assert.include(loggingMessageResult, expectedLoggingMessage1);
});
after(() => {
consoleLogSpy.restore();
});
});
});
after(() => {
rimraf.sync(outputDirName);
});
@@ -3,7 +3,6 @@ import * as mkdirp from 'mkdirp';
import * as rimraf from 'rimraf';
import { assert } from 'chai';
import * as sinon from 'sinon';
import { IFileData } from '../../../../src/interfaces/cli/IFileData';
@@ -547,41 +546,6 @@ describe('SourceCodeReader', () => {
});
});
});
describe('Variant #3: logging', () => {
const tmpFileName: string = 'test.js';
const inputPath: string = `${tmpDirectoryPath}/${tmpFileName}`;
const expectedConsoleLogCallResult: boolean = true;
const expectedLoggingMessage: string = `[javascript-obfuscator-cli] Obfuscating file: ${inputPath}...`;
let consoleLogSpy: sinon.SinonSpy<any, void>,
consoleLogCallResult: boolean,
loggingMessageResult: string;
before(() => {
consoleLogSpy = sinon.spy(console, 'log');
fs.writeFileSync(inputPath, fileContent);
new SourceCodeReader(inputPath, {}).readSourceCode();
consoleLogCallResult = consoleLogSpy.called;
loggingMessageResult = consoleLogSpy.getCall(0).args[0];
});
it('should call `console.log`', () => {
assert.equal(consoleLogCallResult, expectedConsoleLogCallResult);
});
it('should log file name to the console', () => {
assert.include(loggingMessageResult, expectedLoggingMessage);
});
after(() => {
consoleLogSpy.restore();
fs.unlinkSync(inputPath);
});
});
});
after(() => {
@@ -1,7 +1,5 @@
import { assert } from 'chai';
import { IASTParserFacadeInputData } from '../../../src/interfaces/IASTParserFacadeInputData';
import { ecmaVersion } from '../../../src/constants/EcmaVersion';
import { ASTParserFacade } from '../../../src/ASTParserFacade';
@@ -20,13 +18,11 @@ describe('ASTParserFacade', () => {
let testFunc: () => void;
before(() => {
const inputData: IASTParserFacadeInputData = {sourceCode};
testFunc = () => ASTParserFacade.parse(inputData, { ecmaVersion });
testFunc = () => ASTParserFacade.parse(sourceCode, { ecmaVersion });
});
it('should output code preview when AST parser throws a parse error', () => {
assert.throws(testFunc, /ERROR in line 3: Unexpected token \(3:28\)\n.*\.\.\.var baz = 3;,\.\.\./);
assert.throws(testFunc, /ERROR at line 3: Unexpected token \(3:28\)\n.*\.\.\.var baz = 3;,\.\.\./);
});
});
@@ -51,13 +47,11 @@ describe('ASTParserFacade', () => {
let testFunc: () => void;
before(() => {
const inputData: IASTParserFacadeInputData = {sourceCode};
testFunc = () => ASTParserFacade.parse(inputData, { ecmaVersion });
testFunc = () => ASTParserFacade.parse(sourceCode, { ecmaVersion });
});
it('should output code preview when AST parser throws a parse error', () => {
assert.throws(testFunc, /ERROR in line 13: Unexpected token \(13:28\)\n.*\.\.\.var baz = 3;,\.\.\./);
assert.throws(testFunc, /ERROR at line 13: Unexpected token \(13:28\)\n.*\.\.\.var baz = 3;,\.\.\./);
});
});
@@ -76,37 +70,11 @@ describe('ASTParserFacade', () => {
let testFunc: () => void;
before(() => {
const inputData: IASTParserFacadeInputData = {sourceCode};
testFunc = () => ASTParserFacade.parse(inputData, { ecmaVersion });
testFunc = () => ASTParserFacade.parse(sourceCode, { ecmaVersion });
});
it('should output code preview when AST parser throws a parse error', () => {
assert.throws(testFunc, /ERROR in line 4: Unexpected token \(4:28\)\n.*\.\.\.functin baz \(\) {\.\.\./);
});
});
describe('Variant #4: input file path is set', () => {
const sourceCode: string = `` +
`var foo = 1;
var bar = 2;
var baz = 3;,
var bark = 4;
var hawk = 5;`;
let testFunc: () => void;
before(() => {
const inputData: IASTParserFacadeInputData = {
sourceCode,
inputFilePath: '/src/foo.js'
};
testFunc = () => ASTParserFacade.parse(inputData, { ecmaVersion });
});
it('should output code preview when AST parser throws a parse error', () => {
assert.throws(testFunc, /ERROR in \/src\/foo\.js, line 3: Unexpected token \(3:32\)\n.*\.\.\.var baz = 3;,\.\.\./);
assert.throws(testFunc, /ERROR at line 4: Unexpected token \(4:28\)\n.*\.\.\.functin baz \(\) {\.\.\./);
});
});
});