Skip to content
代码片段 群组 项目
未验证 提交 0034b3fb 编辑于 作者: Lukas 'ai-pi' Eipert's avatar Lukas 'ai-pi' Eipert 提交者: GitLab
浏览文件

Consume icon.json files as assets

Instead of loading `icons.json` and `file_icons/file_icons.json` from
`node_modules/@gitlab/svgs/dist`, we can also make them assets and
consume them from the asset pipeline. This means that if assets are
pre-compiled inside of `compile-test-assets`, they will be included in
the asset bundle and our rspec tests can pass _without_ relying on the
`node_modules` folder.
上级 53bb0812
No related branches found
No related tags found
1 合并请求!2419Fix TanukiBot spec relying on outdated code
......@@ -74,8 +74,6 @@ compile-test-assets:
paths:
- public/assets/
- config/helpers/tailwind/ # Assets created during tailwind compilation
- node_modules/@gitlab/svgs/dist/icons.json # app/helpers/icons_helper.rb uses this file
- node_modules/@gitlab/svgs/dist/file_icons/file_icons.json # app/helpers/icons_helper.rb uses this file
- "${WEBPACK_COMPILE_LOG_PATH}"
when: always
......
......@@ -184,13 +184,24 @@ def unknown_file_icon_sprite(icon_name)
def known_sprites
return if Rails.env.production?
@known_sprites ||= Gitlab::Json.parse(File.read(Rails.root.join('node_modules/@gitlab/svgs/dist/icons.json')))['icons']
@known_sprites ||= parse_sprite_definition('icons.json')['icons']
end
def known_file_icon_sprites
return if Rails.env.production?
@known_file_icon_sprites ||= Gitlab::Json.parse(File.read(Rails.root.join('node_modules/@gitlab/svgs/dist/file_icons/file_icons.json')))['icons']
@known_file_icon_sprites ||= parse_sprite_definition('file_icons/file_icons.json')['icons']
end
def parse_sprite_definition(sprite_definition)
Gitlab::Json.parse(
Rails.application
.assets_manifest
.find_sources(sprite_definition)
.first
.to_s
.force_encoding('UTF-8')
)
end
def memoized_icon(key)
......
......@@ -398,6 +398,7 @@ class Application < Rails::Application
config.assets.precompile << "icons.svg"
config.assets.precompile << "icons.json"
config.assets.precompile << "file_icons/file_icons.svg"
config.assets.precompile << "file_icons/file_icons.json"
config.assets.precompile << "illustrations/*.svg"
config.assets.precompile << "illustrations/*.png"
......
......@@ -21,6 +21,7 @@ module Tasks
# or have a direct impact on asset compilation (e.g. scss) and therefore
# we should compile when these change
RAILS_ASSET_FILES = %w[
config/application.rb
Gemfile
Gemfile.lock
].freeze
......
......@@ -66,28 +66,36 @@
end
describe 'non existing icon' do
let(:helper) do
Class.new do
include IconsHelper
end.new
end
non_existing = 'non_existing_icon_sprite'
it 'raises in development mode' do
stub_rails_env('development')
expect { sprite_icon(non_existing) }.to raise_error(ArgumentError, /is not a known icon/)
expect { sprite_icon(non_existing, file_icon: true) }.to raise_error(ArgumentError, /is not a known icon/)
expect(helper).to receive(:parse_sprite_definition).with('icons.json').once.and_call_original
expect(helper).to receive(:parse_sprite_definition).with('file_icons/file_icons.json').once.and_call_original
expect { helper.sprite_icon(non_existing) }.to raise_error(ArgumentError, /is not a known icon/)
expect { helper.sprite_icon(non_existing, file_icon: true) }.to raise_error(ArgumentError, /is not a known icon/)
end
it 'raises in test mode' do
stub_rails_env('test')
expect { sprite_icon(non_existing) }.to raise_error(ArgumentError, /is not a known icon/)
expect { sprite_icon(non_existing, file_icon: true) }.to raise_error(ArgumentError, /is not a known icon/)
expect(helper).to receive(:parse_sprite_definition).with('icons.json').once.and_call_original
expect(helper).to receive(:parse_sprite_definition).with('file_icons/file_icons.json').once.and_call_original
expect { helper.sprite_icon(non_existing) }.to raise_error(ArgumentError, /is not a known icon/)
expect { helper.sprite_icon(non_existing, file_icon: true) }.to raise_error(ArgumentError, /is not a known icon/)
end
it 'does not raise in production mode' do
stub_rails_env('production')
expect_file_not_to_read(Rails.root.join('node_modules/@gitlab/svgs/dist/icons.json'))
expect_file_not_to_read(Rails.root.join('node_modules/@gitlab/svgs/dist/file_icons/file_icons.json'))
expect(helper).not_to receive(:parse_sprite_definition)
expect { sprite_icon(non_existing) }.not_to raise_error
expect { sprite_icon(non_existing, file_icon: true) }.not_to raise_error
end
......
0% 加载中 .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册