jellyfish-contrib 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,54 @@
1
+ # Gemgem
2
+
3
+ ## DESCRIPTION:
4
+
5
+ Provided tasks:
6
+
7
+ rake clean # Remove ignored files
8
+ rake gem:build # Build gem
9
+ rake gem:install # Install gem
10
+ rake gem:release # Release gem
11
+ rake gem:spec # Generate gemspec
12
+ rake test # Run tests in memory
13
+
14
+ ## REQUIREMENTS:
15
+
16
+ * Tested with MRI (official CRuby) 1.9.3, 2.0.0, Rubinius and JRuby.
17
+
18
+ ## INSTALLATION:
19
+
20
+ git submodule add git://github.com/godfat/gemgem.git task
21
+
22
+ And in Rakefile:
23
+
24
+ ``` ruby
25
+ begin
26
+ require "#{dir = File.dirname(__FILE__)}/task/gemgem"
27
+ rescue LoadError
28
+ sh 'git submodule update --init'
29
+ exec Gem.ruby, '-S', $PROGRAM_NAME, *ARGV
30
+ end
31
+
32
+ Gemgem.init(dir) do |s|
33
+ s.name = 'your-gem'
34
+ s.version = '0.1.0'
35
+ end
36
+ ```
37
+
38
+ ## LICENSE:
39
+
40
+ Apache License 2.0
41
+
42
+ Copyright (c) 2011-2013, Lin Jen-Shin (godfat)
43
+
44
+ Licensed under the Apache License, Version 2.0 (the "License");
45
+ you may not use this file except in compliance with the License.
46
+ You may obtain a copy of the License at
47
+
48
+ <http://www.apache.org/licenses/LICENSE-2.0>
49
+
50
+ Unless required by applicable law or agreed to in writing, software
51
+ distributed under the License is distributed on an "AS IS" BASIS,
52
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
53
+ See the License for the specific language governing permissions and
54
+ limitations under the License.
@@ -0,0 +1,316 @@
1
+
2
+ module Gemgem
3
+ class << self
4
+ attr_accessor :dir, :spec, :spec_create
5
+ end
6
+
7
+ module_function
8
+ def gem_tag ; "#{spec.name}-#{spec.version}" ; end
9
+ def gem_path ; "#{pkg_dir}/#{gem_tag}.gem" ; end
10
+ def spec_path ; "#{dir}/#{spec.name}.gemspec" ; end
11
+ def pkg_dir ; "#{dir}/pkg" ; end
12
+ def escaped_dir; @escaped_dir ||= Regexp.escape(dir); end
13
+
14
+ def init dir, &block
15
+ self.dir = dir
16
+ $LOAD_PATH.unshift("#{dir}/lib")
17
+ ENV['RUBYLIB'] = "#{dir}/lib:#{ENV['RUBYLIB']}"
18
+ ENV['PATH'] = "#{dir}/bin:#{ENV['PATH']}"
19
+ self.spec_create = block
20
+ end
21
+
22
+ def create
23
+ spec = Gem::Specification.new do |s|
24
+ s.authors = ['Lin Jen-Shin (godfat)']
25
+ s.email = ['godfat (XD) godfat.org']
26
+
27
+ s.description = description.join
28
+ s.summary = description.first
29
+ s.license = readme['LICENSE'].sub(/.+\n\n/, '').lines.first.strip
30
+
31
+ s.date = Time.now.strftime('%Y-%m-%d')
32
+ s.files = gem_files
33
+ s.test_files = test_files
34
+ s.executables = bin_files
35
+ end
36
+ spec_create.call(spec)
37
+ spec.homepage ||= "https://github.com/godfat/#{spec.name}"
38
+ self.spec = spec
39
+ end
40
+
41
+ def gem_install
42
+ require 'rubygems/commands/install_command'
43
+ # read ~/.gemrc
44
+ Gem.use_paths(Gem.configuration[:gemhome], Gem.configuration[:gempath])
45
+ Gem::Command.extra_args = Gem.configuration[:gem]
46
+
47
+ # setup install options
48
+ cmd = Gem::Commands::InstallCommand.new
49
+ cmd.handle_options([])
50
+
51
+ # install
52
+ install = Gem::Installer.new(gem_path, cmd.options)
53
+ install.install
54
+ puts "\e[35mGem installed: \e[33m#{strip_path(install.gem_dir)}\e[0m"
55
+ end
56
+
57
+ def gem_spec
58
+ create
59
+ write
60
+ end
61
+
62
+ def gem_build
63
+ require 'fileutils'
64
+ require 'rubygems/package'
65
+ gem = nil
66
+ Dir.chdir(dir) do
67
+ gem = Gem::Package.build(Gem::Specification.load(spec_path))
68
+ FileUtils.mkdir_p(pkg_dir)
69
+ FileUtils.mv(gem, pkg_dir) # gem is relative path, but might be ok
70
+ end
71
+ puts "\e[35mGem built: \e[33m#{strip_path("#{pkg_dir}/#{gem}")}\e[0m"
72
+ end
73
+
74
+ def gem_release
75
+ sh_git('tag', gem_tag)
76
+ sh_git('push')
77
+ sh_git('push', '--tags')
78
+ sh_gem('push', gem_path)
79
+ end
80
+
81
+ def gem_check
82
+ unless git('status', '--porcelain').empty?
83
+ puts("\e[35mWorking copy is not clean.\e[0m")
84
+ exit(3)
85
+ end
86
+
87
+ ver = spec.version.to_s
88
+
89
+ if ENV['VERSION'].nil?
90
+ puts("\e[35mExpected " \
91
+ "\e[33mVERSION\e[35m=\e[33m#{ver}\e[0m")
92
+ exit(1)
93
+
94
+ elsif ENV['VERSION'] != ver
95
+ puts("\e[35mExpected \e[33mVERSION\e[35m=\e[33m#{ver} " \
96
+ "\e[35mbut got\n " \
97
+ "\e[33mVERSION\e[35m=\e[33m#{ENV['VERSION']}\e[0m")
98
+ exit(2)
99
+ end
100
+ end
101
+
102
+ def test
103
+ return if test_files.empty?
104
+
105
+ if ENV['COV'] || ENV['CI']
106
+ require 'simplecov'
107
+ if ENV['CI']
108
+ begin
109
+ require 'coveralls'
110
+ SimpleCov.formatter = Coveralls::SimpleCov::Formatter
111
+ rescue LoadError => e
112
+ puts "Cannot load coveralls, skip: #{e}"
113
+ end
114
+ end
115
+ SimpleCov.start do
116
+ add_filter('test/')
117
+ add_filter('test.rb')
118
+ end
119
+ end
120
+
121
+ test_files.each{ |file| require "#{dir}/#{file[0..-4]}" }
122
+ end
123
+
124
+ def clean
125
+ return if ignored_files.empty?
126
+
127
+ require 'fileutils'
128
+ trash = File.expand_path("~/.Trash/#{spec.name}")
129
+ puts "Move the following files into: \e[35m#{strip_path(trash)}\e[33m"
130
+
131
+ ignored_files.each do |file|
132
+ from = "#{dir}/#{file}"
133
+ to = "#{trash}/#{File.dirname(file)}"
134
+ puts strip_path(from)
135
+
136
+ FileUtils.mkdir_p(to)
137
+ FileUtils.mv(from, to)
138
+ end
139
+
140
+ print "\e[0m"
141
+ end
142
+
143
+ def write
144
+ File.open(spec_path, 'w'){ |f| f << split_lines(spec.to_ruby) }
145
+ end
146
+
147
+ def split_lines ruby
148
+ ruby.gsub(/(.+?)\s*=\s*\[(.+?)\]/){ |s|
149
+ if $2.index(',')
150
+ "#{$1} = [\n #{$2.split(',').map(&:strip).join(",\n ")}]"
151
+ else
152
+ s
153
+ end
154
+ }
155
+ end
156
+
157
+ def strip_path path
158
+ strip_home_path(strip_cwd_path(path))
159
+ end
160
+
161
+ def strip_home_path path
162
+ path.sub(ENV['HOME'], '~')
163
+ end
164
+
165
+ def strip_cwd_path path
166
+ path.sub(Dir.pwd, '.')
167
+ end
168
+
169
+ def git *args
170
+ `git --git-dir=#{dir}/.git #{args.join(' ')}`
171
+ end
172
+
173
+ def sh_git *args
174
+ Rake.sh('git', "--git-dir=#{dir}/.git", *args)
175
+ end
176
+
177
+ def sh_gem *args
178
+ Rake.sh(Gem.ruby, '-S', 'gem', *args)
179
+ end
180
+
181
+ def glob path=dir
182
+ Dir.glob("#{path}/**/*", File::FNM_DOTMATCH)
183
+ end
184
+
185
+ def readme
186
+ @readme ||=
187
+ if (path = "#{Gemgem.dir}/README.md") && File.exist?(path)
188
+ ps = "##{File.read(path)}".
189
+ scan(/((#+)[^\n]+\n\n.+?(?=(\n\n\2[^#\n]+\n)|\Z))/m).map(&:first)
190
+ ps.inject('HEADER' => ps.first){ |r, s, i|
191
+ r[s[/\w+/]] = s
192
+ r
193
+ }
194
+ else
195
+ {}
196
+ end
197
+ end
198
+
199
+ def description
200
+ # JRuby String#lines is returning an enumerator
201
+ @description ||= (readme['DESCRIPTION']||'').sub(/.+\n\n/, '').lines.to_a
202
+ end
203
+
204
+ def all_files
205
+ @all_files ||= fold_files(glob).sort
206
+ end
207
+
208
+ def fold_files files
209
+ files.inject([]){ |r, path|
210
+ if File.file?(path) && path !~ %r{/\.git(/|$)} &&
211
+ (rpath = path[%r{^#{escaped_dir}/(.*$)}, 1])
212
+ r << rpath
213
+ elsif File.symlink?(path) # walk into symlinks...
214
+ r.concat(fold_files(glob(File.expand_path(path,
215
+ File.readlink(path)))))
216
+ else
217
+ r
218
+ end
219
+ }
220
+ end
221
+
222
+ def gem_files
223
+ @gem_files ||= all_files.reject{ |f|
224
+ f =~ ignored_pattern && !git_files.include?(f)
225
+ }
226
+ end
227
+
228
+ def test_files
229
+ @test_files ||= gem_files.grep(%r{^test/(.+?/)*test_.+?\.rb$})
230
+ end
231
+
232
+ def bin_files
233
+ @bin_files ||= gem_files.grep(%r{^bin/}).map{ |f| File.basename(f) }
234
+ end
235
+
236
+ def git_files
237
+ @git_files ||= if File.exist?("#{dir}/.git")
238
+ git('ls-files').split("\n")
239
+ else
240
+ []
241
+ end
242
+ end
243
+
244
+ def ignored_files
245
+ @ignored_files ||= all_files.grep(ignored_pattern)
246
+ end
247
+
248
+ def ignored_pattern
249
+ @ignored_pattern ||= if gitignore.empty?
250
+ /^$/
251
+ else
252
+ Regexp.new(expand_patterns(gitignore).join('|'))
253
+ end
254
+ end
255
+
256
+ def expand_patterns pathes
257
+ # http://git-scm.com/docs/gitignore
258
+ pathes.flat_map{ |path|
259
+ # we didn't implement negative pattern for now
260
+ Regexp.escape(path).sub(%r{^/}, '^').gsub(/\\\*/, '[^/]*')
261
+ }
262
+ end
263
+
264
+ def gitignore
265
+ @gitignore ||= if File.exist?(path = "#{dir}/.gitignore")
266
+ File.read(path).lines.
267
+ reject{ |l| l == /^\s*(#|\s+$)/ }.map(&:strip)
268
+ else
269
+ []
270
+ end
271
+ end
272
+ end
273
+
274
+ namespace :gem do
275
+
276
+ desc 'Install gem'
277
+ task :install => [:build] do
278
+ Gemgem.gem_install
279
+ end
280
+
281
+ desc 'Build gem'
282
+ task :build => [:spec] do
283
+ Gemgem.gem_build
284
+ end
285
+
286
+ desc 'Generate gemspec'
287
+ task :spec do
288
+ Gemgem.gem_spec
289
+ end
290
+
291
+ desc 'Release gem'
292
+ task :release => [:spec, :check, :build] do
293
+ Gemgem.gem_release
294
+ end
295
+
296
+ task :check do
297
+ Gemgem.gem_check
298
+ end
299
+
300
+ end # of gem namespace
301
+
302
+ desc 'Run tests'
303
+ task :test do
304
+ Gemgem.test
305
+ end
306
+
307
+ desc 'Trash ignored files'
308
+ task :clean => ['gem:spec'] do
309
+ Gemgem.clean
310
+ end
311
+
312
+ task :default do
313
+ # Is there a reliable way to do this in the current process?
314
+ # It failed miserably before between Rake versions...
315
+ exec "#{Gem.ruby} -S #{$PROGRAM_NAME} -f #{Rake.application.rakefile} -T"
316
+ end
@@ -0,0 +1,57 @@
1
+
2
+ require 'jellyfish/test'
3
+ require 'uri'
4
+ require 'stringio'
5
+
6
+ describe 'from README.md' do
7
+ after do
8
+ [:Tank, :Heater, :Protector].each do |const|
9
+ Object.send(:remove_const, const) if Object.const_defined?(const)
10
+ end
11
+ Muack.verify
12
+ end
13
+
14
+ readme = File.read(
15
+ "#{File.dirname(File.expand_path(__FILE__))}/../README.md")
16
+ codes = readme.scan(
17
+ /### ([^\n]+).+?``` ruby\n(.+?)\n```\n\n<!---(.+?)-->/m)
18
+
19
+ codes.each.with_index do |(title, code, test), index|
20
+ next if title =~ /NewRelic/i
21
+
22
+ would "pass from README.md #%02d #{title}" % index do
23
+ app = Rack::Builder.app{ eval(code) }
24
+
25
+ test.split("\n\n").each do |t|
26
+ method_path, expect = t.strip.split("\n", 2)
27
+ method, path = method_path.split(' ')
28
+ uri = URI.parse(path)
29
+ pinfo, query = uri.path, uri.query
30
+
31
+ sock = nil
32
+ status, headers, body = File.open(File::NULL) do |input|
33
+ app.call(
34
+ 'HTTP_VERSION' => 'HTTP/1.1',
35
+ 'REQUEST_METHOD' => method, 'PATH_INFO' => pinfo,
36
+ 'QUERY_STRING' => query , 'SCRIPT_NAME'=> '' ,
37
+ 'rack.input' => input ,
38
+ 'rack.hijack' => lambda{
39
+ sock = StringIO.new
40
+ # or TypeError: no implicit conversion of StringIO into IO
41
+ mock(IO).select([sock]){ [[sock], [], []] }
42
+ sock
43
+ })
44
+ end
45
+
46
+ if hijack = headers.delete('rack.hijack')
47
+ sock = StringIO.new
48
+ hijack.call(sock)
49
+ body = sock.string.each_line("\n\n")
50
+ end
51
+
52
+ body.extend(Enumerable)
53
+ [status, headers, body.to_a].should.eq eval(expect, binding, __FILE__)
54
+ end
55
+ end
56
+ end
57
+ end
@@ -0,0 +1,277 @@
1
+
2
+ require 'jellyfish/test'
3
+
4
+ # stolen from sinatra
5
+ describe 'Sinatra filter_test.rb' do
6
+ paste :jellyfish
7
+
8
+ def new_app base=Object, &block
9
+ Class.new(base){
10
+ include Jellyfish
11
+ controller_include(Jellyfish::MultiActions)
12
+ instance_eval(&block)
13
+ }.new
14
+ end
15
+
16
+ would 'executes filters in the order defined' do
17
+ count = 0
18
+ app = new_app{
19
+ get { count.should.eq 0; count = 1 }
20
+ get { count.should.eq 1; count = 2 }
21
+ get('/'){ 'Hello World' }
22
+ }
23
+
24
+ status, _, body = get('/', app)
25
+ status.should.eq 200
26
+ count .should.eq 2
27
+ body .should.eq ['Hello World']
28
+ end
29
+
30
+ would 'modify env' do
31
+ app = new_app{
32
+ get{ env['BOO'] = 'MOO' }
33
+ get('/foo'){ env['BOO'] }
34
+ }
35
+
36
+ status, _, body = get('/foo', app)
37
+ status.should.eq 200
38
+ body .should.eq ['MOO']
39
+ end
40
+
41
+ would 'modify instance variables available to routes' do
42
+ app = new_app{
43
+ get{ @foo = 'bar' }
44
+ get('/foo') { @foo }
45
+ }
46
+
47
+ status, _, body = get('/foo', app)
48
+ status.should.eq 200
49
+ body .should.eq ['bar']
50
+ end
51
+
52
+ would 'allows redirects' do
53
+ app = new_app{
54
+ get{ found '/bar' }
55
+ get('/foo') do
56
+ fail 'before block should have halted processing'
57
+ 'ORLY?!'
58
+ end
59
+ }
60
+
61
+ status, headers, body = get('/foo', app)
62
+ status .should.eq 302
63
+ headers['Location'].should.eq '/bar'
64
+ body.join .should =~ %r{<h1>Jellyfish found: /bar</h1>}
65
+ end
66
+
67
+ would 'not modify the response with its return value' do
68
+ app = new_app{
69
+ get{ 'Hello World!' }
70
+ get '/foo' do
71
+ body.should.eq nil
72
+ 'cool'
73
+ end
74
+ }
75
+
76
+ status, _, body = get('/foo', app)
77
+ status.should.eq 200
78
+ body .should.eq ['cool']
79
+ end
80
+
81
+ would 'modify the response with halt' do
82
+ app = new_app{
83
+ get('/foo'){ halt [302, {}, ['Hi']] }
84
+ get('/foo'){ 'should not happen' }
85
+ get('/bar'){ status 402; body 'Ho'; halt }
86
+ get('/bar'){ 'should not happen' }
87
+ }
88
+
89
+ get('/foo', app).should.eq [302, {}, ['Hi']]
90
+ get('/bar', app).should.eq [402, {}, ['Ho']]
91
+ end
92
+
93
+ would 'give you access to params' do
94
+ app = new_app{
95
+ get{ @foo = Rack::Request.new(env).params['foo'] }
96
+ get('/foo'){ @foo.reverse }
97
+ }
98
+
99
+ status, _, body = get('/foo', app, 'QUERY_STRING' => 'foo=cool')
100
+ status.should.eq 200
101
+ body .should.eq ['looc']
102
+ end
103
+
104
+ would 'run filters defined in superclasses' do
105
+ sup = new_app{ get{ @foo = 'hello from superclass' } }.class
106
+ app = new_app(sup){ get('/foo'){ @foo } }
107
+
108
+ _, _, body = get('/foo', app)
109
+ body.should.eq ['hello from superclass']
110
+
111
+ sup .routes['get'].size.should.eq 1
112
+ app.class.routes['get'].size.should.eq 2
113
+ end
114
+
115
+ would 'take an optional route pattern' do
116
+ ran_filter = false
117
+ app = new_app{
118
+ get(%r{^/b}){ ran_filter = true }
119
+ get('/foo') {}
120
+ get('/bar') {}
121
+ }
122
+ get('/foo', app)
123
+ ran_filter.should.eq false
124
+ get('/bar', app)
125
+ ran_filter.should.eq true
126
+ end
127
+
128
+ would 'generate block arguments from route pattern' do
129
+ subpath = nil
130
+ app = new_app{
131
+ get(%r{^/foo/(\w+)}){ |m| subpath = m[1] }
132
+ }
133
+ get('/foo/bar', app)
134
+ subpath.should.eq 'bar'
135
+ end
136
+
137
+ would 'execute before and after filters in correct order' do
138
+ invoked = 0
139
+ app = new_app{
140
+ get { invoked = 2 }
141
+ get('/'){ invoked += 2; body 'hello' }
142
+ get { invoked *= 2 }
143
+ }
144
+
145
+ status, _, body = get('/', app)
146
+ status .should.eq 200
147
+ body .should.eq ['hello']
148
+ invoked.should.eq 8
149
+ end
150
+
151
+ would 'execute filters in the order defined' do
152
+ count = 0
153
+ app = new_app{
154
+ get('/'){ body 'Hello World' }
155
+ get{
156
+ count.should.eq 0
157
+ count = 1
158
+ }
159
+ get{
160
+ count.should.eq 1
161
+ count = 2
162
+ }
163
+ }
164
+
165
+ status, _, body = get('/', app)
166
+ status.should.eq 200
167
+ count .should.eq 2
168
+ body .should.eq ['Hello World']
169
+ end
170
+
171
+ would 'allow redirects' do
172
+ app = new_app{
173
+ get('/foo'){ 'ORLY' }
174
+ get { found '/bar' }
175
+ }
176
+
177
+ status, headers, body = get('/foo', app)
178
+ status .should.eq 302
179
+ headers['Location'].should.eq '/bar'
180
+ body.join .should =~ %r{<h1>Jellyfish found: /bar</h1>}
181
+ end
182
+
183
+ would 'not modify the response with its return value' do
184
+ app = new_app{
185
+ get('/foo'){ body 'cool' }
186
+ get { 'Hello World!' }
187
+ }
188
+
189
+ status, _, body = get('/foo', app)
190
+ status.should.eq 200
191
+ body .should.eq ['cool']
192
+ end
193
+
194
+ would 'modify the response with halt' do
195
+ app = new_app{
196
+ get('/foo'){ 'should not be returned' }
197
+ get{ halt [302, {}, ['Hi']] }
198
+ }
199
+
200
+ status, _, body = get('/foo', app)
201
+ status.should.eq 302
202
+ body .should.eq ['Hi']
203
+ end
204
+
205
+ would 'take an optional route pattern' do
206
+ ran_filter = false
207
+ app = new_app{
208
+ get('/foo') {}
209
+ get('/bar') {}
210
+ get(%r{^/b}){ ran_filter = true }
211
+ }
212
+ get('/foo', app)
213
+ ran_filter.should.eq false
214
+ get('/bar', app)
215
+ ran_filter.should.eq true
216
+ end
217
+
218
+ would 'return response immediately on next or halt' do
219
+ app = Class.new{
220
+ include Jellyfish
221
+ controller_include Jellyfish::MultiActions
222
+
223
+ get '/next' do
224
+ body 'Hello World'
225
+ next
226
+ end
227
+
228
+ get '/halt' do
229
+ body 'Hello World'
230
+ halt
231
+ 'Boo-hoo World'
232
+ end
233
+ }.new
234
+
235
+ %w[/next /halt].each do |path|
236
+ status, _, body = get(path, app)
237
+ status.should.eq 200
238
+ body .should.eq ['Hello World']
239
+ end
240
+ end
241
+
242
+ would 'halt with a response tuple' do
243
+ app = Class.new{
244
+ include Jellyfish
245
+ controller_include Jellyfish::MultiActions
246
+
247
+ get '/' do
248
+ halt [295, {'Content-Type' => 'text/plain'}, ['Hello World']]
249
+ end
250
+ }.new
251
+
252
+ status, headers, body = get('/', app)
253
+ status .should.eq 295
254
+ headers['Content-Type'].should.eq 'text/plain'
255
+ body .should.eq ['Hello World']
256
+ end
257
+
258
+ would 'transition to the next matching route on next' do
259
+ app = Class.new{
260
+ include Jellyfish
261
+ controller_include Jellyfish::MultiActions, Jellyfish::NormalizedParams
262
+ get %r{^/(?<foo>\w+)} do
263
+ params['foo'].should.eq 'bar'
264
+ next
265
+ end
266
+
267
+ get do
268
+ params.should.not.include?('foo')
269
+ 'Hello World'
270
+ end
271
+ }.new
272
+
273
+ status, _, body = get('/bar', app)
274
+ status.should.eq 200
275
+ body .should.eq ['Hello World']
276
+ end
277
+ end