zombie_scout 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: cd9585ab8fa23daa27c71cfc11fcf41ebc6a371c
4
+ data.tar.gz: dec2c2a121cc1fb3f3ab9bd9bafdccc1f865ad72
5
+ SHA512:
6
+ metadata.gz: 43c329a1bdfd59df278b325ff65e1d9d879b9ee8dfa0e12195a5db39a63b516ef928e3ef8a7b0c5166f3e6e79e16612a05871d08d10709a81d2340514618eaf8
7
+ data.tar.gz: 1365f7a6b810c2ec97447d8efeb52d4c055f9f9a62dd688ba9587baa6308c11f365a7db9b0e2e1c73d078c9c47b8c6337d3bfb337097a97471ab596346ea538a
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ coverage
6
+ InstalledFiles
7
+ lib/bundler/man
8
+ pkg
9
+ rdoc
10
+ spec/reports
11
+ test/tmp
12
+ test/version_tmp
13
+ tmp
14
+
15
+ # YARD artifacts
16
+ .yardoc
17
+ _yardoc
18
+ doc/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --color
2
+ --format nested
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ zombie_scout
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.1.1
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1
4
+ - 2.0
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'parser', '~> 2.1'
4
+ gem 'thor', '~> 0.18'
5
+
6
+ group :test do
7
+ gem 'rake'
8
+ gem 'rspec'
9
+ gem 'fakefs', require: 'fakefs/safe'
10
+ end
11
+
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ ast (1.1.0)
5
+ diff-lcs (1.1.3)
6
+ fakefs (0.5.0)
7
+ parser (2.1.4)
8
+ ast (~> 1.1)
9
+ slop (~> 3.4, >= 3.4.5)
10
+ rake (10.1.0)
11
+ rspec (2.11.0)
12
+ rspec-core (~> 2.11.0)
13
+ rspec-expectations (~> 2.11.0)
14
+ rspec-mocks (~> 2.11.0)
15
+ rspec-core (2.11.1)
16
+ rspec-expectations (2.11.3)
17
+ diff-lcs (~> 1.1.3)
18
+ rspec-mocks (2.11.3)
19
+ slop (3.4.7)
20
+ thor (0.18.1)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ fakefs
27
+ parser (~> 2.1)
28
+ rake
29
+ rspec
30
+ thor (~> 0.18)
data/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute must
108
+ include a readable copy of the attribution notices contained
109
+ within such NOTICE file, excluding those notices that do not
110
+ pertain to any part of the Derivative Works, in at least one
111
+ of the following places: within a NOTICE text file distributed
112
+ as part of the Derivative Works; within the Source form or
113
+ documentation, if provided along with the Derivative Works; or,
114
+ within a display generated by the Derivative Works, if and
115
+ wherever such third-party notices normally appear. The contents
116
+ of the NOTICE file are for informational purposes only and
117
+ do not modify the License. You may add Your own attribution
118
+ notices within Derivative Works that You distribute, alongside
119
+ or as an addendum to the NOTICE text from the Work, provided
120
+ that such additional attribution notices cannot be construed
121
+ as modifying the License.
122
+
123
+ You may add Your own copyright statement to Your modifications and
124
+ may provide additional or different license terms and conditions
125
+ for use, reproduction, or distribution of Your modifications, or
126
+ for any such Derivative Works as a whole, provided Your use,
127
+ reproduction, and distribution of the Work otherwise complies with
128
+ the conditions stated in this License.
129
+
130
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
131
+ any Contribution intentionally submitted for inclusion in the Work
132
+ by You to the Licensor shall be under the terms and conditions of
133
+ this License, without any additional terms or conditions.
134
+ Notwithstanding the above, nothing herein shall supersede or modify
135
+ the terms of any separate license agreement you may have executed
136
+ with Licensor regarding such Contributions.
137
+
138
+ 6. Trademarks. This License does not grant permission to use the trade
139
+ names, trademarks, service marks, or product names of the Licensor,
140
+ except as required for reasonable and customary use in describing the
141
+ origin of the Work and reproducing the content of the NOTICE file.
142
+
143
+ 7. Disclaimer of Warranty. Unless required by applicable law or
144
+ agreed to in writing, Licensor provides the Work (and each
145
+ Contributor provides its Contributions) on an "AS IS" BASIS,
146
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147
+ implied, including, without limitation, any warranties or conditions
148
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149
+ PARTICULAR PURPOSE. You are solely responsible for determining the
150
+ appropriateness of using or redistributing the Work and assume any
151
+ risks associated with Your exercise of permissions under this License.
152
+
153
+ 8. Limitation of Liability. In no event and under no legal theory,
154
+ whether in tort (including negligence), contract, or otherwise,
155
+ unless required by applicable law (such as deliberate and grossly
156
+ negligent acts) or agreed to in writing, shall any Contributor be
157
+ liable to You for damages, including any direct, indirect, special,
158
+ incidental, or consequential damages of any character arising as a
159
+ result of this License or out of the use or inability to use the
160
+ Work (including but not limited to damages for loss of goodwill,
161
+ work stoppage, computer failure or malfunction, or any and all
162
+ other commercial damages or losses), even if such Contributor
163
+ has been advised of the possibility of such damages.
164
+
165
+ 9. Accepting Warranty or Additional Liability. While redistributing
166
+ the Work or Derivative Works thereof, You may choose to offer,
167
+ and charge a fee for, acceptance of support, warranty, indemnity,
168
+ or other liability obligations and/or rights consistent with this
169
+ License. However, in accepting such obligations, You may act only
170
+ on Your own behalf and on Your sole responsibility, not on behalf
171
+ of any other Contributor, and only if You agree to indemnify,
172
+ defend, and hold each Contributor harmless for any liability
173
+ incurred by, or claims asserted against, such Contributor by reason
174
+ of your accepting any such warranty or additional liability.
175
+
176
+ END OF TERMS AND CONDITIONS
177
+
178
+ APPENDIX: How to apply the Apache License to your work.
179
+
180
+ To apply the Apache License to your work, attach the following
181
+ boilerplate notice, with the fields enclosed by brackets "{}"
182
+ replaced with your own identifying information. (Don't include
183
+ the brackets!) The text should be enclosed in the appropriate
184
+ comment syntax for the file format. We also recommend that a
185
+ file or class name and description of purpose be included on the
186
+ same "printed page" as the copyright notice for easier
187
+ identification within third-party archives.
188
+
189
+ Copyright {yyyy} {name of copyright owner}
190
+
191
+ Licensed under the Apache License, Version 2.0 (the "License");
192
+ you may not use this file except in compliance with the License.
193
+ You may obtain a copy of the License at
194
+
195
+ http://www.apache.org/licenses/LICENSE-2.0
196
+
197
+ Unless required by applicable law or agreed to in writing, software
198
+ distributed under the License is distributed on an "AS IS" BASIS,
199
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200
+ See the License for the specific language governing permissions and
201
+ limitations under the License.
202
+
data/README.md ADDED
@@ -0,0 +1,95 @@
1
+ zombie_scout
2
+ =============
3
+
4
+ Find dead methods in your Ruby app
5
+
6
+ **zom·bie code** *noun* Undead code that shambles around your repository, and
7
+ eats your brains.
8
+
9
+ You don't want zombie code around. But you can't spend all your time looking
10
+ for it. So get yourself a Zombie Scout.
11
+
12
+ Zombie Scout is light & quick. Its only tools are `parser` and `grep`. It
13
+ parses your code to find method declarations, and then greps through your
14
+ project's source, looking for each method. If Zombie Scout can't find any
15
+ calls to a method, it presumes the method is dead, and reports back to you.
16
+
17
+ ### Fair Warning
18
+
19
+ Zombie Scout isn't exhaustive or thorough - it's a scout, not a spy. (That
20
+ could be another project, though - a Zombie Spy.)
21
+
22
+ If you generate methods in a way that's hard to grep for...
23
+
24
+ method_name = ['s', 'e', 'c', 'r', 'e', 't']
25
+ object.send(method_name.join(''))
26
+
27
+ ...then Zombie Scout won't find it. Remember: light & quick.
28
+
29
+ That said, it *will* find methods defined with `attr_reader` & friends, or
30
+ `Forwardable` delegates, or Rails scopes. Rails delegators are on the To-Do
31
+ list.
32
+
33
+ If you have methods that are used by another library - say, callbacks - Zombie
34
+ Scout will probably think they're dead, because it's not looking at the source
35
+ for that other library.
36
+
37
+ Finally, if you have a method named after a common human-language word, and
38
+ that word appears in (say) hard-coded strings or comments, Zombie Scout will
39
+ think it's calling the method, and assume that the method is used.
40
+
41
+ # this is an awesome method! <-- false positive right there
42
+ def awesome
43
+ 'AWESOME!'
44
+ end
45
+
46
+ Be wise.
47
+
48
+ ## Installation
49
+
50
+ The basics:
51
+
52
+ gem install 'zombie_scout'
53
+
54
+ Or, add this to your Gemfile:
55
+
56
+ gem 'zombie_scout', github: 'danbernier/zombie_scout'
57
+
58
+ ## Usage
59
+
60
+ You can run it on a whole folder:
61
+
62
+ dan@aleph:~/projects/zombie_scout$ zombie_scout scout
63
+ Scouting out /home/dan/projects/zombie_scout!
64
+ lib/zombie_scout/method_finder.rb:23 on_def
65
+ lib/zombie_scout/method_finder.rb:29 on_defs
66
+ lib/zombie_scout/method_finder.rb:35 on_send
67
+ lib/zombie_scout/method_finder.rb:77 on_sym
68
+ Scouted 23 methods in 8 files. Found 4 potential zombies.
69
+
70
+ (See what I meant about callbacks and false-positives?)
71
+
72
+ Or you can run it on a given file or glob:
73
+
74
+ dan@aleph:~/projects/zombie_scout$ zombie_scout scout lib/zombie_scout.rb
75
+ Scouting out /home/dan/projects/zombie_scout!
76
+ Scouted 0 methods in 1 files. Found 0 potential zombies.
77
+
78
+ ## Code Status
79
+
80
+ * [![Build Status](https://travis-ci.org/danbernier/zombie_scout.png?branch=master)](https://travis-ci.org/danbernier/zombie_scout)
81
+
82
+ ## TODOs
83
+
84
+ * [x] parse for attr_reader/writer/accessors, & forwardables, & rails scopes
85
+ * [ ] parse for rails delegators
86
+ * [ ] let users configure: files to search for methods, files to search for calls...probably in `.zombie_scout`.
87
+
88
+ ToThinkAbouts:
89
+ * [ ] extract a hash-y report structure that can be used by whatever, from the default report
90
+ * [ ] pass a method name, or a ruby class
91
+ * [ ] rspec/mini-test drop-in tests that can be added easily, to fail your
92
+ build if the scout or spy finds dead code. (This is probably a bad idea.)
93
+
94
+ Have ZombieScout make a hash-y report, & feed it to ZombieSpy, so the Spy knows where to focus.
95
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new
5
+
6
+ task :default => :spec
data/bin/zombie_scout ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env ruby
2
+ require 'rubygems'
3
+ require 'zombie_scout'
4
+
5
+ ZombieScout::App.start(ARGV)
@@ -0,0 +1,11 @@
1
+ require 'thor'
2
+ require 'zombie_scout/mission'
3
+
4
+ module ZombieScout
5
+ class App < Thor
6
+ desc "scout", "scout for zombie code in current directory"
7
+ def scout(*globs)
8
+ Mission.new(globs).scout
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,29 @@
1
+ module ZombieScout
2
+ class MethodCallFinder
3
+
4
+ def initialize(ruby_project)
5
+ @ruby_project = ruby_project
6
+ end
7
+
8
+ def count_calls(method_name)
9
+ method_name = method_name.to_s
10
+ method_name.sub!(/=$/, ' *=')
11
+ find_occurrances(method_name).size
12
+ end
13
+
14
+ private
15
+
16
+ def find_occurrances(method_name)
17
+ # TODO somehow expose some of this config for end-users
18
+ command = "grep -rnw --include=\"*.rb\" --include=\"*.erb\" --binary-files=without-match \"#{method_name}\" #{files_to_search} "
19
+ # grep -r --include="*.rb" --include="*.erb" -nw PATTERN app lib
20
+
21
+ grep_lines = `#{command}`
22
+ grep_lines.split("\n")
23
+ end
24
+
25
+ def files_to_search
26
+ @ruby_project.folders.join(' ')
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,102 @@
1
+ require 'parser/current'
2
+
3
+ module ZombieScout
4
+ Method = Class.new(Struct.new(:name, :location))
5
+
6
+ class MethodFinder < Parser::AST::Processor
7
+ def initialize(ruby_source)
8
+ @ruby_source = ruby_source
9
+ end
10
+
11
+ def find_methods
12
+ @methods = []
13
+ @private_method_calls = []
14
+
15
+ node = Parser::CurrentRuby.parse(@ruby_source.source)
16
+ process(node)
17
+
18
+ @methods.reject { |method|
19
+ @private_method_calls.include?(method.name)
20
+ }
21
+ end
22
+
23
+ def on_def(node)
24
+ method_name, args, body = *node
25
+ stash_method(method_name, node)
26
+ process(body)
27
+ end
28
+
29
+ def on_defs(node)
30
+ self_node, method_name, args, body = *node
31
+ stash_method(method_name, node)
32
+ process(body)
33
+ end
34
+
35
+ def on_send(node)
36
+ receiver, method_name, *args = *node
37
+ if respond_to?(:"on_#{method_name}", true)
38
+ send(:"on_#{method_name}", args, node)
39
+ elsif receiver.nil? # Then it's a private method call
40
+ @private_method_calls << method_name
41
+ process_all(args)
42
+ end
43
+ end
44
+
45
+ private
46
+
47
+ def on_attr_reader(args, node)
48
+ args.each do |arg|
49
+ attr_method_name = symbol(arg)
50
+ stash_method(attr_method_name, node)
51
+ end
52
+ end
53
+
54
+ def on_attr_writer(args, node)
55
+ args.each do |arg|
56
+ attr_method_name = symbol(arg)
57
+ stash_method(:"#{attr_method_name}=", node)
58
+ end
59
+ end
60
+
61
+ def on_attr_accessor(args, node)
62
+ args.each do |arg|
63
+ attr_method_name = symbol(arg)
64
+ stash_method(attr_method_name, node)
65
+ stash_method(:"#{attr_method_name}=", node)
66
+ end
67
+ end
68
+
69
+ def on_def_delegators(args, node)
70
+ args.drop(1).each do |arg|
71
+ attr_method_name = symbol(arg)
72
+ stash_method(attr_method_name, node)
73
+ end
74
+ end
75
+
76
+ def on_def_delegator(args, node)
77
+ attr_method_name = symbol(args.last)
78
+ stash_method(attr_method_name, node)
79
+ end
80
+
81
+ def on_scope(args, node)
82
+ attr_method_name = symbol(args.first)
83
+ stash_method(attr_method_name, node)
84
+ end
85
+
86
+ def symbol(node)
87
+ SymbolExtracter.new.process(node)
88
+ end
89
+
90
+ def stash_method(method_name, node)
91
+ line_number = node.location.line
92
+ location = [@ruby_source.path, line_number].join(":")
93
+ @methods << Method.new(method_name, location)
94
+ end
95
+ end
96
+
97
+ class SymbolExtracter < Parser::AST::Processor
98
+ def on_sym(node)
99
+ node.to_a[0]
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,43 @@
1
+ require 'zombie_scout/ruby_project'
2
+ require 'zombie_scout/method_finder'
3
+ require 'zombie_scout/method_call_finder'
4
+
5
+ module ZombieScout
6
+ class Mission
7
+ def initialize(globs)
8
+ puts "Scouting out #{Dir.pwd}!"
9
+ @ruby_project = RubyProject.new(*globs)
10
+ end
11
+
12
+ def scout
13
+ zombies.each do |zombie|
14
+ puts [zombie.location, zombie.name] * "\t"
15
+ end
16
+
17
+ puts "Scouted #{methods.size} methods in #{sources.size} files. Found #{zombies.size} potential zombies."
18
+ end
19
+
20
+ private
21
+
22
+ def zombies
23
+ @zombies ||= methods.select { |method|
24
+ might_be_dead?(method)
25
+ }
26
+ end
27
+
28
+ def methods
29
+ @methods ||= sources.map { |ruby_source|
30
+ MethodFinder.new(ruby_source).find_methods
31
+ }.flatten
32
+ end
33
+
34
+ def sources
35
+ @sources ||= @ruby_project.ruby_sources
36
+ end
37
+
38
+ def might_be_dead?(method)
39
+ @method_call_counter ||= MethodCallFinder.new(@ruby_project)
40
+ @method_call_counter.count_calls(method.name) < 2
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,39 @@
1
+ require 'zombie_scout/ruby_source'
2
+ require 'pathname'
3
+
4
+ module ZombieScout
5
+ class RubyProject
6
+
7
+ def initialize(*globs)
8
+ @globs = globs
9
+ @globs = %w[app config lib] if @globs.empty?
10
+ end
11
+
12
+ def ruby_sources
13
+ ruby_file_paths.map { |path| RubySource.new(path) }
14
+ end
15
+
16
+ def ruby_file_paths
17
+ globs.map { |glob| Dir.glob(glob) }.flatten.map { |path|
18
+ path.sub(/^\//, '')
19
+ }
20
+ end
21
+
22
+ def globs
23
+ pathnames = @globs.map { |g| Pathname.new(g) }
24
+ pathnames.map { |pathname|
25
+ if pathname.directory?
26
+ "#{pathname}/**/*.rb"
27
+ else
28
+ pathname.to_s
29
+ end
30
+ }
31
+ end
32
+
33
+ def folders # TODO this is only called from Mission...weird?
34
+ %w[app config lib].select { |folder|
35
+ Dir.exist?(folder)
36
+ }
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,13 @@
1
+ module ZombieScout
2
+ class RubySource
3
+ def initialize(filename)
4
+ @path = filename
5
+ end
6
+
7
+ attr_reader :path
8
+
9
+ def source
10
+ @source ||= File.read(path)
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,3 @@
1
+ module ZombieScout
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ require 'zombie_scout/app'
@@ -0,0 +1 @@
1
+ require 'fakefs/spec_helpers'
@@ -0,0 +1,122 @@
1
+ require 'spec_helper'
2
+ require 'zombie_scout/method_finder'
3
+
4
+ describe ZombieScout::MethodFinder, '#find_methods' do
5
+ let(:ruby_source) {
6
+ double(:ruby_source, path: 'lib/fizzbuzz.rb', source: ruby_code)
7
+ }
8
+ let(:zombies) {
9
+ ZombieScout::MethodFinder.new(ruby_source).find_methods.sort_by(&:name)
10
+ }
11
+
12
+ context 'when a ruby file has instance or class methods' do
13
+ let(:ruby_code) {
14
+ "class FizzBuzz
15
+ def fizz
16
+ 'plop plop'
17
+ end
18
+
19
+ def self.buzz
20
+ 'bzz bzz bzz'
21
+ end
22
+ end"
23
+ }
24
+
25
+ it 'can find the methods' do
26
+ expect(zombies[0].name).to eq :buzz
27
+ expect(zombies[0].location).to eq 'lib/fizzbuzz.rb:6'
28
+
29
+ expect(zombies[1].name).to eq :fizz
30
+ expect(zombies[1].location).to eq 'lib/fizzbuzz.rb:2'
31
+ end
32
+ end
33
+
34
+ context 'when a ruby file has attr_readers' do
35
+ let(:ruby_code) {
36
+ "class Book
37
+ attr_reader :title, :author
38
+ end"
39
+ }
40
+
41
+ it 'can find attr_readers' do
42
+ expect(zombies.map(&:name)).to eq(%i[author title])
43
+ end
44
+ end
45
+
46
+ context 'when a ruby file has attr_writers' do
47
+ let(:ruby_code) {
48
+ "class Book
49
+ attr_writer :title, :author
50
+ end"
51
+ }
52
+ it 'can find attr_writers' do
53
+ expect(zombies.map(&:name)).to eq(%i[author= title=])
54
+ end
55
+ end
56
+
57
+ context 'when a ruby file has attr_accessors' do
58
+ let(:ruby_code) {
59
+ "class Book
60
+ attr_accessor :title, :author
61
+ end"
62
+ }
63
+ it 'can find attr_accessors' do
64
+ expect(zombies.map(&:name)).to eq(%i[author author= title title=])
65
+ end
66
+ end
67
+
68
+ context 'when a ruby file uses Forwardable::def_delegator' do
69
+ let(:ruby_code) {
70
+ "class RecordCollection
71
+ extend Forwardable
72
+ def_delegator :@records, :[], :record_number
73
+ end"
74
+ }
75
+ it 'can find methods created by def_delegator' do
76
+ expect(zombies.map(&:name)).to match_array([:record_number])
77
+ end
78
+ end
79
+
80
+ context 'when a ruby file uses Forwardable::def_delegators' do
81
+ let(:ruby_code) {
82
+ "class RecordCollection
83
+ extend Forwardable
84
+ def_delegators :@records, :size, :<<, :map
85
+ end"
86
+ }
87
+ it 'can find methods created by def_delegators' do
88
+ expect(zombies.map(&:name)).to eq(%i[<< map size])
89
+ end
90
+ end
91
+
92
+ context 'when a rails model has scopes' do
93
+ let(:ruby_code) {
94
+ "class Post
95
+ scope :published, -> { where(published: true) }
96
+ scope :draft, -> { where(published: true) }
97
+ end"
98
+ }
99
+ it 'can find scopes' do
100
+ expect(zombies.map(&:name)).to eq(%i[draft published])
101
+ end
102
+ end
103
+
104
+ context 'when a ruby file has private methods' do
105
+ let(:ruby_code) {
106
+ "class FizzBuzz
107
+ def fizz
108
+ magick_helper
109
+ end
110
+
111
+ private
112
+ def magick_helper
113
+ 'magick sauce'
114
+ end
115
+ end"
116
+ }
117
+ it 'excludes private method calls, since we KNOW they are called' do
118
+ expect(zombies.map(&:name)).to match_array([:fizz])
119
+ expect(zombies.map(&:name)).not_to include :magick_helper
120
+ end
121
+ end
122
+ end
@@ -0,0 +1,108 @@
1
+ require 'spec_helper'
2
+
3
+ require 'zombie_scout/ruby_project'
4
+
5
+ describe ZombieScout::RubyProject, '#globs' do
6
+ include FakeFS::SpecHelpers
7
+ before do
8
+ FileUtils.mkdir('lib')
9
+ FileUtils.touch('lib/app.rb')
10
+ end
11
+
12
+ context 'when given a dirname' do
13
+ it 'appends **/*.rb' do
14
+ project = ZombieScout::RubyProject.new('lib')
15
+ expect(project.globs).to eq ['lib/**/*.rb']
16
+ end
17
+ end
18
+
19
+ context 'when given a globs' do
20
+ it 'leaves it alone' do
21
+ project = ZombieScout::RubyProject.new('lib/*.rb')
22
+ expect(project.globs).to eq ['lib/*.rb']
23
+ end
24
+ end
25
+
26
+ context 'when given a file path' do
27
+ it 'leaves it alone' do
28
+ project = ZombieScout::RubyProject.new('lib/app.rb')
29
+ expect(project.globs).to eq ['lib/app.rb']
30
+ end
31
+ end
32
+ end
33
+
34
+ describe ZombieScout::RubyProject, '#ruby_sources' do
35
+ include FakeFS::SpecHelpers
36
+
37
+ def touch(path)
38
+ FileUtils.mkdir_p(File.dirname(path))
39
+ FileUtils.touch(path)
40
+ end
41
+
42
+ context 'in a normal ruby project' do
43
+ let(:files) do
44
+ [
45
+ 'lib/ironman/jarvis.rb',
46
+ 'lib/ironman/suit.rb',
47
+ 'lib/hulk/green/smash.rb',
48
+ 'spec/ironman/jarvis_spec.rb',
49
+ 'test/ironman/test_suit.rb'
50
+ ]
51
+ end
52
+ before do
53
+ files.each do |file|
54
+ touch(file)
55
+ end
56
+ end
57
+
58
+ context 'without globs' do
59
+ it 'returns a RubySource for each *.rb under the lib dir' do
60
+ sources = ZombieScout::RubyProject.new.ruby_sources
61
+ expect(sources.map(&:path).sort).to eq [
62
+ 'lib/hulk/green/smash.rb',
63
+ 'lib/ironman/jarvis.rb',
64
+ 'lib/ironman/suit.rb',
65
+ ]
66
+ end
67
+ end
68
+
69
+ context 'with globs' do
70
+ it 'returns a RubySource for each *.rb matching the globs' do
71
+ sources = ZombieScout::RubyProject.new('lib/**/jarv*').ruby_sources
72
+ expect(sources.map(&:path)).to eq [
73
+ 'lib/ironman/jarvis.rb'
74
+ ]
75
+ end
76
+ end
77
+
78
+ context 'with directory names' do
79
+ it 'returns a RubySource for each *.rb (recursively) in the dir' do
80
+ sources = ZombieScout::RubyProject.new('lib/hulk').ruby_sources
81
+ expect(sources.map(&:path)).to eq [
82
+ 'lib/hulk/green/smash.rb'
83
+ ]
84
+ end
85
+ end
86
+ end
87
+
88
+ context 'in a rails project' do
89
+ it 'returns a RubySource for each *.rb under app, config, & lib' do
90
+ files = [
91
+ 'app/models/suit.rb',
92
+ 'app/controllers/suits_controller.rb',
93
+ 'config/initializers/extensions.rb',
94
+ 'spec/views/suit_view_spec.rb',
95
+ 'test/models/test_suit.rb'
96
+ ]
97
+
98
+ files.each { |file| touch(file) }
99
+
100
+ sources = ZombieScout::RubyProject.new.ruby_sources
101
+ expect(sources.map(&:path).sort).to eq [
102
+ 'app/controllers/suits_controller.rb',
103
+ 'app/models/suit.rb',
104
+ 'config/initializers/extensions.rb'
105
+ ]
106
+ end
107
+ end
108
+ end
@@ -0,0 +1,26 @@
1
+ require 'spec_helper'
2
+ require 'zombie_scout/ruby_source'
3
+
4
+ describe ZombieScout::RubySource do
5
+ include FakeFS::SpecHelpers
6
+
7
+ let(:ruby_code) do
8
+ "
9
+ class RubyCode
10
+ def awesome?
11
+ true
12
+ end
13
+ end
14
+ "
15
+ end
16
+
17
+ let(:filename) { 'ruby_code.rb' }
18
+
19
+ subject do
20
+ File.open(filename, 'w') { |f| f << ruby_code }
21
+ ZombieScout::RubySource.new(filename)
22
+ end
23
+
24
+ its(:path) { should eq(filename) }
25
+ its(:source) { should eq(ruby_code) }
26
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'zombie_scout/version'
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'zombie_scout'
8
+ s.version = ZombieScout::VERSION
9
+ s.date = Date.today.to_s
10
+
11
+ s.summary = "Find dead methods in your Ruby app"
12
+ s.description = "
13
+ zombie_scout finds methods in classes in your ruby project,
14
+ and then searches for calls to them, and tells you which ones
15
+ are never called.".strip.gsub(/^\s*/, '')
16
+ s.homepage = 'https://github.com/danbernier/zombie_scout'
17
+ s.license = 'ASL2'
18
+
19
+ s.authors = ["Dan Bernier"]
20
+ s.email = ['danbernier@gmail.com']
21
+
22
+ s.files = `git ls-files -z`.split("\x0")
23
+ s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
24
+ s.test_files = s.files.grep(%r{^(test|spec|features)/})
25
+ s.require_paths = ["lib"]
26
+
27
+ s.add_dependency('parser', '~> 2.1')
28
+ s.add_dependency('thor', '~> 0.18')
29
+ s.add_development_dependency "bundler", "~> 1.5"
30
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: zombie_scout
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Dan Bernier
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: parser
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '2.1'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '2.1'
27
+ - !ruby/object:Gem::Dependency
28
+ name: thor
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '0.18'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '0.18'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '1.5'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.5'
55
+ description: "zombie_scout finds methods in classes in your ruby project, \nand then
56
+ searches for calls to them, and tells you which ones \nare never called."
57
+ email:
58
+ - danbernier@gmail.com
59
+ executables:
60
+ - zombie_scout
61
+ extensions: []
62
+ extra_rdoc_files: []
63
+ files:
64
+ - .gitignore
65
+ - .rspec
66
+ - .ruby-gemset
67
+ - .ruby-version
68
+ - .travis.yml
69
+ - Gemfile
70
+ - Gemfile.lock
71
+ - LICENSE
72
+ - README.md
73
+ - Rakefile
74
+ - bin/zombie_scout
75
+ - lib/zombie_scout.rb
76
+ - lib/zombie_scout/app.rb
77
+ - lib/zombie_scout/method_call_finder.rb
78
+ - lib/zombie_scout/method_finder.rb
79
+ - lib/zombie_scout/mission.rb
80
+ - lib/zombie_scout/ruby_project.rb
81
+ - lib/zombie_scout/ruby_source.rb
82
+ - lib/zombie_scout/version.rb
83
+ - spec/spec_helper.rb
84
+ - spec/zombie_scout/method_finder_spec.rb
85
+ - spec/zombie_scout/ruby_project_spec.rb
86
+ - spec/zombie_scout/ruby_source_spec.rb
87
+ - zombie_scout.gemspec
88
+ homepage: https://github.com/danbernier/zombie_scout
89
+ licenses:
90
+ - ASL2
91
+ metadata: {}
92
+ post_install_message:
93
+ rdoc_options: []
94
+ require_paths:
95
+ - lib
96
+ required_ruby_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - '>='
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ required_rubygems_version: !ruby/object:Gem::Requirement
102
+ requirements:
103
+ - - '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ requirements: []
107
+ rubyforge_project:
108
+ rubygems_version: 2.1.10
109
+ signing_key:
110
+ specification_version: 4
111
+ summary: Find dead methods in your Ruby app
112
+ test_files:
113
+ - spec/spec_helper.rb
114
+ - spec/zombie_scout/method_finder_spec.rb
115
+ - spec/zombie_scout/ruby_project_spec.rb
116
+ - spec/zombie_scout/ruby_source_spec.rb