Skip to content

Commit

Permalink
Implement basic Guard module
Browse files Browse the repository at this point in the history
  • Loading branch information
textgoeshere committed Sep 22, 2011
1 parent bc3d8a6 commit 69671b1
Show file tree
Hide file tree
Showing 11 changed files with 372 additions and 2 deletions.
1 change: 1 addition & 0 deletions .gitignore
Expand Up @@ -2,3 +2,4 @@
.bundle .bundle
Gemfile.lock Gemfile.lock
pkg/* pkg/*
/.rvmrc
1 change: 1 addition & 0 deletions .travis.yml
@@ -0,0 +1 @@
# travis-ci ftw
6 changes: 6 additions & 0 deletions Guardfile
@@ -0,0 +1,6 @@
guard 'rspec', :version => 2 do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end

8 changes: 7 additions & 1 deletion Rakefile
@@ -1 +1,7 @@
require 'bundler/gem_tasks' require 'bundler'
Bundler::GemHelper.install_tasks

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

task :default => :spec
6 changes: 5 additions & 1 deletion guard-jasmine-node.gemspec
Expand Up @@ -4,7 +4,7 @@ require "guard/jasmine_node/version"


Gem::Specification.new do |s| Gem::Specification.new do |s|
s.name = "guard-jasmine-node" s.name = "guard-jasmine-node"
s.version = Guard::JasmineNode::VERSION s.version = Guard::JasmineNodeVersion::VERSION
s.authors = ["dave@kapoq.com"] s.authors = ["dave@kapoq.com"]
s.email = ["dave@kapoq.com"] s.email = ["dave@kapoq.com"]
s.homepage = "https://github.com/kapoq/guard-jasmine-node" s.homepage = "https://github.com/kapoq/guard-jasmine-node"
Expand All @@ -17,6 +17,10 @@ Gem::Specification.new do |s|


s.add_development_dependency "rspec" s.add_development_dependency "rspec"
s.add_development_dependency "guard-rspec" s.add_development_dependency "guard-rspec"
if RUBY_PLATFORM =~ /linux/
s.add_development_dependency "rb-inotify"
s.add_development_dependency "libnotify"
end


s.files = `git ls-files`.split("\n") s.files = `git ls-files`.split("\n")
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
Expand Down
63 changes: 63 additions & 0 deletions lib/guard/jasmine_node.rb
Expand Up @@ -3,5 +3,68 @@


module Guard module Guard
class JasmineNode < Guard class JasmineNode < Guard
DEFAULT_OPTIONS = {
:jasmine_node_bin => "jasmine-node",
:all_after_pass => true,
:all_on_start => true,
:keep_failed => true
}

autoload :Runner, "guard/jasmine_node/runner"

def initialize(watchers = [], options = {})
super(watchers, DEFAULT_OPTIONS.merge(options))
clear_pass_state
end

def start
run_all if options[:all_on_start]
end

def run_all
outcome = Runner.run(["spec"], { :message => "Running all specs" })
set_pass_state(outcome)
end

def run_on_change(changed_paths = [])
run_paths = if options[:keep_failed]
failing_paths + changed_paths
else
changed_paths
end

outcome = Runner.run(run_paths)
set_pass_state(outcome, run_paths)

if passing?
run_all if options[:all_after_pass]
end
end

def passing?
@passing
end

def failing_paths
@failing_paths
end

private

def clear_pass_state
@passing = true
@failing_paths = []
end

def set_pass_state(passed, run_paths = [])
@passing = passed
if run_paths.any?
@failing_paths = if passing?
@failing_paths - run_paths
else
@failing_paths && run_paths
end
end
end
end end
end end
22 changes: 22 additions & 0 deletions lib/guard/jasmine_node/runner.rb
@@ -0,0 +1,22 @@
require 'guard/ui'

module Guard
class JasmineNode
module Runner
def self.run(paths = [], options = {})
return false if paths.empty?

message = options.fetch(:message, "Running: #{paths.join(' ')}")
::Guard::UI.info(message, :reset => true)

system(jasmine_node_command(paths, options))
end

private

def self.jasmine_node_command(paths = [], options = {})
"#{options[:jasmine_node_bin]} #{paths.join(' ')}"
end
end
end
end
5 changes: 5 additions & 0 deletions lib/guard/jasmine_node/templates/Guardfile
@@ -0,0 +1,5 @@
guard "JasmineNode" do
watch(%r{^spec/.+_spec\.js$})
watch(%r{^lib/(.+)\.js$}) { |m| "spec/lib/#{m[1]}_spec.js" }
watch('spec/spec_helper.js') { "spec" }
end
214 changes: 214 additions & 0 deletions spec/spec/jasmine_node_spec.rb
@@ -0,0 +1,214 @@
require 'spec_helper'

describe Guard::JasmineNode do
let(:guard) { Guard::JasmineNode.new }
let(:runner) { Guard::JasmineNode::Runner }

describe "#initialize" do
context "when no options are given" do
it "sets a default path to jasmine-node bin" do
guard.options[:jasmine_node_bin].should eql "jasmine-node"
end

it "sets a default :all_after_pass option" do
guard.options[:all_after_pass].should be_true
end

it "sets a default :all_on_start option" do
guard.options[:all_on_start].should be_true
end

it "sets a default :keep_failed option" do
guard.options[:keep_failed].should be_true
end

it "is passing" do
guard.should be_passing
end

it "has no failing paths" do
guard.failing_paths.should be_empty
end
end

context "when options are given" do
let(:a_path) { "/foo/bar/jasmine-node" }
let(:guard) { Guard::JasmineNode.new([], {
:jasmine_node_bin => a_path,
:all_on_start => false,
:all_after_pass => false,
:keep_failed => false
}) }

it "sets the path to jasmine-node bin" do
guard.options[:jasmine_node_bin].should eql a_path
end

it "sets the :all_after_pass option" do
guard.options[:all_after_pass].should be_false
end

it "sets the :all_on_start option" do
guard.options[:all_on_start].should be_false
end

it "sets the :keep_failed option" do
guard.options[:keep_failed].should be_false
end
end
end

describe "#start" do
context "when :all_on_start is true" do
it "runs all" do
guard.should_receive(:run_all)
guard.start
end
end

context "when :all_on_start is false" do
let(:guard) { Guard::JasmineNode.new([], { :all_on_start => false }) }

it "does not run all" do
guard.should_not_receive(:run_all)
guard.start
end
end
end

describe "#reload" do
end

describe "#stop" do
end

describe "#run_all" do
it "runs the runner with the spec dir" do
runner.should_receive(:run).with(["spec"], anything)
guard.run_all
end

it "tells the message to the runner" do
runner.should_receive(:run).with(anything, hash_including(:message => "Running all specs"))
guard.run_all
end

context "when specs pass" do
before do
runner.stub(:run => true)
guard.run_all
end

it "is passing" do
guard.should be_passing
end

it "has no failed paths" do
guard.failing_paths.should be_empty
end
end

context "when specs fail" do
before do
runner.stub(:run => false)
end

it "is not passing" do
guard.run_all
guard.should_not be_passing
end

it "keeps previously failing specs" do
failing_paths = %w(foo bar)
guard.run_on_change(failing_paths)
guard.run_all
guard.failing_paths.should eql failing_paths
end
end
end

describe "#run_on_change" do
it "runs the runner with paths" do
runner.should_receive(:run).with(["/a/path"])
guard.run_on_change(["/a/path"])
end

context "when specs pass" do
before do
runner.stub(:run => true)
end

it "is passing" do
guard.run_on_change
guard.should be_passing
end

context "and :all_after_pass is true" do
before do
guard.options[:all_after_pass] = true
end

it "runs all" do
guard.should_receive(:run_all)
guard.run_on_change
end
end

context "and :all_after_pass is false" do
before do
guard.options[:all_after_pass] = false
end

context "if :all_after_pass is true" do
it "does not run all" do
guard.should_not_receive(:run_all)
guard.run_on_change
end
end
end
end

context "when specs fail" do
before do
runner.stub(:run => false)
end

it "is not passing" do
guard.run_on_change
guard.should_not be_passing
end
end

context "when there are failing paths" do
let(:failing_paths) { %w(foo/bar zip/zap) }
let(:changed_paths) { %w(aaa/bbb ccc/ddd) }
let(:all_paths) { failing_paths + changed_paths }

before do
guard.stub(:failing_paths => failing_paths)
end

context "and :keep_failed is true" do
before do
guard.options[:keep_failed] = true
end

it "runs the runner failing paths and the changed paths" do
runner.should_receive(:run).with(all_paths)
guard.run_on_change(changed_paths)
end
end

context "and :keep_failed is false" do
before do
guard.options[:keep_failed] = false
end

it "runs the runner with only the changed paths" do
runner.should_receive(:run).with(changed_paths)
guard.run_on_change(changed_paths)
end
end
end
end
end
31 changes: 31 additions & 0 deletions spec/spec/runner_spec.rb
@@ -0,0 +1,31 @@
require 'spec_helper'

describe Guard::JasmineNode::Runner do
let(:runner) { Guard::JasmineNode::Runner }

describe ".run" do
context "when passed no paths" do
it "returns false" do
runner.run.should be_false
end
end

context "when passed paths" do
let(:some_paths) { %w(/foo/bar /zip/zap) }

context "when message option is given" do
it "outputs message" do
Guard::UI.should_receive(:info).with("hello", anything)
runner.run(some_paths, :message => "hello")
end
end

context "when no message option is given" do
it "outputs default message" do
Guard::UI.should_receive(:info).with("Running: /foo/bar /zip/zap", anything)
runner.run(some_paths)
end
end
end
end
end

0 comments on commit 69671b1

Please sign in to comment.