diff --git a/core/lib/spree/testing_support/i18n.rb b/core/lib/spree/testing_support/i18n.rb deleted file mode 100644 index 541c2df047b..00000000000 --- a/core/lib/spree/testing_support/i18n.rb +++ /dev/null @@ -1,97 +0,0 @@ -module Spree - # This file exists solely to test whether or not there are missing translations - # within the code that Spree's test suite covers. - # - # If there is a translation referenced which has no corresponding key within the - # .yml file, then there will be a message output at the end of the suite showing - # that. - # - # If there is a translation within the locale file which *isn't* used in the - # test, this will also be shown at the end of the suite run. - - class << self - attr_accessor :used_translations, :missing_translation_messages, - :unused_translations, :unused_translation_messages - alias_method :normal_t, :t - end - - def self.t(*args) - original_args = args.dup - options = args.extract_options! - self.used_translations ||= [] - [*args.first].each do |translation_key| - key = ([*options[:scope]] << translation_key).join('.') - self.used_translations << key - end - normal_t(*original_args) - end - - def self.check_missing_translations - self.missing_translation_messages = [] - self.used_translations ||= [] - used_translations.map { |a| a.split('.') }.each do |translation_keys| - root = translations - processed_keys = [] - translation_keys.each do |key| - begin - root = root.fetch(key.to_sym) - processed_keys << key.to_sym - rescue KeyError - error = "#{(processed_keys << key).join('.')} (#{I18n.locale})" - unless Spree.missing_translation_messages.include?(error) - Spree.missing_translation_messages << error - end - end - end - end - end - - def self.check_unused_translations - self.used_translations ||= [] - self.unused_translation_messages = [] - self.unused_translations = [] - load_translations(translations) - translation_diff = unused_translations - used_translations - translation_diff.each do |translation| - Spree.unused_translation_messages << "#{translation} (#{I18n.locale})" - end - end - - private - - def self.load_translations(hash, root = []) - hash.each do |k, v| - if v.is_a?(Hash) - load_translations(v, root.dup << k) - else - key = (root + [k]).join('.') - unused_translations << key - end - end - end - - def self.translations - @translations ||= I18n.backend.send(:translations)[I18n.locale][:spree] - end -end - -RSpec.configure do |config| - # Need to check here again because this is used in i18n_spec too. - if ENV['CHECK_TRANSLATIONS'] - config.after :suite do - Spree.check_missing_translations - if Spree.missing_translation_messages.any? - puts "\nThere are missing translations within Spree:" - puts Spree.missing_translation_messages.sort - exit(1) - end - - Spree.check_unused_translations - if false && Spree.unused_translation_messages.any? - puts "\nThere are unused translations within Spree:" - puts Spree.unused_translation_messages.sort - exit(1) - end - end - end -end diff --git a/core/spec/lib/i18n_spec.rb b/core/spec/lib/i18n_spec.rb index 41c22a2d0e3..cd53adad8c8 100644 --- a/core/spec/lib/i18n_spec.rb +++ b/core/spec/lib/i18n_spec.rb @@ -1,6 +1,5 @@ require 'spec_helper' require 'spree/i18n' -require 'spree/testing_support/i18n' RSpec.describe "i18n" do before do @@ -20,20 +19,20 @@ end it "translates within the spree scope" do - expect(Spree.normal_t(:foo)).to eql("bar") + expect(Spree.t(:foo)).to eql("bar") expect(Spree.translate(:foo)).to eql("bar") end it "prepends a string scope" do - expect(Spree.normal_t(:foo, scope: "bar")).to eql("bar within bar scope") + expect(Spree.t(:foo, scope: "bar")).to eql("bar within bar scope") end it "prepends to an array scope" do - expect(Spree.normal_t(:foo, scope: ["bar"])).to eql("bar within bar scope") + expect(Spree.t(:foo, scope: ["bar"])).to eql("bar within bar scope") end it "returns two translations" do - expect(Spree.normal_t([:foo, 'bar.foo'])).to eql(["bar", "bar within bar scope"]) + expect(Spree.t([:foo, 'bar.foo'])).to eql(["bar", "bar within bar scope"]) end it "returns reasonable string for missing translations" do @@ -43,68 +42,4 @@ it "should have a Spree::I18N_GENERIC_PLURAL constant" do expect(Spree::I18N_GENERIC_PLURAL).to eq 2.1 end - - context "missed + unused translations" do - def key_with_locale(key) - "#{key} (#{I18n.locale})" - end - - before do - Spree.used_translations = [] - end - - context "missed translations" do - def assert_missing_translation(key) - key = key_with_locale(key) - message = Spree.missing_translation_messages.detect { |m| m == key } - expect(message).not_to(be_nil, "expected '#{key}' to be missing, but it wasn't.") - end - - it "logs missing translations" do - Spree.t(:missing, scope: [:else, :where]) - Spree.check_missing_translations - assert_missing_translation("else") - assert_missing_translation("else.where") - assert_missing_translation("else.where.missing") - end - - it "does not log present translations" do - Spree.t(:foo) - Spree.check_missing_translations - expect(Spree.missing_translation_messages).to be_empty - end - - it "does not break when asked for multiple translations" do - Spree.t [:foo, 'bar.foo'] - Spree.check_missing_translations - expect(Spree.missing_translation_messages).to be_empty - end - end - - context "unused translations" do - def assert_unused_translation(key) - key = key_with_locale(key) - message = Spree.unused_translation_messages.detect { |m| m == key } - expect(message).not_to(be_nil, "expected '#{key}' to be unused, but it was used.") - end - - def assert_used_translation(key) - key = key_with_locale(key) - message = Spree.unused_translation_messages.detect { |m| m == key } - expect(message).to(be_nil, "expected '#{key}' to be used, but it wasn't.") - end - - it "logs translations that aren't used" do - Spree.check_unused_translations - assert_unused_translation("bar.legacy_translation") - assert_unused_translation("legacy_translation") - end - - it "does not log used translations" do - Spree.t(:foo) - Spree.check_unused_translations - assert_used_translation("foo") - end - end - end end diff --git a/core/spec/rails_helper.rb b/core/spec/rails_helper.rb index 0042a614731..e48b3e7697b 100644 --- a/core/spec/rails_helper.rb +++ b/core/spec/rails_helper.rb @@ -13,10 +13,6 @@ Dir["./spec/support/**/*.rb"].sort.each { |f| require f } -if ENV["CHECK_TRANSLATIONS"] - require "spree/testing_support/i18n" -end - require 'spree/testing_support/factories' require 'spree/testing_support/preferences' require 'cancan/matchers' diff --git a/frontend/spec/spec_helper.rb b/frontend/spec/spec_helper.rb index cb11446d295..bf74153441b 100644 --- a/frontend/spec/spec_helper.rb +++ b/frontend/spec/spec_helper.rb @@ -28,10 +28,6 @@ require 'database_cleaner' -if ENV["CHECK_TRANSLATIONS"] - require "spree/testing_support/i18n" -end - require 'spree/testing_support/authorization_helpers' require 'spree/testing_support/capybara_ext' require 'spree/testing_support/factories'