Skip to content

Commit

Permalink
improve logMessage helper and update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
stanislav-atr committed Dec 27, 2022
1 parent 1caed03 commit 8342527
Show file tree
Hide file tree
Showing 18 changed files with 55 additions and 27 deletions.
24 changes: 21 additions & 3 deletions src/helpers/log-message.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,26 @@
* @param {boolean} [forced=false] to log message unconditionally
*/
export const logMessage = (source, message, forced = false) => {
if (forced || source.verbose) {
// eslint-disable-next-line no-console
console.log(`${source.name}: ${message}`);
const {
name,
ruleText,
verbose,
} = source;

if (!forced && !verbose) {
return;
}

let messageStr = `${name}: ${message};`;

// Extract scriptlet part from rule text
if (ruleText) {
const RULE_MARKER = '#%#';
const markerIdx = ruleText.indexOf(RULE_MARKER);
const extension = ruleText.slice(markerIdx, ruleText.length);
messageStr += ` cannot apply rule: ${extension}`;
}

// eslint-disable-next-line no-console
console.log(messageStr);
};
2 changes: 1 addition & 1 deletion src/scriptlets/adjust-setInterval.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function adjustSetInterval(source, matchCallback, matchDelay, boost) {
// https://github.com/AdguardTeam/Scriptlets/issues/221
if (!isValidCallback(callback)) {
// eslint-disable-next-line max-len
const message = `Scriptlet can't be applied because of invalid callback: '${String(callback)}'.`;
const message = `Scriptlet can't be applied because of invalid callback: '${String(callback)}'`;
logMessage(source, message);
} else if (matchRegexp.test(callback.toString()) && isDelayMatched(matchDelay, delay)) {
delay *= getBoostMultiplier(boost);
Expand Down
2 changes: 1 addition & 1 deletion src/scriptlets/adjust-setTimeout.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ export function adjustSetTimeout(source, matchCallback, matchDelay, boost) {
// https://github.com/AdguardTeam/Scriptlets/issues/221
if (!isValidCallback(callback)) {
// eslint-disable-next-line max-len
const message = `Scriptlet can't be applied because of invalid callback: '${String(callback)}'.`;
const message = `Scriptlet can't be applied because of invalid callback: '${String(callback)}'`;
logMessage(source, message);
} else if (matchRegexp.test(callback.toString()) && isDelayMatched(matchDelay, delay)) {
delay *= getBoostMultiplier(boost);
Expand Down
2 changes: 1 addition & 1 deletion src/scriptlets/trusted-replace-fetch-response.js
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ export function trustedReplaceFetchResponse(source, pattern = '', replacement =

// Only allow pattern as empty string for logging purposes
if (pattern === '' && replacement !== '') {
logMessage(source, 'Pattern argument should not be empty string.');
logMessage(source, 'Pattern argument should not be empty string');
return;
}
const shouldLog = pattern === '' && replacement === '';
Expand Down
4 changes: 2 additions & 2 deletions src/scriptlets/trusted-set-cookie-reload.js
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ import {

export function trustedSetCookieReload(source, name, value, offsetExpiresSec = '', path = '/') {
if (typeof name === 'undefined') {
logMessage(source, 'Cookie name should be specified.');
logMessage(source, 'Cookie name should be specified');
return;
}
if (typeof value === 'undefined') {
logMessage(source, 'Cookie value should be specified.');
logMessage(source, 'Cookie value should be specified');
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/scriptlets/trusted-set-cookie.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ import {

export function trustedSetCookie(source, name, value, offsetExpiresSec = '', path = '/') {
if (typeof name === 'undefined') {
logMessage(source, 'Cookie name should be specified.');
logMessage(source, 'Cookie name should be specified');
return;
}
if (typeof value === 'undefined') {
logMessage(source, 'Cookie value should be specified.');
logMessage(source, 'Cookie value should be specified');
return;
}

Expand Down
4 changes: 2 additions & 2 deletions src/scriptlets/trusted-set-local-storage-item.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,12 @@ import {

export function trustedSetLocalStorageItem(source, key, value) {
if (typeof key === 'undefined') {
logMessage(source, 'Item key should be specified.');
logMessage(source, 'Item key should be specified');
return;
}

if (typeof value === 'undefined') {
logMessage(source, 'Item value should be specified.');
logMessage(source, 'Item value should be specified');
return;
}

Expand Down
2 changes: 1 addition & 1 deletion tests/scriptlets/adjust-setInterval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ test('no match -- invalid callback - undefined', (assert) => {
assert.strictEqual(window.hit, undefined, 'hit should not fire');
assert.strictEqual(
loggedMessage,
`${name}: Scriptlet can't be applied because of invalid callback: '${String(callback)}'.`, // eslint-disable-line max-len
`${name}: Scriptlet can't be applied because of invalid callback: '${String(callback)}';`, // eslint-disable-line max-len
'console.logged warning ok',
);
clearInterval(testInterval);
Expand Down
2 changes: 1 addition & 1 deletion tests/scriptlets/adjust-setTimeout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ test('no match -- invalid callback - undefined', (assert) => {
assert.strictEqual(window.hit, undefined, 'hit should not fire');
assert.strictEqual(
loggedMessage,
`${name}: Scriptlet can't be applied because of invalid callback: '${String(callback)}'.`, // eslint-disable-line max-len
`${name}: Scriptlet can't be applied because of invalid callback: '${String(callback)}';`, // eslint-disable-line max-len
'console.logged warning ok',
);
clearTimeout(testTimeout);
Expand Down
4 changes: 2 additions & 2 deletions tests/scriptlets/log-eval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test('logs eval calls', (assert) => {
if (input.indexOf('trace') > -1) {
return;
}
assert.strictEqual(input, `${name}: eval("${evalStr}")`, 'console.hit input should be equal');
assert.strictEqual(input, `${name}: eval("${evalStr}");`, 'console.hit input should be equal');
};
runScriptlet(name);
const evalWrap = eval;
Expand All @@ -52,7 +52,7 @@ test('logs new Function() calls', (assert) => {
if (input.indexOf('trace') > -1) {
return;
}
assert.strictEqual(input, `${name}: new Function(${args.join(', ')})`, 'console.hit input should be equal');
assert.strictEqual(input, `${name}: new Function(${args.join(', ')});`, 'console.hit input should be equal');
};

runScriptlet(name);
Expand Down
2 changes: 1 addition & 1 deletion tests/scriptlets/nowebrtc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ if (!isSupported) {
return;
}
// eslint-disable-next-line max-len
const EXPECTED_LOG_STR = `${name}: Document tried to create an RTCPeerConnection: ${TEST_URL_VALUE}`;
const EXPECTED_LOG_STR = `${name}: Document tried to create an RTCPeerConnection: ${TEST_URL_VALUE};`;
assert.ok(endsWith(input, EXPECTED_LOG_STR), 'console.hit input');
};

Expand Down
6 changes: 5 additions & 1 deletion tests/scriptlets/prevent-requestAnimationFrame.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,11 @@ test('prevent-requestAnimationFrame: no args -- logging', (assert) => {
// do test checking after scriptlet's execution end
setTimeout(() => {
assert.strictEqual(window.hit, 'FIRED', 'hit fired');
assert.strictEqual(loggedMessage, `prevent-requestAnimationFrame: requestAnimationFrame(${testFunction.toString()})`, 'console.hit input');
assert.strictEqual(
loggedMessage,
`prevent-requestAnimationFrame: requestAnimationFrame(${testFunction.toString()});`,
'console.hit input',
);
assert.strictEqual(window[logProperty], 'changed', 'property changed');
clearGlobalProps(logProperty);
done();
Expand Down
2 changes: 1 addition & 1 deletion tests/scriptlets/prevent-setInterval.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test('no args -- logging', (assert) => {
assert.strictEqual(window.hit, 'FIRED', 'hit fired');
assert.strictEqual(
loggedMessage,
`prevent-setInterval: setInterval(${callback.toString()}, ${timeout})`,
`prevent-setInterval: setInterval(${callback.toString()}, ${timeout});`,
'console.hit input ok',
);
assert.strictEqual(window[agLogSetInterval], 'changed', 'property changed');
Expand Down
2 changes: 1 addition & 1 deletion tests/scriptlets/prevent-setTimeout.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ test('no args -- logging', (assert) => {
assert.strictEqual(window.hit, 'FIRED', 'hit fired');
assert.strictEqual(
loggedMessage,
`prevent-setTimeout: setTimeout(${callback.toString()}, ${timeout})`,
`prevent-setTimeout: setTimeout(${callback.toString()}, ${timeout});`,
'console.hit input ok',
);
assert.strictEqual(window[agLogSetTimeout], 'changed', 'property changed');
Expand Down
4 changes: 2 additions & 2 deletions tests/scriptlets/prevent-window-open.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ test('new syntax: log checking - only url', (assert) => {
if (input.indexOf('trace') > -1) {
return;
}
const EXPECTED_LOG_STR = `${name}: ${testUrl}`;
const EXPECTED_LOG_STR = `${name}: ${testUrl};`;
assert.strictEqual(input, EXPECTED_LOG_STR, 'console.hit input');
};

Expand All @@ -208,7 +208,7 @@ test('new syntax: log checking - url + args', (assert) => {
return;
}
// eslint-disable-next-line max-len
const EXPECTED_LOG_STR = `${name}: ${testUrl}, ${testWindowName}, ${testWindowFeatures}`;
const EXPECTED_LOG_STR = `${name}: ${testUrl}, ${testWindowName}, ${testWindowFeatures};`;
assert.strictEqual(input, EXPECTED_LOG_STR, 'console.hit input');
};

Expand Down
6 changes: 5 additions & 1 deletion tests/scriptlets/remove-attr.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,11 @@ test('invalid selector — no match', (assert) => {
if (input.indexOf('trace') > -1) {
return;
}
assert.strictEqual(input, `${name}: Invalid selector arg: '${selector}'`, 'logged error for invalid remove-attr selector');
assert.strictEqual(
input,
`${name}: Invalid selector arg: '${selector}';`,
'logged error for invalid remove-attr selector;',
);
};

runScriptlet(name, scriptletArgs);
Expand Down
2 changes: 1 addition & 1 deletion tests/scriptlets/remove-class.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ test('invalid selector — no match', (assert) => {
if (input.indexOf('trace') > -1) {
return;
}
assert.strictEqual(input, `${name}: Invalid selector arg: '${selectors}'`, 'logged error for invalid remove-class selector');
assert.strictEqual(input, `${name}: Invalid selector arg: '${selectors}';`, 'logged error for invalid remove-class selector');
};

assert.strictEqual(window.hit, undefined, 'hit SHOULD NOT fire');
Expand Down
8 changes: 5 additions & 3 deletions tests/scriptlets/trusted-set-cookie.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,11 @@ test('Set cookie with invalid expires', (assert) => {
if (input.indexOf('trace') > -1) {
return;
}
assert.strictEqual(input,
`${name}: Invalid offsetExpiresSec value: ${expiresSec}`,
'logs correctly on invalid offsetExpiresSec');
assert.strictEqual(
input,
`${name}: Invalid offsetExpiresSec value: ${expiresSec};`,
'logs correctly on invalid offsetExpiresSec',
);
};
runScriptlet(name, [cName, cValue, `${expiresSec}`]);

Expand Down

0 comments on commit 8342527

Please sign in to comment.