deadweight 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -12,15 +12,12 @@ Ryan Bates has worked his magic once again. Head over here for an excellent intr
12
12
 
13
13
  require 'deadweight'
14
14
 
15
- desc "run Deadweight (script/server needs to be running)"
16
- task :deadweight do
17
- dw = Deadweight.new
15
+ Deadweight::RakeTask.new do |dw|
18
16
  dw.stylesheets = %w( /stylesheets/style.css )
19
17
  dw.pages = %w( / /page/1 /about )
20
- puts dw.run
21
18
  end
22
19
 
23
- This will output all unused rules, one per line.
20
+ Running <tt>rake deadweight</tt> will output all unused rules, one per line. Note that it looks at http://localhost:3000 by default, so you'll need to have <tt>script/server</tt> (or whatever your server command looks like) running.
24
21
 
25
22
  Alternately, you can run it from the command-line:
26
23
 
@@ -36,12 +33,10 @@ And you can use it as an HTTP proxy:
36
33
 
37
34
  === How You Install It
38
35
 
39
- gem sources -a http://gems.github.com
40
- sudo gem install aanand-deadweight
36
+ gem install deadweight -s http://gemcutter.org
41
37
 
42
38
  === Things to Note
43
39
 
44
- - By default, it looks at http://localhost:3000.
45
40
  - It's completely dumb about any classes, IDs or tags that are only added by your Javascript layer, but you can filter them out by setting +ignore_selectors+.
46
41
  - You can optionally tell it to use Mechanize, and set up more complicated targets for scraping by specifying them as Procs.
47
42
  - There is experimental support for Lyndon (http://github.com/defunkt/lyndon) with -L
@@ -52,10 +47,7 @@ And you can use it as an HTTP proxy:
52
47
 
53
48
  require 'deadweight'
54
49
 
55
- desc "run Deadweight on staging server"
56
- task :deadweight do
57
- dw = Deadweight.new
58
-
50
+ Deadweight::RakeTask.new do |dw|
59
51
  dw.mechanize = true
60
52
 
61
53
  dw.root = 'http://staging.example.com'
@@ -74,8 +66,6 @@ And you can use it as an HTTP proxy:
74
66
  }
75
67
 
76
68
  dw.ignore_selectors = /hover|lightbox|superimposed_kittens/
77
-
78
- puts dw.run
79
69
  end
80
70
 
81
71
  == Copyright
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.1.1
data/deadweight.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{deadweight}
8
- s.version = "0.1.0"
8
+ s.version = "0.1.1"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Aanand Prasad"]
12
- s.date = %q{2009-09-25}
12
+ s.date = %q{2009-10-18}
13
13
  s.default_executable = %q{deadweight}
14
14
  s.email = %q{aanand.prasad@gmail.com}
15
15
  s.executables = ["deadweight"]
@@ -28,11 +28,13 @@ Gem::Specification.new do |s|
28
28
  "deadweight.gemspec",
29
29
  "lib/deadweight.rb",
30
30
  "lib/deadweight/cli.rb",
31
+ "lib/deadweight/rake_task.rb",
31
32
  "test/cli_test.rb",
32
33
  "test/deadweight_test.rb",
33
34
  "test/fixtures/index.html",
34
35
  "test/fixtures/index2.html",
35
36
  "test/fixtures/style.css",
37
+ "test/rake_task_test.rb",
36
38
  "test/test_helper.rb"
37
39
  ]
38
40
  s.homepage = %q{http://github.com/aanand/deadweight}
@@ -43,6 +45,7 @@ Gem::Specification.new do |s|
43
45
  s.test_files = [
44
46
  "test/cli_test.rb",
45
47
  "test/deadweight_test.rb",
48
+ "test/rake_task_test.rb",
46
49
  "test/test_helper.rb"
47
50
  ]
48
51
 
data/lib/deadweight.rb CHANGED
@@ -5,7 +5,7 @@ require 'logger'
5
5
 
6
6
  class Deadweight
7
7
  attr_accessor :root, :stylesheets, :rules, :pages, :ignore_selectors, :mechanize, :log_file
8
- attr_reader :unused_selectors
8
+ attr_reader :unused_selectors, :parsed_rules
9
9
 
10
10
  def initialize
11
11
  @root = 'http://localhost:3000'
@@ -15,6 +15,7 @@ class Deadweight
15
15
  @ignore_selectors = []
16
16
  @mechanize = false
17
17
  @log_file = STDERR
18
+ yield self and run if block_given?
18
19
  end
19
20
 
20
21
  def analyze(html)
@@ -42,20 +43,25 @@ class Deadweight
42
43
 
43
44
  css.add_block!(rules)
44
45
 
45
- @unused_selectors = {}
46
- total_selectors = 0
46
+ @parsed_rules = {}
47
+ @unused_selectors = []
48
+ total_selectors = 0
47
49
 
48
50
  css.each_selector do |selector, declarations, specificity|
49
- unless @unused_selectors[selector]
51
+ unless @unused_selectors.include?(selector)
50
52
  total_selectors += 1
51
- @unused_selectors[selector] = declarations unless selector =~ ignore_selectors
53
+
54
+ unless selector =~ ignore_selectors
55
+ @unused_selectors << selector
56
+ @parsed_rules[selector] = declarations
57
+ end
52
58
  end
53
59
  end
54
60
 
55
61
  # Remove selectors with pseudo classes that already have an equivalent
56
62
  # without the pseudo class. Keep the ones that don't, we need to test
57
63
  # them.
58
- @unused_selectors.keys.each do |selector|
64
+ @unused_selectors.each do |selector|
59
65
  if has_pseudo_classes(selector) && @unused_selectors.include?(strip(selector))
60
66
  @unused_selectors.delete(selector)
61
67
  end
@@ -85,6 +91,10 @@ class Deadweight
85
91
  @unused_selectors
86
92
  end
87
93
 
94
+ def dump(output)
95
+ output.puts(@unused_selectors)
96
+ end
97
+
88
98
  def process!(html)
89
99
  analyze(html).each do |selector|
90
100
  @unused_selectors.delete(selector)
@@ -143,3 +153,5 @@ private
143
153
  end
144
154
  end
145
155
 
156
+ require 'deadweight/rake_task'
157
+
@@ -104,10 +104,8 @@ class Deadweight
104
104
  dw.pages = arguments
105
105
  end
106
106
 
107
- unused_rules = dw.run
108
- unused_rules.each do |k,v|
109
- output.puts "#{k} { #{v} }"
110
- end
107
+ dw.run
108
+ dw.dump(output)
111
109
  end
112
110
 
113
111
  def proxy
@@ -0,0 +1,12 @@
1
+ class Deadweight
2
+ class RakeTask
3
+ def initialize output=STDOUT, &block
4
+ desc "run deadweight"
5
+ task :deadweight do
6
+ dw = Deadweight.new(&block)
7
+ dw.dump(output)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
data/test/cli_test.rb CHANGED
@@ -4,17 +4,15 @@ class CliTest < Test::Unit::TestCase
4
4
  COMMAND = "ruby -rubygems -Ilib bin/deadweight -s test/fixtures/style.css test/fixtures/index.html 2>/dev/null"
5
5
 
6
6
  should "output unused selectors on STDOUT" do
7
- @result = `#{COMMAND}`.split("\n")
7
+ @result = `#{COMMAND}`
8
8
 
9
- assert_equal 1, @result.grep(/^#foo \.bar \.baz \{/).length
10
- assert_equal 0, @result.grep(/^#foo \{/).length
11
- assert_equal 0, @result.grep(/^#foo .bar \{/).length
9
+ assert_correct_selectors_in_output(@result)
12
10
  end
13
11
 
14
12
  should "accept CSS rules on STDIN" do
15
- @result = `echo ".something { display: block; }" | #{COMMAND}`.split("\n")
13
+ @result = `echo ".something { display: block; }" | #{COMMAND}`
16
14
 
17
- assert_equal 1, @result.grep(/^\.something \{/).length
15
+ assert @result.include?('.something')
18
16
  end
19
17
  end
20
18
 
@@ -3,23 +3,31 @@ require 'test_helper'
3
3
  class DeadweightTest < Test::Unit::TestCase
4
4
  def setup
5
5
  @dw = Deadweight.new
6
- @dw.log_file = 'test.log'
7
- @dw.root = File.dirname(__FILE__) + '/fixtures'
8
- @dw.stylesheets << '/style.css'
9
- @dw.pages << '/index.html'
10
-
6
+ default_settings(@dw)
11
7
  @result = @dw.run
12
8
  end
13
9
 
14
- should "report unused selectors" do
15
- assert @result.include?('#foo .bar .baz')
16
- end
10
+ context "when initialized with a block" do
11
+ setup do
12
+ @dwb = Deadweight.new do |dw|
13
+ default_settings(dw)
14
+ end
15
+
16
+ @result = @dwb.run
17
+ end
18
+
19
+ should "have the same attributes" do
20
+ assert_equal(@dw.log_file, @dwb.log_file)
21
+ assert_equal(@dw.root, @dwb.root)
22
+ assert_equal(@dw.stylesheets, @dwb.stylesheets)
23
+ assert_equal(@dw.pages, @dwb.pages)
24
+ end
17
25
 
18
- should "not report used selectors" do
19
- assert !@result.include?('#foo')
20
- assert !@result.include?('#foo .bar')
26
+ should_correctly_report_selectors
21
27
  end
22
28
 
29
+ should_correctly_report_selectors
30
+
23
31
  should 'strip pseudo classes from selectors' do
24
32
  # #oof:hover (#oof does not exist)
25
33
  assert @result.include?('#oof:hover'), @result.inspect
@@ -57,4 +65,8 @@ class DeadweightTest < Test::Unit::TestCase
57
65
  should 'provide the results of its last run with #unused_selectors' do
58
66
  assert_equal @result, @dw.unused_selectors
59
67
  end
68
+
69
+ should 'provide the parsed CSS rules with #parsed_rules' do
70
+ assert_equal 'color: green;', @dw.parsed_rules['#foo']
71
+ end
60
72
  end
@@ -0,0 +1,26 @@
1
+ require 'test_helper'
2
+ require 'rake'
3
+
4
+ class RakeTaskTest < Test::Unit::TestCase
5
+ context "Deadweight::RakeTask.new" do
6
+ setup do
7
+ @io = StringIO.new("", "w")
8
+
9
+ Deadweight::RakeTask.new(@io) do |d|
10
+ default_settings(d)
11
+ end
12
+
13
+ @task_names = Rake::Task.tasks.map { |t| t.name }
14
+ @task = Rake::Task.tasks.find { |t| t.name == 'deadweight' }
15
+ end
16
+
17
+ should "define a `deadweight` task that automatically runs" do
18
+ assert @task, "no deadweight task found in: #{@task_names.inspect}"
19
+
20
+ @task.execute
21
+ @io.close
22
+
23
+ assert_correct_selectors_in_output(@io.string)
24
+ end
25
+ end
26
+ end
data/test/test_helper.rb CHANGED
@@ -7,4 +7,41 @@ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
7
  require 'deadweight'
8
8
 
9
9
  class Test::Unit::TestCase
10
+ UNUSED_SELECTORS = ['#foo .bar .baz']
11
+ USED_SELECTORS = ['#foo', '#foo .bar']
12
+
13
+ def self.should_correctly_report_selectors
14
+ should "report unused selectors" do
15
+ assert_reports_unused_selectors(@result)
16
+ end
17
+
18
+ should "not report used selectors" do
19
+ assert_does_not_report_used_selectors(@result)
20
+ end
21
+ end
22
+
23
+ def assert_correct_selectors_in_output(output)
24
+ selectors = output.split("\n")
25
+ assert_reports_unused_selectors(selectors)
26
+ assert_does_not_report_used_selectors(selectors)
27
+ end
28
+
29
+ def assert_reports_unused_selectors(output)
30
+ UNUSED_SELECTORS.each do |s|
31
+ assert output.include?(s)
32
+ end
33
+ end
34
+
35
+ def assert_does_not_report_used_selectors(output)
36
+ USED_SELECTORS.each do |s|
37
+ assert !output.include?(s)
38
+ end
39
+ end
40
+
41
+ def default_settings(dw)
42
+ dw.log_file = 'test.log'
43
+ dw.root = File.dirname(__FILE__) + '/fixtures'
44
+ dw.stylesheets << '/style.css'
45
+ dw.pages << '/index.html'
46
+ end
10
47
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: deadweight
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Aanand Prasad
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-25 00:00:00 -04:00
12
+ date: 2009-10-18 00:00:00 -04:00
13
13
  default_executable: deadweight
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -52,11 +52,13 @@ files:
52
52
  - deadweight.gemspec
53
53
  - lib/deadweight.rb
54
54
  - lib/deadweight/cli.rb
55
+ - lib/deadweight/rake_task.rb
55
56
  - test/cli_test.rb
56
57
  - test/deadweight_test.rb
57
58
  - test/fixtures/index.html
58
59
  - test/fixtures/index2.html
59
60
  - test/fixtures/style.css
61
+ - test/rake_task_test.rb
60
62
  - test/test_helper.rb
61
63
  has_rdoc: true
62
64
  homepage: http://github.com/aanand/deadweight
@@ -89,4 +91,5 @@ summary: A coverage tool for finding unused CSS
89
91
  test_files:
90
92
  - test/cli_test.rb
91
93
  - test/deadweight_test.rb
94
+ - test/rake_task_test.rb
92
95
  - test/test_helper.rb