janitor 0.1.2 → 0.2.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.
@@ -31,3 +31,8 @@ Configure your app to load the Janitor rake tasks by adding the following to you
31
31
  # Code cleanup goodies
32
32
  require 'janitor/tasks'
33
33
 
34
+ To Do
35
+ =====
36
+
37
+ * YML-ize the various search tasks to allow a project to specify the things that shouldn't be in the codebase.
38
+ * Create a profanity filter
data/Rakefile CHANGED
@@ -7,8 +7,8 @@ begin
7
7
  require 'jeweler'
8
8
  Jeweler::Tasks.new do |gem|
9
9
  gem.name = "janitor"
10
- gem.summary = "janitor scans your code for leftover debug statements, consolr logs, and vulgarity"
11
- gem.description = "ever leave a breakpoint in your code and do a deploy? not good, right? maybe you just want to remove the logger.info calls from your Rails project. janitor cleans your code"
10
+ gem.summary = "janitor scans your code for leftover debug statements, console.logs, and vulgarity"
11
+ gem.description = "ever leave a breakpoint in your code and do a deploy? not good, right? maybe you just want to remove the logger.info calls from your Rails project. janitor cleans your code."
12
12
  gem.email = "mike@mikekrisher.com"
13
13
  gem.homepage = "http://github.com/tjh/janitor"
14
14
  gem.authors = ["Tim Harvey", "Michael Krisher"]
@@ -17,7 +17,7 @@ begin
17
17
  end
18
18
  Jeweler::GemcutterTasks.new
19
19
  rescue LoadError
20
- puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
20
+ raise "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
21
21
  end
22
22
 
23
23
 
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.2.0
@@ -5,12 +5,12 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{janitor}
8
- s.version = "0.1.2"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Tim Harvey", "Michael Krisher"]
12
12
  s.date = %q{2010-09-04}
13
- s.description = %q{ever leave a breakpoint in your code and do a deploy? not good, right? maybe you just want to remove the logger.info calls from your Rails project. janitor cleans your code}
13
+ s.description = %q{ever leave a breakpoint in your code and do a deploy? not good, right? maybe you just want to remove the logger.info calls from your Rails project. janitor cleans your code.}
14
14
  s.email = %q{mike@mikekrisher.com}
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE",
@@ -25,10 +25,10 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "init.rb",
27
27
  "janitor.gemspec",
28
- "lib/finder.rb",
29
28
  "lib/janitor.rb",
30
29
  "lib/janitor/finder.rb",
31
30
  "lib/janitor/janitor.rb",
31
+ "lib/janitor/rak_hit.rb",
32
32
  "lib/janitor/rak_result.rb",
33
33
  "lib/janitor/tasks.rb",
34
34
  "lib/tasks/tasks.rake",
@@ -43,7 +43,7 @@ Gem::Specification.new do |s|
43
43
  s.rdoc_options = ["--charset=UTF-8"]
44
44
  s.require_paths = ["lib"]
45
45
  s.rubygems_version = %q{1.3.7}
46
- s.summary = %q{janitor scans your code for leftover debug statements, consolr logs, and vulgarity}
46
+ s.summary = %q{janitor scans your code for leftover debug statements, console.logs, and vulgarity}
47
47
  s.test_files = [
48
48
  "spec/lib/finder_spec.rb",
49
49
  "spec/lib/janitor_spec.rb",
@@ -1,4 +1,4 @@
1
1
  require 'janitor/janitor'
2
2
  require 'janitor/finder'
3
- require 'janitor/tasks'
4
- require 'janitor/rak_result'
3
+ require 'janitor/rak_result'
4
+ require 'janitor/rak_hit'
@@ -7,11 +7,16 @@ class Finder
7
7
 
8
8
  def number_of_matches(pattern)
9
9
  rak_result = search_by_regex(pattern)
10
- rak_result.files.size
10
+ rak_result.hits.size
11
+ end
12
+
13
+ def hits(pattern)
14
+ rak_result = search_by_regex(pattern)
15
+ rak_result.hits.collect { |hit| "#{hit.file_name}:#{hit.line_number}" }
11
16
  end
12
17
 
13
18
  def search_by_regex(pattern)
14
- exec_rak(pattern, '-all')
19
+ exec_rak(pattern, '--all')
15
20
  end
16
21
 
17
22
  def exec_rak(pattern, options)
@@ -1,7 +1,11 @@
1
1
  class Janitor
2
2
  def self.count (pattern, path = "*")
3
3
  finder = Finder.new(path)
4
- count = finder.number_of_matches(pattern)
5
- raise "Found #{pattern} #{count} times in #{path}" if count > 0
4
+ finder.number_of_matches(pattern)
5
+ end
6
+
7
+ def self.hits (pattern, path = "*")
8
+ finder = Finder.new(path)
9
+ finder.hits(pattern)
6
10
  end
7
11
  end
@@ -0,0 +1,17 @@
1
+ # -----------------------------------------
2
+ # This represents a single search hit from
3
+ # the Rak gem. The raw output looks like:
4
+ #
5
+ # lib/file_name.rb 10|require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
6
+ #
7
+ # This is a file name, then line number, followed by
8
+ # a pipe, then finally the context within that file
9
+ # -----------------------------------------
10
+
11
+ class RakHit
12
+ attr_accessor :file_name, :line_number, :context
13
+
14
+ def initialize(raw_hit)
15
+ self.file_name, self.line_number, self.context = raw_hit.scan(/(.+?)\s*(\d+)\|\s*(.+)/).first
16
+ end
17
+ end
@@ -2,11 +2,14 @@ class RakResult
2
2
  attr_accessor :rak_output
3
3
 
4
4
  def initialize(rak_output)
5
- self.rak_output = rak_output
5
+ self.rak_output = rak_output.strip
6
6
  end
7
7
 
8
- def files
9
- rows = rak_output.split("\n")
10
- rows.select { |r| r.scan(/^[^\s]+/).size > 0 }
8
+ def file_names
9
+ hits.collect { |hit| hit.file_name }.uniq
11
10
  end
12
- end
11
+
12
+ def hits
13
+ rak_output.split("\n").collect { |row| RakHit.new(row) }
14
+ end
15
+ end
@@ -7,14 +7,50 @@ namespace :janitor do
7
7
 
8
8
  desc "Find any 'console.log' statements"
9
9
  task :console_log do
10
- count = Janitor.count "console.log", "tmp/javascript"
11
- raise "Found console.log statements" if count > 0
10
+ puts "Searching the current folder for 'console.log'"
11
+ count = Janitor.count "console.log"
12
+ if count > 0
13
+ puts "FAILED: Found #{count} console.log statements"
14
+ else
15
+ puts "Success...didn't find 'console.log'"
16
+ end
12
17
  end
13
18
 
14
19
  desc "Count 'debugger' statements"
15
20
  task :debugger do
21
+ puts "Searching the current folder for 'debugger'"
16
22
  count = Janitor.count "debugger"
17
- raise "Found debugger statements" if count > 0
23
+ if count > 0
24
+ puts "FAILED: Found #{count} debugger statements"
25
+ else
26
+ puts "Success...didn't find 'debugger'"
27
+ end
28
+ end
29
+ end
30
+
31
+ namespace :hits do
32
+ desc "Run all count tasks"
33
+ task :all => [:debugger, :console_log]
34
+
35
+ desc "Find any 'console.log' statements"
36
+ task :console_log do
37
+ hits = Janitor.hits "console.log"
38
+ if hits.count > 0
39
+ puts hits
40
+ puts "FAILED: Found #{hits.count} console.log statements"
41
+ end
42
+ end
43
+
44
+ desc "Count 'debugger' statements"
45
+ task :debugger do
46
+ puts "Searching the current folder for 'debugger'"
47
+ hits = Janitor.hits "debugger"
48
+ if hits.count > 0
49
+ puts hits
50
+ puts "FAILED: Found #{hits.count} debugger statements"
51
+ else
52
+ puts "Success...didn't find 'debugger'"
53
+ end
18
54
  end
19
55
  end
20
56
  end
@@ -8,11 +8,34 @@ describe "Finder" do
8
8
  end
9
9
  end
10
10
 
11
+ describe " #number_of_matches" do
12
+ it "should do a search_by_regex, returning the number of hits the RakResult gives" do
13
+ pattern = 'a_regex'
14
+ finder = Finder.new
15
+ rak_result = mock('rak_result', :hits => ['janitor.rb:3'])
16
+ finder.should_receive(:search_by_regex).with(pattern).and_return(rak_result)
17
+ finder.number_of_matches(pattern).should == 1
18
+ end
19
+ end
20
+
21
+ describe " #hits" do
22
+ it "should return the array of hits from the RakResult returned by exec_rak" do
23
+ pattern = 'a_regex'
24
+ hit = mock('rak_hit',
25
+ :file_name => "filename.rb",
26
+ :line_number => 10)
27
+ rak_result = mock('rak_result', :hits => [hit])
28
+ finder = Finder.new
29
+ finder.should_receive(:search_by_regex).and_return(rak_result)
30
+ finder.hits(pattern).should == ["filename.rb:10"]
31
+ end
32
+ end
33
+
11
34
  describe " #search_by_regex" do
12
35
  it "should execute a rak search with the given pattern and -all" do
13
36
  pattern = 'a_regex'
14
37
  finder = Finder.new
15
- finder.should_receive(:exec_rak).with(pattern, '-all')
38
+ finder.should_receive(:exec_rak).with(pattern, '--all')
16
39
  finder.search_by_regex(pattern)
17
40
  end
18
41
  end
@@ -5,19 +5,41 @@ describe "Janitor" do
5
5
  it "should call number_of_matches on Finder with the given path" do
6
6
  path = "a_path"
7
7
  pattern = "a_pattern"
8
- finder = mock('finder')
9
- finder.should_receive(:number_of_matches).with(pattern).and_return(0)
10
- Finder.should_receive(:new).with(path).and_return(finder)
11
- Janitor.count(path, pattern)
8
+ mock_finder(pattern, path)
9
+ Janitor.count(pattern, path)
12
10
  end
13
11
 
14
- it "should raise an exception when number_of_matches returns a non-zero count" do
12
+ it "should return the value returned by the call to number_of_matches" do
15
13
  path = "a_path"
16
14
  pattern = "a_pattern"
17
- finder = mock('finder')
18
- finder.should_receive(:number_of_matches).with(pattern).and_return(1)
19
- Finder.should_receive(:new).with(path).and_return(finder)
20
- lambda { Janitor.count(path, pattern) }.should raise_error
15
+ mock_finder(pattern, path, :number_of_matches => 1)
16
+ Janitor.count(pattern, path).should == 1
21
17
  end
22
18
  end
19
+
20
+ describe " #hits" do
21
+ it "should call #hits on Finder and return the result" do
22
+ path = "a_path"
23
+ pattern = "a_pattern"
24
+ hits = ['result']
25
+ mock_finder(pattern, path, :hits => hits)
26
+ Janitor.hits(pattern, path).should == hits
27
+ end
28
+ end
29
+
30
+ describe " integration testing" do
31
+ it " should find debugger once in 'tmp/'" do
32
+ Janitor.count('console\.log', 'tmp/').should == 3
33
+ end
34
+ end
35
+
36
+ def mock_finder(pattern, path, *args)
37
+ options = args.first ? args.first : {}
38
+ options = {:number_of_matches => 0}.merge options
39
+ options = {:hits => 0}.merge options
40
+ finder = mock('finder',
41
+ :number_of_matches => options[:number_of_matches],
42
+ :hits => options[:hits])
43
+ Finder.should_receive(:new).and_return(finder)
44
+ end
23
45
  end
@@ -3,26 +3,37 @@ require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
3
3
  describe "RakResult" do
4
4
  before(:all) do
5
5
  @result = "
6
- lib/janitor.rb
7
- 1|require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
8
- 9|end
9
-
10
- lib/finder.rb
11
- 7|Dir.glob(File.join(File.dirname(__FILE__), '../lib/*.rb')).each {|f| require f }
12
- 9|Spec::Runner.configure do |config|
13
- 11|end
6
+ lib/janitor.rb 1|require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
7
+ lib/janitor.rb 9|end
8
+ lib/finder.rb 7|Dir.glob(File.join(File.dirname(__FILE__), '../lib/*.rb')).each {|f| require f }
9
+ lib/finder.rb 9|Spec::Runner.configure do |config|
10
+ lib/finder.rb 11|end
14
11
  "
15
12
  end
16
13
 
14
+ before(:each) do
15
+ @rak_result = RakResult.new(@result)
16
+ end
17
+
17
18
  describe " #initialize" do
18
19
  it "should store the given rak output string" do
19
20
  RakResult.new('a_result').rak_output.should == 'a_result'
20
21
  end
22
+
23
+ it "should strip leading and trailing whitespace" do
24
+ @rak_result.rak_output.should == @result.strip
25
+ end
21
26
  end
22
27
 
23
- describe " #file_matches" do
28
+ describe " #file_names" do
24
29
  it "should return an array of files that were matched" do
25
- RakResult.new(@result).files.should == ['lib/janitor.rb', 'lib/finder.rb']
30
+ @rak_result.file_names.should == ['lib/janitor.rb', 'lib/finder.rb']
31
+ end
32
+ end
33
+
34
+ describe " #hits" do
35
+ it "should return an array of RakHits objects" do
36
+ @rak_result.hits.first.should be_instance_of(RakHit)
26
37
  end
27
38
  end
28
39
  end
@@ -1 +1,4 @@
1
- --color
1
+ --colour
2
+ --format specdoc
3
+ --loadby mtime
4
+ --reverse
@@ -3,8 +3,7 @@ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
3
  require 'spec'
4
4
  require 'spec/autorun'
5
5
 
6
- # Require each of our libraries
7
- Dir.glob(File.join(File.dirname(__FILE__), '../lib/**/*.rb')).each {|f| require f }
6
+ require 'janitor'
8
7
 
9
8
  Spec::Runner.configure do |config|
10
9
 
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: janitor
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
8
  - 2
10
- version: 0.1.2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Tim Harvey
@@ -33,7 +33,7 @@ dependencies:
33
33
  version: "0"
34
34
  type: :runtime
35
35
  version_requirements: *id001
36
- description: ever leave a breakpoint in your code and do a deploy? not good, right? maybe you just want to remove the logger.info calls from your Rails project. janitor cleans your code
36
+ description: ever leave a breakpoint in your code and do a deploy? not good, right? maybe you just want to remove the logger.info calls from your Rails project. janitor cleans your code.
37
37
  email: mike@mikekrisher.com
38
38
  executables: []
39
39
 
@@ -51,10 +51,10 @@ files:
51
51
  - VERSION
52
52
  - init.rb
53
53
  - janitor.gemspec
54
- - lib/finder.rb
55
54
  - lib/janitor.rb
56
55
  - lib/janitor/finder.rb
57
56
  - lib/janitor/janitor.rb
57
+ - lib/janitor/rak_hit.rb
58
58
  - lib/janitor/rak_result.rb
59
59
  - lib/janitor/tasks.rb
60
60
  - lib/tasks/tasks.rake
@@ -97,7 +97,7 @@ rubyforge_project:
97
97
  rubygems_version: 1.3.7
98
98
  signing_key:
99
99
  specification_version: 3
100
- summary: janitor scans your code for leftover debug statements, consolr logs, and vulgarity
100
+ summary: janitor scans your code for leftover debug statements, console.logs, and vulgarity
101
101
  test_files:
102
102
  - spec/lib/finder_spec.rb
103
103
  - spec/lib/janitor_spec.rb
@@ -1,20 +0,0 @@
1
- class Finder
2
- attr_reader :path
3
-
4
- def initialize(path = File.dirname(__FILE__))
5
- @path = path
6
- end
7
-
8
- def number_of_matches(pattern)
9
- rak_result = search_by_regex(pattern)
10
- rak_result.files.size
11
- end
12
-
13
- def search_by_regex(pattern)
14
- exec_rak(pattern, '-all')
15
- end
16
-
17
- def exec_rak(pattern, options)
18
- RakResult.new(%x{rak #{options} #{pattern} #{@path}})
19
- end
20
- end