Skip to content

Commit

Permalink
Create watchers from Compass configuration.
Browse files Browse the repository at this point in the history
  • Loading branch information
oliamb committed Oct 31, 2010
1 parent 21c2e7b commit ca1ffb7
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 21 deletions.
20 changes: 18 additions & 2 deletions lib/guard/compass.rb
@@ -1,5 +1,6 @@
require 'guard'
require 'guard/guard'
require 'guard/watcher'
require 'guard/reporter'

require 'compass'
Expand All @@ -23,8 +24,9 @@ def initialize(watchers = [], options = {})

# Compile all the sass|scss stylesheets
def start
create_updater
UI.info "Guard::Compass is watching at your stylesheets."
load_compass_configuration
create_updater
true
end

Expand All @@ -35,6 +37,7 @@ def stop

# Reload the configuration
def reload
load_compass_configuration
create_updater
true
end
Expand All @@ -59,7 +62,8 @@ def perform
end
end

def create_updater
def load_compass_configuration
::Compass.default_configuration
if(options[:configuration_file])
filepath = Pathname.new(options[:configuration_file])
if(filepath.relative?)
Expand All @@ -72,6 +76,18 @@ def create_updater
reporter.failure "Compass configuration file not found: " + filepath + "\nPlease check Guard configuration."
end
end

::Compass.configuration.sass_dir ||= "#{options[:workdir]}/src"
watchers.clear
watchers.push Watcher.new("^#{ File.expand_path(::Compass.configuration.sass_dir, options[:workdir]) }/.*")
if(options[:configuration_file])
watchers.push Watcher.new("^#{options[:configuration_file]}$")
elsif conf_file = ::Compass.detect_configuration_file(options[:workdir])
watchers.push Watcher.new("^#{conf_file}$")
end
end

def create_updater
@updater = ::Compass::Commands::UpdateProject.new(@options[:workdir] , @options)
valid_sass_path?
end
Expand Down
105 changes: 86 additions & 19 deletions spec/guard/compass_spec.rb
Expand Up @@ -2,27 +2,43 @@
require 'spec_helper'
require 'guard/compass'

def create_fixture(name)
FileUtils.mkdir(TMP_PATH) if ! File.exists? TMP_PATH
@project_path = "#{TMP_PATH}/#{name}"
FileUtils.cp_r "#{FIXTURES_PATH}/#{name}", TMP_PATH
subject.options.merge!(:workdir => @project_path)
end

def remove_fixtures
FileUtils.rm_rf(TMP_PATH)
end

describe Guard::Compass do
subject { Guard::Compass.new }

it "has a reporter" do
subject.reporter.should_not be_nil
end

it "might be initialized with options" do
g = Guard::Compass.new([], :workdir => 'test', :configuration_file => 'test_also')
g.options[:workdir].should == 'test'
g.options[:configuration_file].should == 'test_also'
after :each do
::Compass.reset_configuration!
end

describe "In a standard project" do
subject { Guard::Compass.new }

it "has a reporter" do
subject.reporter.should_not be_nil
end

it "might be initialized with options" do
g = Guard::Compass.new([], :workdir => 'test', :configuration_file => 'test_also')
g.options[:workdir].should == 'test'
g.options[:configuration_file].should == 'test_also'
end

before :each do
create_fixture(:compass_prj)
end

after :each do
remove_fixtures
::Compass.reset_configuration!
end

describe "start" do
Expand Down Expand Up @@ -52,6 +68,7 @@

after :each do
subject.stop
::Compass.reset_configuration!
end

describe "the updater" do
Expand Down Expand Up @@ -91,13 +108,16 @@
end

describe "with custom configuration and locations" do
subject { Guard::Compass.new }

before :each do
create_fixture(:custom_config_file)
end

after :each do
remove_fixtures
subject.stop
::Compass.reset_configuration!
end

it "configure Compass correctly with an absolute path" do
Expand All @@ -123,6 +143,8 @@
end

describe "without config file" do
subject { Guard::Compass.new }

before :each do
create_fixture(:no_config_file)
subject.start
Expand All @@ -131,6 +153,7 @@
after :each do
remove_fixtures
subject.stop
::Compass.reset_configuration!
end

describe "run_on_change" do
Expand All @@ -144,6 +167,8 @@
end

describe "with a bad directory configuration" do
subject { Guard::Compass.new }

before :each do
create_fixture(:bad_src_directory)
subject.reporter.stub!(:failure).with("Sass files src directory not found: #{@project_path}/src\nPlease check your Compass configuration.")
Expand All @@ -153,6 +178,7 @@
after :each do
remove_fixtures
subject.stop
::Compass.reset_configuration!
end

it "rebuilds failed to build sass" do
Expand All @@ -173,6 +199,7 @@
after :each do
subject.stop
remove_fixtures
::Compass.reset_configuration!
end

it "reports an error" do
Expand All @@ -184,15 +211,55 @@

end

private
def create_fixture(name)
FileUtils.mkdir(TMP_PATH) if ! File.exists? TMP_PATH
@project_path = "#{TMP_PATH}/#{name}"
FileUtils.cp_r "#{FIXTURES_PATH}/#{name}", TMP_PATH
subject.options.merge!(:workdir => @project_path)
end

def remove_fixtures
FileUtils.rm_rf(TMP_PATH)
describe "Watchers creation" do

after :each do
remove_fixtures
::Compass.reset_configuration!
end

describe "in standard project" do
subject {
Guard::Compass.new([], :workdir => "#{TMP_PATH}/compass_prj")
}

before :each do
create_fixture :compass_prj
end

it "should have some watchers" do
subject.options.should eql :workdir => "#{TMP_PATH}/compass_prj"
subject.start
subject.watchers.size.should(eql(2), subject.watchers.inspect)
subject.watchers.first.pattern.should == "^#{@project_path}/src/.*"
end
end

describe "in customized project" do
subject {
Guard::Compass.new([], :workdir => "#{TMP_PATH}/custom_config_file", :configuration_file => 'another_config_location/config.rb')
}

before :each do
create_fixture :custom_config_file
end

it "should have some watchers" do
subject.start
subject.watchers.size.should(eql(2), subject.watchers.inspect)
subject.watchers.first.pattern.should == "^#{@project_path}/another_src_location/.*"
end
end
end
end

def create_fixture(name)
FileUtils.mkdir(TMP_PATH) if ! File.exists? TMP_PATH
@project_path = "#{TMP_PATH}/#{name}"
FileUtils.cp_r "#{FIXTURES_PATH}/#{name}", TMP_PATH
subject.options.merge!(:workdir => @project_path)
end

def remove_fixtures
FileUtils.rm_rf(TMP_PATH)
end

0 comments on commit ca1ffb7

Please sign in to comment.