Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 373
- Log:
Initial import of Radiant 0.9.1, which is now packaged as a gem. This is an
import of the tagged 0.9.1 source checked out from GitHub, which isn't quite
the same as the gem distribution - but it doesn't seem to be available in an
archived form and the installed gem already has modifications, so this is
the closest I can get.
- Author:
- rool
- Date:
- Mon Mar 21 13:40:05 +0000 2011
- Size:
- 4792 Bytes
1 | require 'pp' |
2 | require 'tmpdir' |
3 | require 'stringio' |
4 | |
5 | [ STDOUT, STDERR ].each { |io| io.sync = true } |
6 | |
7 | begin |
8 | require 'test/spec' |
9 | rescue LoadError => boom |
10 | require 'rubygems' rescue nil |
11 | require 'test/spec' |
12 | end |
13 | |
14 | # Set the MEMCACHED environment variable as follows to enable testing |
15 | # of the MemCached meta and entity stores. |
16 | ENV['MEMCACHED'] ||= 'localhost:11215' |
17 | $memcached = nil |
18 | |
19 | def have_memcached?(server=ENV['MEMCACHED']) |
20 | return $memcached unless $memcached.nil? |
21 | v, $VERBOSE = $VERBOSE, nil # silence warnings from memcached |
22 | require 'memcached' |
23 | $VERBOSE = v |
24 | $memcached = Memcached.new(server) |
25 | $memcached.set('ping', '') |
26 | true |
27 | rescue LoadError => boom |
28 | $memcached = false |
29 | false |
30 | rescue => boom |
31 | STDERR.puts "memcached not working. related tests will be skipped." |
32 | $memcached = false |
33 | false |
34 | end |
35 | |
36 | have_memcached? |
37 | |
38 | def need_memcached(forwhat) |
39 | if have_memcached? |
40 | yield |
41 | else |
42 | STDERR.puts "skipping memcached #{forwhat} (MEMCACHED environment variable not set)" |
43 | end |
44 | end |
45 | |
46 | # Setup the load path .. |
47 | $LOAD_PATH.unshift File.dirname(File.dirname(__FILE__)) + '/lib' |
48 | $LOAD_PATH.unshift File.dirname(__FILE__) |
49 | |
50 | require 'rack/cache' |
51 | |
52 | |
53 | # Methods for constructing downstream applications / response |
54 | # generators. |
55 | module CacheContextHelpers |
56 | |
57 | # The Rack::Cache::Context instance used for the most recent |
58 | # request. |
59 | attr_reader :cache |
60 | |
61 | # An Array of Rack::Cache::Context instances used for each request, in |
62 | # request order. |
63 | attr_reader :caches |
64 | |
65 | # The Rack::Response instance result of the most recent request. |
66 | attr_reader :response |
67 | |
68 | # An Array of Rack::Response instances for each request, in request order. |
69 | attr_reader :responses |
70 | |
71 | # The backend application object. |
72 | attr_reader :app |
73 | |
74 | def setup_cache_context |
75 | # holds each Rack::Cache::Context |
76 | @app = nil |
77 | |
78 | # each time a request is made, a clone of @cache_template is used |
79 | # and appended to @caches. |
80 | @cache_template = nil |
81 | @cache = nil |
82 | @caches = [] |
83 | @errors = StringIO.new |
84 | @cache_config = nil |
85 | |
86 | @called = false |
87 | @request = nil |
88 | @response = nil |
89 | @responses = [] |
90 | |
91 | @storage = Rack::Cache::Storage.new |
92 | end |
93 | |
94 | def teardown_cache_context |
95 | @app, @cache_template, @cache, @caches, @called, |
96 | @request, @response, @responses, @cache_config = nil |
97 | end |
98 | |
99 | # A basic response with 200 status code and a tiny body. |
100 | def respond_with(status=200, headers={}, body=['Hello World']) |
101 | called = false |
102 | @app = |
103 | lambda do |env| |
104 | called = true |
105 | response = Rack::Response.new(body, status, headers) |
106 | request = Rack::Request.new(env) |
107 | yield request, response if block_given? |
108 | response.finish |
109 | end |
110 | @app.meta_def(:called?) { called } |
111 | @app.meta_def(:reset!) { called = false } |
112 | @app |
113 | end |
114 | |
115 | def cache_config(&block) |
116 | @cache_config = block |
117 | end |
118 | |
119 | def request(method, uri='/', opts={}) |
120 | opts = { |
121 | 'rack.run_once' => true, |
122 | 'rack.errors' => @errors, |
123 | 'rack-cache.storage' => @storage |
124 | }.merge(opts) |
125 | |
126 | fail 'response not specified (use respond_with)' if @app.nil? |
127 | @app.reset! if @app.respond_to?(:reset!) |
128 | |
129 | @cache_prototype ||= Rack::Cache::Context.new(@app, &@cache_config) |
130 | @cache = @cache_prototype.clone |
131 | @caches << @cache |
132 | @request = Rack::MockRequest.new(@cache) |
133 | yield @cache if block_given? |
134 | @response = @request.request(method.to_s.upcase, uri, opts) |
135 | @responses << @response |
136 | @response |
137 | end |
138 | |
139 | def get(stem, env={}, &b) |
140 | request(:get, stem, env, &b) |
141 | end |
142 | |
143 | def head(stem, env={}, &b) |
144 | request(:head, stem, env, &b) |
145 | end |
146 | |
147 | def post(*args, &b) |
148 | request(:post, *args, &b) |
149 | end |
150 | end |
151 | |
152 | |
153 | module TestHelpers |
154 | include FileUtils |
155 | F = File |
156 | |
157 | @@temp_dir_count = 0 |
158 | |
159 | def create_temp_directory |
160 | @@temp_dir_count += 1 |
161 | path = F.join(Dir.tmpdir, "rack-cache-#{$$}-#{@@temp_dir_count}") |
162 | mkdir_p path |
163 | if block_given? |
164 | yield path |
165 | remove_entry_secure path |
166 | end |
167 | path |
168 | end |
169 | |
170 | def create_temp_file(root, file, data='') |
171 | path = F.join(root, file) |
172 | mkdir_p F.dirname(path) |
173 | F.open(path, 'w') { |io| io.write(data) } |
174 | end |
175 | |
176 | end |
177 | |
178 | class Test::Unit::TestCase |
179 | include TestHelpers |
180 | include CacheContextHelpers |
181 | end |
182 | |
183 | # Metaid == a few simple metaclass helper |
184 | # (See http://whytheluckystiff.net/articles/seeingMetaclassesClearly.html.) |
185 | class Object |
186 | # The hidden singleton lurks behind everyone |
187 | def metaclass; class << self; self; end; end |
188 | def meta_eval(&blk); metaclass.instance_eval(&blk); end |
189 | # Adds methods to a metaclass |
190 | def meta_def name, &blk |
191 | meta_eval { define_method name, &blk } |
192 | end |
193 | # Defines an instance method within a class |
194 | def class_def name, &blk |
195 | class_eval { define_method name, &blk } |
196 | end |
197 | |
198 | # True when the Object is neither false or nil. |
199 | def truthy? |
200 | !!self |
201 | end |
202 | end |