checker 0.7.0 → 0.8.0.beta

Sign up to get free protection for your applications and to get access to all the features.
Files changed (50) hide show
  1. checksums.yaml +5 -5
  2. data/.travis.yml +5 -6
  3. data/Gemfile +1 -1
  4. data/Gemfile.lock +89 -45
  5. data/Guardfile +16 -0
  6. data/Rakefile +2 -2
  7. data/bin/checker +2 -2
  8. data/checker.gemspec +13 -11
  9. data/lib/checker.rb +14 -16
  10. data/lib/checker/cli.rb +19 -22
  11. data/lib/checker/core_ext.rb +7 -9
  12. data/lib/checker/helper.rb +1 -1
  13. data/lib/checker/installator.rb +8 -8
  14. data/lib/checker/modules/base.rb +42 -44
  15. data/lib/checker/modules/coffeescript.rb +5 -3
  16. data/lib/checker/modules/conflict.rb +4 -4
  17. data/lib/checker/modules/console_log.rb +4 -3
  18. data/lib/checker/modules/haml.rb +4 -2
  19. data/lib/checker/modules/javascript.rb +5 -3
  20. data/lib/checker/modules/multifile.rb +16 -0
  21. data/lib/checker/modules/pry.rb +4 -4
  22. data/lib/checker/modules/rubocop.rb +12 -4
  23. data/lib/checker/modules/ruby.rb +6 -3
  24. data/lib/checker/modules/sass.rb +16 -14
  25. data/lib/checker/modules/slim.rb +4 -2
  26. data/lib/checker/modules/yaml.rb +5 -3
  27. data/lib/checker/options.rb +1 -1
  28. data/lib/checker/results/console_log.rb +1 -1
  29. data/lib/checker/rvm.rb +2 -2
  30. data/lib/checker/version.rb +1 -1
  31. data/spec/checker/cli_spec.rb +10 -8
  32. data/spec/checker/fixtures/conflict/without_conflict.rb +1 -2
  33. data/spec/checker/fixtures/pry/with_both.rb +1 -2
  34. data/spec/checker/fixtures/pry/with_pry.rb +1 -1
  35. data/spec/checker/fixtures/pry/with_pry_remote.rb +1 -1
  36. data/spec/checker/fixtures/pry/without_pry.rb +1 -2
  37. data/spec/checker/fixtures/ruby/good.rb +1 -1
  38. data/spec/checker/modules/base_spec.rb +15 -15
  39. data/spec/checker/modules/coffeescript_spec.rb +11 -11
  40. data/spec/checker/modules/conflict_spec.rb +8 -8
  41. data/spec/checker/modules/console_log_spec.rb +13 -13
  42. data/spec/checker/modules/haml_spec.rb +13 -13
  43. data/spec/checker/modules/javascript_spec.rb +22 -22
  44. data/spec/checker/modules/pry_spec.rb +10 -10
  45. data/spec/checker/modules/ruby_spec.rb +9 -10
  46. data/spec/checker/modules/sass_spec.rb +46 -46
  47. data/spec/checker/modules/slim_spec.rb +12 -12
  48. data/spec/checker/modules/yaml_spec.rb +12 -12
  49. data/spec/spec_helper.rb +6 -14
  50. metadata +39 -10
@@ -1,7 +1,7 @@
1
1
  module Checker
2
2
  class Installator
3
3
  def self.template
4
- dir = File.expand_path('../../..', __FILE__)
4
+ dir = File.expand_path("../../..", __FILE__)
5
5
  temp = File.read(File.join(dir, "/templates/checker-prepare-commit-msg"))
6
6
  ERB.new(temp).result
7
7
  end
@@ -21,7 +21,7 @@ module Checker
21
21
  check_hook!
22
22
 
23
23
  pre_commit = "#{hooks_dir}/prepare-commit-msg"
24
- if File.exists?(pre_commit)
24
+ if File.exist?(pre_commit)
25
25
  puts "Removing current git precommit hook..."
26
26
  File.delete(pre_commit)
27
27
  end
@@ -36,9 +36,9 @@ module Checker
36
36
  if File.exist?(pre_commit)
37
37
  puts "Appending checker script to existing prepare-commit-msg hook..."
38
38
  begin
39
- open(pre_commit, 'a') do |f|
40
- f.puts(self.template)
41
- f.chmod(0755)
39
+ open(pre_commit, "a") do |f|
40
+ f.puts(template)
41
+ f.chmod(0o755)
42
42
  end
43
43
  rescue Exception => e
44
44
  puts "Couldn't append checker script: #{e.message}"
@@ -46,12 +46,12 @@ module Checker
46
46
  end
47
47
  exit 0
48
48
  else
49
- tmp = self.template
49
+ tmp = template
50
50
  str = "#!/bin/bash \n #{tmp}"
51
51
  begin
52
- open(pre_commit, "w") do |f|
52
+ open(pre_commit, "w") do |f|
53
53
  f.puts(str)
54
- f.chmod(0755)
54
+ f.chmod(0o755)
55
55
  end
56
56
  rescue Exception => e
57
57
  puts "Couldn't write checker script: #{e.message}"
@@ -5,13 +5,13 @@ module Checker
5
5
 
6
6
  def initialize(file_list = nil)
7
7
  self.files = file_list
8
- self.full_results = {:total => 0, :ok => 0, :warning => 0, :fail => 0}
8
+ self.full_results = { total: 0, ok: 0, warning: 0, fail: 0 }
9
9
  end
10
10
 
11
11
  def check
12
- check_files_existing or return true
12
+ check_files_existing || (return true)
13
13
  print_module_header
14
- check_executable or return true
14
+ check_executable || (return true)
15
15
  check_all_files
16
16
  valid?
17
17
  end
@@ -23,17 +23,17 @@ module Checker
23
23
  def files_to_check
24
24
  @files_to_check ||= begin
25
25
  if self.class.extensions.any?
26
- self.files.select { |f|
26
+ files.select do |f|
27
27
  self.class.extensions.map { |ex| f.ends_with?(".#{ex}") }.any?
28
- }
28
+ end
29
29
  else
30
- self.files
30
+ files
31
31
  end
32
32
  end
33
33
  end
34
34
 
35
35
  def classname
36
- self.class.to_s.split('::').last
36
+ self.class.to_s.split("::").last
37
37
  end
38
38
 
39
39
  private
@@ -63,33 +63,33 @@ module Checker
63
63
  true
64
64
  end
65
65
 
66
+ def after_check; end
67
+
66
68
  def check_all_files
67
- with_checker_cache do
68
- @results = files_to_check.map do |file_name|
69
- gather_result :total
70
- color " Checking #{file_name}...", :yellow
71
- result = check_one_file(file_name)
72
- show_status result.status
73
- gather_result result.status
74
- flush_and_forget_output result.status
75
- result.success?
76
- end
69
+ @results = files_to_check.map do |file_name|
70
+ gather_result :total
71
+ color " Checking #{file_name}...", :yellow
72
+ result = check_one_file(file_name)
73
+ show_status result.status
74
+ gather_result result.status
75
+ flush_and_forget_output result.status
76
+ after_check
77
+ result.success?
77
78
  end
78
79
  end
79
80
 
80
- def check_one(filename, options = {})
81
+ def check_one(_filename, _options = {})
81
82
  puts "Called from #{self.class} - extend me in here!"
82
83
  false
83
84
  end
84
85
 
85
- def check_one_file file_name
86
+ def check_one_file(file_name)
86
87
  checksum = ::Digest::MD5.hexdigest(file_name)
87
88
  debug(file_name)
88
- checkout_file(file_name, checksum)
89
- check_one(checkout_file_name(checksum), :extension => File.extname(file_name))
89
+ check_one(file_name, extension: File.extname(file_name))
90
90
  end
91
91
 
92
- def self.extensions *args
92
+ def self.extensions(*args)
93
93
  @extensions ||= []
94
94
  if args.empty?
95
95
  @extensions
@@ -98,8 +98,8 @@ module Checker
98
98
  end
99
99
  end
100
100
 
101
- def gather_result result
102
- self.full_results[result] += 1
101
+ def gather_result(result)
102
+ full_results[result] += 1
103
103
  end
104
104
 
105
105
  def plain_command(cmd, options = {})
@@ -108,13 +108,13 @@ module Checker
108
108
  end
109
109
 
110
110
  def silent_command(cmd, options = {})
111
- options = { :output => false, :return_boolean => true }.merge(options)
111
+ options = { output: false, return_boolean: true }.merge(options)
112
112
  cmd = parse_command(cmd, options)
113
113
  execute(cmd, options)
114
114
  end
115
115
 
116
116
  def flush_and_forget_output(status)
117
- print @buffer.to_s if (status == :warning or status == :fail)
117
+ print @buffer.to_s if (status == :warning) || (status == :fail)
118
118
  @buffer = ""
119
119
  end
120
120
 
@@ -150,15 +150,15 @@ module Checker
150
150
  end
151
151
 
152
152
  def exitstatus
153
- $? && $?.exitstatus
153
+ $CHILD_STATUS && $CHILD_STATUS.exitstatus
154
154
  end
155
155
 
156
156
  def success?
157
- $? && $?.success?
157
+ $CHILD_STATUS && $CHILD_STATUS.success?
158
158
  end
159
159
 
160
- def parse_command command, options
161
- options = { :bundler => true, :output => true, :rvm => true }.merge(options)
160
+ def parse_command(command, options)
161
+ options = { bundler: true, output: true, rvm: true }.merge(options)
162
162
  command = bundler_command(command) if use_bundler? && options[:bundler]
163
163
  command = rvm_command(command) if use_rvm? && options[:rvm]
164
164
  command << " > /dev/null" unless options[:output]
@@ -166,7 +166,7 @@ module Checker
166
166
  end
167
167
 
168
168
  def color(str, color)
169
- print str.colorize(color) if str.length > 0
169
+ print str.colorize(color) unless str.empty?
170
170
  end
171
171
 
172
172
  def name
@@ -174,7 +174,7 @@ module Checker
174
174
  end
175
175
 
176
176
  def use_bundler?
177
- File.exists?("Gemfile.lock")
177
+ File.exist?("Gemfile.lock")
178
178
  end
179
179
 
180
180
  def bundler_command(command)
@@ -182,7 +182,7 @@ module Checker
182
182
  end
183
183
 
184
184
  def use_rvm?
185
- File.exists?(rvm_shell)
185
+ false
186
186
  end
187
187
 
188
188
  def rails_project?
@@ -190,7 +190,7 @@ module Checker
190
190
  end
191
191
 
192
192
  def rails_with_ap?
193
- rails_project? && File.exists?("app/assets")
193
+ rails_project? && File.exist?("app/assets")
194
194
  end
195
195
 
196
196
  def rvm_command(command)
@@ -202,21 +202,19 @@ module Checker
202
202
  end
203
203
 
204
204
  def with_checker_cache
205
- begin
206
- `mkdir .checker-cache`
207
- yield if block_given?
208
- ensure
209
- `rm -rf .checker-cache > /dev/null 2>&1`
210
- ## for sass check
211
- `rm -rf app/assets/stylesheets/checker-cache*` if rails_with_ap?
212
- end
205
+ `mkdir .checker-cache`
206
+ yield if block_given?
207
+ ensure
208
+ `rm -rf .checker-cache > /dev/null 2>&1`
209
+ ## for sass check
210
+ `rm -rf app/assets/stylesheets/checker-cache*` if rails_with_ap?
213
211
  end
214
212
 
215
- def checkout_file file_name, target
213
+ def checkout_file(file_name, target)
216
214
  `git show :0:#{file_name} > #{checkout_file_name(target)} 2>/dev/null`
217
215
  end
218
216
 
219
- def checkout_file_name target
217
+ def checkout_file_name(target)
220
218
  ".checker-cache/#{target}"
221
219
  end
222
220
  end
@@ -1,14 +1,16 @@
1
1
  module Checker
2
2
  module Modules
3
3
  class Coffeescript < Base
4
- extensions 'coffee'
4
+ extensions "coffee"
5
+
5
6
  private
6
- def check_one(file, opts = {})
7
+
8
+ def check_one(file, _opts = {})
7
9
  Checker::Result.result(self, plain_command("cat #{file} | egrep -v '^//=' | coffee -sc > /dev/null"))
8
10
  end
9
11
 
10
12
  def check_for_executable
11
- silent_command('coffee -v', :bundler => false)
13
+ silent_command("coffee -v", bundler: false)
12
14
  end
13
15
 
14
16
  def dependency_message
@@ -1,19 +1,19 @@
1
1
  module Checker
2
2
  module Modules
3
3
  class Conflict < Base
4
-
5
4
  private
6
- def check_one(file, opts = {})
5
+
6
+ def check_one(file, _opts = {})
7
7
  status = [check_for_conflict_start(file), check_for_conflict_end(file)].all_true?
8
8
  Checker::Result.result(self, status ? 0 : 1)
9
9
  end
10
10
 
11
11
  def check_for_conflict_start(file)
12
- !plain_command("grep -n \"<<<<<<< \" #{file}", :bundler => false, :return_boolean => true, :rvm => false)
12
+ !plain_command("grep -n \"<<<<<<< \" #{file}", bundler: false, return_boolean: true, rvm: false)
13
13
  end
14
14
 
15
15
  def check_for_conflict_end(file)
16
- !plain_command("grep -n \">>>>>>> \" #{file}", :bundler => false, :return_boolean => true, :rvm => false)
16
+ !plain_command("grep -n \">>>>>>> \" #{file}", bundler: false, return_boolean: true, rvm: false)
17
17
  end
18
18
  end
19
19
  end
@@ -1,11 +1,12 @@
1
1
  module Checker
2
2
  module Modules
3
3
  class ConsoleLog < Base
4
- extensions 'coffee', 'js'
4
+ extensions "coffee", "js"
5
5
 
6
6
  private
7
- def check_one(file, opts = {})
8
- Checker::Result.result(self, plain_command("grep -n \"console\\.log\" #{file}", :bundler => false, :rvm => false))
7
+
8
+ def check_one(file, _opts = {})
9
+ Checker::Result.result(self, plain_command("grep -n \"console\\.log\" #{file}", bundler: false, rvm: false))
9
10
  end
10
11
 
11
12
  def show_status(status)
@@ -1,9 +1,11 @@
1
1
  module Checker
2
2
  module Modules
3
3
  class Haml < Base
4
- extensions 'haml'
4
+ extensions "haml"
5
+
5
6
  private
6
- def check_one(file, opts = {})
7
+
8
+ def check_one(file, _opts = {})
7
9
  Checker::Result.result(self, plain_command("haml --check #{file} >> /dev/null"))
8
10
  end
9
11
 
@@ -1,14 +1,16 @@
1
1
  module Checker
2
2
  module Modules
3
3
  class Javascript < Base
4
- extensions 'js'
4
+ extensions "js"
5
+
5
6
  private
6
- def check_one(file, opts = {})
7
+
8
+ def check_one(file, _opts = {})
7
9
  Checker::Result.result(self, plain_command("jsl -process #{file}"))
8
10
  end
9
11
 
10
12
  def check_for_executable
11
- silent_command('jsl -help:conf', :bundler => false)
13
+ silent_command("jsl -help:conf", bundler: false)
12
14
  end
13
15
 
14
16
  def dependency_message
@@ -0,0 +1,16 @@
1
+ module Checker
2
+ module Modules
3
+ class Multifile < Base
4
+ def check_all_files
5
+ gather_result :total
6
+ color " Checking #{files_to_check.join(', ')}...", :yellow
7
+ result = check_all(files_to_check)
8
+ show_status result.status
9
+ gather_result result.status
10
+ flush_and_forget_output result.status
11
+ @results = [result.success?]
12
+ after_check
13
+ end
14
+ end
15
+ end
16
+ end
@@ -1,19 +1,19 @@
1
1
  module Checker
2
2
  module Modules
3
3
  class Pry < Base
4
-
5
4
  private
6
- def check_one(file, opts = {})
5
+
6
+ def check_one(file, _opts = {})
7
7
  status = [check_for_binding_pry(file), check_for_binding_remote_pry(file)].all_true?
8
8
  Checker::Result.result(self, status ? 0 : 1)
9
9
  end
10
10
 
11
11
  def check_for_binding_pry(file)
12
- !plain_command("grep -n \"binding\\.pry\" #{file}", :bundler => false, :return_boolean => true, :rvm => false)
12
+ !plain_command("grep -n \"binding\\.pry\" #{file}", bundler: false, return_boolean: true, rvm: false)
13
13
  end
14
14
 
15
15
  def check_for_binding_remote_pry(file)
16
- !plain_command("grep -n \"binding\\.remote_pry\" #{file}", :bundler => false, :return_boolean => true, :rvm => false)
16
+ !plain_command("grep -n \"binding\\.remote_pry\" #{file}", bundler: false, return_boolean: true, rvm: false)
17
17
  end
18
18
  end
19
19
  end
@@ -1,10 +1,10 @@
1
1
  module Checker
2
2
  module Modules
3
- class Rubocop < Base
4
- extensions 'rb'
3
+ class Rubocop < Multifile
5
4
  private
6
- def check_one(file, opts = {})
7
- Checker::Result.result(self, plain_command("rubocop #{file}", :bundler => false))
5
+
6
+ def check_all(files, _opts = {})
7
+ Checker::Result.result(self, plain_command("rubocop -f c #{files.join(' ')}", bundler: false))
8
8
  end
9
9
 
10
10
  def dependency_message
@@ -12,6 +12,14 @@ module Checker
12
12
  str << "Install rubocop using rubygems: 'gem install rubocop'"
13
13
  str
14
14
  end
15
+
16
+ def fix_message(files)
17
+ "\nTo Autocorrect, execute: rubocop -a #{files.join(' ')}\n"
18
+ end
19
+
20
+ def after_check
21
+ puts fix_message(files_to_check).colorize(:green) if @results.first == false
22
+ end
15
23
  end
16
24
  end
17
25
  end
@@ -1,10 +1,13 @@
1
1
  module Checker
2
2
  module Modules
3
3
  class Ruby < Base
4
- extensions 'rb'
4
+ extensions "rb"
5
+
5
6
  private
6
- def check_one(file, opts = {})
7
- Checker::Result.result(self, plain_command("ruby -c #{file}", :bundler => false))
7
+
8
+ def check_one(file, _opts = {})
9
+ puts " using version: #{`ruby -v`.chomp}"
10
+ Checker::Result.result(self, plain_command("ruby -c #{file}", bundler: false))
8
11
  end
9
12
  end
10
13
  end
@@ -1,23 +1,25 @@
1
1
  module Checker
2
2
  module Modules
3
3
  class Sass < Base
4
- extensions 'scss', 'sass'
4
+ extensions "scss", "sass"
5
+
5
6
  private
7
+
6
8
  def check_one(file, opts = {})
7
- if preconditions_for_rails?(file) && Checker::Options.use_rails_for_sass
9
+ if Checker::Options.use_rails_for_sass && preconditions_for_rails?(file)
8
10
  rails_check(file, opts)
9
11
  else
10
12
  normal_check(file, opts)
11
13
  end
12
14
  end
13
15
 
14
- def rails_check(file, opts)
16
+ def rails_check(file, _opts)
15
17
  debug("Rails project detected") if rails_project?
16
18
  Checker::Result.result(self, plain_command(%(rails runner "Rails.application.assets.find_asset(\\"#{Dir.pwd}/#{file}\\").to_s")))
17
19
  end
18
20
 
19
21
  def normal_check(file, opts)
20
- Checker::Result.result(self, plain_command("sass #{"--scss" if opts[:extension] == ".scss"} -c #{file}"))
22
+ Checker::Result.result(self, plain_command("sass #{'--scss' if opts[:extension] == '.scss'} -c #{file}"))
21
23
  end
22
24
 
23
25
  def check_for_executable
@@ -36,41 +38,41 @@ module Checker
36
38
  end
37
39
 
38
40
  ## for rails project
39
- def checkout_file file_name, target
41
+ def checkout_file(file_name, target)
40
42
  debug("git show :0:#{file_name} > #{checkout_file_name(target)} 2>/dev/null")
41
43
  mkdir_if_necessary(checkout_file_name(target))
42
44
  `git show :0:#{file_name} > #{checkout_file_name(target)} 2>/dev/null`
43
45
  end
44
46
 
45
47
  def mkdir_if_necessary(target)
46
- dir = target.gsub(File.basename(target), '')
48
+ dir = target.gsub(File.basename(target), "")
47
49
  create_dir(dir)
48
50
  end
49
51
 
50
52
  def create_dir(dir)
51
53
  debug("Creating dir #{dir}")
52
- Dir.mkdir(dir) unless File.exists?(dir)
54
+ Dir.mkdir(dir) unless File.exist?(dir)
53
55
  end
54
56
 
55
57
  def stylesheets_dir
56
58
  "app/assets/stylesheets/"
57
59
  end
58
60
 
59
- def checkout_file_name target
60
- (Checker::Options.use_rails_for_sass && rails_with_ap?) ? "#{stylesheets_dir}checker-cache#{target}" : super
61
+ def checkout_file_name(target)
62
+ Checker::Options.use_rails_for_sass && rails_with_ap? ? "#{stylesheets_dir}checker-cache#{target}" : super
61
63
  end
62
64
 
63
- def proper_path file_name
64
- file_name.gsub(/app\/assets\/stylesheets\//, '')
65
+ def proper_path(file_name)
66
+ file_name.gsub(/app\/assets\/stylesheets\//, "")
65
67
  end
66
68
 
67
- def check_one_file file_name
68
- if (Checker::Options.use_rails_for_sass && rails_with_ap?)
69
+ def check_one_file(file_name)
70
+ if Checker::Options.use_rails_for_sass && rails_with_ap?
69
71
  color " (using rails runner)... ", :blue
70
72
  debug(file_name)
71
73
  debug(proper_path(file_name))
72
74
  checkout_file(file_name, proper_path(file_name))
73
- check_one(checkout_file_name(proper_path(file_name)), :extension => File.extname(file_name))
75
+ check_one(checkout_file_name(proper_path(file_name)), extension: File.extname(file_name))
74
76
  else
75
77
  super
76
78
  end