ymdp 0.1.11 → 0.1.12

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.
@@ -1,122 +0,0 @@
1
- require 'ymdp/base'
2
- require 'ymdp/support/file'
3
- require 'w3c_validators'
4
-
5
-
6
- module YMDP
7
- module Validator
8
- class Base < YMDP::Base
9
- end
10
-
11
- class HTML < Base
12
- class << self
13
- def validator
14
- @validator ||= W3CValidators::MarkupValidator.new
15
- end
16
-
17
- def validate(path)
18
- $stdout.print " #{path} validating . . . "
19
- doctype = configuration.validate["html"]["doctype"]
20
- validator.set_doctype!(doctype)
21
-
22
- results = validator.validate_file(path)
23
-
24
- valid = results.errors.length <= 0
25
-
26
- if valid
27
- $stdout.puts "OK"
28
- else
29
- $stdout.puts "validation errors"
30
- results.errors.each do |err|
31
- $stdout.puts
32
- $stdout.puts err.to_s
33
- end
34
- end
35
-
36
- valid
37
- end
38
- end
39
- end
40
-
41
- class JavaScript < Base
42
- def self.validate(filename)
43
- validate_javascript(filename)
44
- end
45
-
46
- def self.use_jslint_settings?
47
- !jslint_settings.blank?
48
- end
49
-
50
- def self.jslint_settings
51
- configuration.jslint_settings
52
- end
53
-
54
- def self.jslint_settings_count
55
- jslint_settings.to_s.split("\n").size
56
- end
57
-
58
- def self.pre_process(content)
59
- content
60
- end
61
-
62
- def self.validate_javascript(path)
63
- display = display_path(path)
64
- $stdout.print " #{display} validating . . . "
65
- output = ""
66
-
67
- File.open(path) do |f|
68
- output = f.read
69
- end
70
-
71
- output = pre_process(output)
72
-
73
- js_fragment_path = File.expand_path("#{TMP_PATH}/#{File.basename(path)}_fragment")
74
- fragment_display_path = display_path(js_fragment_path)
75
-
76
- unless File.exists?(js_fragment_path)
77
- File.open(js_fragment_path,'w') do |f|
78
- f.puts jslint_settings if use_jslint_settings?
79
- f.puts output
80
- end
81
-
82
- jslint_path = File.expand_path("#{File.dirname(__FILE__)}/jslint.js")
83
- raise "#{jslint_path} does not exist" unless File.exists?(jslint_path)
84
- results = F.execute("java org.mozilla.javascript.tools.shell.Main #{jslint_path} #{js_fragment_path}", :return => true)
85
-
86
- if results =~ /jslint: No problems found/
87
- $stdout.puts "OK"
88
- else
89
- $stdout.puts "errors found!"
90
- results.split("\n").each do |result|
91
- if result =~ /line (\d+) character (\d+): (.*)/
92
- line_number = $1.to_i
93
- error = "Error at #{fragment_display_path} line #{line_number-jslint_settings_count} character #{$2}: #{$3}"
94
- error += F.get_line_from_file(js_fragment_path, line_number)
95
-
96
- $stdout.puts error
97
- end
98
- end
99
- message = "JavaScript Errors embedded in #{display}"
100
- g(message)
101
- raise message
102
- end
103
- end
104
- end
105
- end
106
-
107
- class JSON < JavaScript
108
- def self.pre_process(output)
109
- output
110
- end
111
-
112
- def self.jslint_settings
113
- end
114
- end
115
-
116
- class Stylesheet < Base
117
- def self.validate(filename)
118
- true
119
- end
120
- end
121
- end
122
- end
@@ -1,127 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- describe "Compressor" do
4
- before(:each) do
5
- stub_io
6
- File.stub!(:exists?).with(/yuicompressor/).and_return(true)
7
- end
8
-
9
- describe "Base" do
10
- it "should use the compressed file if it already exists" do
11
- File.stub!(:exists?).with(/file.js/).and_return(true)
12
- @file = "compressed file"
13
- File.stub(:read).and_return(@file)
14
- YMDP::Compressor::Base.compress("file.js", "type" => "js").should == @file
15
- end
16
-
17
- describe "generate a compressed file if one doesn't exist" do
18
- before(:each) do
19
- File.stub!(:exists?).with(/file.js/).and_return(false, true)
20
- File.stub!(:exists?).with(/file.css/).and_return(false, true)
21
- end
22
-
23
- it "should log what it's doing" do
24
- $stdout.should_receive(:print).with(/file.js compressing . . ./)
25
- YMDP::Compressor::Base.compress("file.js", "type" => "js")
26
- end
27
-
28
- it "should run the compressor" do
29
- F.should_receive(:execute).with(/yuicompressor/, :return => true).and_return("")
30
- YMDP::Compressor::Base.compress("file.js", "type" => "js")
31
- end
32
-
33
- describe "options" do
34
- it "should set nomunge" do
35
- F.should_receive(:execute).with(/nomunge/, :return => true).and_return("")
36
- YMDP::Compressor::Base.compress("file.js", "type" => "js", "obfuscate" => true)
37
- end
38
-
39
- it "should not set nomunge" do
40
- F.stub!(:execute).with(/yuicompressor/, :return => true).and_return("")
41
- F.should_not_receive(:execute).with(/nomunge/, :return => true).and_return("")
42
- YMDP::Compressor::Base.compress("file.js", "type" => "js", "obfuscate" => false)
43
- end
44
-
45
- it "should set verbose" do
46
- F.should_receive(:execute).with(/verbose/, :return => true).and_return("")
47
- YMDP::Compressor::Base.compress("file.js", "type" => "js", "verbose" => true)
48
- end
49
-
50
- it "should not set verbose" do
51
- F.stub!(:execute).with(/yuicompressor/, :return => true).and_return("")
52
- F.should_not_receive(:execute).with(/verbose/, :return => true).and_return("")
53
- YMDP::Compressor::Base.compress("file.js", "type" => "js", "verbose" => false)
54
- end
55
-
56
- it "should set preserve-semi on javascript" do
57
- F.should_receive(:execute).with(/preserve-semi/, :return => true).and_return("")
58
- YMDP::Compressor::Base.compress("file.js", "type" => "js")
59
- end
60
-
61
- it "should not set preserve-semi on css" do
62
- File.stub!(:exists?).with(/file.css/).and_return(true)
63
- F.stub!(:execute).with(/yuicompressor/, :return => true).and_return("")
64
- F.should_not_receive(:execute).with(/preserve-semi/, :return => true).and_return("")
65
- YMDP::Compressor::Base.compress("file.css", "type" => "css")
66
- end
67
- end
68
-
69
- describe "on errors" do
70
- before(:each) do
71
- F.stub!(:execute).with(/yuicompressor/, :return => true).and_return("[ERROR] 12:13: Too much fruzzlegump")
72
- end
73
-
74
- it "should raise an exception" do
75
- lambda {
76
- YMDP::Compressor::Base.compress("file.js", "type" => "js")
77
- }.should raise_error(/JavaScript errors/)
78
- end
79
-
80
- it "should growl" do
81
- @g.should_receive(:notify)
82
- lambda {
83
- YMDP::Compressor::Base.compress("file.js", "type" => "js")
84
- }.should raise_error(/JavaScript errors/)
85
- end
86
-
87
- it "should show the source code" do
88
- F.should_receive(:get_line_from_file).with("file.js", 12).and_return("")
89
- lambda {
90
- YMDP::Compressor::Base.compress("file.js", "type" => "js")
91
- }.should raise_error(/JavaScript errors/)
92
- end
93
- end
94
-
95
- it "should report OK" do
96
- $stdout.should_receive(:puts).with("OK")
97
- YMDP::Compressor::Base.compress("file.js", "type" => "js")
98
- end
99
-
100
- it "should raise an error if the compressed file doesn't exist" do
101
- # File.stub!(:exists?).with(/file.js/).and_return(true)
102
- # File.stub!(:exists?).with(/file.js.min/).and_return(false)
103
- # lambda {
104
- YMDP::Compressor::Base.compress("file.js", "type" => "js")
105
- # }.should raise_error(/File does not exist/)
106
- end
107
- end
108
- end
109
-
110
- describe "JavaScript" do
111
- it "should call Base with type js" do
112
- File.stub(:exists?).with(/file.js/).and_return(true)
113
- @file = "compressed file"
114
- File.stub(:read).and_return(@file)
115
- YMDP::Compressor::JavaScript.compress("file.js").should == @file
116
- end
117
- end
118
-
119
- describe "Stylesheet" do
120
- it "should call Base with type css" do
121
- File.stub(:exists?).with(/file.css/).and_return(true)
122
- @file = "compressed file"
123
- File.stub(:read).and_return(@file)
124
- YMDP::Compressor::Stylesheet.compress("file.css").should == @file
125
- end
126
- end
127
- end
@@ -1,195 +0,0 @@
1
- require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
-
3
- require 'validator/validator'
4
-
5
- describe "Validator" do
6
- before(:each) do
7
- stub_io
8
- stub_config
9
- stub_ymdp_configuration
10
- File.stub!(:exists?).with(/jslint.js/).and_return(true)
11
- end
12
-
13
- describe "HTML" do
14
- before(:each) do
15
- class ResultsMock
16
- def errors
17
- []
18
- end
19
- end
20
-
21
- class ValidatorMock
22
- def validate_file(path)
23
- ResultsMock.new
24
- end
25
-
26
- def set_doctype!(doctype)
27
-
28
- end
29
- end
30
-
31
- @validator = ValidatorMock.new
32
- W3CValidators::MarkupValidator.stub!(:new).and_return(@validator)
33
- end
34
-
35
- it "should call validator" do
36
- W3CValidators::MarkupValidator.should_receive(:new).and_return(@validator)
37
- YMDP::Validator::HTML.validate("path")
38
- end
39
-
40
- it "should print OK" do
41
- $stdout.should_receive(:puts).with(/OK/)
42
- YMDP::Validator::HTML.validate("path")
43
- end
44
-
45
- describe "errors" do
46
- before(:each) do
47
- class ResultsMock
48
- def errors
49
- ["HTML Error 1", "HTML Error 2"]
50
- end
51
- end
52
-
53
- class ValidatorMock
54
- def validate_file(path)
55
- ResultsMock.new
56
- end
57
-
58
- def set_doctype!(doctype)
59
-
60
- end
61
- end
62
-
63
- @validator = ValidatorMock.new
64
- W3CValidators::MarkupValidator.stub!(:new).and_return(@validator)
65
- end
66
-
67
- it "should be false" do
68
- YMDP::Validator::HTML.validate("path").should be_false
69
- end
70
-
71
- it "should output a message" do
72
- $stdout.should_receive(:puts).with("validation errors")
73
- YMDP::Validator::HTML.validate("path")
74
- end
75
- end
76
- end
77
-
78
- describe "JavaScript" do
79
- describe "jslint" do
80
- before(:each) do
81
- File.stub!(:exists?).with(/jslint.js/).and_return(true)
82
- @jslint_settings = "/* These are JSLint settings %/"
83
- F.stub!(:execute).with(/java/, :return => true).and_return("jslint: No problems found")
84
- end
85
-
86
- it "should set jslint settings" do
87
- YMDP::Validator::JavaScript.configure do |config|
88
- config.jslint_settings = @jslint_settings
89
- end
90
- YMDP::Validator::JavaScript.jslint_settings.should == @jslint_settings
91
- end
92
-
93
- it "should put jslint settings in the file" do
94
- @file.should_receive(:puts).with(@jslint_settings)
95
- YMDP::Validator::JavaScript.validate("path")
96
- end
97
- end
98
-
99
- describe "valid" do
100
- before(:each) do
101
- F.stub!(:execute).with(/java/, :return => true).and_return("jslint: No problems found")
102
- end
103
-
104
- it "should output 'OK'" do
105
- $stdout.should_receive(:puts).with(/OK/)
106
- YMDP::Validator::JavaScript.validate("path")
107
- end
108
- end
109
-
110
- describe "invalid" do
111
- before(:each) do
112
- @lines_with_errors = [
113
- "line 3 character 2: Unnecessary semicolon",
114
- "line 8 character 3: Unknown thingamajig"
115
- ].join("\n")
116
- F.stub!(:execute).with(/java/, :return => true).and_return(@lines_with_errors)
117
- F.stub!(:get_line_from_file).with(anything, 1)
118
- F.stub!(:get_line_from_file).with(anything, 5)
119
- end
120
-
121
- it "should growl" do
122
- @g.should_receive(:notify).with(anything, anything, /JavaScript Errors/, anything, anything)
123
- lambda {
124
- YMDP::Validator::JavaScript.validate("path")
125
- }.should raise_error
126
- end
127
-
128
- it "should output errors" do
129
- $stdout.should_receive(:puts).with(/Unnecessary semicolon/)
130
- $stdout.should_receive(:puts).with(/Unknown thingamajig/)
131
- lambda {
132
- YMDP::Validator::JavaScript.validate("path")
133
- }.should raise_error
134
- end
135
-
136
- it "should raise error" do
137
- lambda {
138
- YMDP::Validator::JavaScript.validate("path")
139
- }.should raise_error(/JavaScript Errors/)
140
- end
141
- end
142
- end
143
-
144
- describe "JSON" do
145
- describe "valid" do
146
- before(:each) do
147
- F.stub!(:execute).with(/java/, :return => true).and_return("jslint: No problems found")
148
- end
149
-
150
- it "should output 'OK'" do
151
- $stdout.should_receive(:puts).with(/OK/)
152
- YMDP::Validator::JSON.validate("path")
153
- end
154
- end
155
-
156
- describe "invalid" do
157
- before(:each) do
158
- @lines_with_errors = [
159
- "line 3 character 2: Unnecessary semicolon",
160
- "line 8 character 3: Unknown thingamajig"
161
- ].join("\n")
162
- F.stub!(:execute).with(/java/, :return => true).and_return(@lines_with_errors)
163
- F.stub!(:get_line_from_file).with(anything, 1)
164
- F.stub!(:get_line_from_file).with(anything, 5)
165
- end
166
-
167
- it "should growl" do
168
- @g.should_receive(:notify).with(anything, anything, /JavaScript Errors/, anything, anything)
169
- lambda {
170
- YMDP::Validator::JSON.validate("path")
171
- }.should raise_error
172
- end
173
-
174
- it "should output errors" do
175
- $stdout.should_receive(:puts).with(/Unnecessary semicolon/)
176
- $stdout.should_receive(:puts).with(/Unknown thingamajig/)
177
- lambda {
178
- YMDP::Validator::JSON.validate("path")
179
- }.should raise_error
180
- end
181
-
182
- it "should raise error" do
183
- lambda {
184
- YMDP::Validator::JSON.validate("path")
185
- }.should raise_error(/JavaScript Errors/)
186
- end
187
- end
188
-
189
- describe "Stylesheet" do
190
- it "should validate" do
191
- YMDP::Validator::Stylesheet.validate("path")
192
- end
193
- end
194
- end
195
- end