fasterer 0.1.11 → 0.1.12

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
  SHA1:
3
- metadata.gz: 5fc06a003ad7ef35818e525f60ef7bc3bbc7b015
4
- data.tar.gz: c33294e5d4ae9d8f731cf018ff4f6e834731e230
3
+ metadata.gz: a3553ce8184fc77f1acf47f6853044df9e21f5dc
4
+ data.tar.gz: cc19c0ab4f3e1f57d865d8dd734da0e5edaddc0a
5
5
  SHA512:
6
- metadata.gz: 13e41010ea909bc9990f61ac6423b91855ce9ea8d17323ed10ed1ca8d44613e22e9929895dbc8e9cf213ebb1acc1613e24e89d2ffab2840f3d06c769ea52f278
7
- data.tar.gz: 654bd69d4a16bffab862c955e50aba3e9364118a23436f6aaf10a912ac65376a6bb9e9db1e8cfabeeae3b850eb0f6c9c6dcfa0466e50da2370b1d6889bd593d5
6
+ metadata.gz: 81fdc75a473a31b92c754da7f7a71c93b3f632b03f3a3fb0a7b1e514e27c24429247de20298579663772e92ac447359b409ecad1d712ce4f77ac12ecd7caaa03
7
+ data.tar.gz: aa1a4e40b67a12654028fa0f2e2ec9e980436631f3437230a120f95349c608b209627d762e4b915cf88745f7e7b3e92e0be449814177ff1677729b67d18c1b91
@@ -21,4 +21,9 @@ speedups:
21
21
  setter_vs_attr_writer: true
22
22
 
23
23
  exclude_paths:
24
+ - 'spec/support/analyzer/*.rb'
25
+ - 'spec/support/binary_call/*.rb'
26
+ - 'spec/support/method_call/*.rb'
27
+ - 'spec/support/method_definition/*.rb'
28
+ - 'spec/support/rescue_call/*.rb'
24
29
  - 'vendor/**/*.rb'
@@ -1,6 +1,9 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.3
4
- - 2.0.0
5
- - 2.1.2
6
- - 2.2.0
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.7
6
+ - 2.2.3
7
+ env:
8
+ global:
9
+ secure: MBTxmpWCjrsNKPlOoFwJUMHze3GPMz8YCXQFQG3zJBh3WGNQnz4z91ra/1P/DClyVCxHGSFCOswxCNe4kJ2zAnPyQLqGSinXy9uDpqZQUEdaRoQbPnh4/bguZNSJ429gtTpMdDSNOgQ+Hra2EFnWwHA+rLF6ImksMsu3XGKGxGE=
data/Gemfile CHANGED
@@ -2,3 +2,5 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in fasterer.gemspec
4
4
  gemspec
5
+
6
+ gem "codeclimate-test-reporter", group: :test, require: nil
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  [![Build Status](https://travis-ci.org/DamirSvrtan/fasterer.svg?branch=master)](https://travis-ci.org/DamirSvrtan/fasterer)
2
2
  [![Code Climate](https://codeclimate.com/github/DamirSvrtan/fasterer/badges/gpa.svg)](https://codeclimate.com/github/DamirSvrtan/fasterer)
3
3
  [![Gem Version](https://badge.fury.io/rb/fasterer.svg)](http://badge.fury.io/rb/fasterer)
4
+ [![Test Coverage](https://codeclimate.com/github/DamirSvrtan/fasterer/badges/coverage.svg)](https://codeclimate.com/github/DamirSvrtan/fasterer/coverage)
4
5
  # Fasterer
5
6
 
6
7
  Make your Rubies go faster with this command line tool highly inspired by [fast-ruby](https://github.com/JuanitoFatas/fast-ruby) and [Sferik's talk at Baruco Conf](https://speakerdeck.com/sferik/writing-fast-ruby).
@@ -5,6 +5,7 @@ module Fasterer
5
5
  def self.execute
6
6
  file_traverser = Fasterer::FileTraverser.new('.')
7
7
  file_traverser.traverse
8
+ abort if file_traverser.offenses_found?
8
9
  end
9
10
  end
10
11
  end
@@ -0,0 +1,33 @@
1
+ require 'yaml'
2
+
3
+ module Fasterer
4
+ class Config
5
+ FILE_NAME = '.fasterer.yml'
6
+ SPEEDUPS_KEY = 'speedups'
7
+ EXCLUDE_PATHS_KEY = 'exclude_paths'
8
+
9
+ def ignored_speedups
10
+ @ignored_speedups ||=
11
+ file[SPEEDUPS_KEY].select { |_, value| value == false }.keys.map(&:to_sym)
12
+ end
13
+
14
+ def ignored_files
15
+ @ignored_files ||=
16
+ file[EXCLUDE_PATHS_KEY].flat_map { |path| Dir[path] }
17
+ end
18
+
19
+ def file
20
+ @file ||= begin
21
+ return nil_file unless File.exist?(FILE_NAME)
22
+ # Yaml.load_file returns false if the content is blank
23
+ loaded = YAML.load_file(FILE_NAME) || nil_file
24
+ # if the loaded file misses any of the two keys.
25
+ loaded.merge!(nil_file) { |_k, v1, v2| v1 || v2 }
26
+ end
27
+ end
28
+
29
+ def nil_file
30
+ { SPEEDUPS_KEY => {}, EXCLUDE_PATHS_KEY => [] }
31
+ end
32
+ end
33
+ end
@@ -1,19 +1,21 @@
1
1
  require 'pathname'
2
2
  require 'colorize'
3
- require 'yaml'
4
3
 
5
4
  require_relative 'analyzer'
5
+ require_relative 'config'
6
6
 
7
7
  module Fasterer
8
8
  class FileTraverser
9
+ CONFIG_FILE_NAME = Config::FILE_NAME
10
+ SPEEDUPS_KEY = Config::SPEEDUPS_KEY
11
+ EXCLUDE_PATHS_KEY = Config::EXCLUDE_PATHS_KEY
9
12
 
10
- CONFIG_FILE_NAME = '.fasterer.yml'
11
- SPEEDUPS_KEY = 'speedups'
12
- EXCLUDE_PATHS_KEY = 'exclude_paths'
13
+ attr_reader :config
13
14
 
14
15
  def initialize(path)
15
16
  @path = Pathname(path)
16
17
  @parse_error_paths = []
18
+ @config = Config.new
17
19
  end
18
20
 
19
21
  def traverse
@@ -25,31 +27,18 @@ module Fasterer
25
27
  output_parse_errors if parse_error_paths.any?
26
28
  end
27
29
 
28
- def ignored_speedups
29
- @ignored_speedups ||=
30
- config_file[SPEEDUPS_KEY].select { |_, value| value == false }.keys.map(&:to_sym)
30
+ def config_file
31
+ config.file
31
32
  end
32
33
 
33
- def ignored_files
34
- @ignored_files ||=
35
- config_file[EXCLUDE_PATHS_KEY].flat_map { |path| Dir[path] }
36
- end
37
-
38
- def config_file
39
- @config_file ||= if File.exists?(CONFIG_FILE_NAME)
40
- YAML.load_file(CONFIG_FILE_NAME)
41
- else
42
- nil_config_file
43
- end
34
+ def offenses_found?
35
+ !!offenses_found
44
36
  end
45
37
 
46
38
  private
47
39
 
48
40
  attr_reader :parse_error_paths
49
-
50
- def nil_config_file
51
- { SPEEDUPS_KEY => {}, EXCLUDE_PATHS_KEY => [] }
52
- end
41
+ attr_accessor :offenses_found
53
42
 
54
43
  def scan_file(path)
55
44
  analyzer = Analyzer.new(path)
@@ -57,7 +46,10 @@ module Fasterer
57
46
  rescue RubyParser::SyntaxError, Racc::ParseError, Timeout::Error
58
47
  parse_error_paths.push(path)
59
48
  else
60
- output(analyzer) if offenses_grouped_by_type(analyzer).any?
49
+ if offenses_grouped_by_type(analyzer).any?
50
+ output(analyzer)
51
+ self.offenses_found = true
52
+ end
61
53
  end
62
54
 
63
55
  def scannable_files
@@ -95,5 +87,17 @@ module Fasterer
95
87
  puts parse_error_paths
96
88
  puts
97
89
  end
90
+
91
+ def ignored_speedups
92
+ config.ignored_speedups
93
+ end
94
+
95
+ def ignored_files
96
+ config.ignored_files
97
+ end
98
+
99
+ def nil_config_file
100
+ config.nil_file
101
+ end
98
102
  end
99
103
  end
@@ -1,3 +1,3 @@
1
1
  module Fasterer
2
- VERSION = '0.1.11'
2
+ VERSION = '0.1.12'
3
3
  end
@@ -0,0 +1,33 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fasterer::FileTraverser do
4
+ include FileHelper
5
+ include_context 'isolated environment'
6
+
7
+ describe 'exit status should be' do
8
+ context 'success' do
9
+ it 'when no files exist' do
10
+ `#{fasterer_bin}`
11
+ expect($?.exitstatus).to eq(0)
12
+ end
13
+
14
+ it 'when no files with offenses exist' do
15
+ create_file('user.rb', '[].sample')
16
+ `#{fasterer_bin}`
17
+ expect($?.exitstatus).to eq(0)
18
+ end
19
+ end
20
+
21
+ context 'fail' do
22
+ it 'when file with offenses exists' do
23
+ create_file('user.rb', '[].shuffle.first')
24
+ `#{fasterer_bin}`
25
+ expect($?.exitstatus).to eq(1)
26
+ end
27
+ end
28
+ end
29
+
30
+ def fasterer_bin
31
+ File.expand_path('../../../../bin/fasterer', __FILE__)
32
+ end
33
+ end
@@ -0,0 +1,201 @@
1
+ require 'spec_helper'
2
+
3
+ describe Fasterer::FileTraverser do
4
+ include FileHelper
5
+ include_context 'isolated environment'
6
+
7
+ describe 'config_file' do
8
+ context 'with no config file' do
9
+ let(:file_traverser) { Fasterer::FileTraverser.new('.') }
10
+
11
+ it 'returns nil_config_file' do
12
+ expect(file_traverser.config_file)
13
+ .to eq(file_traverser.send(:nil_config_file))
14
+ end
15
+ end
16
+
17
+ context 'with empty config file' do
18
+ before(:each) do
19
+ create_file(Fasterer::FileTraverser::CONFIG_FILE_NAME, '')
20
+ end
21
+
22
+ let(:file_traverser) { Fasterer::FileTraverser.new('.') }
23
+
24
+ it 'returns nil_config_file' do
25
+ expect(file_traverser.config_file)
26
+ .to eq(file_traverser.send(:nil_config_file))
27
+ end
28
+ end
29
+
30
+ context 'missing exclude_paths key' do
31
+ before(:each) do
32
+ create_file(Fasterer::FileTraverser::CONFIG_FILE_NAME,
33
+ ['speedups:'])
34
+ end
35
+
36
+ let(:file_traverser) { Fasterer::FileTraverser.new('.') }
37
+
38
+ it 'returns nil_config_file' do
39
+ expect(file_traverser.config_file)
40
+ .to eq(file_traverser.send(:nil_config_file))
41
+ end
42
+ end
43
+
44
+ context 'with speedups content but no exclude_paths' do
45
+ let(:config_file_content) do
46
+ "speedups:\n"\
47
+ " keys_each_vs_each_key: true"
48
+ end
49
+
50
+ before(:each) do
51
+ create_file(Fasterer::FileTraverser::CONFIG_FILE_NAME,
52
+ config_file_content)
53
+ end
54
+
55
+ let(:file_traverser) { Fasterer::FileTraverser.new('.') }
56
+
57
+ it 'returns config_file with added exclude paths key' do
58
+ expect(file_traverser.config_file)
59
+ .to eq('speedups' => { 'keys_each_vs_each_key' => true },
60
+ 'exclude_paths' => [])
61
+ end
62
+ end
63
+
64
+ context 'with exclude_paths content but no speedups' do
65
+ let(:config_file_content) do
66
+ "exclude_paths:\n"\
67
+ " - 'spec/support/analyzer/*.rb'"
68
+ end
69
+
70
+ before(:each) do
71
+ create_file(Fasterer::FileTraverser::CONFIG_FILE_NAME,
72
+ config_file_content)
73
+ end
74
+
75
+ let(:file_traverser) { Fasterer::FileTraverser.new('.') }
76
+
77
+ it 'returns config_file with added speedups key' do
78
+ expect(file_traverser.config_file)
79
+ .to eq('speedups' => {},
80
+ 'exclude_paths' => ['spec/support/analyzer/*.rb'])
81
+ end
82
+ end
83
+
84
+ context 'with exclude_paths and speedups content' do
85
+ let(:config_file_content) do
86
+ "speedups:\n"\
87
+ " keys_each_vs_each_key: true\n"\
88
+ "exclude_paths:\n"\
89
+ " - 'spec/support/analyzer/*.rb'"
90
+ end
91
+
92
+ before(:each) do
93
+ create_file(Fasterer::FileTraverser::CONFIG_FILE_NAME,
94
+ config_file_content)
95
+ end
96
+
97
+ let(:file_traverser) { Fasterer::FileTraverser.new('.') }
98
+
99
+ it 'returns config_file' do
100
+ expect(file_traverser.config_file)
101
+ .to eq('speedups' => { 'keys_each_vs_each_key' => true },
102
+ 'exclude_paths' => ['spec/support/analyzer/*.rb'])
103
+ end
104
+ end
105
+
106
+ context 'with empty values' do
107
+ before(:each) do
108
+ create_file(Fasterer::FileTraverser::CONFIG_FILE_NAME,
109
+ ['speedups:',
110
+ '',
111
+ 'exclude_paths:'])
112
+ end
113
+
114
+ let(:file_traverser) { Fasterer::FileTraverser.new('.') }
115
+
116
+ it 'returns nil_config_file' do
117
+ expect(file_traverser.config_file)
118
+ .to eq(file_traverser.send(:nil_config_file))
119
+ end
120
+ end
121
+ end
122
+
123
+ describe 'scannable files' do
124
+ context 'when no files in folder' do
125
+ let(:file_traverser) { Fasterer::FileTraverser.new('.') }
126
+
127
+ it 'returns empty array' do
128
+ expect(file_traverser.send(:scannable_files)).to eq([])
129
+ end
130
+ end
131
+
132
+ context 'only a non-ruby file inside' do
133
+ before do
134
+ create_file('something.yml')
135
+ end
136
+
137
+ let(:file_traverser) { Fasterer::FileTraverser.new('.') }
138
+
139
+ it 'returns empty array' do
140
+ expect(file_traverser.send(:scannable_files)).to eq([])
141
+ end
142
+ end
143
+
144
+ context 'a ruby file inside' do
145
+ let(:file_name) { 'something.rb' }
146
+
147
+ before do
148
+ create_file(file_name)
149
+ end
150
+
151
+ let(:file_traverser) { Fasterer::FileTraverser.new('.') }
152
+
153
+ it 'returns array with that file inside' do
154
+ expect(file_traverser.send(:scannable_files)).to eq([file_name])
155
+ end
156
+ end
157
+
158
+ context 'a ruby file inside that is ignored' do
159
+ let(:file_name) { 'something.rb' }
160
+
161
+ let(:config_file_content) do
162
+ "exclude_paths:\n"\
163
+ " - '#{file_name}'"
164
+ end
165
+
166
+ before(:each) do
167
+ create_file(Fasterer::FileTraverser::CONFIG_FILE_NAME,
168
+ config_file_content)
169
+
170
+ create_file(file_name)
171
+ end
172
+
173
+ let(:file_traverser) { Fasterer::FileTraverser.new('.') }
174
+
175
+ it 'returns empty array' do
176
+ expect(file_traverser.send(:scannable_files)).to eq([])
177
+ end
178
+ end
179
+
180
+ context 'a ruby file inside that is not ignored' do
181
+ let(:file_name) { 'something.rb' }
182
+
183
+ let(:config_file_content) do
184
+ "exclude_paths:\n"\
185
+ " - 'sumthing.rb'"
186
+ end
187
+
188
+ before(:each) do
189
+ create_file(Fasterer::FileTraverser::CONFIG_FILE_NAME,
190
+ config_file_content)
191
+ create_file(file_name)
192
+ end
193
+
194
+ let(:file_traverser) { Fasterer::FileTraverser.new('.') }
195
+
196
+ it 'returns empty array' do
197
+ expect(file_traverser.send(:scannable_files)).to eq([file_name])
198
+ end
199
+ end
200
+ end
201
+ end
@@ -1,9 +1,19 @@
1
1
  require 'bundler/setup'
2
2
  require 'simplecov'
3
- SimpleCov.start
3
+ SimpleCov.start do
4
+ add_filter '/spec/'
5
+ add_filter '/vendor/'
6
+ end
4
7
 
5
8
  require 'fasterer'
9
+ require 'fasterer/cli'
6
10
  require 'pry'
11
+ Dir["#{File.dirname(__FILE__)}/support/*.rb"].each { |f| require f }
12
+
13
+ if ENV['TRAVIS']
14
+ require 'codeclimate-test-reporter'
15
+ CodeClimate::TestReporter.start
16
+ end
7
17
 
8
18
  def RSpec.root
9
19
  @root_path = Pathname.new(File.dirname(__FILE__))
@@ -0,0 +1,33 @@
1
+ require 'fileutils'
2
+
3
+ # Util methods for processing file.
4
+ module FileHelper
5
+ # Create file with at given path with given content. Ensure parent directories
6
+ # exist.
7
+ #
8
+ # path - An String path of file.
9
+ # content - An String or an Array of Strings content of file.
10
+ def create_file(path, content = '')
11
+ file_path = File.expand_path(path)
12
+ dir_path = File.dirname(file_path)
13
+ FileUtils.makedirs(dir_path) unless File.exist?(dir_path)
14
+ write_file(path, content)
15
+ end
16
+
17
+ # Write file content at given path.
18
+ #
19
+ # path - An String path of file.
20
+ # content - An String or an Array of Strings content of file.
21
+ def write_file(path, content)
22
+ File.open(path, 'w') do |file|
23
+ case content
24
+ when ''
25
+ # Create empty file.
26
+ when String
27
+ file.puts content
28
+ when Array
29
+ file.puts content.join("\n")
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,12 @@
1
+ require 'tmpdir'
2
+
3
+ shared_context 'isolated environment' do
4
+ around do |example|
5
+ Dir.mktmpdir do |tmpdir|
6
+ tmpdir = File.realpath(tmpdir)
7
+ Dir.chdir(tmpdir) do
8
+ example.run
9
+ end
10
+ end
11
+ end
12
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fasterer
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.11
4
+ version: 0.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Damir Svrtan
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-05 00:00:00.000000000 Z
11
+ date: 2015-12-19 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: colorize
@@ -131,6 +131,7 @@ files:
131
131
  - lib/fasterer/analyzer.rb
132
132
  - lib/fasterer/binary_call.rb
133
133
  - lib/fasterer/cli.rb
134
+ - lib/fasterer/config.rb
134
135
  - lib/fasterer/file_traverser.rb
135
136
  - lib/fasterer/method_call.rb
136
137
  - lib/fasterer/method_definition.rb
@@ -166,6 +167,8 @@ files:
166
167
  - spec/lib/fasterer/analyzer/29_include_vs_cover_on_range_spec.rb
167
168
  - spec/lib/fasterer/analyzer/98_misc_spec.rb
168
169
  - spec/lib/fasterer/analyzer/99_exceptional_files_spec.rb
170
+ - spec/lib/fasterer/cli_spec.rb
171
+ - spec/lib/fasterer/file_traverser_spec.rb
169
172
  - spec/lib/fasterer/method_call_spec.rb
170
173
  - spec/lib/fasterer/method_definition_spec.rb
171
174
  - spec/lib/fasterer/rescue_call_spec.rb
@@ -193,6 +196,8 @@ files:
193
196
  - spec/support/analyzer/98_misc.rb
194
197
  - spec/support/analyzer/99_exceptional_files.rb
195
198
  - spec/support/binary_call/simple_comparison.rb
199
+ - spec/support/file_helper.rb
200
+ - spec/support/isolated_environment.rb
196
201
  - spec/support/method_call/method_call_on_constant.rb
197
202
  - spec/support/method_call/method_call_on_integer.rb
198
203
  - spec/support/method_call/method_call_on_method_call.rb
@@ -242,7 +247,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
242
247
  version: '0'
243
248
  requirements: []
244
249
  rubyforge_project:
245
- rubygems_version: 2.4.5
250
+ rubygems_version: 2.4.5.1
246
251
  signing_key:
247
252
  specification_version: 4
248
253
  summary: Run Ruby more than fast. Fasterer
@@ -269,6 +274,8 @@ test_files:
269
274
  - spec/lib/fasterer/analyzer/29_include_vs_cover_on_range_spec.rb
270
275
  - spec/lib/fasterer/analyzer/98_misc_spec.rb
271
276
  - spec/lib/fasterer/analyzer/99_exceptional_files_spec.rb
277
+ - spec/lib/fasterer/cli_spec.rb
278
+ - spec/lib/fasterer/file_traverser_spec.rb
272
279
  - spec/lib/fasterer/method_call_spec.rb
273
280
  - spec/lib/fasterer/method_definition_spec.rb
274
281
  - spec/lib/fasterer/rescue_call_spec.rb
@@ -296,6 +303,8 @@ test_files:
296
303
  - spec/support/analyzer/98_misc.rb
297
304
  - spec/support/analyzer/99_exceptional_files.rb
298
305
  - spec/support/binary_call/simple_comparison.rb
306
+ - spec/support/file_helper.rb
307
+ - spec/support/isolated_environment.rb
299
308
  - spec/support/method_call/method_call_on_constant.rb
300
309
  - spec/support/method_call/method_call_on_integer.rb
301
310
  - spec/support/method_call/method_call_on_method_call.rb
@@ -325,4 +334,3 @@ test_files:
325
334
  - spec/support/rescue_call/rescue_with_multiple_classes.rb
326
335
  - spec/support/rescue_call/rescue_with_multiple_classes_and_variable.rb
327
336
  - spec/support/rescue_call/rescue_with_variable.rb
328
- has_rdoc: