cup 0.0.1 → 0.0.3

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.
@@ -0,0 +1,164 @@
1
+ require 'spec_helper'
2
+
3
+ describe Cup::Builder do
4
+
5
+ let(:directory) {Cup::Directory.current}
6
+ let(:builder) {Cup::Builder.new(directory)}
7
+ let(:options){Hash.new}
8
+
9
+ describe '#build' do
10
+
11
+ def build
12
+ builder.build options
13
+ end
14
+
15
+ share_examples_for :building do
16
+ it 'should call the before_build hook before building' do
17
+ directory.cupfile.before_build.should_receive(:call)
18
+ build
19
+ end
20
+
21
+ it 'should call the after_build after the build' do
22
+ directory.cupfile.after_build.should_receive(:call)
23
+ build
24
+ end
25
+
26
+ it 'should call the concatentate_variant' do
27
+ builder.should_receive(concatentate_variant)
28
+ build
29
+ end
30
+
31
+ it 'should call the minify_variant' do
32
+ builder.should_receive(minify_variant)
33
+ build
34
+ end
35
+
36
+ it 'should return a Hash with the results of both minification and concatenation' do
37
+ result = build
38
+ result[:concatenated].should be_instance_of Hash
39
+ result[:minified].should be_instance_of Hash
40
+ end
41
+ end
42
+
43
+ context 'when suppressing errors' do
44
+ def build
45
+ builder.build options
46
+ end
47
+ let(:concatentate_variant){:concatenate}
48
+ let(:minify_variant){:minify}
49
+ end
50
+
51
+ context 'when not suppressing errors' do
52
+ def build
53
+ builder.build! options
54
+ end
55
+ let(:concatentate_variant){:concatenate!}
56
+ let(:minify_variant){:minify!}
57
+ end
58
+ end
59
+
60
+ share_examples_for 'javascript processing' do
61
+
62
+ it 'should create and write an output file by default' do
63
+ process_scripts!
64
+ File.should exist(output_file)
65
+ end
66
+
67
+ context 'when a license file is specified' do
68
+
69
+ let(:license){"\n\nLICENSE\nFILE\nCONTENT\n\n\n"}
70
+ let(:license_comment) {"/*\n" + license + "\n*/"}
71
+
72
+ before :each do
73
+ cupfile = directory.cupfile
74
+ cupfile.stub(:license).and_return('license.txt')
75
+ File.should_receive(:read).with(/.*license\.txt$/).and_return(license)
76
+ end
77
+
78
+ it 'should prepend the license unaltered to the output' do
79
+ process_scripts!
80
+ File.read(output_file).start_with?(license_comment).should be_true
81
+ end
82
+ end
83
+
84
+ context 'when not suppressing exceptions' do
85
+
86
+ it 'should throw an exception if something goes wrong' do
87
+ File.stub(:read).and_raise
88
+
89
+ expect {
90
+ process_scripts!
91
+ }.to raise_error
92
+
93
+ end
94
+
95
+ it 'should return the output' do
96
+ process_scripts!.should_not be_nil
97
+ process_scripts!.length.should_not == 0
98
+ end
99
+ end
100
+
101
+ context 'when supressing exceptions' do
102
+
103
+ context 'and there are errors' do
104
+
105
+ before :each do
106
+ builder.stub(:process).and_raise 'and_raise ERROR'
107
+ end
108
+
109
+ it 'should return a Hash with failed status and an exception message' do
110
+ process_scripts[:status].should == :failed
111
+ process_scripts[:exception].should_not be_nil
112
+ end
113
+ end
114
+
115
+ context 'and there are no errors' do
116
+ it 'should return a Hash with ok status output and no exception message' do
117
+ process_scripts[:status].should == :ok
118
+ process_scripts[:exception].should be_nil
119
+ process_scripts[:output].should_not be_nil
120
+ end
121
+ end
122
+ end
123
+
124
+ context 'when a cli is specified' do
125
+ it 'should report to the cli' do
126
+ cli = stub
127
+ options[:cli] = cli
128
+ cli.should_receive(:say)
129
+ process_scripts!
130
+ end
131
+ end
132
+ end
133
+
134
+ describe 'concatenating' do
135
+
136
+ def process_scripts!
137
+ builder.concatenate!(options)
138
+ end
139
+
140
+ def process_scripts
141
+ builder.concatenate(options)
142
+ end
143
+
144
+ let(:output_file){directory.concatenated}
145
+
146
+ it_behaves_like 'javascript processing'
147
+
148
+ end
149
+
150
+ describe 'minifying' do
151
+
152
+ def process_scripts!
153
+ builder.minify!(options)
154
+ end
155
+
156
+ def process_scripts
157
+ builder.minify(options)
158
+ end
159
+
160
+ let(:output_file){directory.minified}
161
+
162
+ it_behaves_like 'javascript processing'
163
+ end
164
+ end
@@ -0,0 +1,182 @@
1
+ require 'spec_helper'
2
+ require 'cup/command_line'
3
+ require 'fileutils'
4
+
5
+ describe Cup::CommandLine do
6
+
7
+ before :each do
8
+ puts "\n"
9
+ end
10
+
11
+ after :each do
12
+ puts "\n"
13
+ end
14
+
15
+ let(:cli) {Cup::CommandLine.new}
16
+ let(:test_cup_name) { "test_cup_#{Time.now.utc.to_i}" }
17
+ let(:test_cup_path) { Pathname.new("test_cup_#{Time.now.utc.to_i}") }
18
+
19
+ describe '#create' do
20
+
21
+ after :each do
22
+ FileUtils.rm_r test_cup_name if File.exists? test_cup_name
23
+ end
24
+
25
+ it 'should raise an exception if the name is not a valid filename' do
26
+ expect {
27
+ cli.create 'invalid/filename'
28
+ }.to raise_error
29
+ end
30
+
31
+ it 'should not create a new cup dir if one of the same name exists' do
32
+ Dir.stub(:exists?).and_return(true)
33
+ cli.create test_cup_name
34
+ Dir.unstub!(:exists?)
35
+ File.should_not exist(test_cup_name)
36
+ end
37
+
38
+ it 'should create a new cup dir' do
39
+ cli.create test_cup_name
40
+ File.should exist(test_cup_name)
41
+ end
42
+
43
+ it 'creates a default Cupfile' do
44
+ cli.create test_cup_name
45
+ cupfile_path = "#{test_cup_name}/Cupfile"
46
+ File.should exist(cupfile_path)
47
+ File.size(cupfile_path).should be > 0
48
+ end
49
+
50
+ it "creates empty src, lib and spec and spec/visual directories" do
51
+ cli.create test_cup_name
52
+ %w(src lib spec spec/visual).each do |dir|
53
+ dir = test_cup_path + dir
54
+ File.should exist(dir)
55
+
56
+ File.should be_directory(dir)
57
+
58
+ if dir == test_cup_path + 'spec'
59
+ Dir["#{dir}/*"].count.should == 1
60
+ else
61
+ Dir["#{dir}/*"].should be_empty
62
+ end
63
+ end
64
+ end
65
+
66
+ it "it downloads jquery into the vendor directory by default" do
67
+ cli.create test_cup_name
68
+ File.should exist("#{test_cup_name}/vendor/jquery.min.js")
69
+ end
70
+
71
+ it 'should not raise an error if jquery fails to download' do
72
+
73
+ Cup::CommandLine::Helper.module_eval do
74
+ def get_jquery
75
+ nil
76
+ end
77
+ end
78
+
79
+ expect {
80
+ cli.create test_cup_name
81
+ }.to_not raise_error
82
+
83
+ stub_create_helper_downloaders
84
+ end
85
+
86
+ it 'should not raise an error if jasmine fails to download/unpack' do
87
+
88
+ Cup::CommandLine::Helper.module_eval do
89
+ def download_jasmine
90
+ false
91
+ end
92
+ end
93
+
94
+ expect {
95
+ cli.create test_cup_name
96
+ }.to_not raise_error
97
+
98
+ stub_create_helper_downloaders
99
+ end
100
+
101
+ it 'should download jasmine into vendor' do
102
+ cli.create test_cup_name
103
+ path = Pathname.new(test_cup_name)
104
+
105
+ File.should exist(path + 'vendor' + 'jasmine-html.js')
106
+ File.should exist(path + 'vendor' + 'jasmine.js')
107
+ File.should exist(path + 'vendor' + 'jasmine.css')
108
+ end
109
+
110
+ it 'should copy extra_matchers.js from the gem into vendor' do
111
+ cli.create test_cup_name
112
+ path = Pathname.new(test_cup_name)
113
+ File.should exist(path + 'vendor' + 'extra_matchers.js')
114
+ end
115
+
116
+ it 'should create a license file populated with the year and user as author' do
117
+ cli.create test_cup_name
118
+ path = "#{test_cup_name}/license.txt"
119
+ File.should exist(path)
120
+ license = File.read path
121
+ license.should include Time.now.year.to_s
122
+ license.should include ENV['USER']
123
+ end
124
+ end
125
+
126
+ describe '#rackup' do
127
+
128
+ let(:config_ru_path){(Cup::Directory.current.path + "config.ru").to_s}
129
+
130
+ it 'should output a config.ru to the current directory' do
131
+ cli.rackup
132
+ File.should exist(config_ru_path)
133
+ File.delete(config_ru_path)
134
+ end
135
+
136
+ context 'when there is no cupfile in the directory' do
137
+
138
+ before :each do
139
+ helper = stub
140
+ helper.stub(:cupfile?).and_return(false)
141
+ cli.stub(:pwd_helper).and_return(helper)
142
+ end
143
+
144
+ it 'should not output a config.ru to the current directory' do
145
+ cli.rackup
146
+ File.should_not exist(config_ru_path)
147
+ end
148
+ end
149
+ end
150
+
151
+ describe '#build' do
152
+ it 'creates a minified and concatenated version of lib+src in build/' do
153
+ Cup.should_receive(:build!)
154
+ cli.build
155
+ end
156
+ end
157
+
158
+ describe 'stdout output' do
159
+
160
+ before :each do
161
+ @stdout = $stdout
162
+ $stdout = StringIO.new
163
+ end
164
+
165
+ after :each do
166
+ $stdout = @stdout
167
+ end
168
+
169
+ describe '#concatenate' do
170
+ it 'should output concatenated version' do
171
+ cli.concatenate
172
+ $stdout.pos.should be > 0
173
+ end
174
+ end
175
+ describe '#minify' do
176
+ it 'should output minified version' do
177
+ cli.minify
178
+ $stdout.pos.should be > 0
179
+ end
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,228 @@
1
+ require 'spec_helper'
2
+ require 'cup'
3
+
4
+ describe Cup::Cupfile do
5
+
6
+ let(:cupfile) do
7
+ Cup::Directory.current.cupfile
8
+ end
9
+
10
+ describe '.new' do
11
+
12
+ it 'should raise an error if the specified path is a directory' do
13
+ expect do
14
+ Cup::Cupfile.new('/')
15
+ end.to raise_error
16
+ end
17
+
18
+ it 'should raise an error if the specified path does not exist' do
19
+ expect do
20
+ Cup::Cupfile.new('non/existing/path')
21
+ end.to raise_error
22
+ end
23
+
24
+ end
25
+
26
+ describe 'validations' do
27
+
28
+ before(:each){ cupfile.should be_valid }
29
+
30
+ it '{ should validate_presence_of :name }' do
31
+ cupfile.instance_variable_set :@name, nil
32
+ cupfile.should_not be_valid
33
+ end
34
+
35
+ it '{ should validate_presence_of :version }' do
36
+ cupfile.instance_variable_set :@version, nil
37
+ cupfile.should_not be_valid
38
+ end
39
+
40
+ it '{ should validate_presence_of :path }' do
41
+ cupfile.instance_variable_set :@path, nil
42
+ cupfile.should_not be_valid
43
+ end
44
+
45
+ it 'should be invalid if the license file is specified but does not exist' do
46
+ cupfile.instance_variable_set :@license, 'path/that/is/not/there.txt'
47
+ cupfile.should_not be_valid
48
+ end
49
+ end
50
+
51
+ describe '#uglifier_options' do
52
+ it 'returns an empty hash by default' do
53
+ cupfile.uglifier_options.should be_instance_of Hash
54
+ end
55
+ end
56
+
57
+ describe '#before_build' do
58
+ it 'should be a noop by default' do
59
+ cupfile.before_build.should be_instance_of Proc
60
+ end
61
+ end
62
+
63
+ describe '#after_build' do
64
+ it 'should be a noop by default' do
65
+ cupfile.after_build.should be_instance_of Proc
66
+ end
67
+ end
68
+
69
+ describe '#javascripts' do
70
+ it 'should return a hash from dir for array for vendor, spec, lib and src' do
71
+
72
+ cupfile.javascripts.should be_instance_of(Hash)
73
+
74
+ %w{vendor spec lib src}.each do |dir|
75
+ cupfile.javascripts[dir.to_sym].should be_instance_of Array
76
+ cupfile.javascripts[dir.to_sym].should_not be_empty
77
+ end
78
+ end
79
+ end
80
+
81
+ describe '#set_javascript_patterns_for' do
82
+
83
+ let(:cupfile) { Cup::Directory.current.cupfile }
84
+
85
+ def set_patterns dir, *patterns
86
+ cupfile.send :set_javascript_patterns_for, dir, *patterns
87
+ end
88
+
89
+ it 'should not assign nil to javascripts key' do
90
+ set_patterns :spec, nil
91
+ cupfile.javascripts[:spec].should_not be_nil
92
+ end
93
+
94
+ it 'should return a match-all pattern for vendor,spec,lib,src by default' do
95
+ %w{vendor spec lib src}.each do |dir|
96
+ cupfile.javascripts[dir.to_sym].should_not be_empty
97
+ end
98
+ end
99
+
100
+ it 'should allow the pattern to be an empty array' do
101
+ set_patterns :src, []
102
+ cupfile.javascripts[:src].should == []
103
+ end
104
+
105
+ it 'should expand paths to within their key directory' do
106
+ filelist = FileList["#{File.expand_path(test_example_cup_path.to_s)}/lib/**/*.js"]
107
+ set_patterns :lib, '**/*.js'
108
+ cupfile.javascripts[:lib].should == filelist
109
+ end
110
+
111
+ it 'should turn :* into a match-all pattern' do
112
+ filelist = FileList["#{File.expand_path(test_example_cup_path.to_s)}/lib/**/*.js"]
113
+
114
+ set_patterns :lib, :*
115
+ cupfile.javascripts[:lib].should == filelist
116
+ end
117
+
118
+ it 'should not match-all javascripts within visual spec directory' do
119
+ cupfile.javascripts[:spec].should_not include_javascripts_from_load_paths 'spec/visual'
120
+ end
121
+
122
+ it 'should not allow none javascripts files to be included' do
123
+ set_patterns :lib, '1.css'
124
+ cupfile.javascripts[:lib].should be_empty
125
+ end
126
+ end
127
+
128
+ describe '#path' do
129
+
130
+ it 'should return the path that the cupfile was initialized with' do
131
+ path = test_example_cup_path + 'Cupfile'
132
+ cupfile = Cup::Cupfile.new(path)
133
+ cupfile.path.should == path
134
+ end
135
+ end
136
+
137
+ it 'should allow a version to be set' do
138
+
139
+ Cup::Cupfile::DSL.interpret cupfile do
140
+ version '1.0.3'
141
+ end
142
+
143
+ cupfile.version.should == '1.0.3'
144
+ end
145
+
146
+ it 'should allow a name to be set' do
147
+
148
+ Cup::Cupfile::DSL.interpret cupfile do
149
+ name 'mycup'
150
+ end
151
+
152
+ cupfile.name.should == 'mycup'
153
+ end
154
+
155
+ it 'should allow a license file to be set' do
156
+
157
+ Cup::Cupfile::DSL.interpret cupfile do
158
+ license 'license.txt'
159
+ end
160
+
161
+ cupfile.license.should == 'license.txt'
162
+ end
163
+
164
+ it 'should allow a before_build block to be set' do
165
+
166
+ proc = lambda {}
167
+
168
+ Cup::Cupfile::DSL.interpret cupfile do
169
+ before_build &proc
170
+ end
171
+
172
+ cupfile.before_build.should equal proc
173
+ end
174
+
175
+ it 'should allow a after_build block to be set' do
176
+
177
+ proc = lambda {}
178
+
179
+ Cup::Cupfile::DSL.interpret cupfile do
180
+ after_build &proc
181
+ end
182
+
183
+ cupfile.after_build.should equal proc
184
+ end
185
+
186
+ it 'should allow uglifier options to be specified' do
187
+
188
+ uglifier_opts = Hash.new
189
+
190
+ Cup::Cupfile::DSL.interpret cupfile do
191
+ uglifier_options uglifier_opts
192
+ end
193
+
194
+ cupfile.uglifier_options.should equal uglifier_opts
195
+ end
196
+
197
+ describe Cup::Cupfile::DSL do
198
+ describe Cup::Cupfile::DSL::JavascriptsDSL do
199
+
200
+ let(:cupfile){Cup::Directory.current.cupfile}
201
+
202
+ def interpret &block
203
+ Cup::Cupfile::DSL::JavascriptsDSL.interpret cupfile, &block
204
+ cupfile
205
+ end
206
+
207
+ it 'should allow lists of arguments' do
208
+ patterns = ['1.js', 'subdir/*.js']
209
+ cupfile.should_receive(:set_javascript_patterns_for).with(:spec, *patterns)
210
+
211
+ interpret do
212
+ spec *patterns
213
+ end
214
+ end
215
+
216
+ it 'should allow an array of patterns' do
217
+
218
+ patterns = ['1.js', 'subdir/*.js']
219
+ cupfile.should_receive(:set_javascript_patterns_for).with(:vendor, *patterns)
220
+
221
+ interpret do
222
+ vendor patterns
223
+ end
224
+ end
225
+
226
+ end
227
+ end
228
+ end