i18n-coverage 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 776761959a5e1226e1622b341e3db434020b336a02e23f9666707293484a64d0
4
+ data.tar.gz: d98f78f24993f50ebef9ea318a4d3c99be7a1e033c97c8e92368e9b387606948
5
+ SHA512:
6
+ metadata.gz: '0963aedd591d0f12af222870069d01df77dfdae86280c1737dca2a1613247d7f1cbf43132a626182f658a2fbf4696bacb84bccf0fefd2a64039274ca3bb8552b'
7
+ data.tar.gz: f3df449e75079be37369ef8104fddbac7915917070dc81cc847e19affbd31f98645d16d400eea0f841f4468fcc76ee59b9c497d79e8796eebec4573b9c6fd653
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.2
7
+ before_install: gem install bundler -v 1.17.2
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in i18n-coverage.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,89 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ i18n-coverage (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ addressable (2.6.0)
10
+ public_suffix (>= 2.0.2, < 4.0)
11
+ builder (3.2.3)
12
+ concurrent-ruby (1.1.5)
13
+ descendants_tracker (0.0.4)
14
+ thread_safe (~> 0.3, >= 0.3.1)
15
+ diff-lcs (1.3)
16
+ faraday (0.15.4)
17
+ multipart-post (>= 1.2, < 3)
18
+ git (1.5.0)
19
+ github_api (0.18.2)
20
+ addressable (~> 2.4)
21
+ descendants_tracker (~> 0.0.4)
22
+ faraday (~> 0.8)
23
+ hashie (~> 3.5, >= 3.5.2)
24
+ oauth2 (~> 1.0)
25
+ hashie (3.6.0)
26
+ highline (2.0.2)
27
+ i18n (0.9.5)
28
+ concurrent-ruby (~> 1.0)
29
+ juwelier (2.4.9)
30
+ builder
31
+ bundler
32
+ git
33
+ github_api
34
+ highline
35
+ kamelcase (~> 0)
36
+ nokogiri
37
+ psych
38
+ rake
39
+ rdoc
40
+ semver2
41
+ jwt (2.2.1)
42
+ kamelcase (0.0.2)
43
+ semver2 (~> 3)
44
+ mini_portile2 (2.4.0)
45
+ multi_json (1.13.1)
46
+ multi_xml (0.6.0)
47
+ multipart-post (2.1.1)
48
+ nokogiri (1.10.3)
49
+ mini_portile2 (~> 2.4.0)
50
+ oauth2 (1.4.1)
51
+ faraday (>= 0.8, < 0.16.0)
52
+ jwt (>= 1.0, < 3.0)
53
+ multi_json (~> 1.3)
54
+ multi_xml (~> 0.5)
55
+ rack (>= 1.2, < 3)
56
+ psych (3.1.0)
57
+ public_suffix (3.1.0)
58
+ rack (2.0.7)
59
+ rake (10.5.0)
60
+ rdoc (6.1.1)
61
+ rspec (3.8.0)
62
+ rspec-core (~> 3.8.0)
63
+ rspec-expectations (~> 3.8.0)
64
+ rspec-mocks (~> 3.8.0)
65
+ rspec-core (3.8.0)
66
+ rspec-support (~> 3.8.0)
67
+ rspec-expectations (3.8.3)
68
+ diff-lcs (>= 1.2.0, < 2.0)
69
+ rspec-support (~> 3.8.0)
70
+ rspec-mocks (3.8.0)
71
+ diff-lcs (>= 1.2.0, < 2.0)
72
+ rspec-support (~> 3.8.0)
73
+ rspec-support (3.8.0)
74
+ semver2 (3.4.2)
75
+ thread_safe (0.3.6)
76
+
77
+ PLATFORMS
78
+ ruby
79
+
80
+ DEPENDENCIES
81
+ bundler (~> 1.17)
82
+ i18n (~> 0.7)
83
+ i18n-coverage!
84
+ juwelier (~> 2.4)
85
+ rake (~> 10.0)
86
+ rspec (~> 3.0)
87
+
88
+ BUNDLED WITH
89
+ 1.17.2
data/README.md ADDED
@@ -0,0 +1,50 @@
1
+ # I18n-coverage
2
+ [![Build Status](https://travis-ci.org/hiptest/i18n-coverage.svg?branch=master)](https://travis-ci.org/hiptest/i18n-coverage)
3
+
4
+ Simple gem to see how much of your i18n keys are used during tests. It requires that the translations are handled by the [i18n](https://rubygems.org/gems/i18n/) gem.
5
+
6
+ This gem is not meant to replace tools such as [i18n-tasks](https://rubygems.org/gems/i18n-tasks), but more to give you an idea of which keys are not covered by tests (especially when adding translations to a gem where strings were hard-coded previously).
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'i18n-coverage'
14
+ ```
15
+
16
+ ## Usage
17
+
18
+ First, you need to ensure that the gem is correctly loaded when running the tests. When using rspec for example:
19
+
20
+ ```ruby
21
+ # file spec_helper.rb
22
+ require 'i18n-coverage'
23
+ ```
24
+
25
+ You also need to output/read the results of the coverage. This should be done once all the tests have been executed. Once again, an example with rspec:
26
+
27
+ ```ruby
28
+ RSpec.configure do |config|
29
+ config.after(:suite) do
30
+ I18n::Coverage::Reporter.report if ENV['I18N_COVERAGE']
31
+ end
32
+ end
33
+ ```
34
+
35
+ Then, when running the tests, you need to enable coverage by setting the environment variable `I18N_COVERAGE`. For example with rspec:
36
+
37
+ ```shell
38
+ I18N_COVERAGE=1 bundle exec rspec
39
+ ```
40
+
41
+
42
+ ## Development
43
+
44
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
45
+
46
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
47
+
48
+ ## Contributing
49
+
50
+ Bug reports and pull requests are welcome on GitHub at https://github.com/hiptest/i18n-coverage.
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "i18n/coverage"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,41 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "i18n/coverage/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "i18n-coverage"
8
+ spec.version = I18n::Coverage::VERSION
9
+ spec.authors = ["Hiptest"]
10
+ spec.email = ["contact@hiptest.com"]
11
+
12
+ spec.summary = %q{Provides a coverage of I18n keys used during test suite}
13
+ spec.homepage = "https://github.com/hiptest/i18n-coverage"
14
+
15
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
16
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
17
+ if spec.respond_to?(:metadata)
18
+ spec.metadata["homepage_uri"] = spec.homepage
19
+ spec.metadata["source_code_uri"] = "https://github.com/hiptest/i18n-coverage"
20
+ spec.metadata["changelog_uri"] = "https://github.com/hiptest/i18n-coverage/CHANGELOG.md"
21
+ else
22
+ raise "RubyGems 2.0 or newer is required to protect against " \
23
+ "public gem pushes."
24
+ end
25
+
26
+ # Specify which files should be added to the gem when it is released.
27
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
28
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
29
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
30
+ end
31
+ spec.bindir = "exe"
32
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
33
+ spec.require_paths = ["lib"]
34
+
35
+ spec.add_development_dependency "i18n", "~> 0.7"
36
+ spec.add_development_dependency "bundler", "~> 1.17"
37
+ spec.add_development_dependency "rake", "~> 10.0"
38
+ spec.add_development_dependency "rspec", "~> 3.0"
39
+ spec.add_development_dependency "juwelier", "~> 2.4"
40
+
41
+ end
@@ -0,0 +1,12 @@
1
+ require 'i18n/coverage/key_logger'
2
+
3
+ module I18n
4
+ module Backend
5
+ module KeyLogger
6
+ def translate(*args)
7
+ I18n::Coverage::KeyLogger.store_key(args[1])
8
+ super
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,9 @@
1
+ require "i18n/coverage/version"
2
+ require "i18n/backend/key_logger"
3
+
4
+ module I18n
5
+ module Coverage
6
+ end
7
+ end
8
+
9
+ I18n::Backend::Simple.send(:include, I18n::Backend::KeyLogger) if ENV['I18N_COVERAGE']
@@ -0,0 +1,43 @@
1
+ require 'yaml'
2
+
3
+ module I18n
4
+ module Coverage
5
+ class KeyLister
6
+ def self.list_keys(locale: 'en', locale_dir_path: 'config/locales')
7
+ KeyLister.new(locale, locale_dir_path).list_keys
8
+ end
9
+
10
+ def initialize(locale, locale_dir_path)
11
+ @locale = locale
12
+ @source = YAML.load(File.open(File.expand_path("#{locale_dir_path}/#{locale}.yml")))
13
+ @keys = Set[]
14
+ end
15
+
16
+ def list_keys
17
+ visit_childs(path: [])
18
+ @keys
19
+ end
20
+
21
+ private
22
+
23
+ def visit_childs(path: )
24
+ node = @source.dig(*[@locale, path].flatten.compact)
25
+ if node.respond_to? :keys
26
+ keys = node.keys
27
+
28
+ if pluralization_keys?(keys)
29
+ @keys.add(path.join('.'))
30
+ else
31
+ keys.map {|key| visit_childs(path: [path, key].flatten.compact)}
32
+ end
33
+ else
34
+ @keys.add(path.join('.'))
35
+ end
36
+ end
37
+
38
+ def pluralization_keys?(keys)
39
+ return (keys - ['zero', 'one', 'other']).empty?
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,29 @@
1
+ module I18n
2
+ module Coverage
3
+ class KeyLogger
4
+ def self.store_key(key)
5
+ KeyLogger.new.store_key(key)
6
+ end
7
+
8
+ def self.stored_keys
9
+ KeyLogger.new.stored_keys
10
+ end
11
+
12
+ def self.clear_keys
13
+ KeyLogger.new.clear_keys
14
+ end
15
+
16
+ def store_key(key)
17
+ stored_keys.add(key)
18
+ end
19
+
20
+ def clear_keys
21
+ stored_keys.clear
22
+ end
23
+
24
+ def stored_keys
25
+ @@stored_keys ||= Set[]
26
+ end
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,39 @@
1
+ require 'i18n/coverage/key_lister'
2
+ require 'i18n/coverage/key_logger'
3
+
4
+ module I18n
5
+ module Coverage
6
+ class Reporter
7
+ def self.report(locale: 'en', locale_dir_path: 'config/locales')
8
+ Reporter.new(locale: locale, locale_dir_path: locale_dir_path).report
9
+ end
10
+
11
+ def initialize(locale: 'en', locale_dir_path: 'config/locales')
12
+ @existing_keys = KeyLister.list_keys(locale: locale, locale_dir_path: locale_dir_path)
13
+ @used_keys = KeyLogger.stored_keys
14
+ @percentage_used = (@used_keys.count.to_f / @existing_keys.count.to_f) * 100
15
+ @unused_keys = @existing_keys - @used_keys
16
+ end
17
+
18
+ def report
19
+ puts ""
20
+ puts "I18n Coverage: #{@percentage_used.round(2)}% of the keys used"
21
+ puts "#{@existing_keys.count} keys found in yml file, #{@used_keys.count} keys used during the tests"
22
+
23
+ if @unused_keys
24
+ puts "Unused keys:"
25
+ @unused_keys.map {|k| puts " #{k}"}
26
+ end
27
+ end
28
+
29
+ def hash_report
30
+ {
31
+ key_count: @existing_keys.count,
32
+ used_key_count: @used_keys.count,
33
+ percentage_used: @percentage_used,
34
+ unused_keys: @unused_keys
35
+ }
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,5 @@
1
+ module I18n
2
+ module Coverage
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,131 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: i18n-coverage
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hiptest
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2019-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: i18n
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.7'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.17'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.17'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: juwelier
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '2.4'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '2.4'
83
+ description:
84
+ email:
85
+ - contact@hiptest.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".rspec"
92
+ - ".travis.yml"
93
+ - Gemfile
94
+ - Gemfile.lock
95
+ - README.md
96
+ - Rakefile
97
+ - bin/console
98
+ - bin/setup
99
+ - i18n-coverage.gemspec
100
+ - lib/i18n/backend/key_logger.rb
101
+ - lib/i18n/coverage.rb
102
+ - lib/i18n/coverage/key_lister.rb
103
+ - lib/i18n/coverage/key_logger.rb
104
+ - lib/i18n/coverage/reporter.rb
105
+ - lib/i18n/coverage/version.rb
106
+ homepage: https://github.com/hiptest/i18n-coverage
107
+ licenses: []
108
+ metadata:
109
+ homepage_uri: https://github.com/hiptest/i18n-coverage
110
+ source_code_uri: https://github.com/hiptest/i18n-coverage
111
+ changelog_uri: https://github.com/hiptest/i18n-coverage/CHANGELOG.md
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ requirements:
118
+ - - ">="
119
+ - !ruby/object:Gem::Version
120
+ version: '0'
121
+ required_rubygems_version: !ruby/object:Gem::Requirement
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: '0'
126
+ requirements: []
127
+ rubygems_version: 3.0.3
128
+ signing_key:
129
+ specification_version: 4
130
+ summary: Provides a coverage of I18n keys used during test suite
131
+ test_files: []