|
| 1 | +require 'spec_helper' |
| 2 | +require 'tmpdir' |
| 3 | + |
| 4 | +describe Fastlane::Actions::IosDownloadStringsFilesFromGlotpressAction do |
| 5 | + let(:test_data_dir) { File.join(File.dirname(__FILE__), 'test-data', 'translations', 'ios_generate_strings_file_from_code') } |
| 6 | + let(:gp_fake_url) { 'https://stub.glotpress.com/rspec-fake-project' } |
| 7 | + let(:locales_subset) { { 'fr-FR': 'fr', 'zh-cn': 'zh-Hans' } } |
| 8 | + |
| 9 | + def gp_stub(locale:, query:) |
| 10 | + stub_request(:get, "#{gp_fake_url}/#{locale}/default/export-translations").with(query: query) |
| 11 | + end |
| 12 | + |
| 13 | + describe 'downloading export files from GlotPress' do |
| 14 | + def test_gp_download(filters:, tablename:, expected_gp_params:) |
| 15 | + Dir.mktmpdir('a8c-release-toolkit-tests-') do |tmp_dir| |
| 16 | + # Arrange |
| 17 | + stub_fr = gp_stub(locale: 'fr-FR', query: expected_gp_params).to_return(body: '"key" = "fr-copy";') |
| 18 | + stub_zh = gp_stub(locale: 'zh-cn', query: expected_gp_params).to_return(body: '"key" = "zh-copy";') |
| 19 | + |
| 20 | + # Act |
| 21 | + # Note: The use of `.compact` here allows us to remove the keys (like `table_basename` and `filters`) from the `Hash` whose values are `nil`, |
| 22 | + # so that we don't include those parameters at all in the action's call site -- making them use the default value from their respective |
| 23 | + # `ConfigItem` (as opposed to passing a value of `nil` explicitly and overwrite the default value, which is not what we want to test). |
| 24 | + run_described_fastlane_action({ |
| 25 | + project_url: gp_fake_url, |
| 26 | + locales: locales_subset, |
| 27 | + download_dir: tmp_dir, |
| 28 | + table_basename: tablename, |
| 29 | + filters: filters |
| 30 | + }.compact) |
| 31 | + |
| 32 | + # Assert |
| 33 | + expect(stub_fr).to have_been_made.once |
| 34 | + file_fr = File.join(tmp_dir, 'fr.lproj', "#{tablename || 'Localizable'}.strings") |
| 35 | + expect(File).to exist(file_fr) |
| 36 | + expect(File.read(file_fr)).to eq('"key" = "fr-copy";') |
| 37 | + |
| 38 | + expect(stub_zh).to have_been_made.once |
| 39 | + file_zh = File.join(tmp_dir, 'zh-Hans.lproj', "#{tablename || 'Localizable'}.strings") |
| 40 | + expect(File).to exist(file_zh) |
| 41 | + expect(File.read(file_zh)).to eq('"key" = "zh-copy";') |
| 42 | + end |
| 43 | + end |
| 44 | + |
| 45 | + it 'downloads all the locales into the expected directories' do |
| 46 | + test_gp_download(filters: nil, tablename: nil, expected_gp_params: { 'filters[status]': 'current', format: 'strings' }) |
| 47 | + end |
| 48 | + |
| 49 | + it 'uses the proper filters when exporting the files from GlotPress' do |
| 50 | + test_gp_download( |
| 51 | + filters: { term: 'foo', status: 'review' }, |
| 52 | + tablename: nil, |
| 53 | + expected_gp_params: { 'filters[term]': 'foo', 'filters[status]': 'review', format: 'strings' } |
| 54 | + ) |
| 55 | + end |
| 56 | + |
| 57 | + it 'uses a custom table name for the `.strings` files if provided' do |
| 58 | + test_gp_download( |
| 59 | + filters: nil, |
| 60 | + tablename: 'MyApp', |
| 61 | + expected_gp_params: { 'filters[status]': 'current', format: 'strings' } |
| 62 | + ) |
| 63 | + end |
| 64 | + end |
| 65 | + |
| 66 | + describe 'error handling' do |
| 67 | + it 'shows an error if an invalid locale is provided (404)' do |
| 68 | + Dir.mktmpdir('a8c-release-toolkit-tests-') do |tmp_dir| |
| 69 | + # Arrange |
| 70 | + stub = gp_stub(locale: 'unknown-locale', query: { 'filters[status]': 'current', format: 'strings' }).to_return(status: [404, 'Not Found']) |
| 71 | + error_messages = [] |
| 72 | + allow(FastlaneCore::UI).to receive(:error) { |message| error_messages.append(message) } |
| 73 | + |
| 74 | + # Act |
| 75 | + run_described_fastlane_action( |
| 76 | + project_url: gp_fake_url, |
| 77 | + locales: { 'unknown-locale': 'Base' }, |
| 78 | + download_dir: tmp_dir |
| 79 | + ) |
| 80 | + |
| 81 | + # Assert |
| 82 | + expect(stub).to have_been_made.once |
| 83 | + expect(File).not_to exist(File.join(tmp_dir, 'Base.lproj', 'Localizable.strings')) |
| 84 | + expect(error_messages).to eq(['Error downloading locale `unknown-locale` — 404 Not Found']) |
| 85 | + end |
| 86 | + end |
| 87 | + |
| 88 | + it 'shows an error if the file cannot be written in the destination' do |
| 89 | + # Arrange |
| 90 | + download_dir = '/these/are/not/the/dirs/you/are/looking/for/' |
| 91 | + |
| 92 | + # Act |
| 93 | + act = lambda do |
| 94 | + run_described_fastlane_action( |
| 95 | + project_url: gp_fake_url, |
| 96 | + locales: { 'fr-FR': 'fr' }, |
| 97 | + download_dir: download_dir |
| 98 | + ) |
| 99 | + end |
| 100 | + |
| 101 | + # Assert |
| 102 | + # Note: FastlaneError is the exception raised by UI.user_error! |
| 103 | + expect { act.call }.to raise_error(FastlaneCore::Interface::FastlaneError, "The parent directory `#{download_dir}` (which contains all the `*.lproj` subdirectories) must already exist") |
| 104 | + end |
| 105 | + |
| 106 | + it 'reports if a downloaded file is not a valid `.strings` file' do |
| 107 | + Dir.mktmpdir('a8c-release-toolkit-tests-') do |tmp_dir| |
| 108 | + # Arrange |
| 109 | + stub = gp_stub(locale: 'fr-FR', query: { 'filters[status]': 'current', format: 'strings' }).to_return(body: 'some invalid strings file content') |
| 110 | + error_messages = [] |
| 111 | + allow(FastlaneCore::UI).to receive(:error) { |message| error_messages.append(message) } |
| 112 | + |
| 113 | + # Act |
| 114 | + run_described_fastlane_action( |
| 115 | + project_url: gp_fake_url, |
| 116 | + locales: { 'fr-FR': 'fr' }, |
| 117 | + download_dir: tmp_dir |
| 118 | + ) |
| 119 | + |
| 120 | + # Assert |
| 121 | + expect(stub).to have_been_made.once |
| 122 | + file = File.join(tmp_dir, 'fr.lproj', 'Localizable.strings') |
| 123 | + expect(File).to exist(file) |
| 124 | + expected_error = 'Property List error: Unexpected character s at line 1 / JSON error: JSON text did not start with array or object and option to allow fragments not set.' |
| 125 | + expect(error_messages.count).to eq(1) |
| 126 | + expect(error_messages.first).to start_with("Error while validating the file exported from GlotPress (`#{file}`) - #{file}: #{expected_error}") # Different versions of `plutil` might append the line/column as well, but not all. |
| 127 | + end |
| 128 | + end |
| 129 | + |
| 130 | + it 'reports if a downloaded file has empty translations' do |
| 131 | + Dir.mktmpdir('a8c-release-toolkit-tests-') do |tmp_dir| |
| 132 | + # Arrange |
| 133 | + stub = gp_stub(locale: 'fr-FR', query: { 'filters[status]': 'current', format: 'strings' }) |
| 134 | + .to_return(body: ['"key1" = "value1";', '"key2" = "";', '"key3" = "";', '/* translators: use "" quotes please */', '"key4" = "value4";'].join("\n")) |
| 135 | + error_messages = [] |
| 136 | + allow(FastlaneCore::UI).to receive(:error) { |message| error_messages.append(message) } |
| 137 | + |
| 138 | + # Act |
| 139 | + run_described_fastlane_action( |
| 140 | + project_url: gp_fake_url, |
| 141 | + locales: { 'fr-FR': 'fr' }, |
| 142 | + download_dir: tmp_dir |
| 143 | + ) |
| 144 | + |
| 145 | + # Assert |
| 146 | + expect(stub).to have_been_made.once |
| 147 | + file = File.join(tmp_dir, 'fr.lproj', 'Localizable.strings') |
| 148 | + expect(File).to exist(file) |
| 149 | + expected_error = <<~MSG.chomp |
| 150 | + Found empty translations in `#{file}` for the following keys: ["key2", "key3"]. |
| 151 | + This is likely a GlotPress bug, and will lead to copies replaced by empty text in the UI. |
| 152 | + Please report this to the GlotPress team, and fix the file locally before continuing. |
| 153 | + MSG |
| 154 | + expect(error_messages).to eq([expected_error]) |
| 155 | + end |
| 156 | + end |
| 157 | + |
| 158 | + it 'does not report invalid downloaded files if `skip_file_validation:true`' do |
| 159 | + Dir.mktmpdir('a8c-release-toolkit-tests-') do |tmp_dir| |
| 160 | + # Arrange |
| 161 | + stub = gp_stub(locale: 'fr-FR', query: { 'filters[status]': 'current', format: 'strings' }).to_return(body: 'some invalid strings file content') |
| 162 | + error_messages = [] |
| 163 | + allow(FastlaneCore::UI).to receive(:error) { |message| error_messages.append(message) } |
| 164 | + |
| 165 | + # Act |
| 166 | + act = lambda do |
| 167 | + run_described_fastlane_action( |
| 168 | + project_url: gp_fake_url, |
| 169 | + locales: { 'fr-FR': 'fr' }, |
| 170 | + download_dir: tmp_dir, |
| 171 | + skip_file_validation: true |
| 172 | + ) |
| 173 | + end |
| 174 | + |
| 175 | + # Assert |
| 176 | + expect { act.call }.not_to raise_error |
| 177 | + expect(stub).to have_been_made.once |
| 178 | + file = File.join(tmp_dir, 'fr.lproj', 'Localizable.strings') |
| 179 | + expect(File).to exist(file) |
| 180 | + expect(error_messages).to eq([]) |
| 181 | + end |
| 182 | + end |
| 183 | + end |
| 184 | +end |
0 commit comments