|
3 | 3 |
|
4 | 4 | describe Fastlane::Actions::IosDownloadStringsFilesFromGlotpressAction do
|
5 | 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' } } |
6 | 8 |
|
7 |
| - describe 'downloading export files from GlotPress' do |
8 |
| - let(:gp_fake_url) { 'https://stub.glotpress.com/rspec-fake-project' } |
9 |
| - let(:locales_subset) { {'fr-FR': 'fr', 'zh-cn': 'zh-Hans' } } |
10 |
| - |
11 |
| - def gp_stub(locale:, query:) |
12 |
| - stub_request(:get, "#{gp_fake_url}/#{locale}/default/export-translations").with(query: query) |
13 |
| - end |
| 9 | + def gp_stub(locale:, query:) |
| 10 | + stub_request(:get, "#{gp_fake_url}/#{locale}/default/export-translations").with(query: query) |
| 11 | + end |
14 | 12 |
|
| 13 | + describe 'downloading export files from GlotPress' do |
15 | 14 | def test_gp_download(filters:, tablename:, expected_gp_params:)
|
16 | 15 | Dir.mktmpdir('a8c-release-toolkit-tests-') do |tmp_dir|
|
17 | 16 | # Arrange
|
@@ -55,16 +54,127 @@ def test_gp_download(filters:, tablename:, expected_gp_params:)
|
55 | 54 | it 'uses a custom table name for the `.strings` files if provided' do
|
56 | 55 | test_gp_download(
|
57 | 56 | filters: nil,
|
58 |
| - tablename: "MyApp", |
| 57 | + tablename: 'MyApp', |
59 | 58 | expected_gp_params: { 'filters[status]': 'current', format: 'strings' }
|
60 | 59 | )
|
61 | 60 | end
|
62 | 61 | end
|
63 | 62 |
|
64 | 63 | describe 'error handling' do
|
65 |
| - it 'shows an error if an invalid locale is provided (404)' |
66 |
| - it 'shows an error if the file cannot be written in the destination' |
67 |
| - it 'reports if a downloaded file is invalid by default' |
68 |
| - it 'does not report invalid downloaded files if skip_file_validation:true' |
| 64 | + it 'shows an error if an invalid locale is provided (404)' do |
| 65 | + Dir.mktmpdir('a8c-release-toolkit-tests-') do |tmp_dir| |
| 66 | + # Arrange |
| 67 | + stub = gp_stub(locale: 'unknown-locale', query: { 'filters[status]': 'current', format: 'strings' }).to_return(status: [404, 'Not Found']) |
| 68 | + error_messages = [] |
| 69 | + allow(FastlaneCore::UI).to receive(:error) { |message| error_messages.append(message) } |
| 70 | + |
| 71 | + # Act |
| 72 | + run_described_fastlane_action( |
| 73 | + project_url: gp_fake_url, |
| 74 | + locales: { 'unknown-locale': 'Base' }, |
| 75 | + download_dir: tmp_dir |
| 76 | + ) |
| 77 | + |
| 78 | + # Assert |
| 79 | + expect(stub).to have_been_made.once |
| 80 | + file = File.join(tmp_dir, 'Base.lproj', 'Localizable.strings') |
| 81 | + expect(File).not_to exist(file) |
| 82 | + expect(error_messages).to eq(['Error downloading locale `unknown-locale` — 404 Not Found']) |
| 83 | + end |
| 84 | + end |
| 85 | + |
| 86 | + it 'shows an error if the file cannot be written in the destination' do |
| 87 | + # Arrange |
| 88 | + download_dir = '/these/are/not/the/dirs/you/are/looking/for/' |
| 89 | + |
| 90 | + # Act |
| 91 | + act = lambda do |
| 92 | + run_described_fastlane_action( |
| 93 | + project_url: gp_fake_url, |
| 94 | + locales: { 'fr-FR': 'fr' }, |
| 95 | + download_dir: download_dir |
| 96 | + ) |
| 97 | + end |
| 98 | + |
| 99 | + # Assert |
| 100 | + expect { act.call }.to raise_error(FastlaneCore::Interface::FastlaneError, "The parent directory `#{download_dir}` (which contains all the `*.lproj` subdirectories) must already exist") |
| 101 | + end |
| 102 | + |
| 103 | + it 'reports if a downloaded file is not a valid `.strings` file' do |
| 104 | + Dir.mktmpdir('a8c-release-toolkit-tests-') do |tmp_dir| |
| 105 | + # Arrange |
| 106 | + stub = gp_stub(locale: 'fr-FR', query: { 'filters[status]': 'current', format: 'strings' }).to_return(body: 'some invalid strings file content') |
| 107 | + error_messages = [] |
| 108 | + allow(FastlaneCore::UI).to receive(:error) { |message| error_messages.append(message) } |
| 109 | + |
| 110 | + # Act |
| 111 | + run_described_fastlane_action( |
| 112 | + project_url: gp_fake_url, |
| 113 | + locales: { 'fr-FR': 'fr' }, |
| 114 | + download_dir: tmp_dir |
| 115 | + ) |
| 116 | + |
| 117 | + # Assert |
| 118 | + expect(stub).to have_been_made.once |
| 119 | + file = File.join(tmp_dir, 'fr.lproj', 'Localizable.strings') |
| 120 | + expect(File).to exist(file) |
| 121 | + 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. around line 1, column 0.' |
| 122 | + expect(error_messages).to eq(["Error while validating the file exported from GlotPress (`#{file}`) - #{file}: #{expected_error}"]) |
| 123 | + end |
| 124 | + end |
| 125 | + |
| 126 | + it 'reports if a downloaded file has empty translations' do |
| 127 | + Dir.mktmpdir('a8c-release-toolkit-tests-') do |tmp_dir| |
| 128 | + # Arrange |
| 129 | + stub = gp_stub(locale: 'fr-FR', query: { 'filters[status]': 'current', format: 'strings' }) |
| 130 | + .to_return(body: ['"key1" = "value1";', '"key2" = "";', '"key3" = "";', '/* translators: use "" quotes please */', '"key4" = "value4";'].join("\n")) |
| 131 | + error_messages = [] |
| 132 | + allow(FastlaneCore::UI).to receive(:error) { |message| error_messages.append(message) } |
| 133 | + |
| 134 | + # Act |
| 135 | + run_described_fastlane_action( |
| 136 | + project_url: gp_fake_url, |
| 137 | + locales: { 'fr-FR': 'fr' }, |
| 138 | + download_dir: tmp_dir |
| 139 | + ) |
| 140 | + |
| 141 | + # Assert |
| 142 | + expect(stub).to have_been_made.once |
| 143 | + file = File.join(tmp_dir, 'fr.lproj', 'Localizable.strings') |
| 144 | + expect(File).to exist(file) |
| 145 | + expected_error = <<~MSG.chomp |
| 146 | + Found empty translations in `#{file}` for the following keys: ["key2", "key3"]. |
| 147 | + This is likely a GlotPress bug, and will lead to copies replaced by empty text in the UI. |
| 148 | + Please report this to the GlotPress team, and fix the file locally before continuing. |
| 149 | + MSG |
| 150 | + expect(error_messages).to eq([expected_error]) |
| 151 | + end |
| 152 | + end |
| 153 | + |
| 154 | + it 'does not report invalid downloaded files if `skip_file_validation:true`' do |
| 155 | + Dir.mktmpdir('a8c-release-toolkit-tests-') do |tmp_dir| |
| 156 | + # Arrange |
| 157 | + stub = gp_stub(locale: 'fr-FR', query: { 'filters[status]': 'current', format: 'strings' }).to_return(body: 'some invalid strings file content') |
| 158 | + error_messages = [] |
| 159 | + allow(FastlaneCore::UI).to receive(:error) { |message| error_messages.append(message) } |
| 160 | + |
| 161 | + # Act |
| 162 | + act = lambda do |
| 163 | + run_described_fastlane_action( |
| 164 | + project_url: gp_fake_url, |
| 165 | + locales: { 'fr-FR': 'fr' }, |
| 166 | + download_dir: tmp_dir, |
| 167 | + skip_file_validation: true |
| 168 | + ) |
| 169 | + end |
| 170 | + |
| 171 | + # Assert |
| 172 | + expect { act.call }.not_to raise_error |
| 173 | + expect(stub).to have_been_made.once |
| 174 | + file = File.join(tmp_dir, 'fr.lproj', 'Localizable.strings') |
| 175 | + expect(File).to exist(file) |
| 176 | + expect(error_messages).to eq([]) |
| 177 | + end |
| 178 | + end |
69 | 179 | end
|
70 | 180 | end
|
0 commit comments