reek 1.1.3 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (131) hide show
  1. data/History.txt +44 -4
  2. data/License.txt +20 -0
  3. data/README.rdoc +83 -0
  4. data/Rakefile +0 -1
  5. data/bin/reek +3 -11
  6. data/config/defaults.reek +20 -1
  7. data/features/masking_smells.feature +111 -0
  8. data/features/options.feature +49 -0
  9. data/features/reports.feature +90 -0
  10. data/features/samples.feature +284 -0
  11. data/features/stdin.feature +43 -0
  12. data/features/step_definitions/reek_steps.rb +35 -0
  13. data/features/support/env.rb +38 -0
  14. data/lib/reek.rb +1 -1
  15. data/lib/reek/adapters/application.rb +47 -0
  16. data/lib/reek/adapters/config_file.rb +31 -0
  17. data/lib/reek/adapters/core_extras.rb +72 -0
  18. data/lib/reek/{object_source.rb → adapters/object_source.rb} +15 -19
  19. data/lib/reek/{rake_task.rb → adapters/rake_task.rb} +2 -2
  20. data/lib/reek/adapters/report.rb +91 -0
  21. data/lib/reek/adapters/source.rb +53 -0
  22. data/lib/reek/{spec.rb → adapters/spec.rb} +45 -60
  23. data/lib/reek/block_context.rb +1 -1
  24. data/lib/reek/class_context.rb +26 -6
  25. data/lib/reek/code_context.rb +8 -0
  26. data/lib/reek/code_parser.rb +82 -39
  27. data/lib/reek/command_line.rb +85 -0
  28. data/lib/reek/configuration.rb +51 -0
  29. data/lib/reek/detector_stack.rb +39 -0
  30. data/lib/reek/exceptions.reek +8 -1
  31. data/lib/reek/method_context.rb +53 -11
  32. data/lib/reek/module_context.rb +1 -2
  33. data/lib/reek/name.rb +8 -2
  34. data/lib/reek/sexp_formatter.rb +2 -0
  35. data/lib/reek/smell_warning.rb +26 -8
  36. data/lib/reek/smells/control_couple.rb +8 -4
  37. data/lib/reek/smells/data_clump.rb +88 -0
  38. data/lib/reek/smells/duplication.rb +11 -9
  39. data/lib/reek/smells/feature_envy.rb +3 -4
  40. data/lib/reek/smells/large_class.rb +17 -17
  41. data/lib/reek/smells/long_method.rb +10 -8
  42. data/lib/reek/smells/long_parameter_list.rb +16 -10
  43. data/lib/reek/smells/long_yield_list.rb +1 -1
  44. data/lib/reek/smells/nested_iterators.rb +3 -3
  45. data/lib/reek/smells/simulated_polymorphism.rb +58 -0
  46. data/lib/reek/smells/smell_detector.rb +94 -27
  47. data/lib/reek/smells/uncommunicative_name.rb +23 -23
  48. data/lib/reek/smells/utility_function.rb +27 -11
  49. data/lib/reek/sniffer.rb +183 -0
  50. data/reek.gemspec +5 -5
  51. data/spec/quality/reek_source_spec.rb +15 -0
  52. data/spec/reek/adapters/report_spec.rb +49 -0
  53. data/spec/reek/adapters/should_reek_of_spec.rb +108 -0
  54. data/spec/reek/adapters/should_reek_only_of_spec.rb +87 -0
  55. data/spec/reek/adapters/should_reek_spec.rb +92 -0
  56. data/spec/reek/block_context_spec.rb +7 -1
  57. data/spec/reek/class_context_spec.rb +39 -16
  58. data/spec/reek/code_context_spec.rb +7 -7
  59. data/spec/reek/code_parser_spec.rb +6 -1
  60. data/spec/reek/config_spec.rb +3 -3
  61. data/spec/reek/configuration_spec.rb +12 -0
  62. data/spec/reek/method_context_spec.rb +2 -2
  63. data/spec/reek/name_spec.rb +24 -0
  64. data/spec/reek/object_source_spec.rb +23 -0
  65. data/spec/reek/singleton_method_context_spec.rb +2 -2
  66. data/spec/reek/smell_warning_spec.rb +53 -0
  67. data/spec/reek/smells/data_clump_spec.rb +87 -0
  68. data/spec/reek/smells/duplication_spec.rb +13 -17
  69. data/spec/reek/smells/feature_envy_spec.rb +23 -28
  70. data/spec/reek/smells/large_class_spec.rb +109 -34
  71. data/spec/reek/smells/long_method_spec.rb +140 -3
  72. data/spec/reek/smells/long_parameter_list_spec.rb +1 -2
  73. data/spec/reek/smells/simulated_polymorphism_spec.rb +50 -0
  74. data/spec/reek/smells/smell_detector_spec.rb +53 -0
  75. data/spec/reek/smells/uncommunicative_name_spec.rb +20 -7
  76. data/spec/reek/smells/utility_function_spec.rb +76 -67
  77. data/spec/reek/sniffer_spec.rb +10 -0
  78. data/spec/samples/all_but_one_masked/clean_one.rb +6 -0
  79. data/spec/samples/all_but_one_masked/dirty.rb +7 -0
  80. data/spec/samples/all_but_one_masked/masked.reek +5 -0
  81. data/spec/samples/clean_due_to_masking/clean_one.rb +6 -0
  82. data/spec/samples/clean_due_to_masking/clean_three.rb +6 -0
  83. data/spec/samples/clean_due_to_masking/clean_two.rb +6 -0
  84. data/spec/samples/clean_due_to_masking/dirty_one.rb +7 -0
  85. data/spec/samples/clean_due_to_masking/dirty_two.rb +7 -0
  86. data/spec/samples/clean_due_to_masking/masked.reek +7 -0
  87. data/spec/samples/corrupt_config_file/corrupt.reek +1 -0
  88. data/spec/samples/corrupt_config_file/dirty.rb +7 -0
  89. data/spec/samples/empty_config_file/dirty.rb +7 -0
  90. data/spec/samples/empty_config_file/empty.reek +0 -0
  91. data/spec/samples/exceptions.reek +4 -0
  92. data/spec/{slow/samples → samples}/inline.rb +0 -0
  93. data/spec/samples/masked/dirty.rb +7 -0
  94. data/spec/samples/masked/masked.reek +3 -0
  95. data/spec/samples/mixed_results/clean_one.rb +6 -0
  96. data/spec/samples/mixed_results/clean_three.rb +6 -0
  97. data/spec/samples/mixed_results/clean_two.rb +6 -0
  98. data/spec/samples/mixed_results/dirty_one.rb +7 -0
  99. data/spec/samples/mixed_results/dirty_two.rb +7 -0
  100. data/spec/samples/not_quite_masked/dirty.rb +8 -0
  101. data/spec/samples/not_quite_masked/masked.reek +5 -0
  102. data/spec/{slow/samples → samples}/optparse.rb +0 -0
  103. data/spec/samples/overrides/masked/dirty.rb +7 -0
  104. data/spec/samples/overrides/masked/lower.reek +5 -0
  105. data/spec/samples/overrides/upper.reek +5 -0
  106. data/spec/{slow/samples → samples}/redcloth.rb +0 -0
  107. data/spec/samples/three_clean_files/clean_one.rb +6 -0
  108. data/spec/samples/three_clean_files/clean_three.rb +6 -0
  109. data/spec/samples/three_clean_files/clean_two.rb +6 -0
  110. data/spec/samples/two_smelly_files/dirty_one.rb +7 -0
  111. data/spec/samples/two_smelly_files/dirty_two.rb +7 -0
  112. data/spec/spec.opts +1 -1
  113. data/spec/spec_helper.rb +4 -4
  114. data/tasks/reek.rake +8 -5
  115. data/tasks/test.rake +51 -0
  116. metadata +75 -25
  117. data/README.txt +0 -6
  118. data/lib/reek/options.rb +0 -92
  119. data/lib/reek/report.rb +0 -81
  120. data/lib/reek/smells/smells.rb +0 -81
  121. data/lib/reek/source.rb +0 -127
  122. data/spec/reek/options_spec.rb +0 -13
  123. data/spec/reek/report_spec.rb +0 -48
  124. data/spec/reek/smells/smell_spec.rb +0 -24
  125. data/spec/slow/inline_spec.rb +0 -43
  126. data/spec/slow/optparse_spec.rb +0 -108
  127. data/spec/slow/redcloth_spec.rb +0 -101
  128. data/spec/slow/reek_source_spec.rb +0 -20
  129. data/spec/slow/script_spec.rb +0 -55
  130. data/spec/slow/source_list_spec.rb +0 -40
  131. data/tasks/rspec.rake +0 -21
data/README.txt DELETED
@@ -1,6 +0,0 @@
1
- = Reek
2
-
3
- Code smell detection for Ruby.
4
-
5
- The documentation is at http://wiki.github.com/kevinrutherford/reek
6
- The code lives at http://github.com/kevinrutherford/reek/tree
data/lib/reek/options.rb DELETED
@@ -1,92 +0,0 @@
1
- require 'optparse'
2
- require 'reek/source'
3
-
4
- module Reek
5
-
6
- class Options
7
-
8
- CTX_SORT = '%c %w (%s)'
9
- SMELL_SORT = '[%s] %c %w'
10
-
11
- def self.default_options
12
- {
13
- :format => CTX_SORT
14
- }
15
- end
16
-
17
- @@opts = default_options
18
-
19
- def self.[](key)
20
- @@opts[key]
21
- end
22
-
23
- def self.parse_args(args)
24
- result = default_options
25
- parser = OptionParser.new { |opts| set_options(opts, result) }
26
- parser.parse!(args)
27
- result
28
- end
29
-
30
- def self.set_options(opts, config)
31
- opts.banner = <<EOB
32
- Usage: #{opts.program_name} [options] files...
33
-
34
- If no files are given, Reek reads source code from standard input.
35
- See http://wiki.github.com/kevinrutherford/reek for detailed help.
36
- EOB
37
-
38
- opts.separator "\nOptions:"
39
- set_help_option(opts)
40
- set_sort_option(config, opts)
41
- set_version_option(opts)
42
- end
43
-
44
- def self.parse(args)
45
- begin
46
- @@opts = parse_args(args)
47
- if ARGV.length > 0
48
- return Source.from_pathlist(ARGV)
49
- else
50
- return Source.from_io($stdin, 'stdin')
51
- end
52
- rescue OptionParser::ParseError, SystemCallError => err
53
- fatal_error(err)
54
- end
55
- end
56
-
57
- private
58
-
59
- def self.set_version_option(opts)
60
- opts.on("-v", "--version", "Show version") do
61
- puts "#{opts.program_name} #{Reek::VERSION}"
62
- exit(0)
63
- end
64
- end
65
-
66
- def self.set_help_option(opts)
67
- opts.on("-h", "--help", "Show this message") do
68
- puts opts
69
- exit(0)
70
- end
71
- end
72
-
73
- def self.set_sort_option(config, opts)
74
- opts.on('-f', "--format FORMAT", 'Specify the format of smell warnings') do |arg|
75
- config[:format] = arg unless arg.nil?
76
- end
77
- opts.on('-c', '--context-first', "Sort by context; sets the format string to \"#{CTX_SORT}\"") do
78
- config[:format] = CTX_SORT
79
- end
80
- opts.on('-s', '--smell-first', "Sort by smell; sets the format string to \"#{SMELL_SORT}\"") do
81
- config[:format] = SMELL_SORT
82
- end
83
- end
84
-
85
- def self.fatal_error(err) # :nodoc:
86
- puts "Error: #{err}"
87
- puts "Use '-h' for help."
88
- exit(1)
89
- end
90
-
91
- end
92
- end
data/lib/reek/report.rb DELETED
@@ -1,81 +0,0 @@
1
- require 'set'
2
- require 'reek/smells/smell_detector'
3
-
4
- module Reek
5
- class Report
6
- include Enumerable
7
-
8
- def initialize # :nodoc:
9
- @report = SortedSet.new
10
- end
11
-
12
- #
13
- # Yields, in turn, each SmellWarning in this report.
14
- #
15
- def each
16
- @report.each { |smell| yield smell }
17
- end
18
-
19
- def <<(smell) # :nodoc:
20
- @report << smell
21
- true
22
- end
23
-
24
- def empty?
25
- @report.empty?
26
- end
27
-
28
- def length
29
- @report.length
30
- end
31
-
32
- alias size length
33
-
34
- def [](index) # :nodoc:
35
- @report.to_a[index]
36
- end
37
-
38
- # Creates a formatted report of all the +Smells::SmellWarning+ objects recorded in
39
- # this report, with a heading.
40
- def full_report(desc)
41
- "\"#{desc}\" -- #{length} warnings:\n#{to_s}\n"
42
- end
43
-
44
- # Creates a formatted report of all the +Smells::SmellWarning+ objects recorded in
45
- # this report.
46
- def to_s
47
- @report.map {|smell| smell.report}.join("\n")
48
- end
49
- end
50
-
51
- class ReportList
52
- include Enumerable
53
-
54
- def initialize(sources)
55
- @sources = sources
56
- end
57
-
58
- #
59
- # Yields, in turn, each SmellWarning in every report in this report.
60
- #
61
- def each(&blk)
62
- @sources.each {|src| src.report.each(&blk) }
63
- end
64
-
65
- def empty?
66
- length == 0
67
- end
68
-
69
- def length
70
- @sources.inject(0) {|sum, src| sum + src.report.length }
71
- end
72
-
73
- def smelly_sources
74
- @sources.select {|src| src.smelly? }
75
- end
76
-
77
- def to_s
78
- smelly_sources.map { |src| src.full_report }.join("\n")
79
- end
80
- end
81
- end
@@ -1,81 +0,0 @@
1
- require 'reek/smells/control_couple'
2
- require 'reek/smells/duplication'
3
- require 'reek/smells/feature_envy'
4
- require 'reek/smells/large_class'
5
- require 'reek/smells/long_method'
6
- require 'reek/smells/long_parameter_list'
7
- require 'reek/smells/long_yield_list'
8
- require 'reek/smells/nested_iterators'
9
- require 'reek/smells/uncommunicative_name'
10
- require 'reek/smells/utility_function'
11
- require 'yaml'
12
-
13
- class Hash
14
- def push_keys(hash)
15
- keys.each {|key| hash[key].adopt!(self[key]) }
16
- end
17
-
18
- def adopt!(other)
19
- other.keys.each do |key|
20
- ov = other[key]
21
- if Array === ov and has_key?(key)
22
- self[key] += ov
23
- else
24
- self[key] = ov
25
- end
26
- end
27
- self
28
- end
29
-
30
- def adopt(other)
31
- self.deep_copy.adopt!(other)
32
- end
33
-
34
- def deep_copy
35
- YAML::load(YAML::dump(self))
36
- end
37
- end
38
-
39
- module Reek
40
- class SmellConfig
41
-
42
- SMELL_CLASSES = [
43
- Smells::ControlCouple,
44
- Smells::Duplication,
45
- Smells::FeatureEnvy,
46
- Smells::LargeClass,
47
- Smells::LongMethod,
48
- Smells::LongParameterList,
49
- Smells::LongYieldList,
50
- Smells::NestedIterators,
51
- Smells::UncommunicativeName,
52
- Smells::UtilityFunction,
53
- ]
54
-
55
- def initialize
56
- defaults_file = File.join(File.dirname(__FILE__), '..', '..', '..', 'config', 'defaults.reek')
57
- @config = YAML.load_file(defaults_file)
58
- end
59
-
60
- def smell_listeners()
61
- result = Hash.new {|hash,key| hash[key] = [] }
62
- SMELL_CLASSES.each { |smell| smell.listen(result, @config) }
63
- return result
64
- end
65
-
66
- def load_local(file)
67
- path = File.expand_path(file)
68
- all_reekfiles(path).each do |rfile|
69
- YAML.load_file(rfile).push_keys(@config)
70
- end
71
- self
72
- end
73
-
74
- def all_reekfiles(path)
75
- return [] unless File.exist?(path)
76
- parent = File.dirname(path)
77
- return [] if path == parent
78
- all_reekfiles(parent) + Dir["#{path}/*.reek"]
79
- end
80
- end
81
- end
data/lib/reek/source.rb DELETED
@@ -1,127 +0,0 @@
1
- require 'reek/code_parser'
2
- require 'reek/report'
3
- require 'reek/smells/smells'
4
- require 'ruby_parser'
5
-
6
- module Reek
7
-
8
- #
9
- # A +Source+ object represents a chunk of Ruby source code.
10
- #
11
- # The various class methods are factories that will create +Source+
12
- # instances from various types of input.
13
- #
14
- class Source
15
-
16
- #
17
- # Factory method: creates a +Source+ object by reading Ruby code from
18
- # the +IO+ stream. The stream is consumed upto end-of-file, but the
19
- # source code is not parsed until +report+ is called. +desc+ provides
20
- # a string description to be used in the header of formatted reports.
21
- #
22
- def self.from_io(ios, desc)
23
- code = ios.readlines.join
24
- return new(code, desc)
25
- end
26
-
27
- #
28
- # Factory method: creates a +Source+ object by reading Ruby code from
29
- # the +code+ string. The code is not parsed until +report+ is called.
30
- #
31
- def self.from_s(code)
32
- return new(code, 'string')
33
- end
34
-
35
- #
36
- # Factory method: creates a +Source+ object by reading Ruby code from
37
- # File +file+. The source code is not parsed until +report+ is called.
38
- #
39
- def self.from_f(file)
40
- from_path(file.path)
41
- end
42
-
43
- #
44
- # Factory method: creates a +Source+ object by reading Ruby code from
45
- # the named file. The source code is not parsed until +report+ is called.
46
- #
47
- def self.from_path(filename)
48
- code = IO.readlines(filename).join
49
- return new(code, filename, File.dirname(filename))
50
- end
51
-
52
- #
53
- # Factory method: creates a +Source+ object from an array of file paths.
54
- # No source code is actually parsed until the report is accessed.
55
- #
56
- def self.from_pathlist(paths)
57
- sources = paths.map {|path| Source.from_path(path) }
58
- SourceList.new(sources)
59
- end
60
-
61
- def initialize(code, desc, dir = nil) # :nodoc:
62
- @source = code
63
- @desc = desc
64
- @cf = SmellConfig.new
65
- @cf = @cf.load_local(dir) if dir
66
- end
67
-
68
- def generate_syntax_tree
69
- RubyParser.new.parse(@source, @desc) || s()
70
- end
71
-
72
- #
73
- # Returns a +Report+ listing the smells found in this source. The first
74
- # call to +report+ parses the source code and constructs a list of
75
- # +SmellWarning+s found; subsequent calls simply return this same list.
76
- #
77
- def report
78
- unless @report
79
- @report = Report.new
80
- parser = CodeParser.new(@report, @cf.smell_listeners)
81
- parser.process(generate_syntax_tree)
82
- end
83
- @report
84
- end
85
-
86
- def smelly?
87
- report.length > 0
88
- end
89
-
90
- #
91
- # Checks this source for instances of +smell_class+, and returns +true+
92
- # only if one of them has a report string matching all of the +patterns+.
93
- #
94
- def has_smell?(smell_class, patterns)
95
- report.any? { |smell| smell.matches?(smell_class, patterns) }
96
- end
97
-
98
- # Creates a formatted report of all the +Smells::SmellWarning+ objects recorded in
99
- # this report, with a heading.
100
- def full_report
101
- report.full_report(@desc)
102
- end
103
-
104
- def to_s
105
- @desc
106
- end
107
- end
108
-
109
- #
110
- # Represents a list of Sources as if they were a single source.
111
- #
112
- class SourceList
113
- def initialize(sources)
114
- @sources = sources
115
- end
116
-
117
- def smelly?
118
- @sources.any? {|source| source.smelly? }
119
- end
120
-
121
- def report
122
- ReportList.new(@sources)
123
- end
124
- end
125
- end
126
-
127
- require 'reek/object_source'
@@ -1,13 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper.rb'
2
-
3
- require 'reek/options'
4
-
5
- include Reek
6
-
7
- describe Options, ' when given no arguments' do
8
- it "should retain the default sort order" do
9
- default_order = Options[:format]
10
- Options.parse ['nosuchfile.rb']
11
- Options[:format].should == default_order
12
- end
13
- end
@@ -1,48 +0,0 @@
1
- require File.dirname(__FILE__) + '/../spec_helper.rb'
2
-
3
- require 'reek/smells/smell_detector'
4
- require 'reek/report'
5
- require 'reek/source'
6
- require 'reek/smells/feature_envy'
7
-
8
- include Reek
9
-
10
- describe Report, " when empty" do
11
- before(:each) do
12
- @rpt = Report.new
13
- end
14
-
15
- it 'should have zero length' do
16
- @rpt.length.should == 0
17
- end
18
-
19
- it 'should claim to be empty' do
20
- @rpt.should be_empty
21
- end
22
- end
23
-
24
- describe Report, "to_s" do
25
- before(:each) do
26
- rpt = Source.from_s('def simple(a) a[3] end').report
27
- @report = rpt.to_s.split("\n")
28
- end
29
-
30
- it 'should place each detailed report on a separate line' do
31
- @report.should have_at_least(2).lines
32
- end
33
-
34
- it 'should mention every smell name' do
35
- @report[0].should match(/[Utility Function]/)
36
- @report[1].should match(/[Feature Envy]/)
37
- end
38
- end
39
-
40
- describe Report, " as a SortedSet" do
41
- it 'should only add a smell once' do
42
- rpt = Report.new
43
- rpt << SmellWarning.new(Smells::FeatureEnvy.new, "self", 'too many!')
44
- rpt.length.should == 1
45
- rpt << SmellWarning.new(Smells::FeatureEnvy.new, "self", 'too many!')
46
- rpt.length.should == 1
47
- end
48
- end