lokalise_rails 0.0.2.3 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,49 @@
1
+ # frozen_string_literal: true
2
+
3
+ describe LokaliseRails::TaskDefinition::Importer do
4
+ describe '.open_and_process_zip' do
5
+ let(:faulty_trans) { "#{Rails.root}/public/faulty_trans.zip" }
6
+
7
+ it 'rescues from errors during file processing' do
8
+ expect(-> { described_class.open_and_process_zip(faulty_trans) }).
9
+ to output(/Psych::DisallowedClass/).to_stdout
10
+ end
11
+ end
12
+
13
+ describe '.download_files' do
14
+ it 'returns a proper download URL' do
15
+ allow(LokaliseRails).to receive(:project_id).and_return('189934715f57a162257d74.88352370')
16
+ response = VCR.use_cassette('download_files') do
17
+ described_class.download_files
18
+ end
19
+
20
+ expect(LokaliseRails).to have_received(:project_id)
21
+ expect(response['project_id']).to eq('189934715f57a162257d74.88352370')
22
+ expect(response['bundle_url']).to include('s3-eu-west-1.amazonaws.com')
23
+ end
24
+
25
+ it 'rescues from errors during file download' do
26
+ allow_project_id 'invalid'
27
+ VCR.use_cassette('download_files_error') do
28
+ expect(-> { described_class.download_files }).
29
+ to output(/Lokalise::Error::BadRequest/).to_stdout
30
+ end
31
+ end
32
+ end
33
+
34
+ describe '.import!' do
35
+ it 'halts when the API key is not set' do
36
+ allow(LokaliseRails).to receive(:api_token).and_return(nil)
37
+ expect(-> { described_class.import! }).to output(/API token is not set/).to_stdout
38
+ expect(LokaliseRails).to have_received(:api_token)
39
+ expect(count_translations).to eq(0)
40
+ end
41
+
42
+ it 'halts when the project_id is not set' do
43
+ allow(LokaliseRails).to receive(:project_id).and_return(nil)
44
+ expect(-> { described_class.import! }).to output(/Project ID is not set/).to_stdout
45
+ expect(LokaliseRails).to have_received(:project_id)
46
+ expect(count_translations).to eq(0)
47
+ end
48
+ end
49
+ end
@@ -1,40 +1,77 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  describe LokaliseRails do
4
- it 'should return a proper version' do
4
+ it 'returns a proper version' do
5
5
  expect(LokaliseRails::VERSION).to be_a(String)
6
6
  end
7
7
 
8
+ it 'is possible to provide config options' do
9
+ described_class.config do |c|
10
+ expect(c).to eq(described_class)
11
+ end
12
+ end
13
+
8
14
  describe 'parameters' do
9
15
  let(:fake_class) { class_double('LokaliseRails') }
10
16
 
11
17
  it 'is possible to set project_id' do
12
- expect(fake_class).to receive(:project_id=).with('123.abc')
18
+ allow(fake_class).to receive(:project_id=).with('123.abc')
13
19
  fake_class.project_id = '123.abc'
14
20
  end
15
21
 
22
+ it 'is possible to set file_ext_regexp' do
23
+ allow(fake_class).to receive(:file_ext_regexp=).with(Regexp.new('.*'))
24
+ fake_class.file_ext_regexp = Regexp.new('.*')
25
+ end
26
+
16
27
  it 'is possible to set import_opts' do
17
- expect(fake_class).to receive(:import_opts=).with(duck_type(:each))
28
+ allow(fake_class).to receive(:import_opts=).with(duck_type(:each))
18
29
  fake_class.import_opts = {
19
30
  format: 'json',
20
31
  indentation: '8sp'
21
32
  }
22
33
  end
23
34
 
35
+ it 'is possible to set export_opts' do
36
+ allow(fake_class).to receive(:export_opts=).with(duck_type(:each))
37
+ fake_class.export_opts = {
38
+ convert_placeholders: true,
39
+ detect_icu_plurals: true
40
+ }
41
+ end
42
+
43
+ it 'is possible to set branch' do
44
+ allow(fake_class).to receive(:branch=).with('custom')
45
+ fake_class.branch = 'custom'
46
+ end
47
+
48
+ it 'is possible to set timeouts' do
49
+ allow(fake_class).to receive(:timeouts=).with(duck_type(:each))
50
+ fake_class.timeouts = {
51
+ open_timeout: 100,
52
+ timeout: 500
53
+ }
54
+ end
55
+
24
56
  it 'is possible to set import_safe_mode' do
25
- expect(fake_class).to receive(:import_safe_mode=).with(true)
57
+ allow(fake_class).to receive(:import_safe_mode=).with(true)
26
58
  fake_class.import_safe_mode = true
27
59
  end
28
60
 
29
61
  it 'is possible to set api_token' do
30
- expect(fake_class).to receive(:api_token=).with('abc')
62
+ allow(fake_class).to receive(:api_token=).with('abc')
31
63
  fake_class.api_token = 'abc'
32
64
  end
33
65
 
34
66
  it 'is possible to override locales_path' do
35
- expect(fake_class).to receive(:locales_path).and_return('/demo/path')
67
+ allow(fake_class).to receive(:locales_path=).with('/demo/path')
68
+ fake_class.locales_path = '/demo/path'
69
+ end
36
70
 
37
- expect(fake_class.locales_path).to eq('/demo/path')
71
+ it 'is possible to set skip_file_export' do
72
+ cond = ->(f) { f.nil? }
73
+ allow(fake_class).to receive(:skip_file_export=).with(cond)
74
+ fake_class.skip_file_export = cond
38
75
  end
39
76
  end
40
77
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ RSpec.describe LokaliseRails do
4
+ it 'runs export rake task properly' do
5
+ expect(export_executor).to output(/complete!/).to_stdout
6
+ end
7
+ end
@@ -3,6 +3,10 @@
3
3
  require 'fileutils'
4
4
 
5
5
  RSpec.describe LokaliseRails do
6
+ let(:loc_path) { described_class.locales_path }
7
+ let(:local_trans) { "#{Rails.root}/public/trans.zip" }
8
+ let(:remote_trans) { 'https://github.com/bodrovis/lokalise_rails/blob/master/spec/dummy/public/trans.zip?raw=true' }
9
+
6
10
  context 'when directory is empty' do
7
11
  before do
8
12
  mkdir_locales
@@ -13,70 +17,92 @@ RSpec.describe LokaliseRails do
13
17
  rm_translation_files
14
18
  end
15
19
 
16
- it 'is callable' do
17
- expect(LokaliseRails::TaskDefinition::Importer).to receive(
20
+ it 'import rake task is callable' do
21
+ allow(LokaliseRails::TaskDefinition::Importer).to receive(
18
22
  :download_files
19
23
  ).and_return(
20
24
  {
21
25
  'project_id' => '123.abc',
22
- 'bundle_url' => "#{Rails.root}/public/translations.zip"
26
+ 'bundle_url' => local_trans
23
27
  }
24
28
  )
25
29
 
26
30
  expect(import_executor).to output(/complete!/).to_stdout
27
31
 
28
32
  expect(count_translations).to eq(4)
29
-
30
- main_en = File.join described_class.locales_path, 'main_en.yml'
31
- expect(File.file?(main_en)).to be true
33
+ expect(LokaliseRails::TaskDefinition::Importer).to have_received(:download_files)
34
+ expect_file_exist loc_path, 'en/nested/main_en.yml'
35
+ expect_file_exist loc_path, 'en/nested/deep/secondary_en.yml'
36
+ expect_file_exist loc_path, 'ru/main_ru.yml'
32
37
  end
33
38
 
34
- it 'downloads ZIP archive properly' do
35
- expect(LokaliseRails::TaskDefinition::Importer).to receive(
39
+ it 'import rake task downloads ZIP archive properly' do
40
+ allow(LokaliseRails::TaskDefinition::Importer).to receive(
36
41
  :download_files
37
42
  ).and_return(
38
43
  {
39
44
  'project_id' => '123.abc',
40
- 'bundle_url' => 'https://github.com/bodrovis/lokalise_rails/blob/master/spec/dummy/public/translations.zip?raw=true'
45
+ 'bundle_url' => remote_trans
41
46
  }
42
47
  )
43
48
 
44
49
  expect(import_executor).to output(/complete!/).to_stdout
45
50
 
51
+ expect(LokaliseRails::TaskDefinition::Importer).to have_received(:download_files)
46
52
  expect(count_translations).to eq(4)
47
-
48
- main_en = File.join described_class.locales_path, 'main_en.yml'
49
- expect(File.file?(main_en)).to be true
53
+ expect_file_exist loc_path, 'en/nested/main_en.yml'
54
+ expect_file_exist loc_path, 'en/nested/deep/secondary_en.yml'
55
+ expect_file_exist loc_path, 'ru/main_ru.yml'
50
56
  end
51
57
  end
52
58
 
53
- context 'when directory is not empty' do
59
+ context 'when directory is not empty and safe mode enabled' do
54
60
  before :all do
55
61
  mkdir_locales
56
- rm_translation_files
57
- temp_file = File.join described_class.locales_path, 'kill.me'
58
- File.open(temp_file, 'w+') { |file| file.write('temp') }
59
62
  described_class.import_safe_mode = true
60
63
  end
61
64
 
65
+ before do
66
+ rm_translation_files
67
+ add_translation_files!
68
+ end
69
+
62
70
  after :all do
63
71
  rm_translation_files
64
72
  described_class.import_safe_mode = false
65
73
  end
66
74
 
67
- it 'returns a success message with default settings' do
68
- expect(LokaliseRails::TaskDefinition::Importer).to receive(
75
+ it 'import proceeds when the user agrees' do
76
+ allow(LokaliseRails::TaskDefinition::Importer).to receive(
69
77
  :download_files
70
78
  ).and_return(
71
79
  {
72
80
  'project_id' => '123.abc',
73
- 'bundle_url' => "#{Rails.root}/public/translations.zip"
81
+ 'bundle_url' => local_trans
74
82
  }
75
83
  )
76
- expect($stdin).to receive(:gets).and_return('Y')
84
+
85
+ allow($stdin).to receive(:gets).and_return('Y')
77
86
  expect(import_executor).to output(/is not empty/).to_stdout
78
87
 
79
88
  expect(count_translations).to eq(5)
89
+ expect($stdin).to have_received(:gets)
90
+ expect(LokaliseRails::TaskDefinition::Importer).to have_received(:download_files)
91
+ expect_file_exist loc_path, 'en/nested/main_en.yml'
92
+ expect_file_exist loc_path, 'en/nested/deep/secondary_en.yml'
93
+ expect_file_exist loc_path, 'ru/main_ru.yml'
94
+ end
95
+
96
+ it 'import halts when a user chooses not to proceed' do
97
+ allow(LokaliseRails::TaskDefinition::Importer).to receive(
98
+ :download_files
99
+ ).at_most(0).times
100
+ allow($stdin).to receive(:gets).and_return('N')
101
+ expect(import_executor).to output(/is not empty/).to_stdout
102
+
103
+ expect(LokaliseRails::TaskDefinition::Importer).not_to have_received(:download_files)
104
+ expect($stdin).to have_received(:gets)
105
+ expect(count_translations).to eq(1)
80
106
  end
81
107
  end
82
108
  end
@@ -2,6 +2,7 @@
2
2
 
3
3
  require 'dotenv/load'
4
4
  require 'simplecov'
5
+
5
6
  SimpleCov.start 'rails' do
6
7
  add_filter 'spec/'
7
8
  add_filter '.github/'
@@ -21,14 +22,17 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].sort.each { |f| require f }
21
22
  ENV['RAILS_ENV'] = 'test'
22
23
 
23
24
  require_relative '../spec/dummy/config/environment'
24
- ActiveRecord::Migrator.migrations_paths = [File.expand_path('../spec/dummy/db/migrate', __dir__)]
25
25
  ENV['RAILS_ROOT'] ||= "#{File.dirname(__FILE__)}../../../spec/dummy"
26
26
 
27
- require 'rspec/rails'
28
-
29
27
  RSpec.configure do |config|
30
- config.include FileUtils
28
+ config.include FileManager
31
29
  config.include RakeUtils
30
+ config.include SpecAddons
32
31
  end
33
32
 
33
+ # rubocop:disable Style/MixinUsage
34
+ include FileManager
35
+ # rubocop:enable Style/MixinUsage
36
+
37
+ add_config!
34
38
  Rails.application.load_tasks
@@ -0,0 +1,81 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+
5
+ module FileManager
6
+ def expect_file_exist(path, file)
7
+ file_path = File.join path, file
8
+ expect(File.file?(file_path)).to be true
9
+ end
10
+
11
+ def mkdir_locales
12
+ FileUtils.mkdir_p(LokaliseRails.locales_path) unless File.directory?(LokaliseRails.locales_path)
13
+ end
14
+
15
+ def rm_translation_files
16
+ FileUtils.rm_rf locales_dir
17
+ end
18
+
19
+ def locales_dir
20
+ Dir["#{LokaliseRails.locales_path}/**/*"]
21
+ end
22
+
23
+ def count_translations
24
+ locales_dir.count { |file| File.file?(file) }
25
+ end
26
+
27
+ def add_translation_files!(with_ru: false)
28
+ FileUtils.mkdir_p "#{Rails.root}/config/locales/nested"
29
+ File.open("#{Rails.root}/config/locales/nested/en.yml", 'w+:UTF-8') do |f|
30
+ f.write en_data
31
+ end
32
+
33
+ return unless with_ru
34
+
35
+ File.open("#{Rails.root}/config/locales/ru.yml", 'w+:UTF-8') do |f|
36
+ f.write ru_data
37
+ end
38
+ end
39
+
40
+ def add_config!
41
+ data = <<~DATA
42
+ require 'lokalise_rails'
43
+ LokaliseRails.config do |c|
44
+ c.api_token = ENV['LOKALISE_API_TOKEN']
45
+ c.project_id = ENV['LOKALISE_PROJECT_ID']
46
+ end
47
+ DATA
48
+
49
+ File.open("#{Rails.root}/config/lokalise_rails.rb", 'w+:UTF-8') do |f|
50
+ f.write data
51
+ end
52
+ end
53
+
54
+ def remove_config
55
+ FileUtils.remove_file config_file if File.file?(config_file)
56
+ end
57
+
58
+ def config_file
59
+ "#{Rails.root}/config/lokalise_rails.rb"
60
+ end
61
+
62
+ private
63
+
64
+ def en_data
65
+ <<~DATA
66
+ en:
67
+ my_key: "My value"
68
+ nested:
69
+ key: "Value 2"
70
+ DATA
71
+ end
72
+
73
+ def ru_data
74
+ <<~DATA
75
+ ru_RU:
76
+ my_key: "Моё значение"
77
+ nested:
78
+ key: "Значение 2"
79
+ DATA
80
+ end
81
+ end
@@ -5,6 +5,10 @@ module RakeUtils
5
5
  -> { Rake::Task['lokalise_rails:import'].execute }
6
6
  end
7
7
 
8
+ def export_executor
9
+ -> { Rake::Task['lokalise_rails:export'].execute }
10
+ end
11
+
8
12
  def install_invoker
9
13
  Rails::Generators.invoke 'lokalise_rails:install'
10
14
  end
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SpecAddons
4
+ def allow_project_id(value = '189934715f57a162257d74.88352370')
5
+ allow(LokaliseRails).to receive(:project_id).
6
+ and_return(value)
7
+ end
8
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: lokalise_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2.3
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ilya Bodrov
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-09-26 00:00:00.000000000 Z
11
+ date: 2020-10-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-lokalise-api
@@ -108,20 +108,6 @@ dependencies:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
110
  version: '3.6'
111
- - !ruby/object:Gem::Dependency
112
- name: rspec-rails
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - "~>"
116
- - !ruby/object:Gem::Version
117
- version: '4.0'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - "~>"
123
- - !ruby/object:Gem::Version
124
- version: '4.0'
125
111
  - !ruby/object:Gem::Dependency
126
112
  name: rubocop
127
113
  requirement: !ruby/object:Gem::Requirement
@@ -178,20 +164,6 @@ dependencies:
178
164
  - - "~>"
179
165
  - !ruby/object:Gem::Version
180
166
  version: '0.16'
181
- - !ruby/object:Gem::Dependency
182
- name: sqlite3
183
- requirement: !ruby/object:Gem::Requirement
184
- requirements:
185
- - - "~>"
186
- - !ruby/object:Gem::Version
187
- version: '1.4'
188
- type: :development
189
- prerelease: false
190
- version_requirements: !ruby/object:Gem::Requirement
191
- requirements:
192
- - - "~>"
193
- - !ruby/object:Gem::Version
194
- version: '1.4'
195
167
  - !ruby/object:Gem::Dependency
196
168
  name: vcr
197
169
  requirement: !ruby/object:Gem::Requirement
@@ -228,8 +200,10 @@ files:
228
200
  - lib/lokalise_rails.rb
229
201
  - lib/lokalise_rails/railtie.rb
230
202
  - lib/lokalise_rails/task_definition/base.rb
203
+ - lib/lokalise_rails/task_definition/exporter.rb
231
204
  - lib/lokalise_rails/task_definition/importer.rb
232
205
  - lib/lokalise_rails/version.rb
206
+ - lib/tasks/lokalise_rails_tasks.rake
233
207
  - lokalise_rails.gemspec
234
208
  - spec/dummy/app/controllers/application_controller.rb
235
209
  - spec/dummy/app/helpers/application_helper.rb
@@ -247,19 +221,23 @@ files:
247
221
  - spec/dummy/config/initializers/cookies_serializer.rb
248
222
  - spec/dummy/config/initializers/filter_parameter_logging.rb
249
223
  - spec/dummy/config/initializers/inflections.rb
250
- - spec/dummy/config/initializers/lokalise_rails.rb
251
224
  - spec/dummy/config/initializers/mime_types.rb
252
225
  - spec/dummy/config/initializers/wrap_parameters.rb
226
+ - spec/dummy/config/lokalise_rails.rb
253
227
  - spec/dummy/config/puma.rb
254
228
  - spec/dummy/config/routes.rb
255
229
  - spec/dummy/db/seeds.rb
256
230
  - spec/lib/generators/lokalise_rails/install_generator_spec.rb
257
- - spec/lib/lokalise_rails/importer_spec.rb
231
+ - spec/lib/lokalise_rails/task_definition/base_spec.rb
232
+ - spec/lib/lokalise_rails/task_definition/exporter_spec.rb
233
+ - spec/lib/lokalise_rails/task_definition/importer_spec.rb
258
234
  - spec/lib/lokalise_rails_spec.rb
235
+ - spec/lib/tasks/export_task_spec.rb
259
236
  - spec/lib/tasks/import_task_spec.rb
260
237
  - spec/spec_helper.rb
261
- - spec/support/file_utils.rb
238
+ - spec/support/file_manager.rb
262
239
  - spec/support/rake_utils.rb
240
+ - spec/support/spec_addons.rb
263
241
  - spec/support/vcr.rb
264
242
  homepage: https://github.com/bodrovis/lokalise_rails
265
243
  licenses:
@@ -301,17 +279,21 @@ test_files:
301
279
  - spec/dummy/config/initializers/cookies_serializer.rb
302
280
  - spec/dummy/config/initializers/filter_parameter_logging.rb
303
281
  - spec/dummy/config/initializers/inflections.rb
304
- - spec/dummy/config/initializers/lokalise_rails.rb
305
282
  - spec/dummy/config/initializers/mime_types.rb
306
283
  - spec/dummy/config/initializers/wrap_parameters.rb
284
+ - spec/dummy/config/lokalise_rails.rb
307
285
  - spec/dummy/config/puma.rb
308
286
  - spec/dummy/config/routes.rb
309
287
  - spec/dummy/db/seeds.rb
310
288
  - spec/lib/generators/lokalise_rails/install_generator_spec.rb
311
- - spec/lib/lokalise_rails/importer_spec.rb
289
+ - spec/lib/lokalise_rails/task_definition/base_spec.rb
290
+ - spec/lib/lokalise_rails/task_definition/exporter_spec.rb
291
+ - spec/lib/lokalise_rails/task_definition/importer_spec.rb
312
292
  - spec/lib/lokalise_rails_spec.rb
293
+ - spec/lib/tasks/export_task_spec.rb
313
294
  - spec/lib/tasks/import_task_spec.rb
314
295
  - spec/spec_helper.rb
315
- - spec/support/file_utils.rb
296
+ - spec/support/file_manager.rb
316
297
  - spec/support/rake_utils.rb
298
+ - spec/support/spec_addons.rb
317
299
  - spec/support/vcr.rb