-
Notifications
You must be signed in to change notification settings - Fork 166
Add Make task to validate asset strings #3936
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
811f812
96e15ce
a8ea3cf
deb624c
5ade1bd
758e4b3
8786858
ecc0087
b14cf5f
c810130
ad88fe6
70c9e1f
875e0b5
ed6f631
d57af47
d0a7477
256f2fe
eb4062e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| class AssetChecker | ||
| def self.check_files(argv) | ||
| assets_file = 'app/assets/javascripts/assets.js.erb' | ||
| translations_file = 'app/assets/javascripts/i18n-strings.js.erb' | ||
| @asset_strings = load_included_strings(assets_file) | ||
| @translation_strings = load_included_strings(translations_file) | ||
| argv.any? { |f| file_has_missing?(f) } | ||
| end | ||
|
|
||
| def self.file_has_missing?(file) | ||
| data = File.open(file).read | ||
| missing_translations = find_missing(data, /\Wt\s?\(['"]([^'^"]*)['"]\)/, @translation_strings) | ||
| missing_assets = find_missing(data, /\WassetPath=["'](.*)['"]/, @asset_strings) | ||
| has_missing = (missing_translations.any? || missing_assets.any?) | ||
| if has_missing | ||
| warn file | ||
| missing_translations.each do |t| | ||
| warn "Missing translation, #{t}" | ||
| end | ||
| missing_assets.each do |a| | ||
| warn "Missing asset, #{a}" | ||
| end | ||
|
||
| end | ||
| has_missing | ||
| end | ||
|
|
||
| def self.find_missing(file_data, pattern, source) | ||
| strings = (file_data.scan pattern).flatten | ||
| strings.reject { |s| source.include? s } | ||
| end | ||
|
|
||
| def self.load_included_strings(file) | ||
| data = File.open(file).read | ||
| key_data = data.split('<% keys = [')[1].split('] %>')[0] | ||
| key_data.scan(/['"](.*)['"]/).flatten | ||
| end | ||
| end | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| #!/usr/bin/env ruby | ||
| $LOAD_PATH.unshift(File.expand_path('../lib', File.dirname(__FILE__))) | ||
| require 'asset_checker' | ||
|
|
||
| exit(AssetChecker.check_files(ARGV) ? 1 : 0) | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| require 'asset_checker' | ||
|
|
||
| def get_js_with_strings(asset, translation) | ||
| " | ||
| import React from 'react'; | ||
| import AcuantCapture from './acuant-capture'; | ||
| import DocumentTips from './document-tips'; | ||
| import Image from './image'; | ||
| import useI18n from '../hooks/use-i18n'; | ||
|
|
||
| function DocumentCapture() { | ||
| const t = useI18n(); | ||
|
|
||
| const sample = ( | ||
| <Image | ||
| assetPath=\"#{asset}\" | ||
| alt=\"Sample front of state issued ID\" | ||
| width={450} | ||
| height={338} | ||
| /> | ||
| ); | ||
|
|
||
| return ( | ||
| <> | ||
| <h2>{t('#{translation}')}</h2> | ||
| <DocumentTips sample={sample} /> | ||
| <AcuantCapture /> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| export default DocumentCapture; | ||
| " | ||
| end | ||
|
|
||
| RSpec.describe AssetChecker do | ||
| describe '.check_files' do | ||
| let(:translation_strings) { %w[first_translation second_translation] } | ||
| let(:asset_strings) { %w[first_asset.png second_asset.gif] } | ||
|
|
||
| context 'with matching assets' do | ||
| let(:tempfile) { Tempfile.new } | ||
|
|
||
| before do | ||
| File.open(tempfile.path, 'w') do |f| | ||
| f.puts(get_js_with_strings('first_asset.png', 'first_translation')) | ||
| end | ||
| end | ||
|
|
||
| after { tempfile.unlink } | ||
|
|
||
| it 'identifies no issues ' do | ||
| allow(AssetChecker).to receive(:load_included_strings).and_return(asset_strings, | ||
| translation_strings) | ||
|
|
||
| expect(AssetChecker.check_files([tempfile.path])).to eq(false) | ||
| end | ||
| end | ||
|
|
||
| context 'with an asset mismatch' do | ||
| let(:tempfile) { Tempfile.new } | ||
|
|
||
| before do | ||
| File.open(tempfile.path, 'w') do |f| | ||
| f.puts(get_js_with_strings('wont_find.svg', 'not-found')) | ||
| end | ||
| end | ||
|
|
||
| after { tempfile.unlink } | ||
|
|
||
| it 'identifies issues' do | ||
| allow(AssetChecker).to receive(:load_included_strings).and_return(asset_strings, | ||
| translation_strings) | ||
| expect(AssetChecker).to receive(:warn).with(tempfile.path) | ||
| expect(AssetChecker).to receive(:warn).with('Missing translation, not-found') | ||
| expect(AssetChecker).to receive(:warn).with('Missing asset, wont_find.svg') | ||
| expect(AssetChecker.check_files([tempfile.path])).to eq(true) | ||
| end | ||
| end | ||
| end | ||
| end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I personally prefer:
but not a huge deal