From 7532c2eaeb3faeb50990b9dcd5094c89a215d86d Mon Sep 17 00:00:00 2001 From: Hiroyuki Sano Date: Thu, 13 Aug 2015 09:46:06 +0000 Subject: [PATCH] Wait for started up a web server after rackup Sometimes a connection to web server fails on the "test:templates" rake task. This commit fixes it by adding a "wait" task. ref: https://travis-ci.org/rails/web-console/jobs/75395250 And split Rakefile into "lib/web_console/tasks/*.rake". --- Rakefile | 102 +--------------------- lib/web_console/tasks/extensions.rake | 60 +++++++++++++ lib/web_console/tasks/test_templates.rake | 50 +++++++++++ 3 files changed, 111 insertions(+), 101 deletions(-) create mode 100644 lib/web_console/tasks/extensions.rake create mode 100644 lib/web_console/tasks/test_templates.rake diff --git a/Rakefile b/Rakefile index 871c913c..45177c79 100644 --- a/Rakefile +++ b/Rakefile @@ -20,107 +20,7 @@ Rake::TestTask.new(:test) do |t| t.verbose = false end -namespace :test do - desc "Run tests for templates" - task templates: "templates:all" - - namespace :templates do - task all: [ :daemonize, :npm, :rackup, :mocha, :kill, :exit ] - task serve: [ :npm, :rackup ] - - work_dir = Pathname(__FILE__).dirname.join("test/templates") - pid_file = Pathname(Dir.tmpdir).join("web_console.#{SecureRandom.uuid}.pid") - server_port = 29292 - rackup_opts = "-p #{server_port}" - test_runner = "http://localhost:#{server_port}/html/spec_runner.html" - test_result = nil - - task :daemonize do - rackup_opts += " -D -P #{pid_file}" - end - - task :npm do - Dir.chdir(work_dir) { system "npm install --silent" } - end - - task :rackup do - Dir.chdir(work_dir) { system "bundle exec rackup #{rackup_opts}" } - end - - task :mocha do - Dir.chdir(work_dir) { test_result = system("$(npm bin)/mocha-phantomjs #{test_runner}") } - end - - task :kill do - system "kill #{File.read pid_file}" - end - - task :exit do - exit test_result - end - end -end - -namespace :ext do - rootdir = Pathname('extensions') - - desc 'Build Chrome Extension' - task chrome: 'chrome:build' - - namespace :chrome do - dist = Pathname('dist/crx') - extdir = rootdir.join(dist) - manifest_json = rootdir.join('chrome/manifest.json') - - directory extdir - - task build: [ extdir, 'lib:templates' ] do - cd rootdir do - cp_r [ 'img/', 'tmp/lib/' ], dist - `cd chrome && git ls-files`.split("\n").each do |src| - dest = dist.join(src) - mkdir_p dest.dirname - cp Pathname('chrome').join(src), dest - end - end - end - - # Generate a .crx file. - task crx: [ :build, :npm ] do - out = "crx-web-console-#{JSON.parse(File.read(manifest_json))["version"]}.crx" - cd(extdir) { sh "node \"$(npm bin)/crx\" pack ./ -p ../crx-web-console.pem -o ../#{out}" } - end - - # Generate a .zip file for Chrome Web Store. - task zip: [ :build ] do - version = JSON.parse(File.read(manifest_json))["version"] - cd(extdir) { sh "zip -r ../crx-web-console-#{version}.zip ./" } - end - - desc 'Launch a browser with the chrome extension.' - task run: [ :build ] do - cd(rootdir) { sh "sh ./script/run_chrome.sh --load-extension=#{dist}" } - end - end - - task :npm do - cd(rootdir) { sh "npm install --silent" } - end - - namespace :lib do - templates = Pathname('lib/web_console/templates') - tmplib = rootdir.join('tmp/lib/') - js_erb = FileList.new(templates.join('**/*.js.erb')) - dirs = js_erb.pathmap("%{^#{templates},#{tmplib}}d") - - task templates: dirs + js_erb.pathmap("%{^#{templates},#{tmplib}}X") - - dirs.each { |d| directory d } - rule '.js' => [ "%{^#{tmplib},#{templates}}X.js.erb" ] do |t| - File.write(t.name, WebConsole::Testing::ERBPrecompiler.new(t.source).build) - end - end -end +Dir['lib/web_console/tasks/**/*.rake'].each { |task| load task } Bundler::GemHelper.install_tasks diff --git a/lib/web_console/tasks/extensions.rake b/lib/web_console/tasks/extensions.rake new file mode 100644 index 00000000..2035b841 --- /dev/null +++ b/lib/web_console/tasks/extensions.rake @@ -0,0 +1,60 @@ +namespace :ext do + rootdir = Pathname('extensions') + + desc 'Build Chrome Extension' + task chrome: 'chrome:build' + + namespace :chrome do + dist = Pathname('dist/crx') + extdir = rootdir.join(dist) + manifest_json = rootdir.join('chrome/manifest.json') + + directory extdir + + task build: [ extdir, 'lib:templates' ] do + cd rootdir do + cp_r [ 'img/', 'tmp/lib/' ], dist + `cd chrome && git ls-files`.split("\n").each do |src| + dest = dist.join(src) + mkdir_p dest.dirname + cp Pathname('chrome').join(src), dest + end + end + end + + # Generate a .crx file. + task crx: [ :build, :npm ] do + out = "crx-web-console-#{JSON.parse(File.read(manifest_json))["version"]}.crx" + cd(extdir) { sh "node \"$(npm bin)/crx\" pack ./ -p ../crx-web-console.pem -o ../#{out}" } + end + + # Generate a .zip file for Chrome Web Store. + task zip: [ :build ] do + version = JSON.parse(File.read(manifest_json))["version"] + cd(extdir) { sh "zip -r ../crx-web-console-#{version}.zip ./" } + end + + desc 'Launch a browser with the chrome extension.' + task run: [ :build ] do + cd(rootdir) { sh "sh ./script/run_chrome.sh --load-extension=#{dist}" } + end + end + + task :npm do + cd(rootdir) { sh "npm install --silent" } + end + + namespace :lib do + templates = Pathname('lib/web_console/templates') + tmplib = rootdir.join('tmp/lib/') + js_erb = FileList.new(templates.join('**/*.js.erb')) + dirs = js_erb.pathmap("%{^#{templates},#{tmplib}}d") + + task templates: dirs + js_erb.pathmap("%{^#{templates},#{tmplib}}X") + + dirs.each { |d| directory d } + rule '.js' => [ "%{^#{tmplib},#{templates}}X.js.erb" ] do |t| + File.write(t.name, WebConsole::Testing::ERBPrecompiler.new(t.source).build) + end + end +end diff --git a/lib/web_console/tasks/test_templates.rake b/lib/web_console/tasks/test_templates.rake new file mode 100644 index 00000000..a5b6d5c4 --- /dev/null +++ b/lib/web_console/tasks/test_templates.rake @@ -0,0 +1,50 @@ +namespace :test do + desc "Run tests for templates" + task templates: "templates:all" + + namespace :templates do + task all: [ :daemonize, :npm, :rackup, :wait, :mocha, :kill, :exit ] + task serve: [ :npm, :rackup ] + + work_dir = Pathname(EXPANDED_CWD).join("test/templates") + pid_file = Pathname(Dir.tmpdir).join("web_console.#{SecureRandom.uuid}.pid") + runner_uri = URI.parse("http://localhost:29292/html/spec_runner.html") + rackup_opts = "-p #{runner_uri.port}" + test_result = nil + + def need_to_wait?(uri) + Net::HTTP.start(uri.host, uri.port) { |http| http.get(uri.path) } + rescue Errno::ECONNREFUSED + retry if yield + end + + task :daemonize do + rackup_opts += " -D -P #{pid_file}" + end + + task :npm do + Dir.chdir(work_dir) { system "npm install --silent" } + end + + task :rackup do + Dir.chdir(work_dir) { system "bundle exec rackup #{rackup_opts}" } + end + + task :wait do + cnt = 0 + need_to_wait?(runner_uri) { sleep 1; cnt += 1; cnt < 5 } + end + + task :mocha do + Dir.chdir(work_dir) { test_result = system("$(npm bin)/mocha-phantomjs #{runner_uri}") } + end + + task :kill do + system "kill #{File.read pid_file}" + end + + task :exit do + exit test_result + end + end +end