From 45b1c44e5bb34e8069a02463525e05562a3a8efd Mon Sep 17 00:00:00 2001 From: John Bintz Date: Wed, 22 Feb 2012 11:47:07 -0500 Subject: [PATCH] yeehaw! --- .gitignore | 17 +++++++ Gemfile | 7 +++ LICENSE | 22 ++++++++++ README.md | 29 ++++++++++++ Rakefile | 2 + bin/flowerbox | 44 +++++++++++++++++++ features/basic_run.feature | 30 +++++++++++++ .../given/i_have_the_file_with_content.rb | 7 +++ .../then/i_should_have_tests_and_failures.rb | 7 +++ .../step_definitions/when/i_run_flowerbox.rb | 10 +++++ features/support/env.rb | 6 +++ flowerbox.gemspec | 24 ++++++++++ lib/flowerbox.rb | 34 ++++++++++++++ lib/flowerbox/jasmine.rb | 0 lib/flowerbox/runner/node.rb | 44 +++++++++++++++++++ lib/flowerbox/test_environment/jasmine.rb | 39 ++++++++++++++++ lib/flowerbox/version.rb | 3 ++ 17 files changed, 325 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 LICENSE create mode 100644 README.md create mode 100644 Rakefile create mode 100755 bin/flowerbox create mode 100644 features/basic_run.feature create mode 100644 features/step_definitions/given/i_have_the_file_with_content.rb create mode 100644 features/step_definitions/then/i_should_have_tests_and_failures.rb create mode 100644 features/step_definitions/when/i_run_flowerbox.rb create mode 100644 features/support/env.rb create mode 100644 flowerbox.gemspec create mode 100644 lib/flowerbox.rb create mode 100644 lib/flowerbox/jasmine.rb create mode 100644 lib/flowerbox/runner/node.rb create mode 100644 lib/flowerbox/test_environment/jasmine.rb create mode 100644 lib/flowerbox/version.rb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..766ec5d --- /dev/null +++ b/Gemfile @@ -0,0 +1,7 @@ +source 'https://rubygems.org' + +# Specify your gem's dependencies in flowerbox.gemspec +gemspec + +gem 'flowerbox-delivery', :path => '../flowerbox-delivery' + diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..87b491e --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2012 John Bintz + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..9ccbd7a --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# Flowerbox + +TODO: Write a gem description + +## Installation + +Add this line to your application's Gemfile: + + gem 'flowerbox' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install flowerbox + +## Usage + +TODO: Write usage instructions here + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Added some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..f57ae68 --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +#!/usr/bin/env rake +require "bundler/gem_tasks" diff --git a/bin/flowerbox b/bin/flowerbox new file mode 100755 index 0000000..d87996b --- /dev/null +++ b/bin/flowerbox @@ -0,0 +1,44 @@ +#!/usr/bin/env ruby + +require 'flowerbox' +require 'flowerbox-delivery' +require 'thor' + +class Flowerbox::CLI < Thor + desc "test DIR SPEC_PATTERN", "Run the specs found in spec dir" + method_options :pwd => :string + def test(dir) + Dir.chdir(pwd) do + load File.join(dir, 'spec_helper.rb') + + require 'tilt/coffee' + + Tilt::CoffeeScriptTemplate.default_bare = Flowerbox.bare_coffeescript + + sprockets = Flowerbox::Delivery::SprocketsHandler.new(:asset_paths => [ dir, Flowerbox.asset_paths ].flatten) + + Flowerbox.load_test_environment(sprockets) + + Flowerbox.spec_patterns.each do |pattern| + Dir[File.join(dir, pattern)].each do |file| + + sprockets.add(file.gsub(dir + '/', '')) + end + end + + exit Flowerbox::Runner::Node.run(sprockets.files) + end + end + + no_tasks do + def pwd + options[:pwd] || Dir.pwd + end + + def asset_paths + Flowerbox.asset_paths.collect { |path| File.join(pwd, path) } + end + end +end + +Flowerbox::CLI.start diff --git a/features/basic_run.feature b/features/basic_run.feature new file mode 100644 index 0000000..bc0b921 --- /dev/null +++ b/features/basic_run.feature @@ -0,0 +1,30 @@ +Feature: Basic Run + Scenario: Use the Node runner using Jasmine + Given I have the file "spec/javascripts/spec_helper.rb" with the content: + """ + Flowerbox.configure do |c| + c.test_with :jasmine + c.spec_patterns << "**/*_spec.*" + c.asset_paths << "lib" + c.bare_coffeescript = true + end + """ + Given I have the file "spec/javascripts/cat_spec.js.coffee" with the content: + """ + #= require cat + + describe 'Cat', -> + describe '#meow', -> + it 'should meow', -> + cat = new Cat() + + expect(cat.meow()).toEqual('meow') + """ + Given I have the file "lib/cat.js.coffee" with the content: + """ + class Cat + meow: -> "meow" + """ + When I run Flowerbox with "spec/javascripts" + Then I should have 1 test and 0 failures + diff --git a/features/step_definitions/given/i_have_the_file_with_content.rb b/features/step_definitions/given/i_have_the_file_with_content.rb new file mode 100644 index 0000000..6d82cd5 --- /dev/null +++ b/features/step_definitions/given/i_have_the_file_with_content.rb @@ -0,0 +1,7 @@ +Given /^I have the file "([^"]*)" with the content:$/ do |file, string| + target = File.join(@root, file) + + FileUtils.mkdir_p File.dirname(target) + File.open(target, 'wb') { |fh| fh.print string } +end + diff --git a/features/step_definitions/then/i_should_have_tests_and_failures.rb b/features/step_definitions/then/i_should_have_tests_and_failures.rb new file mode 100644 index 0000000..2bfd045 --- /dev/null +++ b/features/step_definitions/then/i_should_have_tests_and_failures.rb @@ -0,0 +1,7 @@ +Then /^I should have (\d+) tests? and (\d+) failures?$/ do |tests, failures| + parts = @output.lines.to_a.last.strip.split('/') + + parts[0].should == tests + parts[1].should == failures +end + diff --git a/features/step_definitions/when/i_run_flowerbox.rb b/features/step_definitions/when/i_run_flowerbox.rb new file mode 100644 index 0000000..dd1356c --- /dev/null +++ b/features/step_definitions/when/i_run_flowerbox.rb @@ -0,0 +1,10 @@ +When /^I run Flowerbox with "([^"]*)"$/ do |arguments| + command = %{bundle exec bin/flowerbox test #{arguments} --pwd #{@root} 2>&1} + puts command + @output = %x{#{command}} + + puts @output + + raise StandardError.new("Flowerbox failed: #{@output}") if $?.exitstatus != 0 +end + diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000..6febcac --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1,6 @@ +Before do + @root = "tmp/features" + + FileUtils.rm_rf @root +end + diff --git a/flowerbox.gemspec b/flowerbox.gemspec new file mode 100644 index 0000000..42ef5e9 --- /dev/null +++ b/flowerbox.gemspec @@ -0,0 +1,24 @@ +# -*- encoding: utf-8 -*- +require File.expand_path('../lib/flowerbox/version', __FILE__) + +Gem::Specification.new do |gem| + gem.authors = ["John Bintz"] + gem.email = ["john@coswellproductions.com"] + gem.description = %q{TODO: Write a gem description} + gem.summary = %q{TODO: Write a gem summary} + gem.homepage = "" + + gem.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } + gem.files = `git ls-files`.split("\n") + gem.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") + gem.name = "flowerbox" + gem.require_paths = ["lib"] + gem.version = Flowerbox::VERSION + + gem.add_development_dependency 'cucumber' + gem.add_development_dependency 'rspec' + gem.add_development_dependency 'jasmine-core' + + gem.add_dependency 'flowerbox-delivery' + gem.add_dependency 'thor' +end diff --git a/lib/flowerbox.rb b/lib/flowerbox.rb new file mode 100644 index 0000000..c564218 --- /dev/null +++ b/lib/flowerbox.rb @@ -0,0 +1,34 @@ +require "flowerbox/version" + +module Flowerbox + module Runner + autoload :Node, 'flowerbox/runner/node' + end + + class << self + def spec_patterns + @spec_patterns ||= [] + end + + def asset_paths + @asset_paths ||= [] + end + + def test_with(what) + @test_with = what + end + + def load_test_environment(sprockets) + require "flowerbox/test_environment/#{@test_with}" + + Flowerbox.test_environment.inject_into(sprockets) + end + + attr_accessor :test_environment, :bare_coffeescript + + def configure + yield self + end + end +end + diff --git a/lib/flowerbox/jasmine.rb b/lib/flowerbox/jasmine.rb new file mode 100644 index 0000000..e69de29 diff --git a/lib/flowerbox/runner/node.rb b/lib/flowerbox/runner/node.rb new file mode 100644 index 0000000..e27c041 --- /dev/null +++ b/lib/flowerbox/runner/node.rb @@ -0,0 +1,44 @@ +require 'tempfile' + +module Flowerbox + module Runner + class Node + def self.run(files) + new(files).run + end + + def initialize(files) + @files = files + end + + def run + file = Tempfile.new("node") + file.print template + file.close + + system %{node #{file.path}} + + $?.exitstatus + end + + def template + <<-JS +var fs = require('fs'); +var window = this; + +#{template_files.join("\n")} +#{start_test_environment} +JS + end + + def template_files + @files.collect { |file| %{eval(fs.readFileSync('#{file}', 'utf-8'));} } + end + + def start_test_environment + Flowerbox.test_environment.start_for(:node) + end + end + end +end + diff --git a/lib/flowerbox/test_environment/jasmine.rb b/lib/flowerbox/test_environment/jasmine.rb new file mode 100644 index 0000000..57cec70 --- /dev/null +++ b/lib/flowerbox/test_environment/jasmine.rb @@ -0,0 +1,39 @@ +require 'jasmine-core' + +module Flowerbox + module TestEnvironment + class Jasmine + def self.inject_into(sprockets) + sprockets.append_path(::Jasmine::Core.path) + + sprockets.add('jasmine.js') + sprockets.add('jasmine-html.js') + end + + def self.start_for(runner) + <<-JS +jasmine.NodeReporter = function() {} + +jasmine.NodeReporter.prototype.reportSpecResults = function(spec) { + console.log(spec.results().totalCount + '/' + spec.results().failedCount); + + if (spec.results().failedCount == 0) { + process.exit(0); + } else { + process.exit(1); + } +} + +jasmine.NodeReporter.prototype.reportRunnerResults = function(runner) { +} + +jasmine.getEnv().addReporter(new jasmine.NodeReporter()); +jasmine.getEnv().execute(); +JS + end + end + end +end + +Flowerbox.test_environment = Flowerbox::TestEnvironment::Jasmine + diff --git a/lib/flowerbox/version.rb b/lib/flowerbox/version.rb new file mode 100644 index 0000000..516809a --- /dev/null +++ b/lib/flowerbox/version.rb @@ -0,0 +1,3 @@ +module Flowerbox + VERSION = "0.0.1" +end