|
| 1 | +require 'spec_helper' |
| 2 | +require 'tmpdir' |
| 3 | + |
| 4 | +describe Fastlane::Actions::IosExtractKeysFromStringsFilesAction do |
| 5 | + let(:test_data_dir) { File.join(File.dirname(__FILE__), 'test-data', 'translations', 'ios_extract_keys_from_strings_files') } |
| 6 | + |
| 7 | + describe 'extract the right keys from `Localizable.strings` to all locales' do |
| 8 | + def assert_output_files_match(expectations_map) |
| 9 | + expectations_map.each do |output_file, expected_file| |
| 10 | + expect(File).to exist(output_file), "expected `#{output_file}` to exist" |
| 11 | + expect(File.read(output_file)).to eq(File.read(File.join(test_data_dir, expected_file))), "expected content of `#{output_file}` to match `#{expected_file}`" |
| 12 | + end |
| 13 | + end |
| 14 | + |
| 15 | + it 'can extract keys to a single `InfoPlist.strings` target table' do |
| 16 | + in_tmp_dir do |tmp_dir| |
| 17 | + # Arrange |
| 18 | + lproj_source_dir = File.join(tmp_dir, 'LocalizationFiles') |
| 19 | + FileUtils.cp_r(File.join(test_data_dir, 'Resources', '.'), lproj_source_dir) |
| 20 | + |
| 21 | + # Act |
| 22 | + run_described_fastlane_action( |
| 23 | + source_parent_dir: lproj_source_dir, |
| 24 | + target_original_files: File.join(lproj_source_dir, 'en.lproj', 'InfoPlist.strings') |
| 25 | + ) |
| 26 | + |
| 27 | + # Assert |
| 28 | + assert_output_files_match( |
| 29 | + File.join(lproj_source_dir, 'fr.lproj', 'InfoPlist.strings') => 'InfoPlist-expected-fr.strings', |
| 30 | + File.join(lproj_source_dir, 'zh-Hans.lproj', 'InfoPlist.strings') => 'InfoPlist-expected-zh-Hans.strings' |
| 31 | + ) |
| 32 | + end |
| 33 | + end |
| 34 | + |
| 35 | + it 'can extract keys to multiple `.strings` files' do |
| 36 | + in_tmp_dir do |tmp_dir| |
| 37 | + # Arrange |
| 38 | + resources_dir = File.join(tmp_dir, 'Resources') |
| 39 | + siri_intent_dir = File.join(tmp_dir, 'SiriIntentTarget') |
| 40 | + FileUtils.cp_r(File.join(test_data_dir, 'Resources', '.'), resources_dir) |
| 41 | + FileUtils.cp_r(File.join(test_data_dir, 'SiriIntentTarget', '.'), siri_intent_dir) |
| 42 | + |
| 43 | + # Act |
| 44 | + run_described_fastlane_action( |
| 45 | + source_parent_dir: resources_dir, |
| 46 | + target_original_files: [ |
| 47 | + File.join(resources_dir, 'en.lproj', 'InfoPlist.strings'), |
| 48 | + File.join(siri_intent_dir, 'en.lproj', 'Sites.strings'), |
| 49 | + ] |
| 50 | + ) |
| 51 | + |
| 52 | + # Assert |
| 53 | + assert_output_files_match( |
| 54 | + File.join(resources_dir, 'fr.lproj', 'InfoPlist.strings') => 'InfoPlist-expected-fr.strings', |
| 55 | + File.join(siri_intent_dir, 'fr.lproj', 'Sites.strings') => 'Sites-expected-fr.strings', |
| 56 | + File.join(resources_dir, 'zh-Hans.lproj', 'InfoPlist.strings') => 'InfoPlist-expected-zh-Hans.strings', |
| 57 | + File.join(siri_intent_dir, 'zh-Hans.lproj', 'Sites.strings') => 'Sites-expected-zh-Hans.strings' |
| 58 | + ) |
| 59 | + end |
| 60 | + end |
| 61 | + |
| 62 | + it 'supports using an input file other than `Localizable.strings`' do |
| 63 | + in_tmp_dir do |tmp_dir| |
| 64 | + # Arrange |
| 65 | + lproj_source_dir = File.join(tmp_dir, 'NonStandardFiles') |
| 66 | + FileUtils.cp_r(File.join(test_data_dir, 'Resources', '.'), lproj_source_dir) |
| 67 | + Dir.glob('**/Localizable.strings', base: lproj_source_dir).each do |file| |
| 68 | + src_file = File.join(lproj_source_dir, file) |
| 69 | + FileUtils.mv(src_file, File.join(File.dirname(src_file), 'GlotPressTranslations.strings')) |
| 70 | + end |
| 71 | + |
| 72 | + # Act |
| 73 | + run_described_fastlane_action( |
| 74 | + source_parent_dir: lproj_source_dir, |
| 75 | + source_tablename: 'GlotPressTranslations', |
| 76 | + target_original_files: File.join(lproj_source_dir, 'en.lproj', 'InfoPlist.strings') |
| 77 | + ) |
| 78 | + |
| 79 | + # Assert |
| 80 | + assert_output_files_match( |
| 81 | + File.join(lproj_source_dir, 'fr.lproj', 'InfoPlist.strings') => 'InfoPlist-expected-fr.strings', |
| 82 | + File.join(lproj_source_dir, 'zh-Hans.lproj', 'InfoPlist.strings') => 'InfoPlist-expected-zh-Hans.strings' |
| 83 | + ) |
| 84 | + end |
| 85 | + end |
| 86 | + |
| 87 | + it 'does not overwrite the original files' do |
| 88 | + in_tmp_dir do |tmp_dir| |
| 89 | + # Arrange |
| 90 | + lproj_source_dir = File.join(tmp_dir, 'Resources') |
| 91 | + FileUtils.cp_r(File.join(test_data_dir, 'Resources', '.'), lproj_source_dir) |
| 92 | + |
| 93 | + # Act |
| 94 | + run_described_fastlane_action( |
| 95 | + source_parent_dir: lproj_source_dir, |
| 96 | + target_original_files: File.join(lproj_source_dir, 'en.lproj', 'InfoPlist.strings') |
| 97 | + ) |
| 98 | + |
| 99 | + # Assert |
| 100 | + assert_output_files_match( |
| 101 | + File.join(lproj_source_dir, 'en.lproj', 'InfoPlist.strings') => File.join('Resources', 'en.lproj', 'InfoPlist.strings') |
| 102 | + ) |
| 103 | + end |
| 104 | + end |
| 105 | + end |
| 106 | + |
| 107 | + describe 'input parameters validation' do |
| 108 | + it 'errors if the source dir does not exist' do |
| 109 | + in_tmp_dir do |tmp_dir| |
| 110 | + FileUtils.cp_r(File.join(test_data_dir, 'Resources', '.'), tmp_dir) |
| 111 | + |
| 112 | + expect do |
| 113 | + run_described_fastlane_action( |
| 114 | + source_parent_dir: '/this/is/not/the/dir/you/are/looking/for/', |
| 115 | + target_original_files: File.join(tmp_dir, 'en.lproj', 'InfoPlist.strings') |
| 116 | + ) |
| 117 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, '`source_parent_dir` should be a path to an existing directory, but found `/this/is/not/the/dir/you/are/looking/for/`.') |
| 118 | + end |
| 119 | + end |
| 120 | + |
| 121 | + it 'errors if the source dir does not contain any `.lproj` subfolder' do |
| 122 | + in_tmp_dir do |tmp_dir| |
| 123 | + FileUtils.cp_r(File.join(test_data_dir, 'Resources', '.'), tmp_dir) |
| 124 | + src_dir = File.join(tmp_dir, 'EmptyDir') |
| 125 | + FileUtils.mkdir_p(src_dir) |
| 126 | + |
| 127 | + expect do |
| 128 | + run_described_fastlane_action( |
| 129 | + source_parent_dir: src_dir, |
| 130 | + target_original_files: File.join(tmp_dir, 'en.lproj', 'InfoPlist.strings') |
| 131 | + ) |
| 132 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, "`source_parent_dir` should contain at least one `.lproj` subdirectory, but `#{src_dir}` does not contain any.") |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + it 'errors if no target original files provided' do |
| 137 | + in_tmp_dir do |tmp_dir| |
| 138 | + FileUtils.cp_r(File.join(test_data_dir, 'Resources', '.'), tmp_dir) |
| 139 | + |
| 140 | + expect do |
| 141 | + run_described_fastlane_action( |
| 142 | + source_parent_dir: tmp_dir, |
| 143 | + target_original_files: [] |
| 144 | + ) |
| 145 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, '`target_original_files` must contain at least one path to an original `.strings` file.') |
| 146 | + end |
| 147 | + end |
| 148 | + |
| 149 | + it 'errors if one of the target original files does not exist' do |
| 150 | + in_tmp_dir do |tmp_dir| |
| 151 | + FileUtils.cp_r(File.join(test_data_dir, 'Resources', '.'), tmp_dir) |
| 152 | + non_existing_target_file = File.join(tmp_dir, 'does', 'not', 'exist') |
| 153 | + expect do |
| 154 | + run_described_fastlane_action( |
| 155 | + source_parent_dir: tmp_dir, |
| 156 | + target_original_files: [ |
| 157 | + File.join(tmp_dir, 'en.lproj', 'InfoPlist.strings'), |
| 158 | + non_existing_target_file, |
| 159 | + ] |
| 160 | + ) |
| 161 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, "Path `#{non_existing_target_file}` (found in `target_original_files`) does not exist.") |
| 162 | + end |
| 163 | + end |
| 164 | + |
| 165 | + it 'errors if one of the target original files does not point to a path like `**/*.lproj/*.strings`' do |
| 166 | + in_tmp_dir do |tmp_dir| |
| 167 | + FileUtils.cp_r(File.join(test_data_dir, 'Resources', '.'), tmp_dir) |
| 168 | + misleading_target_file = File.join(tmp_dir, 'en.lproj', 'Info.plist') |
| 169 | + FileUtils.cp(File.join(tmp_dir, 'en.lproj', 'InfoPlist.strings'), misleading_target_file) |
| 170 | + |
| 171 | + expect do |
| 172 | + run_described_fastlane_action( |
| 173 | + source_parent_dir: tmp_dir, |
| 174 | + target_original_files: misleading_target_file |
| 175 | + ) |
| 176 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, "Expected `#{misleading_target_file}` (found in `target_original_files`) to be a path ending in a `*.lproj/*.strings`.") |
| 177 | + end |
| 178 | + end |
| 179 | + end |
| 180 | + |
| 181 | + describe 'error handling during processing' do |
| 182 | + it 'errors when failing to read a source file' do |
| 183 | + in_tmp_dir do |tmp_dir| |
| 184 | + lproj_source_dir = File.join(tmp_dir, 'Resources') |
| 185 | + FileUtils.cp_r(File.join(test_data_dir, 'Resources', '.'), lproj_source_dir) |
| 186 | + broken_file_path = File.join(lproj_source_dir, 'fr.lproj', 'Localizable.strings') |
| 187 | + File.write(broken_file_path, 'Invalid strings file content') |
| 188 | + |
| 189 | + expect do |
| 190 | + run_described_fastlane_action( |
| 191 | + source_parent_dir: lproj_source_dir, |
| 192 | + target_original_files: File.join(lproj_source_dir, 'en.lproj', 'InfoPlist.strings') |
| 193 | + ) |
| 194 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, /^Error while reading the translations from source file `#{broken_file_path}`: #{broken_file_path}: Property List error/) |
| 195 | + end |
| 196 | + end |
| 197 | + |
| 198 | + it 'errors if fails to read the keys to extract from a target originals file' do |
| 199 | + in_tmp_dir do |tmp_dir| |
| 200 | + lproj_source_dir = File.join(tmp_dir, 'Resources') |
| 201 | + FileUtils.cp_r(File.join(test_data_dir, 'Resources', '.'), lproj_source_dir) |
| 202 | + broken_file_path = File.join(lproj_source_dir, 'en.lproj', 'InfoPlist.strings') |
| 203 | + File.write(broken_file_path, 'Invalid strings file content') |
| 204 | + |
| 205 | + expect do |
| 206 | + run_described_fastlane_action( |
| 207 | + source_parent_dir: lproj_source_dir, |
| 208 | + target_original_files: File.join(lproj_source_dir, 'en.lproj', 'InfoPlist.strings') |
| 209 | + ) |
| 210 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, /^Failed to read the keys to extract from originals file: #{broken_file_path}: Property List error/) |
| 211 | + end |
| 212 | + end |
| 213 | + |
| 214 | + it 'errors it if fails to write one of the target files' do |
| 215 | + in_tmp_dir do |tmp_dir| |
| 216 | + lproj_source_dir = File.join(tmp_dir, 'Resources') |
| 217 | + FileUtils.cp_r(File.join(test_data_dir, 'Resources', '.'), lproj_source_dir) |
| 218 | + allow(File).to receive(:write) { raise 'Stubbed IO error' } |
| 219 | + |
| 220 | + expect do |
| 221 | + run_described_fastlane_action( |
| 222 | + source_parent_dir: lproj_source_dir, |
| 223 | + target_original_files: File.join(lproj_source_dir, 'en.lproj', 'InfoPlist.strings') |
| 224 | + ) |
| 225 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, /Error while writing extracted translations to `#{File.join(lproj_source_dir, '.*\.lproj', 'InfoPlist\.strings')}`: Stubbed IO error/) |
| 226 | + end |
| 227 | + end |
| 228 | + end |
| 229 | +end |
0 commit comments