polishgeeks-dev-tools 1.0.0 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c2cfc9419ae8508626d77a5455eb886d1a89c20a
4
- data.tar.gz: 413ff1e0091e908259ec11fcbda3856dff8f6c32
3
+ metadata.gz: 5834fe9ee7e13366bca4384980932e6953e31b63
4
+ data.tar.gz: d75fc73b8bfca128506f6770bec0f22cf9e465fc
5
5
  SHA512:
6
- metadata.gz: 5fe73b0e2f5e6cdd1312a020a10f4ee6d420cd42c5ef41fd32c0a325afda7ed2a89140a7720dbe1ad0d2eb3c3a836528923c4f967c4767612340eb55fddfb747
7
- data.tar.gz: ae87dbbaa7e11a683a519702b8c6082aec66fa8f240ab11fb3cf40a8fa6627d4f219c4f4e27b017d86d1a945ed1614e62f18b0990ded21dddf6cb0264a2bfe73
6
+ metadata.gz: 7c13b0baae9e0177a17bb35d5e0965a8936e2c2a865635fd2242375816686d873ce879b0b890842c346573ed24d107bd10ed2781a9bb53703298fef79f07659d
7
+ data.tar.gz: b6631dbe2d166773049bd2821f46d900df4d41ce6ba7164e566a2f876e4a39d0d7b64e119a46e7d3a107c862be0a6cc5f76fb55d95c982ebf6afa28ee63b7545
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- ruby-2.2.2
1
+ ruby-2.2.3
data/.travis.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 2.1.6
4
- - 2.2.2
3
+ - 2.1.7
4
+ - 2.2.3
5
5
  script:
6
6
  - bundle exec rake
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ # PolishGeeks Dev Tools Changelog
2
+
3
+ ## 1.1.0
4
+
5
+ - Added FinalBlankLine command which check if all files have new line at the end
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- polishgeeks-dev-tools (1.0.0)
4
+ polishgeeks-dev-tools (1.1.0)
5
5
  brakeman
6
6
  faker
7
7
  haml-lint
@@ -63,7 +63,7 @@ GEM
63
63
  docile (1.1.5)
64
64
  equalizer (0.0.11)
65
65
  erubis (2.7.0)
66
- faker (1.4.3)
66
+ faker (1.5.0)
67
67
  i18n (~> 0.5)
68
68
  fastercsv (1.5.5)
69
69
  flay (2.4.0)
@@ -95,7 +95,7 @@ GEM
95
95
  mongoid (~> 4.0.0)
96
96
  rake
97
97
  rspec (~> 3.1)
98
- moped (2.0.6)
98
+ moped (2.0.7)
99
99
  bson (~> 3.0)
100
100
  connection_pool (~> 2.0)
101
101
  optionable (~> 0.2.0)
@@ -191,3 +191,6 @@ DEPENDENCIES
191
191
  bundler
192
192
  polishgeeks-dev-tools!
193
193
  rake
194
+
195
+ BUNDLED WITH
196
+ 1.10.6
data/README.md CHANGED
@@ -50,6 +50,7 @@ determine, which you can use in your project:
50
50
  |-----------------------|-----------|---------------------------------------------------------------------------------------|
51
51
  | brakeman | Rails | A static analysis security vulnerability scanner for Ruby on Rails |
52
52
  | rubocop | - | Used to check Ruby syntax according to our styling |
53
+ | final_blank_line | - | Check if all files have final blank line |
53
54
  | expires_in | - | Checks if there are typos like expire_in, etc that might brake app caching |
54
55
  | haml_lint | - | User to check HAML syntax in the app views |
55
56
  | yard | - | YARD documentation standards checking |
@@ -69,9 +70,10 @@ determine, which you can use in your project:
69
70
 
70
71
  Some validators might accept additional config settings - please refer to this table for a description on how to use them:
71
72
 
72
- | Option | Validator | Description |
73
- |-------------------------------|-----------------------|----------------------------------------------------------|
74
- | rspec_files_structure_ignored | rspec_files_structure | You can provide an array of files that should be ignored |
73
+ | Option | Validator | Description |
74
+ |-------------------------------|-----------------------|-----------------------------------------------------------------------------------------------------|
75
+ | rspec_files_structure_ignored | rspec_files_structure | You can provide an array of files that should be ignored |
76
+ | final_blank_line_ignored | final_blank_line | You can provide an array of files (ex. lib/file.rb) or paths (ex. lib/\*\*/\*) that should be ignored |
75
77
 
76
78
  ## Usage in any Rails/Ruby application
77
79
 
@@ -0,0 +1,116 @@
1
+ module PolishGeeks
2
+ module DevTools
3
+ module Command
4
+ # Validator used to check if all files have final blank line
5
+ class FinalBlankLine < Base
6
+ self.type = :validator
7
+
8
+ attr_reader :counter
9
+
10
+ # Default paths which we want to exclude from analyse
11
+ DEFAULT_PATHS_TO_EXCLUDE = %w(
12
+ coverage
13
+ tmp
14
+ log
15
+ vendor
16
+ public
17
+ app/assets/images
18
+ app/assets/fonts
19
+ )
20
+
21
+ # Executes this command and set output and counter variables
22
+ def execute
23
+ @output = []
24
+ @counter = 0
25
+
26
+ files_to_analyze.each do |file|
27
+ @counter += 1
28
+ @output << sanitize(file) unless file_valid?(file)
29
+ end
30
+ end
31
+
32
+ # @return [Boolean] true if all files have final blank line
33
+ def valid?
34
+ output.empty?
35
+ end
36
+
37
+ # @return [String] default label for this command
38
+ def label
39
+ "Final blank line: #{counter} files checked"
40
+ end
41
+
42
+ # @return [String] message that should be printed when some files don't have
43
+ # final blank line
44
+ def error_message
45
+ "Following files don't have final blank line: \n#{output.join("\n")}\n"
46
+ end
47
+
48
+ private
49
+
50
+ # @return [Array<String>] array with files to analyze
51
+ def files_to_analyze
52
+ # expression {*,.*} is needed because glob method don't take unix-like hidden files
53
+ files_from_path('**/{*,.*}') - excludes
54
+ end
55
+
56
+ # @return [Array<String>] list of files that
57
+ # should be excluded from checking
58
+ def excludes
59
+ (default_excludes + config_excludes).flatten
60
+ end
61
+
62
+ # @return [Array<String>] list of default excluded files
63
+ # defined in DEFAULT_PATHS_TO_EXCLUDE
64
+ def default_excludes
65
+ excluded_files = []
66
+
67
+ DEFAULT_PATHS_TO_EXCLUDE.each do |path|
68
+ excluded_files << files_from_path("#{path}/**/{*,.*}")
69
+ end
70
+
71
+ excluded_files
72
+ end
73
+
74
+ # @return [Array<String>] list of excluded files from config file
75
+ def config_excludes
76
+ excluded_files = []
77
+ config_paths = DevTools.config.final_blank_line_ignored
78
+ return [] unless config_paths
79
+
80
+ config_paths.each do |path|
81
+ excluded_files << files_from_path(path)
82
+ end
83
+
84
+ excluded_files
85
+ end
86
+
87
+ # @param [String] path from which we want take files
88
+ # @return [Array<String>] list of files in path with app prefix path
89
+ # @note if path is a file return array with file path with app prefix path
90
+ def files_from_path(path)
91
+ full_path = "#{::PolishGeeks::DevTools.app_root}/#{path}"
92
+ return [full_path] if File.file?(full_path)
93
+
94
+ Dir.glob(full_path).select { |f| File.file? f }
95
+ end
96
+
97
+ # @param [String] file name that we want to sanitize
98
+ # @return [String] sanitized file name
99
+ # @example
100
+ # file = /home/something/app/lib/lib.rb,
101
+ # where /home/something/app/ is a app root path, then
102
+ # sanitize(file) #=> lib/lib.rb
103
+ def sanitize(file)
104
+ file.gsub("#{PolishGeeks::DevTools.app_root}/", '')
105
+ end
106
+
107
+ # @param [String] file name which we want validate
108
+ # @return [Boolean] true if file is empty or has final blank line.
109
+ # Otherwise return false.
110
+ def file_valid?(file)
111
+ File.size(file) == 0 || IO.readlines(file).last[-1] == "\n"
112
+ end
113
+ end
114
+ end
115
+ end
116
+ end
@@ -21,8 +21,14 @@ module PolishGeeks
21
21
  end
22
22
 
23
23
  attr_accessor :simplecov_threshold
24
- attr_accessor :rspec_files_structure_ignored
25
- attr_accessor :expires_in_files_ignored
24
+
25
+ %i(
26
+ rspec_files_structure
27
+ expires_in_files
28
+ final_blank_line
29
+ ).each do |attr|
30
+ attr_accessor "#{attr}_ignored"
31
+ end
26
32
 
27
33
  # Available commands
28
34
  # All commands will be executed in this order (first rubocop, then rspec, etc)
@@ -31,6 +37,7 @@ module PolishGeeks
31
37
  expires_in
32
38
  brakeman
33
39
  rubocop
40
+ final_blank_line
34
41
  haml_lint
35
42
  allowed_extensions
36
43
  yml_parser
@@ -3,6 +3,6 @@ module PolishGeeks
3
3
  # Dev Tools for PolishGeeks developers
4
4
  module DevTools
5
5
  # Current version of dev tools
6
- VERSION = '1.0.0'
6
+ VERSION = '1.1.0'
7
7
  end
8
8
  end
@@ -0,0 +1,269 @@
1
+ require 'spec_helper'
2
+
3
+ RSpec.describe PolishGeeks::DevTools::Command::FinalBlankLine do
4
+ subject { described_class.new }
5
+
6
+ describe '#execute' do
7
+ let(:files) { [rand.to_s, rand.to_s] }
8
+
9
+ before do
10
+ expect(subject)
11
+ .to receive(:files_to_analyze)
12
+ .and_return(files)
13
+ end
14
+
15
+ context 'when all files are valid' do
16
+ before do
17
+ expect(subject)
18
+ .to receive(:file_valid?)
19
+ .exactly(files.count).times
20
+ .and_return true
21
+ subject.execute
22
+ end
23
+
24
+ it 'should set appropriate variables' do
25
+ expect(subject.output).to eq []
26
+ expect(subject.counter).to eq(files.count)
27
+ end
28
+ end
29
+
30
+ context 'when exist not valid file' do
31
+ before do
32
+ expect(subject)
33
+ .to receive(:file_valid?)
34
+ .exactly(files.count).times
35
+ .and_return false
36
+
37
+ files.each do |file|
38
+ expect(subject)
39
+ .to receive(:sanitize)
40
+ .with(file)
41
+ .and_return(file)
42
+ end
43
+ subject.execute
44
+ end
45
+
46
+ it 'should set appropriate variables' do
47
+ expect(subject.output).to eq files
48
+ expect(subject.counter).to eq(files.count)
49
+ end
50
+ end
51
+ end
52
+
53
+ describe '#valid?' do
54
+ before do
55
+ subject.instance_variable_set('@output', output)
56
+ end
57
+
58
+ context 'when output is empty' do
59
+ let(:output) { [] }
60
+ it { expect(subject.valid?).to eq true }
61
+ end
62
+
63
+ context 'when output have some files' do
64
+ let(:output) { ['file_name'] }
65
+ it { expect(subject.valid?).to eq false }
66
+ end
67
+ end
68
+
69
+ describe '#label' do
70
+ let(:counter) { rand(10) }
71
+ let(:expected) { "Final blank line: #{counter} files checked" }
72
+
73
+ before do
74
+ subject.instance_variable_set('@counter', counter)
75
+ end
76
+
77
+ it { expect(subject.label).to eq expected }
78
+ end
79
+
80
+ describe '#error_message' do
81
+ let(:output) { [rand.to_s, rand.to_s] }
82
+ let(:expected) { "Following files don't have final blank line: \n#{output.join("\n")}\n" }
83
+
84
+ before do
85
+ subject.instance_variable_set('@output', output)
86
+ end
87
+
88
+ it { expect(subject.error_message).to eq expected }
89
+ end
90
+
91
+ describe '#files_to_analyze' do
92
+ let(:files) { [rand.to_s, rand.to_s] }
93
+ let(:excludes) { [files[0]] }
94
+ let(:expected) { [files[1]] }
95
+
96
+ before do
97
+ expect(subject)
98
+ .to receive(:files_from_path)
99
+ .with('**/{*,.*}')
100
+ .and_return(files)
101
+
102
+ expect(subject)
103
+ .to receive(:excludes)
104
+ .and_return(excludes)
105
+ end
106
+
107
+ it { expect(subject.send(:files_to_analyze)).to eq expected }
108
+ end
109
+
110
+ describe '#excludes' do
111
+ let(:defaults) { [rand.to_s, rand.to_s] }
112
+ let(:configs) { [rand.to_s] }
113
+ let(:expected) { (defaults + configs).flatten }
114
+
115
+ before do
116
+ expect(subject)
117
+ .to receive(:default_excludes)
118
+ .and_return(defaults)
119
+
120
+ expect(subject)
121
+ .to receive(:config_excludes)
122
+ .and_return(configs)
123
+ end
124
+
125
+ it { expect(subject.send(:excludes)).to eq expected }
126
+ end
127
+
128
+ describe '#default_excludes' do
129
+ before do
130
+ described_class::DEFAULT_PATHS_TO_EXCLUDE.each do |path|
131
+ expect(subject)
132
+ .to receive(:files_from_path)
133
+ .with("#{path}/**/{*,.*}")
134
+ .and_return(path)
135
+ end
136
+ end
137
+
138
+ it { expect(subject.send(:default_excludes)).to eq described_class::DEFAULT_PATHS_TO_EXCLUDE }
139
+ end
140
+
141
+ describe '#config_excludes' do
142
+ context 'final_blank_line_ignored is set' do
143
+ let(:paths) { [rand.to_s, rand.to_s] }
144
+ let(:config) { double(final_blank_line_ignored: paths) }
145
+
146
+ before do
147
+ expect(PolishGeeks::DevTools)
148
+ .to receive(:config)
149
+ .and_return config
150
+
151
+ paths.each do |path|
152
+ expect(subject)
153
+ .to receive(:files_from_path)
154
+ .with("#{path}")
155
+ .and_return(path)
156
+ end
157
+ end
158
+
159
+ it { expect(subject.send(:config_excludes)).to eq paths }
160
+ end
161
+
162
+ context 'final_blank_line_ignored is not set' do
163
+ let(:config) { double(final_blank_line_ignored: nil) }
164
+ before do
165
+ expect(PolishGeeks::DevTools)
166
+ .to receive(:config)
167
+ .and_return config
168
+ end
169
+ it { expect(subject.send(:config_excludes)).to eq [] }
170
+ end
171
+ end
172
+
173
+ describe '#files_from_path' do
174
+ let(:app_root) { PolishGeeks::DevTools.app_root }
175
+
176
+ context 'path is a directory' do
177
+ let(:path) { rand.to_s }
178
+ let(:file_in_path) { "#{app_root}/#{rand}" }
179
+ let(:dir_in_path) { "#{app_root}/#{rand}" }
180
+ before do
181
+ expect(File)
182
+ .to receive(:file?)
183
+ .with("#{app_root}/#{path}")
184
+ .and_return(false)
185
+
186
+ expect(Dir)
187
+ .to receive(:glob)
188
+ .with("#{app_root}/#{path}")
189
+ .and_return([file_in_path, dir_in_path])
190
+
191
+ expect(File)
192
+ .to receive(:file?)
193
+ .with(file_in_path)
194
+ .and_return(true)
195
+
196
+ expect(File)
197
+ .to receive(:file?)
198
+ .with(dir_in_path)
199
+ .and_return(false)
200
+ end
201
+ it { expect(subject.send(:files_from_path, path)).to eq [file_in_path] }
202
+ end
203
+
204
+ context 'path is a file' do
205
+ let(:path) { rand.to_s }
206
+ before do
207
+ expect(File)
208
+ .to receive(:file?)
209
+ .with("#{app_root}/#{path}")
210
+ .and_return(true)
211
+ end
212
+ it { expect(subject.send(:files_from_path, path)).to eq ["#{app_root}/#{path}"] }
213
+ end
214
+ end
215
+
216
+ describe '#sanitize' do
217
+ let(:file) { rand.to_s }
218
+ let(:app_root) { PolishGeeks::DevTools.app_root }
219
+ let(:path) { "#{app_root}/#{file}" }
220
+
221
+ it { expect(subject.send(:sanitize, "#{app_root}/#{path}")).to eq file }
222
+ end
223
+
224
+ describe '#file_valid?' do
225
+ let(:file) { rand.to_s }
226
+
227
+ context 'file is not empty' do
228
+ before do
229
+ expect(File)
230
+ .to receive(:size)
231
+ .with(file)
232
+ .and_return(1)
233
+ end
234
+
235
+ context 'file has final blank line' do
236
+ before do
237
+ expect(IO)
238
+ .to receive(:readlines)
239
+ .with(file)
240
+ .and_return([rand.to_s + "\n"])
241
+ end
242
+
243
+ it { expect(subject.send(:file_valid?, file)).to eq true }
244
+ end
245
+
246
+ context 'file does not have final blank line' do
247
+ before do
248
+ expect(IO)
249
+ .to receive(:readlines)
250
+ .with(file)
251
+ .and_return([rand.to_s + 'end'])
252
+ end
253
+
254
+ it { expect(subject.send(:file_valid?, file)).to eq false }
255
+ end
256
+ end
257
+
258
+ context 'file is empty' do
259
+ before do
260
+ expect(File)
261
+ .to receive(:size?)
262
+ .with(file)
263
+ .and_return(0)
264
+
265
+ it { expect(subject.send(:file_valid?, file)).to eq true }
266
+ end
267
+ end
268
+ end
269
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: polishgeeks-dev-tools
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maciej Mensfeld
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-20 00:00:00.000000000 Z
11
+ date: 2015-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -217,6 +217,7 @@ files:
217
217
  - ".ruby-gemset"
218
218
  - ".ruby-version"
219
219
  - ".travis.yml"
220
+ - CHANGELOG.md
220
221
  - Gemfile
221
222
  - Gemfile.lock
222
223
  - README.md
@@ -231,6 +232,7 @@ files:
231
232
  - lib/polishgeeks/dev-tools/command/coverage.rb
232
233
  - lib/polishgeeks/dev-tools/command/examples_comparator.rb
233
234
  - lib/polishgeeks/dev-tools/command/expires_in.rb
235
+ - lib/polishgeeks/dev-tools/command/final_blank_line.rb
234
236
  - lib/polishgeeks/dev-tools/command/haml_lint.rb
235
237
  - lib/polishgeeks/dev-tools/command/readme.rb
236
238
  - lib/polishgeeks/dev-tools/command/rspec.rb
@@ -258,6 +260,7 @@ files:
258
260
  - spec/lib/polishgeeks/dev-tools/command/coverage_spec.rb
259
261
  - spec/lib/polishgeeks/dev-tools/command/examples_comparator_spec.rb
260
262
  - spec/lib/polishgeeks/dev-tools/command/expires_in_spec.rb
263
+ - spec/lib/polishgeeks/dev-tools/command/final_blank_line_spec.rb
261
264
  - spec/lib/polishgeeks/dev-tools/command/haml_lint_spec.rb
262
265
  - spec/lib/polishgeeks/dev-tools/command/readme_spec.rb
263
266
  - spec/lib/polishgeeks/dev-tools/command/rspec_files_names_spec.rb
@@ -308,6 +311,7 @@ test_files:
308
311
  - spec/lib/polishgeeks/dev-tools/command/coverage_spec.rb
309
312
  - spec/lib/polishgeeks/dev-tools/command/examples_comparator_spec.rb
310
313
  - spec/lib/polishgeeks/dev-tools/command/expires_in_spec.rb
314
+ - spec/lib/polishgeeks/dev-tools/command/final_blank_line_spec.rb
311
315
  - spec/lib/polishgeeks/dev-tools/command/haml_lint_spec.rb
312
316
  - spec/lib/polishgeeks/dev-tools/command/readme_spec.rb
313
317
  - spec/lib/polishgeeks/dev-tools/command/rspec_files_names_spec.rb