lokalise_rails 1.3.1 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2e85d26a390d153b8deabe38694c1b7313fca351232066d58918828388dcc7fc
4
- data.tar.gz: 0ee837ad7426ed5570badcec88245de2f4d35ea27b3c821f3facb5922083b2b2
3
+ metadata.gz: 6448a3718185d09a6712428f671e02121e59c967199ae6e24f27b8623f12b280
4
+ data.tar.gz: c0f2501d9c937b26088ee23a6603a842121cb6a88516ae19d09d40abe1968161
5
5
  SHA512:
6
- metadata.gz: 4af0fd091968510911787fcdc7bd7ffdefada590fb48c4137106de5d3d34296890a868b65debbf2262fc0d82901eb77a35407e3d6a7c5f958babbb2b548b14cd
7
- data.tar.gz: ff6948bb092ab43e837b54de2d593d8e5db319ac872390aa335cb67802d198f11a30d58993d3d5e561a1f3ad823fe52ac7f55b1bcbe3de9a2ba09dfb0d41d99f
6
+ metadata.gz: be178f379eb4abbb4d50c4f3817422c665bed0c8a75f256c083b0229a57594956ca8c42648fe98be40ed1c4183f0e00fd51fcaf153a87ed408279c1d2ac152ec
7
+ data.tar.gz: db7dd0efdb0d9a7b72116adba959a04b899e41d5ccd89181f48e2ec2e1d097ebfebb732e6592c725b831cebf89134e77e0639deb8591876267306583064efa96
data/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # Changelog
2
2
 
3
+ ## 1.4.0 (29-Jun-21)
4
+
5
+ * Re-worked exception handling. Now when something goes wrong during the import or export process, this gem will re-raise all such errors (previously it just printed out some errors to the `$stdout`). If you run a Rake task, it will exit with a status code `1` and the actual error message. If you run a task programattically, you'll get an exception.
6
+ * Dropped support for Ruby 2.5
7
+ * Test against Ruby 3
8
+
3
9
  ## 1.3.1 (01-Apr-21)
4
10
 
5
11
  * A bit better exception handling
data/README.md CHANGED
@@ -3,6 +3,7 @@
3
3
  ![Gem](https://img.shields.io/gem/v/lokalise_rails)
4
4
  [![Build Status](https://travis-ci.com/bodrovis/lokalise_rails.svg?branch=master)](https://travis-ci.com/github/bodrovis/lokalise_rails)
5
5
  [![Test Coverage](https://codecov.io/gh/bodrovis/lokalise_rails/graph/badge.svg)](https://codecov.io/gh/bodrovis/lokalise_rails)
6
+ ![Downloads total](https://img.shields.io/gem/dt/lokalise_rails)
6
7
 
7
8
  This gem provides [Lokalise](http://lokalise.com) integration for Ruby on Rails and allows to exchange translation files easily. It relies on [ruby-lokalise-api](https://lokalise.github.io/ruby-lokalise-api) to send APIv2 requests.
8
9
 
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'lokalise_rails/error'
3
4
  require 'lokalise_rails/task_definition/base'
4
5
  require 'lokalise_rails/task_definition/importer'
5
6
  require 'lokalise_rails/task_definition/exporter'
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module LokaliseRails
4
+ class Error < StandardError
5
+ # Initializes a new Error object
6
+ def initialize(message = '')
7
+ super(message)
8
+ end
9
+ end
10
+ end
@@ -25,11 +25,12 @@ module LokaliseRails
25
25
  # Checks task options
26
26
  #
27
27
  # @return Array
28
- def opt_errors
28
+ def check_options_errors!
29
29
  errors = []
30
- errors << 'Project ID is not set! Aborting...' if LokaliseRails.project_id.nil? || LokaliseRails.project_id.empty?
31
- errors << 'Lokalise API token is not set! Aborting...' if LokaliseRails.api_token.nil? || LokaliseRails.api_token.empty?
32
- errors
30
+ errors << 'Project ID is not set!' if LokaliseRails.project_id.nil? || LokaliseRails.project_id.empty?
31
+ errors << 'Lokalise API token is not set!' if LokaliseRails.api_token.nil? || LokaliseRails.api_token.empty?
32
+
33
+ raise(LokaliseRails::Error, errors.join(' ')) if errors.any?
33
34
  end
34
35
 
35
36
  private
@@ -10,12 +10,7 @@ module LokaliseRails
10
10
  #
11
11
  # @return [Array]
12
12
  def export!
13
- errors = opt_errors
14
-
15
- if errors.any?
16
- errors.each { |e| $stdout.puts e }
17
- return errors
18
- end
13
+ check_options_errors!
19
14
 
20
15
  queued_processes = []
21
16
  each_file do |full_path, relative_path|
@@ -23,7 +18,7 @@ module LokaliseRails
23
18
  project_id_with_branch, opts(full_path, relative_path)
24
19
  )
25
20
  rescue StandardError => e
26
- $stdout.puts "Error while trying to upload #{full_path}: #{e.inspect}"
21
+ raise e.class, "Error while trying to upload #{full_path}: #{e.message}"
27
22
  end
28
23
 
29
24
  $stdout.print 'Task complete!'
@@ -13,12 +13,7 @@ module LokaliseRails
13
13
  #
14
14
  # @return [Boolean]
15
15
  def import!
16
- errors = opt_errors
17
-
18
- if errors.any?
19
- errors.each { |e| $stdout.puts e }
20
- return false
21
- end
16
+ check_options_errors!
22
17
 
23
18
  unless proceed_when_safe_mode?
24
19
  $stdout.print 'Task cancelled!'
@@ -39,7 +34,7 @@ module LokaliseRails
39
34
 
40
35
  api_client.download_files project_id_with_branch, opts
41
36
  rescue StandardError => e
42
- $stdout.puts "There was an error when trying to download files: #{e.inspect}"
37
+ raise e.class, "There was an error when trying to download files: #{e.message}"
43
38
  end
44
39
 
45
40
  # Opens ZIP archive (local or remote) with translations and processes its entries
@@ -50,7 +45,7 @@ module LokaliseRails
50
45
  fetch_zip_entries(zip) { |entry| process!(entry) }
51
46
  end
52
47
  rescue StandardError => e
53
- $stdout.puts "There was an error when trying to process the downloaded files: #{e.inspect}"
48
+ raise e.class, "There was an error when trying to process the downloaded files: #{e.message}"
54
49
  end
55
50
 
56
51
  # Iterates over ZIP entries. Each entry may be a file or folder.
@@ -75,7 +70,7 @@ module LokaliseRails
75
70
  f.write LokaliseRails.translations_converter.call(data)
76
71
  end
77
72
  rescue StandardError => e
78
- $stdout.puts "Error when trying to process #{zip_entry&.name}: #{e.inspect}"
73
+ raise e.class, "Error when trying to process #{zip_entry&.name}: #{e.message}"
79
74
  end
80
75
 
81
76
  # Checks whether the user wishes to proceed when safe mode is enabled and the target directory is not empty
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LokaliseRails
4
- VERSION = '1.3.1'
4
+ VERSION = '1.4.0'
5
5
  end
@@ -6,9 +6,13 @@ require "#{Rails.root}/config/lokalise_rails"
6
6
  namespace :lokalise_rails do
7
7
  task :import do
8
8
  LokaliseRails::TaskDefinition::Importer.import!
9
+ rescue StandardError => e
10
+ abort e.inspect
9
11
  end
10
12
 
11
13
  task :export do
12
14
  LokaliseRails::TaskDefinition::Exporter.export!
15
+ rescue StandardError => e
16
+ abort e.inspect
13
17
  end
14
18
  end
@@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
37
37
  spec.add_development_dependency 'rspec', '~> 3.6'
38
38
  spec.add_development_dependency 'rubocop', '~> 1.0'
39
39
  spec.add_development_dependency 'rubocop-performance', '~> 1.5'
40
- spec.add_development_dependency 'rubocop-rspec', '~> 2.2.0'
40
+ spec.add_development_dependency 'rubocop-rspec', '~> 2.4.0'
41
41
  spec.add_development_dependency 'simplecov', '~> 0.16'
42
42
  spec.add_development_dependency 'vcr', '~> 6.0'
43
43
  end
@@ -34,22 +34,18 @@ describe LokaliseRails::TaskDefinition::Base do
34
34
  end
35
35
  end
36
36
 
37
- describe '.opt_errors' do
38
- it 'returns an error when the API key is not set' do
37
+ describe '.check_options_errors!' do
38
+ it 'raises an error when the API key is not set' do
39
39
  allow(LokaliseRails).to receive(:api_token).and_return(nil)
40
- errors = described_class.opt_errors
40
+
41
+ expect(-> { described_class.check_options_errors! }).to raise_error(LokaliseRails::Error, /API token is not set/i)
41
42
 
42
43
  expect(LokaliseRails).to have_received(:api_token)
43
- expect(errors.length).to eq(1)
44
- expect(errors.first).to include('API token is not set')
45
44
  end
46
45
 
47
46
  it 'returns an error when the project_id is not set' do
48
47
  allow_project_id nil do
49
- errors = described_class.opt_errors
50
-
51
- expect(errors.length).to eq(1)
52
- expect(errors.first).to include('Project ID is not set')
48
+ expect(-> { described_class.check_options_errors! }).to raise_error(LokaliseRails::Error, /ID is not set/i)
53
49
  end
54
50
  end
55
51
  end
@@ -44,13 +44,13 @@ describe LokaliseRails::TaskDefinition::Exporter do
44
44
  it 'halts when the API key is not set' do
45
45
  allow(LokaliseRails).to receive(:api_token).and_return(nil)
46
46
 
47
- expect(-> { described_class.export! }).to output(/API token is not set/).to_stdout
47
+ expect(-> { described_class.export! }).to raise_error(LokaliseRails::Error, /API token is not set/i)
48
48
  expect(LokaliseRails).to have_received(:api_token)
49
49
  end
50
50
 
51
51
  it 'halts when the project_id is not set' do
52
52
  allow_project_id nil do
53
- expect(-> { described_class.export! }).to output(/Project ID is not set/).to_stdout
53
+ expect(-> { described_class.export! }).to raise_error(LokaliseRails::Error, /ID is not set/i)
54
54
  end
55
55
  end
56
56
  end
@@ -107,17 +107,12 @@ describe LokaliseRails::TaskDefinition::Exporter do
107
107
  end
108
108
 
109
109
  describe '.export!' do
110
- it 'rescues from export errors' do
110
+ it 're-raises export errors' do
111
111
  allow_project_id
112
112
 
113
- processes = VCR.use_cassette('upload_files_error') do
114
- described_class.export!
113
+ VCR.use_cassette('upload_files_error') do
114
+ expect { described_class.export! }.to raise_error(Lokalise::Error::BadRequest, /Unknown `lang_iso`/)
115
115
  end
116
-
117
- expect(processes.length).to eq(1)
118
- process = processes.first
119
- expect(process.project_id).to eq(LokaliseRails.project_id)
120
- expect(process.status).to eq('queued')
121
116
  end
122
117
  end
123
118
 
@@ -4,14 +4,14 @@ describe LokaliseRails::TaskDefinition::Importer do
4
4
  describe '.open_and_process_zip' do
5
5
  let(:faulty_trans) { "#{Rails.root}/public/faulty_trans.zip" }
6
6
 
7
- it 'rescues from errors during file processing' do
7
+ it 're-raises errors during file processing' do
8
8
  expect(-> { described_class.open_and_process_zip(faulty_trans) }).
9
- to output(/Psych::DisallowedClass/).to_stdout
9
+ to raise_error(Psych::DisallowedClass, /Error when trying to process fail\.yml/)
10
10
  end
11
11
 
12
- it 'rescues from errors during file opening' do
12
+ it 're-raises errors during file opening' do
13
13
  expect(-> { described_class.open_and_process_zip('http://fake.url/wrong/path.zip') }).
14
- to output(/SocketError/).to_stdout
14
+ to raise_error(SocketError, /Failed to open TCP connection/)
15
15
  end
16
16
  end
17
17
 
@@ -27,11 +27,11 @@ describe LokaliseRails::TaskDefinition::Importer do
27
27
  end
28
28
  end
29
29
 
30
- it 'rescues from errors during file download' do
30
+ it 're-raises errors during file download' do
31
31
  allow_project_id 'invalid'
32
32
  VCR.use_cassette('download_files_error') do
33
33
  expect(-> { described_class.download_files }).
34
- to output(/Lokalise::Error::BadRequest/).to_stdout
34
+ to raise_error(Lokalise::Error::BadRequest, /Invalid `project_id` parameter/)
35
35
  end
36
36
  end
37
37
  end
@@ -39,14 +39,14 @@ describe LokaliseRails::TaskDefinition::Importer do
39
39
  describe '.import!' do
40
40
  it 'halts when the API key is not set' do
41
41
  allow(LokaliseRails).to receive(:api_token).and_return(nil)
42
- expect(-> { described_class.import! }).to output(/API token is not set/).to_stdout
42
+ expect(-> { described_class.import! }).to raise_error(LokaliseRails::Error, /API token is not set/i)
43
43
  expect(LokaliseRails).to have_received(:api_token)
44
44
  expect(count_translations).to eq(0)
45
45
  end
46
46
 
47
47
  it 'halts when the project_id is not set' do
48
48
  allow_project_id nil do
49
- expect(-> { described_class.import! }).to output(/Project ID is not set/).to_stdout
49
+ expect(-> { described_class.import! }).to raise_error(LokaliseRails::Error, /ID is not set/i)
50
50
  expect(count_translations).to eq(0)
51
51
  end
52
52
  end
@@ -1,7 +1,44 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  RSpec.describe LokaliseRails do
4
+ it 'halts when the API key is not set' do
5
+ allow(described_class).to receive(:api_token).and_return(nil)
6
+
7
+ expect(export_executor).to raise_error(SystemExit, /API token is not set/i)
8
+ expect(described_class).to have_received(:api_token)
9
+ end
10
+
11
+ it 'halts when the project ID is not set' do
12
+ allow_project_id nil do
13
+ expect(export_executor).to raise_error(SystemExit, /ID is not set/i)
14
+ end
15
+ end
16
+
4
17
  it 'runs export rake task properly' do
5
18
  expect(export_executor).to output(/complete!/).to_stdout
6
19
  end
20
+
21
+ context 'with two translation files' do
22
+ let(:filename_ru) { 'ru.yml' }
23
+ let(:path_ru) { "#{Rails.root}/config/locales/#{filename_ru}" }
24
+ let(:relative_name_ru) { filename_ru }
25
+
26
+ before :all do
27
+ add_translation_files! with_ru: true
28
+ end
29
+
30
+ after :all do
31
+ rm_translation_files
32
+ end
33
+
34
+ describe '.export!' do
35
+ it 're-raises export errors' do
36
+ allow_project_id
37
+
38
+ VCR.use_cassette('upload_files_error') do
39
+ expect(export_executor).to raise_error(SystemExit, /Unknown `lang_iso`/)
40
+ end
41
+ end
42
+ end
43
+ end
7
44
  end
@@ -7,6 +7,19 @@ RSpec.describe LokaliseRails do
7
7
  let(:local_trans) { "#{Rails.root}/public/trans.zip" }
8
8
  let(:remote_trans) { 'https://github.com/bodrovis/lokalise_rails/blob/master/spec/dummy/public/trans.zip?raw=true' }
9
9
 
10
+ it 'halts when the API key is not set' do
11
+ allow(described_class).to receive(:api_token).and_return(nil)
12
+
13
+ expect(import_executor).to raise_error(SystemExit, /API token is not set/i)
14
+ expect(described_class).to have_received(:api_token)
15
+ end
16
+
17
+ it 'halts when the project ID is not set' do
18
+ allow_project_id nil do
19
+ expect(import_executor).to raise_error(SystemExit, /ID is not set/i)
20
+ end
21
+ end
22
+
10
23
  context 'when directory is empty' do
11
24
  before do
12
25
  mkdir_locales
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: 1.3.1
4
+ version: 1.4.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: 2021-04-01 00:00:00.000000000 Z
11
+ date: 2021-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ruby-lokalise-api
@@ -142,14 +142,14 @@ dependencies:
142
142
  requirements:
143
143
  - - "~>"
144
144
  - !ruby/object:Gem::Version
145
- version: 2.2.0
145
+ version: 2.4.0
146
146
  type: :development
147
147
  prerelease: false
148
148
  version_requirements: !ruby/object:Gem::Requirement
149
149
  requirements:
150
150
  - - "~>"
151
151
  - !ruby/object:Gem::Version
152
- version: 2.2.0
152
+ version: 2.4.0
153
153
  - !ruby/object:Gem::Dependency
154
154
  name: simplecov
155
155
  requirement: !ruby/object:Gem::Requirement
@@ -198,6 +198,7 @@ files:
198
198
  - lib/generators/lokalise_rails/install_generator.rb
199
199
  - lib/generators/templates/lokalise_rails_config.rb
200
200
  - lib/lokalise_rails.rb
201
+ - lib/lokalise_rails/error.rb
201
202
  - lib/lokalise_rails/railtie.rb
202
203
  - lib/lokalise_rails/task_definition/base.rb
203
204
  - lib/lokalise_rails/task_definition/exporter.rb
@@ -258,7 +259,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
258
259
  - !ruby/object:Gem::Version
259
260
  version: '0'
260
261
  requirements: []
261
- rubygems_version: 3.2.11
262
+ rubygems_version: 3.2.24
262
263
  signing_key:
263
264
  specification_version: 4
264
265
  summary: Lokalise integration for Ruby on Rails