# frozen_string_literal: true require "#{File.dirname(__FILE__)}/../ext/localizationlinter/localizationlinter.rb" require File.expand_path('spec_helper', __dir__) # rubocop:disable Metrics/BlockLength module Danger describe Danger::DangerLocalizationlinter do it 'should be a plugin' do expect(Danger::DangerLocalizationlinter.new(nil)).to be_a Danger::Plugin end describe 'with Dangerfile' do before do @fixtures_path = "#{File.dirname(__FILE__)}/support/fixtures" @dangerfile = testing_dangerfile @my_plugin = @dangerfile.localizationlinter @my_plugin.verbose = true @my_plugin.clean_after_execution = false @my_plugin.language_configuration_path = "#{@fixtures_path}/all_target_main_language.yml" @my_plugin.derived_data_path = "#{@fixtures_path}/derived_data" @my_plugin.resource_path = "#{@fixtures_path}/resources" @ok_target = 'TEST_OK' @main_region = 'en' @test_region = 'fr' @main_file_name = @my_plugin.linter.temporary_localizable_check_file_path(@ok_target, @main_region) end it 'Test main_region_for_target' do expect(@my_plugin.linter.main_region_for_target(@ok_target)).to eq(@main_region) end it 'Test temporary_localizable_check_file_path' do expect(@main_file_name).to eq("#{@my_plugin.derived_data_path}/#{@ok_target}_#{@main_region}") end it 'Test get_hash' do # rubocop:disable Style/WordArray result = { 'L10n' => { 'StringKey' => ['Int'], 'localized_key' => [], 'localized_key.sub_path_param' => ['String', 'Int'], 'localized_key_param' => ['String', 'String'] } } # rubocop:enable Style/WordArray expect(@my_plugin.linter.get_hash(@main_file_name)).to eq(result) end it 'Test check_missing_keys_in_target OK' do hash = @my_plugin.linter.get_hash(@main_file_name) expect { @my_plugin.linter.check_missing_key(@ok_target, @test_region, @main_region, hash) } .to raise_error(LocalizationLinter::RegionHashIdenticalAsMainOne) end it 'Test check_missing_keys_in_target KO' do target = 'TEST_KO' main_file_name = @my_plugin.linter.temporary_localizable_check_file_path(target, @main_region) hash = @my_plugin.linter.get_hash(main_file_name) fixtures_aboslute_path = "#{Dir.pwd}/spec/support/fixtures" filepath = "#{fixtures_aboslute_path}/resources/#{target}/#{@test_region}.lproj/Localizable.strings" # rubocop:disable Style/HashSyntax result = [ { :filename => filepath, :key => 'localized_key_useless_key', :line => 8, :message => 'useless key: /L10n/localized_key_useless_key' }, { :filename => filepath, :key => 'localized_key_missing_key', :line => 0, :message => 'missing key: /L10n/localized_key_missing_key' }, { :filename => filepath, :key => 'localized_key.sub_path_param_wrong_param', :line => 11, :message => 'wrong parameters in key: /L10n/localized_key.sub_path_param_wrong_param/1/String' } ] # rubocop:enable Style/HashSyntax expect(@my_plugin.linter.check_missing_key(target, @test_region, @main_region, hash)).to eq(result) end it 'Test write_l10n_check_file with SwiftGen' do target = 'TEST' @my_plugin.swift_gen_path = "#{File.dirname(__FILE__)}/Test/Pods/SwiftGen/bin/swiftgen" base_path = "#{@my_plugin.derived_data_path}/#{@ok_target}_#{@main_region}" result_path = "#{@my_plugin.derived_data_path}/#{target}_#{@main_region}" File.delete(result_path) if File.exist?(result_path) @my_plugin.linter.write_l10n_check_file(target, @main_region) expect(@my_plugin.linter.get_hash(result_path)).to eq(@my_plugin.linter.get_hash(base_path)) File.delete(result_path) if File.exist?(result_path) end it 'Test end to end in a project' do @my_plugin.swift_gen_path = "#{File.dirname(__FILE__)}/Test/Pods/SwiftGen/bin/swiftgen" @my_plugin.project_path = "#{File.dirname(__FILE__)}/Test/Test.xcodeproj" @my_plugin.resource_path = "#{File.dirname(__FILE__)}/Test" @my_plugin.language_configuration_path = "#{File.dirname(__FILE__)}/Test/Test/all_target_main_language.yml" @my_plugin.derived_data_path = "#{File.dirname(__FILE__)}/Test/derived_data" @my_plugin.inline_mode = true @my_plugin.lint output = @my_plugin.status_report[:warnings] expect(output.size).to eq(3) expect(output[0]).to include('useless key: /L10n/french_key') expect(output[1]).to include('missing key: /L10n/english_key') expect(output[2]).to include('missing key: /L10n/common_parametered_key/0') end end end end # rubocop:enable Metrics/BlockLength