DEV: show no icon if icon name is missing and log at error level (#31866)

On 1st April 2025, we start showing no icons if the icon name used is a
deprecated one and therefore no longer part of the svg set.

We'll continue showing the messages with the correct icon name to aid
correction of these names.

Console logging will now be done at an error level for such icons.

We retain the behaviour of raising an error for such icons in plugins
from svg_sprite.rb in test environments, but removed this from
icon-library.js as it's harder to test the actual expected behaviour of
returning the original icon now that it's not part of the deprecation
workflow. (sinon.stub doesn't work well here for `isTesting` - the
alternative would be to override the environment.js module with
`proxyquire`) In any case, once we remove the mapping logic, we won't be
raising errors in test environment either for this scenario.
This commit is contained in:
Kelv
2025-04-01 10:54:48 +08:00
committed by GitHub
parent 377a6554b9
commit 1c1d687283
6 changed files with 33 additions and 67 deletions

View File

@ -1,11 +1,7 @@
import { h } from "virtual-dom";
import attributeHook from "discourse/lib/attribute-hook";
import deprecated from "discourse/lib/deprecated";
import {
isDevelopment,
isRailsTesting,
isTesting,
} from "discourse/lib/environment";
import { isDevelopment } from "discourse/lib/environment";
import escape from "discourse/lib/escape";
import { i18n } from "discourse-i18n";
@ -146,17 +142,13 @@ function handleDeprecatedIcon(id) {
newId = remapFromFA5(newId);
if (newId !== id) {
deprecated(
`The icon name "${id}" has been updated to "${newId}". Please use the new name in your code. Old names will be removed in Q2 2025.`,
{
id: "discourse.fontawesome-6-upgrade",
url: "https://meta.discourse.org/t/325349",
raiseError: isTesting() || isRailsTesting(),
}
);
const error_msg = `Missing icon error: The icon name "${id}" has been removed and should be updated to "${newId}" in your code. More info at https://meta.discourse.org/t/325349.`;
// eslint-disable-next-line no-console
console.error(error_msg);
}
return newId;
return id;
}
function warnIfMissing(id) {

View File

@ -19,7 +19,6 @@ export const CRITICAL_DEPRECATIONS = [
"discourse.header-widget-overrides",
"discourse.plugin-outlet-tag-name",
"discourse.post-menu-widget-overrides",
"discourse.fontawesome-6-upgrade",
"discourse.add-flag-property",
"discourse.breadcrumbs.childCategories",
"discourse.breadcrumbs.firstCategory",

View File

@ -1,15 +1,10 @@
import { setupTest } from "ember-qunit";
import { module, test } from "qunit";
import { withSilencedDeprecations } from "discourse/lib/deprecated";
import {
convertIconClass,
iconHTML,
iconNode,
} from "discourse/lib/icon-library";
import {
disableRaiseOnDeprecation,
enableRaiseOnDeprecation,
} from "discourse/tests/helpers/raise-on-deprecation";
module("Unit | Utility | icon-library", function (hooks) {
setupTest(hooks);
@ -52,13 +47,9 @@ module("Unit | Utility | icon-library", function (hooks) {
});
test("fa5 remaps", function (assert) {
withSilencedDeprecations("discourse.fontawesome-6-upgrade", () => {
const adjustIcon = iconHTML("adjust");
assert.true(adjustIcon.includes("d-icon-adjust"), "class is maintained");
assert.true(
adjustIcon.includes('href="#circle-half-stroke"'),
"has remapped icon"
);
assert.true(adjustIcon.includes('href="#adjust"'), "keeps original icon");
const farIcon = iconHTML("far-dot-circle");
assert.true(
@ -66,29 +57,8 @@ module("Unit | Utility | icon-library", function (hooks) {
"class is maintained"
);
assert.true(
farIcon.includes('href="#far-circle-dot"'),
"has remapped icon"
farIcon.includes('href="#far-dot-circle"'),
"keeps original icon"
);
});
});
test("fa5 remaps throws error", function (assert) {
disableRaiseOnDeprecation();
assert.throws(
() => {
iconHTML("adjust");
},
/Deprecation notice: The icon name "adjust" has been updated to "circle-half-stroke".*\[deprecation id: discourse\.fontawesome-6-upgrade\]/,
"throws an error if icon name is deprecated"
);
assert.throws(
() => {
iconHTML("far-dot-circle");
},
/Deprecation notice: The icon name "far-dot-circle" has been updated to "far-circle-dot".*\[deprecation id: discourse\.fontawesome-6-upgrade\]/,
"throws an error if icon name is deprecated"
);
enableRaiseOnDeprecation();
});
});

View File

@ -687,11 +687,12 @@ module DeprecatedIconHandler
new_name = remap_from_fa5(new_name)
if icon_name != new_name
Discourse.deprecate(
"The icon `#{icon_name}` is deprecated. Use `#{new_name}` instead.",
raise_error: Rails.env.test?,
)
return new_name
error_msg =
`Missing icon error: The icon name "#{icon_name}" has been removed and should be updated to "#{new_name}" in your code. More info at https://meta.discourse.org/t/325349.`
Rails.logger.error(error_msg)
raise Discourse::MissingIconError.new(error_msg) if Rails.env.test?
end
icon_name

View File

@ -117,6 +117,7 @@ module Discourse
class CommandError < RuntimeError
attr_reader :status, :stdout, :stderr
def initialize(message, status: nil, stdout: nil, stderr: nil)
super(message)
@status = status
@ -305,6 +306,9 @@ module Discourse
class Deprecation < StandardError
end
class MissingIconError < StandardError
end
class ScssError < StandardError
end

View File

@ -88,18 +88,18 @@ RSpec.describe SvgSprite do
it "strips whitespace when processing icons" do
Fabricate(:badge, name: "Custom Icon Badge", icon: " fab fa-facebook-messenger ")
expect(SvgSprite.all_icons).to include("fab-facebook-messenger")
expect(SvgSprite.all_icons).not_to include(" fab-facebook-messenger ")
expect(SvgSprite.all_icons).to include("fab fa-facebook-messenger")
expect(SvgSprite.all_icons).not_to include(" fab fa-facebook-messenger ")
end
it "includes Font Awesome 5 icons from badges" do
it "includes icons from badges" do
Fabricate(:badge, name: "Custom Icon Badge", icon: "far fa-building")
expect(SvgSprite.all_icons).to include("far-building")
expect(SvgSprite.all_icons).to include("far fa-building")
end
it "raises an error in test for deprecated icons" do
allow(Rails.env).to receive(:test?).and_return(true)
expect { SvgSprite.search("fa-gamepad") }.to raise_error(Discourse::Deprecation)
expect { SvgSprite.search("fa-gamepad") }.to raise_error(Discourse::MissingIconError)
end
it "includes icons defined in theme settings" do
@ -127,7 +127,7 @@ RSpec.describe SvgSprite do
# FA5 syntax
theme.update_setting(:custom_icon, "fab fa-bandcamp")
theme.save!
expect(SvgSprite.all_icons(theme.id)).to include("fab-bandcamp")
expect(SvgSprite.all_icons(theme.id)).to include("fab fa-bandcamp")
# Internal Discourse syntax + multiple icons
theme.update_setting(:custom_icon, "fab-android|dragon")
@ -227,7 +227,7 @@ RSpec.describe SvgSprite do
DiscoursePluginRegistry.register_svg_icon "fab fa-bandcamp"
expect(SvgSprite.all_icons).to include("blender")
expect(SvgSprite.all_icons).to include("fab-bandcamp")
expect(SvgSprite.all_icons).to include("fab fa-bandcamp")
end
it "includes Font Awesome icon from groups" do