From 7290c71775b7a5ea3e32f6d6241cdf3f78a647b2 Mon Sep 17 00:00:00 2001 From: Cory Forsyth Date: Fri, 12 Jun 2015 17:10:10 -0400 Subject: [PATCH] [BUGFIX beta] Raise by default when `ENV.RAISE_ON_DEPRECATION` is true Fixes an issue introduced by #11419 where the evaluation of `ENV.RAISE_ON_DEPRECATION` was moved to boot-time rather than run-time. Previously, it was possible to set `RAISE_ON_DEPRECATION` to true midway through running an app, and all deprecations after that would throw because `Ember.deprecate` would check the value of the env variable every time it was called. The code in #11419 changed to a [one-time evaulation at boot-time](https://github.com/emberjs/ember.js/blob/master/packages/ember-debug/lib/main.js#L279-L281), breaking that original behavior. This commit restores the old behavior while still allowing changing deprecation behavior for specific deprecations by id. --- packages/ember-debug/lib/main.js | 6 ++-- packages/ember-debug/tests/main_test.js | 41 ++++++++++++++++++++++++- 2 files changed, 43 insertions(+), 4 deletions(-) diff --git a/packages/ember-debug/lib/main.js b/packages/ember-debug/lib/main.js index 4bb376c49cf..92a3a16daeb 100644 --- a/packages/ember-debug/lib/main.js +++ b/packages/ember-debug/lib/main.js @@ -108,6 +108,9 @@ Ember.debug = function(message) { @public */ Ember.deprecate = function(message, test, options) { + if (Ember.ENV.RAISE_ON_DEPRECATION) { + deprecationManager.setDefaultLevel(deprecationLevels.RAISE); + } if (deprecationManager.getLevel(options && options.id) === deprecationLevels.SILENCE) { return; } @@ -269,9 +272,6 @@ if (!Ember.testing) { } } -if (Ember.ENV.RAISE_ON_DEPRECATION) { - deprecationManager.setDefaultLevel(deprecationLevels.RAISE); -} Ember.Debug = { _addDeprecationLevel(id, level) { deprecationManager.setLevel(id, level); diff --git a/packages/ember-debug/tests/main_test.js b/packages/ember-debug/tests/main_test.js index b5cb0614c6c..dea4baca8ee 100644 --- a/packages/ember-debug/tests/main_test.js +++ b/packages/ember-debug/tests/main_test.js @@ -10,7 +10,9 @@ QUnit.module('ember-debug', { originalDeprecationDefault = deprecationManager.defaultLevel; originalDeprecationLevels = deprecationManager.individualLevels; originalEnvValue = Ember.ENV.RAISE_ON_DEPRECATION; - Ember.ENV.RAISE_ON_DEPRECATION = true; + + Ember.ENV.RAISE_ON_DEPRECATION = false; + deprecationManager.setDefaultLevel(deprecationLevels.RAISE); }, teardown() { @@ -32,6 +34,43 @@ QUnit.test('Ember.deprecate does not throw if default level is silence', functio } }); +QUnit.test('Ember.deprecate re-sets deprecation level to RAISE if ENV.RAISE_ON_DEPRECATION is set', function(assert) { + assert.expect(2); + + deprecationManager.setDefaultLevel(deprecationLevels.SILENCE); + + Ember.ENV.RAISE_ON_DEPRECATION = true; + + assert.throws(function() { + Ember.deprecate('Should throw', false); + }, /Should throw/); + + assert.equal(deprecationManager.defaultLevel, deprecationLevels.RAISE, + 'default level re-set to RAISE'); +}); + +QUnit.test('When ENV.RAISE_ON_DEPRECATION is true, it is still possible to silence a deprecation by id', function(assert) { + assert.expect(3); + + Ember.ENV.RAISE_ON_DEPRECATION = true; + deprecationManager.setLevel('my-deprecation', deprecationLevels.SILENCE); + + try { + Ember.deprecate('should be silenced with matching id', false, { id: 'my-deprecation' }); + assert.ok(true, 'Did not throw when level is set by id'); + } catch(e) { + assert.ok(false, `Expected Ember.deprecate not to throw but it did: ${e.message}`); + } + + assert.throws(function() { + Ember.deprecate('Should throw with no id', false); + }, /Should throw with no id/); + + assert.throws(function() { + Ember.deprecate('Should throw with non-matching id', false, { id: 'other-id' }); + }, /Should throw with non-matching id/); +}); + QUnit.test('Ember.deprecate throws deprecation if second argument is falsy', function() { expect(3);