|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'spec_helper' |
| 4 | +require 'tmpdir' |
| 5 | +require 'fileutils' |
| 6 | + |
| 7 | +describe Fastlane::Actions::AndroidPruneOrphanedTranslationsAction do |
| 8 | + # Writes `content` to `path`, creating intermediate directories. |
| 9 | + def write_file(path, content) |
| 10 | + FileUtils.mkdir_p(File.dirname(path)) |
| 11 | + File.write(path, content) |
| 12 | + end |
| 13 | + |
| 14 | + # A default `values/strings.xml` declaring `hello`, `bye`, an array and a plural. |
| 15 | + let(:default_strings) do |
| 16 | + <<~XML |
| 17 | + <?xml version="1.0" encoding="UTF-8"?> |
| 18 | + <resources> |
| 19 | + <string name="hello">Hello</string> |
| 20 | + <string name="bye">Bye</string> |
| 21 | + <string-array name="planets"> |
| 22 | + <item>Earth</item> |
| 23 | + </string-array> |
| 24 | + <plurals name="items"> |
| 25 | + <item quantity="one">%d item</item> |
| 26 | + <item quantity="other">%d items</item> |
| 27 | + </plurals> |
| 28 | + </resources> |
| 29 | + XML |
| 30 | + end |
| 31 | + |
| 32 | + it 'removes only the entries whose key is not in the default strings, keeping the rest intact' do |
| 33 | + Dir.mktmpdir do |dir| |
| 34 | + res_dir = File.join(dir, 'res') |
| 35 | + write_file(File.join(res_dir, 'values', 'strings.xml'), default_strings) |
| 36 | + fr_file = File.join(res_dir, 'values-fr', 'strings.xml') |
| 37 | + write_file(fr_file, <<~XML) |
| 38 | + <?xml version="1.0" encoding="UTF-8"?> |
| 39 | + <resources> |
| 40 | + <string name="hello">Bonjour</string> |
| 41 | + <string name="orphan_string">Orphelin</string> |
| 42 | + <string name="bye">Au revoir</string> |
| 43 | + <plurals name="orphan_plural"> |
| 44 | + <item quantity="one">%d truc</item> |
| 45 | + <item quantity="other">%d trucs</item> |
| 46 | + </plurals> |
| 47 | + </resources> |
| 48 | + XML |
| 49 | + |
| 50 | + pruned = run_described_fastlane_action(res_dir: res_dir) |
| 51 | + |
| 52 | + expect(pruned).to eq(2) |
| 53 | + content = File.read(fr_file) |
| 54 | + expect(content).to include('name="hello"', 'name="bye"') |
| 55 | + expect(content).not_to include('orphan_string', 'orphan_plural') |
| 56 | + # No blank line left behind where the orphaned <string> was removed. |
| 57 | + expect(content).not_to match(/\n[[:space:]]*\n[[:space:]]*<string name="bye"/) |
| 58 | + end |
| 59 | + end |
| 60 | + |
| 61 | + it 'leaves non-locale qualifier directories (e.g. values-night) untouched' do |
| 62 | + Dir.mktmpdir do |dir| |
| 63 | + res_dir = File.join(dir, 'res') |
| 64 | + write_file(File.join(res_dir, 'values', 'strings.xml'), default_strings) |
| 65 | + # A non-locale qualifier dir with a key absent from the default must NOT be pruned. |
| 66 | + night_file = File.join(res_dir, 'values-night', 'strings.xml') |
| 67 | + night_content = <<~XML |
| 68 | + <?xml version="1.0" encoding="UTF-8"?> |
| 69 | + <resources> |
| 70 | + <string name="night_only">Night</string> |
| 71 | + </resources> |
| 72 | + XML |
| 73 | + write_file(night_file, night_content) |
| 74 | + # A real locale dir with an orphan, to confirm pruning still happens there. |
| 75 | + fr_file = File.join(res_dir, 'values-fr', 'strings.xml') |
| 76 | + write_file(fr_file, <<~XML) |
| 77 | + <?xml version="1.0" encoding="UTF-8"?> |
| 78 | + <resources> |
| 79 | + <string name="hello">Bonjour</string> |
| 80 | + <string name="orphan_string">Orphelin</string> |
| 81 | + </resources> |
| 82 | + XML |
| 83 | + |
| 84 | + pruned = run_described_fastlane_action(res_dir: res_dir) |
| 85 | + |
| 86 | + expect(pruned).to eq(1) |
| 87 | + expect(File.read(night_file)).to eq(night_content) |
| 88 | + expect(File.read(fr_file)).not_to include('orphan_string') |
| 89 | + end |
| 90 | + end |
| 91 | + |
| 92 | + it 'leaves car UI mode qualifier directories untouched' do |
| 93 | + Dir.mktmpdir do |dir| |
| 94 | + res_dir = File.join(dir, 'res') |
| 95 | + write_file(File.join(res_dir, 'values', 'strings.xml'), default_strings) |
| 96 | + car_file = File.join(res_dir, 'values-car', 'strings.xml') |
| 97 | + car_content = <<~XML |
| 98 | + <?xml version="1.0" encoding="UTF-8"?> |
| 99 | + <resources> |
| 100 | + <string name="car_only">Car</string> |
| 101 | + </resources> |
| 102 | + XML |
| 103 | + write_file(car_file, car_content) |
| 104 | + |
| 105 | + pruned = run_described_fastlane_action(res_dir: res_dir) |
| 106 | + |
| 107 | + expect(pruned).to eq(0) |
| 108 | + expect(File.read(car_file)).to eq(car_content) |
| 109 | + end |
| 110 | + end |
| 111 | + |
| 112 | + # `kmr` (Northern Kurdish) is a real 3-letter legacy locale used by e.g. WordPress-Android, so it must still be |
| 113 | + # pruned — guarding against an over-eager "restrict locales to 2 letters" fix for the `values-car` collision. |
| 114 | + it 'prunes 3-letter legacy locale directories (e.g. values-kmr)' do |
| 115 | + Dir.mktmpdir do |dir| |
| 116 | + res_dir = File.join(dir, 'res') |
| 117 | + write_file(File.join(res_dir, 'values', 'strings.xml'), default_strings) |
| 118 | + kmr_file = File.join(res_dir, 'values-kmr', 'strings.xml') |
| 119 | + write_file(kmr_file, <<~XML) |
| 120 | + <?xml version="1.0" encoding="UTF-8"?> |
| 121 | + <resources> |
| 122 | + <string name="hello">Silav</string> |
| 123 | + <string name="orphan_string">Sêwî</string> |
| 124 | + </resources> |
| 125 | + XML |
| 126 | + |
| 127 | + pruned = run_described_fastlane_action(res_dir: res_dir) |
| 128 | + |
| 129 | + expect(pruned).to eq(1) |
| 130 | + content = File.read(kmr_file) |
| 131 | + expect(content).to include('name="hello"') |
| 132 | + expect(content).not_to include('orphan_string') |
| 133 | + end |
| 134 | + end |
| 135 | + |
| 136 | + it 'treats keys from `additional_source_strings_paths` as valid (flavor overlay case)' do |
| 137 | + Dir.mktmpdir do |dir| |
| 138 | + res_dir = File.join(dir, 'res') |
| 139 | + write_file(File.join(res_dir, 'values', 'strings.xml'), <<~XML) |
| 140 | + <?xml version="1.0" encoding="UTF-8"?> |
| 141 | + <resources> |
| 142 | + <string name="flavor_only">Flavor</string> |
| 143 | + </resources> |
| 144 | + XML |
| 145 | + base_strings = File.join(dir, 'base', 'values', 'strings.xml') |
| 146 | + write_file(base_strings, default_strings) |
| 147 | + fr_file = File.join(res_dir, 'values-fr', 'strings.xml') |
| 148 | + write_file(fr_file, <<~XML) |
| 149 | + <?xml version="1.0" encoding="UTF-8"?> |
| 150 | + <resources> |
| 151 | + <string name="flavor_only">Saveur</string> |
| 152 | + <string name="hello">Bonjour</string> |
| 153 | + <string name="orphan_string">Orphelin</string> |
| 154 | + </resources> |
| 155 | + XML |
| 156 | + |
| 157 | + pruned = run_described_fastlane_action(res_dir: res_dir, additional_source_strings_paths: [base_strings]) |
| 158 | + |
| 159 | + expect(pruned).to eq(1) |
| 160 | + content = File.read(fr_file) |
| 161 | + expect(content).to include('name="flavor_only"', 'name="hello"') |
| 162 | + expect(content).not_to include('orphan_string') |
| 163 | + end |
| 164 | + end |
| 165 | + |
| 166 | + it 'does nothing and reports zero when there are no orphaned entries' do |
| 167 | + Dir.mktmpdir do |dir| |
| 168 | + res_dir = File.join(dir, 'res') |
| 169 | + write_file(File.join(res_dir, 'values', 'strings.xml'), default_strings) |
| 170 | + fr_file = File.join(res_dir, 'values-fr', 'strings.xml') |
| 171 | + fr_content = <<~XML |
| 172 | + <?xml version="1.0" encoding="UTF-8"?> |
| 173 | + <resources> |
| 174 | + <string name="hello">Bonjour</string> |
| 175 | + <string name="bye">Au revoir</string> |
| 176 | + </resources> |
| 177 | + XML |
| 178 | + write_file(fr_file, fr_content) |
| 179 | + |
| 180 | + pruned = run_described_fastlane_action(res_dir: res_dir) |
| 181 | + |
| 182 | + expect(pruned).to eq(0) |
| 183 | + expect(File.read(fr_file)).to eq(fr_content) |
| 184 | + end |
| 185 | + end |
| 186 | + |
| 187 | + it 'raises a clear error when the res dir has no default strings file' do |
| 188 | + Dir.mktmpdir do |dir| |
| 189 | + res_dir = File.join(dir, 'res') |
| 190 | + FileUtils.mkdir_p(res_dir) |
| 191 | + expect do |
| 192 | + run_described_fastlane_action(res_dir: res_dir) |
| 193 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, /Source strings file not found/) |
| 194 | + end |
| 195 | + end |
| 196 | + |
| 197 | + it 'raises a clear error when an additional source strings path is missing' do |
| 198 | + Dir.mktmpdir do |dir| |
| 199 | + res_dir = File.join(dir, 'res') |
| 200 | + write_file(File.join(res_dir, 'values', 'strings.xml'), default_strings) |
| 201 | + expect do |
| 202 | + run_described_fastlane_action(res_dir: res_dir, additional_source_strings_paths: [File.join(dir, 'missing.xml')]) |
| 203 | + end.to raise_error(FastlaneCore::Interface::FastlaneError, /Source strings file not found/) |
| 204 | + end |
| 205 | + end |
| 206 | +end |
0 commit comments