Skip to content

Commit 7e1dbea

Browse files
committed
Add remaining test cases
1 parent b249b55 commit 7e1dbea

File tree

1 file changed

+122
-12
lines changed

1 file changed

+122
-12
lines changed

spec/ios_download_strings_files_from_glotpress_spec.rb

Lines changed: 122 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33

44
describe Fastlane::Actions::IosDownloadStringsFilesFromGlotpressAction do
55
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' } }
68

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
1412

13+
describe 'downloading export files from GlotPress' do
1514
def test_gp_download(filters:, tablename:, expected_gp_params:)
1615
Dir.mktmpdir('a8c-release-toolkit-tests-') do |tmp_dir|
1716
# Arrange
@@ -55,16 +54,127 @@ def test_gp_download(filters:, tablename:, expected_gp_params:)
5554
it 'uses a custom table name for the `.strings` files if provided' do
5655
test_gp_download(
5756
filters: nil,
58-
tablename: "MyApp",
57+
tablename: 'MyApp',
5958
expected_gp_params: { 'filters[status]': 'current', format: 'strings' }
6059
)
6160
end
6261
end
6362

6463
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
69179
end
70180
end

0 commit comments

Comments
 (0)