From 2da515d6be4cb67688cfe588ef5a7171dea3d095 Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Mon, 3 Mar 2025 17:39:48 +0100 Subject: [PATCH 1/3] test: prevent false positives --- tests/unit/handle-deprecation-workflow-test.js | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/unit/handle-deprecation-workflow-test.js b/tests/unit/handle-deprecation-workflow-test.js index dd1fb6e..a2213d6 100644 --- a/tests/unit/handle-deprecation-workflow-test.js +++ b/tests/unit/handle-deprecation-workflow-test.js @@ -87,6 +87,7 @@ module('handleDeprecationWorkflow', function (hooks) { test('deprecation silenced with string matcher', function (assert) { const config = { + throwOnUnhandled: true, workflow: [{ matchMessage: 'Interesting', handler: 'silence' }], }; @@ -113,6 +114,7 @@ module('handleDeprecationWorkflow', function (hooks) { }; const config = { + throwOnUnhandled: true, workflow: [{ matchMessage: message, handler: 'log' }], }; @@ -151,6 +153,7 @@ module('handleDeprecationWorkflow', function (hooks) { test('deprecation silenced with regex matcher', function (assert) { const config = { + throwOnUnhandled: true, workflow: [{ matchMessage: /Inter/, handler: 'silence' }], }; @@ -184,6 +187,7 @@ module('handleDeprecationWorkflow', function (hooks) { }; const config = { + throwOnUnhandled: true, workflow: [{ matchMessage: /Inter/, handler: 'log' }], }; @@ -245,6 +249,7 @@ module('handleDeprecationWorkflow', function (hooks) { test('deprecation silenced with id matcher', function (assert) { const config = { + throwOnUnhandled: true, workflow: [{ matchId: 'ember.deprecation-workflow', handler: 'silence' }], }; @@ -278,6 +283,7 @@ module('handleDeprecationWorkflow', function (hooks) { }; const config = { + throwOnUnhandled: true, workflow: [{ matchId: 'ember.deprecation-workflow', handler: 'log' }], }; From 0409c38a9990b282b05ebde0d92847f32b32c6c2 Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Mon, 3 Mar 2025 17:43:21 +0100 Subject: [PATCH 2/3] feat: support RegExp for matchId --- addon/index.js | 11 +-- .../unit/handle-deprecation-workflow-test.js | 72 +++++++++++++++++++ 2 files changed, 78 insertions(+), 5 deletions(-) diff --git a/addon/index.js b/addon/index.js index edb8dac..fb14d63 100644 --- a/addon/index.js +++ b/addon/index.js @@ -37,11 +37,12 @@ export function detectWorkflow(config, message, options) { matcher = workflow.matchMessage; idMatcher = workflow.matchId; - if (typeof idMatcher === 'string' && options && idMatcher === options.id) { - return workflow; - } else if (typeof matcher === 'string' && matcher === message) { - return workflow; - } else if (matcher instanceof RegExp && matcher.exec(message)) { + if ( + (typeof idMatcher === 'string' && options && idMatcher === options.id) || + (idMatcher instanceof RegExp && idMatcher.exec(options.id)) || + (typeof matcher === 'string' && matcher === message) || + (matcher instanceof RegExp && matcher.exec(message)) + ) { return workflow; } } diff --git a/tests/unit/handle-deprecation-workflow-test.js b/tests/unit/handle-deprecation-workflow-test.js index a2213d6..77a2fff 100644 --- a/tests/unit/handle-deprecation-workflow-test.js +++ b/tests/unit/handle-deprecation-workflow-test.js @@ -318,4 +318,76 @@ module('handleDeprecationWorkflow', function (hooks) { ); }, 'deprecation throws'); }); + + test('deprecation silenced with id regex', function (assert) { + const config = { + throwOnUnhandled: true, + workflow: [{ matchId: /^ember\..*/, handler: 'silence' }], + }; + + handleDeprecationWorkflow( + config, + 'Slightly interesting', + { + id: 'ember.deprecation-workflow', + since: 'the beginning', + until: '3.0.0', + for: 'testing', + }, + () => {}, + ); + + assert.ok(true, 'Deprecation did not raise'); + }); + + // eslint-disable-next-line qunit/require-expect + test('deprecation logs with id regex', function (assert) { + assert.expect(1); + + let message = 'Slightly interesting'; + + console.warn = function (passedMessage) { + assert.strictEqual( + passedMessage, + 'DEPRECATION: ' + message, + 'deprecation logs', + ); + }; + + const config = { + throwOnUnhandled: true, + workflow: [{ matchId: /^ember\..*/, handler: 'log' }], + }; + + handleDeprecationWorkflow( + config, + 'Slightly interesting', + { + id: 'ember.deprecation-workflow', + since: 'the beginning', + until: '3.0.0', + for: 'testing', + }, + () => {}, + ); + }); + + test('deprecation thrown with id regex', function (assert) { + const config = { + workflow: [{ matchId: /^ember\..*/, handler: 'throw' }], + }; + assert.throws(function () { + handleDeprecationWorkflow( + config, + 'Slightly interesting', + { + id: 'ember.deprecation-workflow', + since: 'the beginning', + until: '3.0.0', + for: 'testing', + }, + () => {}, + ); + }, 'deprecation throws'); + }); }); From 24b7ef2f31918543a278f03a0b861b37367ded18 Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Mon, 3 Mar 2025 17:51:59 +0100 Subject: [PATCH 3/3] chore: extract into matchesWorkflow util --- addon/index.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/addon/index.js b/addon/index.js index fb14d63..c5cfeb3 100644 --- a/addon/index.js +++ b/addon/index.js @@ -26,6 +26,13 @@ setupDeprecationWorkflow({ let postamble = ` ] });`; +function matchesWorkflow(matcher, value) { + return ( + (typeof matcher === 'string' && matcher === value) || + (matcher instanceof RegExp && matcher.exec(value)) + ); +} + export function detectWorkflow(config, message, options) { if (!config || !config.workflow) { return; @@ -38,10 +45,8 @@ export function detectWorkflow(config, message, options) { idMatcher = workflow.matchId; if ( - (typeof idMatcher === 'string' && options && idMatcher === options.id) || - (idMatcher instanceof RegExp && idMatcher.exec(options.id)) || - (typeof matcher === 'string' && matcher === message) || - (matcher instanceof RegExp && matcher.exec(message)) + matchesWorkflow(idMatcher, options?.id) || + matchesWorkflow(matcher, message) ) { return workflow; }