11require 'rake'
22require 'rake/packagetask'
3+ require 'rbconfig'
34require 'yaml'
45
56module PrototypeHelper
7+ extend Rake ::DSL
8+
69 ROOT_DIR = File . expand_path ( File . dirname ( __FILE__ ) )
710 SRC_DIR = File . join ( ROOT_DIR , 'src' )
811 DIST_DIR = File . join ( ROOT_DIR , 'dist' )
@@ -13,9 +16,12 @@ module PrototypeHelper
1316 TEST_UNIT_DIR = File . join ( TEST_DIR , 'unit' )
1417 TMP_DIR = File . join ( TEST_UNIT_DIR , 'tmp' )
1518 VERSION = YAML . load ( IO . read ( File . join ( SRC_DIR , 'constants.yml' ) ) ) [ 'PROTOTYPE_VERSION' ]
16-
19+
1720 DEFAULT_SELECTOR_ENGINE = 'sizzle'
18-
21+
22+ host = RbConfig ::CONFIG [ 'host' ]
23+ IS_WINDOWS = host . include? ( 'mswin' ) || host . include? ( 'mingw32' )
24+
1925 # Possible options for PDoc syntax highlighting, in order of preference.
2026 SYNTAX_HIGHLIGHTERS = [ :pygments , :coderay , :none ]
2127
@@ -27,43 +33,43 @@ module PrototypeHelper
2733 begin
2834 `git --version`
2935 return true
30- rescue Error => e
36+ rescue Error
3137 return false
3238 end
3339 end
34-
40+
3541 def self . require_git
3642 return if has_git?
3743 puts "\n Prototype requires Git in order to load its dependencies."
3844 puts "\n Make sure you've got Git installed and in your path."
3945 puts "\n For more information, visit:\n \n "
40- puts " http://book. git-scm.com/2_installing_git.html "
46+ puts " http://git-scm.com/book/en/v2/Getting-Started-Installing-Git "
4147 exit
4248 end
43-
49+
4450 def self . sprocketize ( options = { } )
4551 options = {
4652 :destination => File . join ( DIST_DIR , options [ :source ] ) ,
4753 :strip_comments => true
4854 } . merge ( options )
49-
55+
5056 require_sprockets
5157 load_path = [ SRC_DIR ]
52-
58+
5359 if selector_path = get_selector_engine ( options [ :selector_engine ] )
5460 load_path << selector_path
5561 end
56-
62+
5763 secretary = Sprockets ::Secretary . new (
5864 :root => File . join ( ROOT_DIR , options [ :path ] ) ,
5965 :load_path => load_path ,
6066 :source_files => [ options [ :source ] ] ,
6167 :strip_comments => options [ :strip_comments ]
6268 )
63-
69+
6470 secretary . concatenation . save_to ( options [ :destination ] )
6571 end
66-
72+
6773 def self . build_doc_for ( file )
6874 rm_rf ( DOC_DIR )
6975 mkdir_p ( DOC_DIR )
96102 :assets => 'doc_assets'
97103 } )
98104 end
99-
105+
106+ def self . require_package ( name )
107+ begin
108+ require name
109+ rescue LoadError
110+ puts "You need the #{ name } package. Try installing it with:\n "
111+ puts " $ gem install #{ name } "
112+ exit
113+ end
114+ end
115+
116+ def self . require_phantomjs
117+ cmd = IS_WINDOWS ? "phantomjs.cmd -v" : "phantomjs -v > /dev/null 2>&1"
118+ success = system ( cmd )
119+ if !success
120+ puts "\n You need phantomjs installed to run this task. Find out how at:"
121+ puts " http://phantomjs.org/download.html"
122+ exit
123+ end
124+ end
125+
100126 def self . syntax_highlighter
101127 if ENV [ 'SYNTAX_HIGHLIGHTER' ]
102128 highlighter = ENV [ 'SYNTAX_HIGHLIGHTER' ] . to_sym
103129 require_highlighter ( highlighter , true )
104130 return highlighter
105131 end
106-
132+
107133 SYNTAX_HIGHLIGHTERS . detect { |n | require_highlighter ( n ) }
108134 end
109-
135+
110136 def self . require_highlighter ( name , verbose = false )
111137 case name
112138 when :pygments
121147 when :coderay
122148 begin
123149 require 'coderay'
124- rescue LoadError => e
150+ rescue LoadError
125151 if verbose
126152 puts "\n You asked to use CodeRay, but I can't find the 'coderay' gem. Just run:\n \n "
127153 puts " $ gem install coderay"
@@ -139,48 +165,42 @@ EOF
139165 exit
140166 end
141167 end
142-
168+
143169 def self . require_sprockets
144170 require_submodule ( 'Sprockets' , 'sprockets' )
145171 end
146-
172+
147173 def self . require_pdoc
148174 require_submodule ( 'PDoc' , 'pdoc' )
149175 end
150-
176+
151177 def self . require_unittest_js
152178 require_submodule ( 'UnittestJS' , 'unittest_js' )
153179 end
154-
180+
155181 def self . require_caja_builder
156182 require_submodule ( 'CajaBuilder' , 'caja_builder' )
157183 end
158-
184+
159185 def self . get_selector_engine ( name )
160186 return if !name
161- # If the submodule exists, we should use it, even if we're using the
162- # default engine; the user might have fetched it manually, and thus would
163- # want to build a distributable with the most recent version of that
164- # engine.
187+ # If the submodule exists, we should use it.
165188 submodule_path = File . join ( ROOT_DIR , "vendor" , name )
166189 return submodule_path if File . exist? ( File . join ( submodule_path , "repository" , ".git" ) )
167190 return submodule_path if name === "legacy_selector"
168-
169- # If it doesn't exist, we should fetch it, _unless_ it's the default
170- # engine. We've already got a known version of the default engine in our
171- # load path.
172- return if name == DEFAULT_SELECTOR_ENGINE
191+
192+ # If it doesn't exist, we should fetch it.
173193 get_submodule ( 'the required selector engine' , "#{ name } /repository" )
174194 unless File . exist? ( submodule_path )
175195 puts "The selector engine you required isn't available at vendor/#{ name } .\n \n "
176196 exit
177197 end
178198 end
179-
199+
180200 def self . get_submodule ( name , path )
181201 require_git
182202 puts "\n You seem to be missing #{ name } . Obtaining it via git...\n \n "
183-
203+
184204 Kernel . system ( "git submodule init" )
185205 return true if Kernel . system ( "git submodule update vendor/#{ path } " )
186206 # If we got this far, something went wrong.
@@ -189,16 +209,18 @@ EOF
189209 puts " $ git submodule update vendor/#{ path } "
190210 false
191211 end
192-
212+
193213 def self . require_submodule ( name , path )
194214 begin
195- require path
215+ full_path = File . join ( PrototypeHelper ::ROOT_DIR , 'vendor' , path , 'lib' , path )
216+ # We need to require the explicit version in the submodule.
217+ require full_path
196218 rescue LoadError => e
197219 # Wait until we notice that a submodule is missing before we bother the
198220 # user about installing git. (Maybe they brought all the files over
199221 # from a different machine.)
200- missing_file = e . message . sub ( 'no such file to load -- ' , '' )
201- if missing_file == path
222+ missing_file = e . message . sub ( 'no such file to load -- ' , '' ) . sub ( 'cannot load such file -- ' , '' )
223+ if missing_file == full_path
202224 # Missing a git submodule.
203225 retry if get_submodule ( name , path )
204226 else
210232 exit
211233 end
212234 end
213-
235+
214236 def self . current_head
215237 `git show-ref --hash HEAD` . chomp [ 0 ..6 ]
216238 end
@@ -232,7 +254,7 @@ namespace :doc do
232254 task :build => [ :require ] do
233255 PrototypeHelper . build_doc_for ( ENV [ 'SECTION' ] ? "#{ ENV [ 'SECTION' ] } .js" : 'prototype.js' )
234256 end
235-
257+
236258 task :require do
237259 PrototypeHelper . require_pdoc
238260 end
@@ -261,83 +283,37 @@ task :clean_package_source do
261283 rm_rf File . join ( PrototypeHelper ::PKG_DIR , "prototype-#{ PrototypeHelper ::VERSION } " )
262284end
263285
264- task :test => [ 'test:build ' , 'test:run ' ]
286+ task :test => [ 'test:require ' , 'test:start ' ]
265287namespace :test do
266- desc 'Runs all the JavaScript unit tests and collects the results'
267- task :run => [ :require ] do
268- testcases = ENV [ 'TESTCASES' ]
269- browsers_to_test = ENV [ 'BROWSERS' ] && ENV [ 'BROWSERS' ] . split ( ',' )
270- tests_to_run = ENV [ 'TESTS' ] && ENV [ 'TESTS' ] . split ( ',' )
271- runner = UnittestJS ::WEBrickRunner ::Runner . new ( :test_dir => PrototypeHelper ::TMP_DIR )
272-
273- Dir [ File . join ( PrototypeHelper ::TMP_DIR , '*_test.html' ) ] . each do |file |
274- file = File . basename ( file )
275- test = file . sub ( '_test.html' , '' )
276- unless tests_to_run && !tests_to_run . include? ( test )
277- runner . add_test ( file , testcases )
278- end
279- end
280-
281- UnittestJS ::Browser ::SUPPORTED . each do |browser |
282- unless browsers_to_test && !browsers_to_test . include? ( browser )
283- runner . add_browser ( browser . to_sym )
284- end
285- end
286-
287- trap ( 'INT' ) { runner . teardown ; exit }
288- runner . run
289- end
290-
291- task :build => [ :clean , :dist ] do
292- builder = UnittestJS ::Builder ::SuiteBuilder . new ( {
293- :input_dir => PrototypeHelper ::TEST_UNIT_DIR ,
294- :assets_dir => PrototypeHelper ::DIST_DIR
295- } )
296- selected_tests = ( ENV [ 'TESTS' ] || '' ) . split ( ',' )
297- builder . collect ( *selected_tests )
298- builder . render
299- end
300-
301- task :clean => [ :require ] do
302- UnittestJS ::Builder . empty_dir! ( PrototypeHelper ::TMP_DIR )
288+ desc 'Starts the test server.'
289+ task :start => [ :require ] do
290+ path_to_app = File . join ( PrototypeHelper ::ROOT_DIR , 'test' , 'unit' , 'server.rb' )
291+ require path_to_app
292+
293+ puts "Starting unit test server..."
294+ puts "Unit tests available at <http://127.0.0.1:4567/test/>\n \n "
295+ UnitTests . run!
303296 end
304-
297+
305298 task :require do
306- PrototypeHelper . require_unittest_js
299+ PrototypeHelper . require_package ( 'sinatra' )
307300 end
308- end
309301
310- task :test_units do
311- puts '"rake test_units" is deprecated. Please use "rake test" instead.'
312- end
313-
314- task :build_unit_tests do
315- puts '"rake test_units" is deprecated. Please use "rake test:build" instead.'
316- end
317-
318- task :clean_tmp do
319- puts '"rake clean_tmp" is deprecated. Please use "rake test:clean" instead.'
320- end
302+ desc "Opens the test suite in several different browsers. (Does not start or stop the server; you should do that separately.)"
303+ task :run => [ :require ] do
304+ browsers , tests , grep = ENV [ 'BROWSERS' ] , ENV [ 'TESTS' ] , ENV [ 'GREP' ]
305+ path_to_runner = File . join ( PrototypeHelper ::ROOT_DIR , 'test' , 'unit' , 'runner.rb' )
306+ require path_to_runner
321307
322- namespace :caja do
323- task :test => [ 'test:build' , 'test:run' ]
324-
325- namespace :test do
326- task :run => [ 'rake:test:run' ]
327-
328- task :build => [ :require , 'rake:test:clean' , :dist ] do
329- builder = UnittestJS ::CajaBuilder ::SuiteBuilder . new ( {
330- :input_dir => PrototypeHelper ::TEST_UNIT_DIR ,
331- :assets_dir => PrototypeHelper ::DIST_DIR ,
332- :whitelist_dir => File . join ( PrototypeHelper ::TEST_DIR , 'unit' , 'caja_whitelists' ) ,
333- :html_attrib_schema => 'html_attrib.json'
334- } )
335- selected_tests = ( ENV [ 'TESTS' ] || '' ) . split ( ',' )
336- builder . collect ( *selected_tests )
337- builder . render
338- end
308+ Runner ::run ( browsers , tests , grep )
339309 end
340- task :require => [ 'rake:test:require' ] do
341- PrototypeHelper . require_caja_builder
310+
311+ desc "Runs the tests in PhantomJS. (Does not start or stop the server; you should do that separately.)"
312+ task :phantom do
313+ PrototypeHelper . require_phantomjs
314+ tests , grep = ENV [ 'TESTS' ] , ENV [ 'GREP' ]
315+ url = "http://127.0.0.1:4567/test/#{ tests } "
316+ url << "?grep=#{ grep } " if grep
317+ system ( %Q[phantomjs ./test/unit/phantomjs/mocha-phantomjs.js "#{ url } "] )
342318 end
343- end
319+ end
0 commit comments