Permalink
Browse files

Tested & refactored Ruby code into a BlueRidge module

	- moved common rake and generator Ruby into a blue_ridge.rb file
	- added Micronaut & Mocha-based tests for BlueRidge Ruby code
	- default "rake" task runs both Ruby & JavaScript specs
	- fixed "rake js:fixtures" for Micronaut 'examples' directory
	- fixed bug where JavaScript specs would not generate in the Micronaut
	  'examples' directory
  • Loading branch information...
1 parent b87569e commit 88fe37342da0b460b7f1f7e2f455e8f304e99784 Larry Karnowski and Jason Rudolph committed Jul 31, 2009
View
13 Rakefile
@@ -1,4 +1,15 @@
ENV["BLUE_RIDGE_PREFIX"] = File.dirname(__FILE__)
import File.dirname(__FILE__) + '/tasks/javascript_testing_tasks.rake'
-task :default => "test:javascripts"
+gem "spicycode-micronaut", ">= 0.2.4"
+require 'micronaut'
+require 'micronaut/rake_task'
+
+namespace :test do
+ desc "Run all micronaut examples using rcov"
+ Micronaut::RakeTask.new :rubies do |t|
+ t.pattern = "spec/rubies/**/*_spec.rb"
+ end
+end
+
+task :default => ["test:rubies", "test:javascripts"]
View
9 generators/blue_ridge/blue_ridge_generator.rb
@@ -1,6 +1,10 @@
+require File.expand_path(File.dirname(__FILE__) + '/../../lib/blue_ridge')
+
class BlueRidgeGenerator < Rails::Generator::Base
def manifest
record do |m|
+ base_dir = BlueRidge.javascript_spec_dir
+
m.directory base_dir
m.file 'application_spec.js', "#{base_dir}/application_spec.js"
m.file 'spec_helper.js', "#{base_dir}/spec_helper.js"
@@ -10,9 +14,4 @@ def manifest
m.file 'screw.css', "#{base_dir}/fixtures/screw.css"
end
end
-
- def base_dir
- @base_dir ||= File.exist?("examples") ? "examples/javascripts" : File.exist?("spec") ? "spec/javascripts" : "test/javascript"
- end
-
end
View
6 generators/javascript_spec/javascript_spec_generator.rb
@@ -3,6 +3,8 @@ def manifest
file_path_with_spec, file_path_without_spec = file_path_with_and_without_spec
record do |m|
+ base_dir = BlueRidge.javascript_spec_dir
+
m.directory base_dir
m.directory "#{base_dir}/fixtures"
@@ -12,10 +14,6 @@ def manifest
end
end
- def base_dir
- @base_dir ||= File.exist?("spec") ? "spec/javascripts" : "test/javascript"
- end
-
def file_path_with_and_without_spec
if (file_path =~ /_spec$/i)
[file_path, file_path.gsub(/_spec$/, "")]
View
50 lib/blue_ridge.rb
@@ -0,0 +1,50 @@
+module BlueRidge
+ JavaScriptSpecDirs = ["examples/javascripts", "spec/javascripts", "test/javascript"]
+
+ def self.plugin_prefix
+ ENV["BLUE_RIDGE_PREFIX"] || "#{RAILS_ROOT}/vendor/plugins/blue-ridge"
+ end
+
+ def self.rhino_command
+ "java -Dblue.ridge.prefix=\"#{plugin_prefix}\" -jar #{plugin_prefix}/lib/js.jar -w -debug"
+ end
+
+ def self.test_runner_command
+ "#{rhino_command} #{plugin_prefix}/lib/test_runner.js"
+ end
+
+ def self.find_base_spec_dir
+ return "examples" if File.exist?("examples")
+ return "spec" if File.exist?("spec")
+ "test"
+ end
+
+ def self.javascript_spec_dir
+ base_spec_dir = find_base_spec_dir
+ return "test/javascript" if base_spec_dir == "test"
+ base_spec_dir + "/javascripts"
+ end
+
+ def self.find_javascript_spec_dir
+ JavaScriptSpecDirs.find {|d| File.exist?(d) }
+ end
+
+ def self.find_specs_under_current_dir
+ Dir.glob("*_spec.js")
+ end
+
+ def self.run_spec(spec_filename)
+ system("#{test_runner_command} #{spec_filename}")
+ end
+
+ def self.run_specs_in_dir(spec_dir, spec_name = nil)
+ result = nil
+ Dir.chdir(spec_dir) { result = run_specs(spec_name) }
+ result
+ end
+
+ def self.run_specs(spec_name = nil)
+ specs = spec_name.nil? ? find_specs_under_current_dir : ["#{spec_name}_spec.js"]
+ all_fine = specs.inject(true) {|result, spec| result &= run_spec(spec) }
+ end
+end
View
155 spec/rubies/blue_ridge_spec.rb
@@ -0,0 +1,155 @@
+require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
+require File.expand_path(File.dirname(__FILE__) + '/../../lib/blue_ridge')
+
+describe BlueRidge do
+ describe "finding the top-level directory containing the project's tests" do
+ it "returns Micronaut's 'examples' directory if it exists" do
+ File.stubs(:exist?).with("examples").returns(true)
+ BlueRidge.find_base_spec_dir.should == "examples"
+ end
+
+ it "returns RSpec's 'spec' directory if it exists and no Micronaut directory exists" do
+ File.stubs(:exist?).with("examples").returns(false)
+ File.stubs(:exist?).with("spec").returns(true)
+ BlueRidge.find_base_spec_dir.should == "spec"
+ end
+
+ it "returns the 'test' directory if no Micronaut or RSpec directories exist" do
+ File.stubs(:exist?).with("examples").returns(false)
+ File.stubs(:exist?).with("spec").returns(false)
+ BlueRidge.find_base_spec_dir.should == "test"
+ end
+ end
+
+ describe "returning the directory name to contain the project's JavaScript tests" do
+ it "returns 'examples/javascripts' if the base spec dir is 'examples'" do
+ BlueRidge.stubs(:find_base_spec_dir).returns("examples")
+ BlueRidge.javascript_spec_dir.should == "examples/javascripts"
+ end
+
+ it "returns 'spec/javascripts' if the base spec dir is 'spec'" do
+ BlueRidge.stubs(:find_base_spec_dir).returns("spec")
+ BlueRidge.javascript_spec_dir.should == "spec/javascripts"
+ end
+
+ it "returns 'test/javascript' if the base spec dir is 'test'" do
+ BlueRidge.stubs(:find_base_spec_dir).returns("test")
+ BlueRidge.javascript_spec_dir.should == "test/javascript"
+ end
+ end
+
+ describe "finding the directory containing the project's JavaScript tests" do
+ it "returns 'examples/javascripts' if it exists" do
+ File.stubs(:exist?).with("examples/javascripts").returns(true)
+ BlueRidge.find_javascript_spec_dir.should == "examples/javascripts"
+ end
+
+ it "returns 'spec/javascripts' if it exists and 'examples/javascripts' does not" do
+ File.stubs(:exist?).with("examples/javascripts").returns(false)
+ File.stubs(:exist?).with("spec/javascripts").returns(true)
+ BlueRidge.find_javascript_spec_dir.should == "spec/javascripts"
+ end
+
+ it "returns 'test/javascript' if it exists and 'examples/javascripts' and 'spec/javascripts' do not" do
+ File.stubs(:exist?).with("examples/javascripts").returns(false)
+ File.stubs(:exist?).with("spec/javascripts").returns(false)
+ File.stubs(:exist?).with("test/javascript").returns(true)
+ BlueRidge.find_javascript_spec_dir.should == "test/javascript"
+ end
+
+ it "returns null if none of the JavaScript spec directories exist" do
+ File.stubs(:exist?).with("examples/javascripts").returns(false)
+ File.stubs(:exist?).with("spec/javascripts").returns(false)
+ File.stubs(:exist?).with("test/javascript").returns(false)
+ BlueRidge.find_javascript_spec_dir.should be_nil
+ end
+ end
+
+ describe "finding all spec names that live under the current directory" do
+ it "returns the list of '*_spec.js' filenames under the current directory" do
+ Dir.expects(:glob).with("*_spec.js").returns(:some_filenames)
+ BlueRidge.find_specs_under_current_dir.should == :some_filenames
+ end
+ end
+
+ describe "running a specific spec" do
+ it "runs the test runner command against the given spec filename in a separate process" do
+ BlueRidge.stubs(:test_runner_command).returns("some_test_runner_command")
+ BlueRidge.expects(:system).with("some_test_runner_command some_spec_file").returns(:some_value)
+ BlueRidge.run_spec("some_spec_file").should == :some_value
+ end
+ end
+
+ describe "runing all specs under a given directory" do
+ it "changes the current working directory to the given directory" do
+ Dir.expects(:chdir).with("some_directory")
+ BlueRidge.run_specs_in_dir("some_directory")
+ end
+
+ it "runs the specs passing in the given spec name" do
+ Dir.stubs(:chdir).yields
+ BlueRidge.expects(:run_specs).with("some_spec_name")
+ BlueRidge.run_specs_in_dir("some_directory", "some_spec_name")
+ end
+
+ it "returns the result of the running the specs" do
+ Dir.stubs(:chdir).yields
+ BlueRidge.stubs(:run_specs).returns(:some_value)
+ BlueRidge.run_specs_in_dir("some_directory").should == :some_value
+ end
+ end
+
+ describe "running specs under the current directory" do
+ describe "when the given spec filename is nil" do
+ it "calls run_spec for each '*_spec.js' file under the current directory" do
+ BlueRidge.stubs(:find_specs_under_current_dir).returns(["first_js_spec.js", "second_js_spec.js"])
+ BlueRidge.expects(:run_spec).with("first_js_spec.js")
+ BlueRidge.expects(:run_spec).with("second_js_spec.js")
+ BlueRidge.run_specs
+ end
+
+ it "calls run_spec for each of the spec files, even if an early one reports a failure" do
+ BlueRidge.stubs(:find_specs_under_current_dir).returns(["first_js_spec.js", "second_js_spec.js"])
+ BlueRidge.expects(:run_spec).with("first_js_spec.js").returns(false)
+ BlueRidge.expects(:run_spec).with("second_js_spec.js")
+ BlueRidge.run_specs
+ end
+
+ it "returns false if ANY of the specs reports a failure" do
+ BlueRidge.stubs(:find_specs_under_current_dir).returns(["first_js_spec.js", "second_js_spec.js"])
+ BlueRidge.stubs(:run_spec).with("first_js_spec.js").returns(false)
+ BlueRidge.stubs(:run_spec).with("second_js_spec.js").returns(true)
+ BlueRidge.run_specs.should == false
+ end
+
+ it "returns true if ALL of the specs reports success" do
+ BlueRidge.stubs(:find_specs_under_current_dir).returns(["first_js_spec.js", "second_js_spec.js"])
+ BlueRidge.stubs(:run_spec).returns(true)
+ BlueRidge.run_specs.should == true
+ end
+ end
+
+ describe "when given a spec name" do
+ it "runs only the single spec and does not look for other specs" do
+ BlueRidge.expects(:find_specs_under_current_dir).never
+ BlueRidge.expects(:run_spec).once
+ BlueRidge.run_specs("some_filename")
+ end
+
+ it "runs the corresponding spec file for the given spec name" do
+ BlueRidge.expects(:run_spec).with("some_filename_spec.js").returns(true)
+ BlueRidge.run_specs("some_filename").should == true
+ end
+
+ it "returns true if the spec reports success" do
+ BlueRidge.stubs(:run_spec).returns(true)
+ BlueRidge.run_specs("some_filename").should == true
+ end
+
+ it "returns false if the spec reports a failure" do
+ BlueRidge.stubs(:run_spec).returns(false)
+ BlueRidge.run_specs("some_filename").should == false
+ end
+ end
+ end
+end
View
14 spec/rubies/spec_helper.rb
@@ -0,0 +1,14 @@
+require "micronaut"
+
+def not_in_editor?
+ ['TM_MODE', 'EMACS', 'VIM'].all? { |k| !ENV.has_key?(k) }
+end
+
+Micronaut.configure do |config|
+ config.formatter = :progress
+ config.mock_with :mocha
+ config.color_enabled = not_in_editor?
+ config.filter_run :focused => true
+ config.alias_example_to :fit, :focused => true
+ config.profile_examples = false
+end
View
40 tasks/javascript_testing_tasks.rake
@@ -1,50 +1,40 @@
-plugin_prefix = ENV["BLUE_RIDGE_PREFIX"] || "#{RAILS_ROOT}/vendor/plugins/blue-ridge"
-rhino_command = "java -Dblue.ridge.prefix=\"#{plugin_prefix}\" -jar #{plugin_prefix}/lib/js.jar -w -debug"
-test_runner_command = "#{rhino_command} #{plugin_prefix}/lib/test_runner.js"
+require File.expand_path(File.dirname(__FILE__) + '/../lib/blue_ridge')
-def find_base_dir
- target_dirs = ["test/javascript", "spec/javascripts", "examples/javascripts"]
- base_dir = target_dirs.find {|d| File.exist?(d) }
- raise "Could not find JavaScript test directory.\nNone of the following directories existed: #{target_dirs.join(", ")}.\nMaybe you need to call './script/generate blue_ridge'?" unless base_dir
- base_dir
+def error_message_for_missing_spec_dir
+ %Q{Could not find JavaScript test directory.
+None of the following directories existed: #{BlueRidge::JavaScriptSpecDirs.join(", ")}.
+Maybe you need to call './script/generate blue_ridge'?
+}
end
# Support Test::Unit & Test/Spec style
namespace :test do
desc "Runs all the JavaScript tests and outputs the results"
task :javascripts do
- Dir.chdir(find_base_dir) do
- all_fine = true
- if ENV["TEST"]
- all_fine = false unless system("#{test_runner_command} #{ENV["TEST"]}_spec.js")
- else
- Dir.glob("**/*_spec.js").each do |file|
- all_fine = false unless system("#{test_runner_command} #{file}")
- end
- end
- raise "JavaScript test failures" unless all_fine
- end
+ js_spec_dir = BlueRidge.find_javascript_spec_dir || (raise error_message_for_missing_spec_dir)
+ raise "JavaScript test failures" unless BlueRidge.run_specs_in_dir(js_spec_dir, ENV["TEST"])
end
task :javascript => :javascripts
end
# Support RSpec style
namespace :spec do
- task :javascripts => ["test:javascripts"]
- task :javascript => ["test:javascripts"]
+ task :javascripts => "test:javascripts"
+ task :javascript => "test:javascripts"
end
# Support Micronaut style
namespace :examples do
- task :javascripts => ["test:javascripts"]
- task :javascript => ["test:javascripts"]
+ task :javascripts => "test:javascripts"
+ task :javascript => "test:javascripts"
end
namespace :js do
task :fixtures do
- fixture_dir = "#{RAILS_ROOT}/#{find_base_dir}/fixtures"
+ js_spec_dir = BlueRidge.find_javascript_spec_dir || (raise error_message_for_missing_spec_dir)
+ fixture_dir = "#{js_spec_dir}/fixtures"
if PLATFORM[/darwin/]
system("open #{fixture_dir}")
@@ -57,6 +47,6 @@ namespace :js do
task :shell do
rlwrap = `which rlwrap`.chomp
- system("#{rlwrap} #{rhino_command} -f #{plugin_prefix}/lib/shell.js -f -")
+ system("#{rlwrap} #{BlueRidge.rhino_command} -f #{BlueRidge.plugin_prefix}/lib/shell.js -f -")
end
end

0 comments on commit 88fe373

Please sign in to comment.