preek 1.0.0 → 1.1.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
  SHA1:
3
- metadata.gz: 2300b0792702884ce539a9e7204c06f31fc1dcae
4
- data.tar.gz: 5150fd3103a539732308a852c8efeb07abde9a9c
3
+ metadata.gz: 37e624d4a67976a8062a37c690c17957dd605706
4
+ data.tar.gz: 744b77bcea99590f34da0ee56b8bc72d15525b3a
5
5
  SHA512:
6
- metadata.gz: d112cda98c69d5b28fd531d64a104e2576f1e656dc1c34adbff5b8f0d6491a342fbe54157201b1a8f99b9f9559db5a4fac7e09f87e084514b5786bffd9caa4dd
7
- data.tar.gz: 3f8db5a1be25a4125ee1a23b292fd896b024002ac972a68fcf0c1e7d6805abf501169a582db549276edbf6780e14710492bf8736bac81eff3a69aee5226652ae
6
+ metadata.gz: 9d7fb7c110fd621346a35d1c6c1eaf46b4888a6d5e25a99c2632e5ecc7dfa69b16bf8caf5b16bc7c80e99021a1fbf1cfdede29a2722c2fffa11b8928d7e196bb
7
+ data.tar.gz: fa3d7538f75edf82b4976ffd2ab4047e7e46096617ae4c81a070b461ee6d725ed622a1ad0c6fd27751a7ddbac3fa4dd849e8c5beec1b3f76c309db0f528d5d8d
data/README.md CHANGED
@@ -9,24 +9,23 @@
9
9
 
10
10
  For a pretty colorful output of [Reek](https://github.com/troessner/reek), which is an awesome gem!
11
11
  This is just something I came up with while learning [Thor](https://github.com/wycats/thor).
12
- As an exercise I also worked on getting Reek and Pelusa to stop whining when running them on the code.
13
12
 
14
13
  ## Installation
15
14
 
16
- Install it yourself as:
15
+ $ gem install preek
17
16
 
18
- $ git clone git@github.com:joenas/preek.git
17
+ From source
19
18
 
19
+ $ git clone git@github.com:joenas/preek.git
20
20
  $ cd preek
21
-
22
21
  $ rake install
23
22
 
24
- or
25
23
 
26
- $ gem install preek
27
24
 
28
25
 
29
26
  ## Usage
27
+
28
+ ### CLI
30
29
  ```bash
31
30
  Usage:
32
31
  preek smell FILE(S)|DIR
@@ -45,6 +44,21 @@ At the moment it only supports files, not stuff like this:
45
44
 
46
45
  $ echo "def x() true end" | reek
47
46
 
47
+ ### Ruby
48
+
49
+ ```ruby
50
+ # Convenience method, prints all smells in files
51
+
52
+ filenames = Dir['**/*.rb']
53
+ Preek::Smell(filenames)
54
+
55
+ # To exclude certain smell classes
56
+
57
+ excludes = %w(IrresponsibleModule)
58
+ Preek::Smell(filenames, excludes)
59
+
60
+
61
+ ```
48
62
 
49
63
  ## Contributing
50
64
 
data/lib/preek/cli.rb CHANGED
@@ -21,33 +21,22 @@ module Preek
21
21
  aliases: '-i',
22
22
  desc: 'include IrresponsibleModule smell in output.'
23
23
  def smell(*args)
24
- args ||= @args
25
- @includes = options.keys.map {|key| _aliases[key.to_sym] }
26
- @files, @not_files = args.partition { |file| File.exists? file }
27
- report_smells
28
- report_not_files
24
+ Preek::Smell(args, excludes)
29
25
  end
30
26
 
31
27
  private
32
- def report_smells
33
- return if @files.empty?
34
- smelly_files = SmellCollector.new(@files, excludes).smelly_files
35
- SmellReporter.new(smelly_files).print_smells
36
- end
37
-
38
- def report_not_files
39
- return if @not_files.empty?
40
- say_status :error, "No such file(s) - #{@not_files.join(', ')}.", :red
41
- end
42
-
43
28
  def _aliases
44
29
  {
45
30
  irresponsible: 'IrresponsibleModule'
46
31
  }
47
32
  end
48
33
 
34
+ def includes
35
+ options.keys.map {|key| _aliases[key.to_sym] }
36
+ end
37
+
49
38
  def excludes
50
- (exclude_list - @includes)
39
+ (exclude_list - includes)
51
40
  end
52
41
 
53
42
  def exclude_list
@@ -8,23 +8,36 @@ require 'preek/smell_klass'
8
8
  module Preek
9
9
  # This is a 'Collector'
10
10
  class SmellCollector
11
- def initialize(files, excludes)
12
- @files = files
11
+ def initialize(files, excludes = [])
12
+ @files = files.delete_if { |file| !File.exists? file }
13
13
  @excludes = excludes
14
14
  end
15
15
 
16
16
  def smelly_files
17
- @smelly_files ||= examine_sources
17
+ @smelly_files ||= files_from_sources
18
18
  end
19
19
 
20
20
  private
21
- def examine_sources
22
- sources.map do |source|
23
- smells = Reek::Examiner.new(source).smells
24
- SmellFile.new(smells, @excludes) unless smells.empty?
21
+ def files_from_sources
22
+ filtered_sources.map do |examiner|
23
+ SmellFile.new(examiner) if examiner.smelly?
25
24
  end.compact
26
25
  end
27
26
 
27
+ def filtered_sources
28
+ sources.map do |source|
29
+ examiner = Reek::Examiner.new(source)
30
+ filter_excludes_from(examiner)
31
+ examiner
32
+ end
33
+ end
34
+
35
+ def filter_excludes_from(examiner)
36
+ examiner.smells.delete_if do |smell|
37
+ @excludes.include? smell.smell_class
38
+ end
39
+ end
40
+
28
41
  def sources
29
42
  Reek::Source::SourceLocator.new(@files).all_sources
30
43
  end
@@ -3,29 +3,27 @@ module Preek
3
3
  class SmellFile
4
4
  attr_reader :klasses
5
5
 
6
- def initialize(smells, excludes)
7
- @smells = smells
8
- @excludes = excludes
6
+ def initialize(examiner)
7
+ @examiner = examiner
9
8
  @klasses = {}
10
9
  add_smells_to_klasses
11
10
  end
12
11
 
13
12
  def file
14
- @smells.first.source
13
+ @examiner.description
15
14
  end
16
15
 
17
16
  alias :filename :file
18
17
 
19
18
  private
20
19
  def add_smells_to_klasses
21
- @smells.each do |smell|
22
- next if @excludes.include? smell.smell_class
23
- find(smell.klass).add smell
20
+ @examiner.smells.each do |smell|
21
+ find(smell.klass) << smell
24
22
  end
25
23
  end
26
24
 
27
25
  def find(klassname)
28
- @klasses[klassname.to_sym] ||= SmellKlass.new
26
+ @klasses[klassname.to_sym] ||= SmellKlass.new(klassname)
29
27
  end
30
28
  end
31
29
  end
@@ -1,7 +1,10 @@
1
1
  module Preek
2
2
  # A container for a smelly klass in a file!
3
3
  class SmellKlass
4
- def initialize
4
+ attr_reader :name
5
+
6
+ def initialize(name)
7
+ @name = name
5
8
  @smells = []
6
9
  end
7
10
 
@@ -9,9 +12,7 @@ module Preek
9
12
  @smells << smell
10
13
  end
11
14
 
12
- def name
13
- @smells.first.klass
14
- end
15
+ alias :<< :add
15
16
 
16
17
  def smells
17
18
  @smells.map(&:smell_string)
@@ -2,20 +2,39 @@ require 'thor/shell/color'
2
2
  module Preek
3
3
  # Here we report the smells in a nice fashion
4
4
  class SmellReporter < Thor::Shell::Color
5
- def initialize smelly_files
6
- @smelly_files = smelly_files.delete_if {|smell_file| smell_file.klasses.empty? }
5
+ def initialize(smelly_files, not_files)
6
+ @smelly_files = smelly_files
7
+ @not_files = not_files
7
8
  @padding = 0
8
9
  end
9
10
 
10
11
  def print_smells
11
- return say_status 'success!', 'No smells detected.' if @smelly_files.empty?
12
+ if success?
13
+ say_status 'success!', 'No smells detected.'
14
+ else
15
+ print_each_smell
16
+ end
17
+ print_not_files
18
+ end
19
+ alias :print_result :print_smells
20
+
21
+ private
22
+ def success?
23
+ @smelly_files.empty?
24
+ end
25
+
26
+ def print_not_files
27
+ return if @not_files.empty?
28
+ say_status :error, "No such file(s) - #{@not_files.join(', ')}.", :red
29
+ end
30
+
31
+ def print_each_smell
12
32
  print_line
13
33
  @smelly_files.each do |smelly|
14
34
  say_status 'file', format_path(smelly.filename), :blue
15
35
  print_klasses smelly.klasses
16
36
  end
17
37
  end
18
- private
19
38
 
20
39
  def print_klasses klasses
21
40
  klasses.each do |index, klass|
data/lib/preek/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Preek
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
data/lib/preek.rb CHANGED
@@ -1,8 +1,11 @@
1
1
  module Preek
2
- require 'thor'
3
-
4
2
  require 'preek/version'
5
3
  require 'preek/smell_collector'
6
4
  require 'preek/smell_reporter'
7
5
 
8
- end
6
+ def self.Smell(filenames, excludes = [])
7
+ files, not_files = filenames.partition { |file| File.exists? file }
8
+ smelly_files = SmellCollector.new(files, excludes).smelly_files
9
+ SmellReporter.new(smelly_files, not_files).print_result
10
+ end
11
+ end
data/spec/cli_spec.rb CHANGED
@@ -22,6 +22,9 @@ describe Preek::CLI do
22
22
 
23
23
  context "with non-existing file in ARGS" do
24
24
  let(:args) { ['i/am/not/a_file'] }
25
+ # it "does not output 'success'" do
26
+ # output.should_not include("success")
27
+ # end
25
28
  it "outputs 'No such file'" do
26
29
  output.should include("No such file")
27
30
  end
@@ -108,6 +111,25 @@ describe Preek::CLI do
108
111
  end
109
112
  end
110
113
 
114
+ context "when given one file without smells and another with smells" do
115
+ let(:args){ [test_file('non_smelly'), test_file('two_smelly_classes')] }
116
+ before :each do
117
+ subject.options = {irresponsible: true}
118
+ end
119
+
120
+ it "output contains all smells" do
121
+ output.should include('IrresponsibleModule', 'UncommunicativeMethodName')
122
+ end
123
+
124
+ it "output contains only smelly filename" do
125
+ output.should include(args[1])
126
+ end
127
+
128
+ it "output contains the names of the smelly method" do
129
+ output.should include("#x")
130
+ end
131
+ end
132
+
111
133
  context "when given file has NilCheck smell" do
112
134
  let(:args){ [test_file('nil_check')] }
113
135
  it "output contains 'NilCheck'" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: preek
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
  - Jon Neverland
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-08-24 00:00:00.000000000 Z
11
+ date: 2013-08-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thor