Permalink
Browse files

Add specs

  • Loading branch information...
1 parent 73a98f1 commit 8fe4cccd236bd05d691482bea577970d28b7decb @ches ches committed Aug 15, 2011
Showing with 287 additions and 2 deletions.
  1. +3 −1 .gitignore
  2. +15 −1 Gemfile
  3. +6 −0 Guardfile
  4. +8 −0 Rakefile
  5. +5 −0 guard-less.gemspec
  6. +229 −0 spec/guard/less_spec.rb
  7. +21 −0 spec/spec_helper.rb
View
4 .gitignore
@@ -1 +1,3 @@
-*.gem
+*.gem
+Gemfile.lock
+
View
16 Gemfile
@@ -1,3 +1,17 @@
source "http://rubygems.org"
-gemspec
+gemspec
+
+gem 'rake'
+
+require 'rbconfig'
+
+if Config::CONFIG['target_os'] =~ /darwin/i
+ gem 'rb-fsevent', '>= 0.4.0'
+ gem 'growl_notify', '~> 0.0.1'
+end
+if Config::CONFIG['target_os'] =~ /linux/i
+ gem 'rb-inotify', '>= 0.8.4'
+ gem 'libnotify', '~> 0.3.0'
+end
+
View
6 Guardfile
@@ -0,0 +1,6 @@
+guard 'rspec', :version => 2 do
+ watch('spec/spec_helper.rb') { 'spec' }
+ watch(%r{spec/.+_spec.rb})
+ watch(%r{lib/(.+).rb}) { |m| "spec/#{ m[1] }_spec.rb" }
+end
+
View
8 Rakefile
@@ -0,0 +1,8 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new(:spec)
+
+task :default => :spec
+
View
5 guard-less.gemspec
@@ -17,6 +17,11 @@ Gem::Specification.new do |s|
s.add_dependency 'guard', '>= 0.2.2'
s.add_dependency 'less', '~> 2.0.5'
+
+ s.add_development_dependency 'bundler', '~> 1.0'
+ s.add_development_dependency 'fakefs', '~> 0.3'
+ s.add_development_dependency 'guard-rspec', '~> 0.4'
+ s.add_development_dependency 'rspec', '~> 2.6'
s.files = Dir.glob('{lib}/**/*') + %w[README.md]
s.require_path = 'lib'
View
229 spec/guard/less_spec.rb
@@ -0,0 +1,229 @@
+require 'spec_helper'
+
+describe Guard::Less do
+ include FakeFS::SpecHelpers
+
+ let(:guard) { Guard::Less.new }
+ let(:watcher) { Guard::Watcher.new(%r{^yes/.+\.less$}) }
+
+ describe '#initialize' do
+ context 'when no options are provided' do
+ it 'enables :all_after_change option' do
+ guard.options[:all_after_change].should be_true
+ end
+
+ it 'enables :all_on_start option' do
+ guard.options[:all_on_start].should be_true
+ end
+
+ it 'sets no :ouput option' do
+ guard.options[:output].should be_nil
+ end
+
+ it 'sets an empty :import_paths option' do
+ guard.options[:import_paths].should be_empty
+ end
+ end
+
+ context 'when providing options' do
+ let(:guard) do
+ Guard::Less.new(nil, {
+ :all_after_change => false,
+ :all_on_start => false,
+ :output => 'public/stylesheets',
+ :import_paths => ['lib/styles']
+ })
+ end
+
+ it 'sets :all_after_change' do
+ guard.options[:all_after_change].should be_false
+ end
+
+ it 'sets :all_on_start' do
+ guard.options[:all_on_start].should be_false
+ end
+
+ it 'sets :output' do
+ guard.options[:output].should eql 'public/stylesheets'
+ end
+
+ it 'sets :import_paths' do
+ guard.options[:import_paths].should eql ['lib/styles']
+ end
+ end
+ end
+
+ describe '.start' do
+ it 'executes run_all if :all_on_start is true' do
+ guard.should_receive(:run_all)
+ guard.start
+ end
+ end
+
+ describe '.run_all' do
+ let(:guard) { Guard::Less.new([watcher]) }
+
+ before do
+ Dir.stub(:glob).and_return ['yes/a.less', 'yes/b.less', 'no/c.less']
+ end
+
+ it 'executes .run passing all watched LESS files' do
+ guard.should_receive(:run).with(['yes/a.less', 'yes/b.less'])
+ guard.run_all
+ end
+ end
+
+ describe '.run_on_change' do
+ it 'executes .run_all if :all_after_change is true' do
+ guard = Guard::Less.new(nil, :all_after_change => true)
+ guard.should_receive(:run_all)
+ guard.run_on_change([])
+ end
+
+ it 'executes .run passing the watched files if :all_after_change is false' do
+ guard = Guard::Less.new(nil, :all_after_change => false)
+ files = ['a.less', 'b.less']
+ guard.should_receive(:run).with(files)
+ guard.run_on_change(files)
+ end
+ end
+
+ describe 'run' do
+ let(:guard) { Guard::Less.new([watcher]) }
+
+ it 'does not compile otherwise matching _partials' do
+ guard.should_not_receive(:compile)
+ guard.run(['yes/_partial.less'])
+ end
+
+ context 'if watcher misconfigured to match CSS' do
+ let(:guard) { Guard::Less.new([Guard::Watcher.new('^yes/.+\.css$')], :output => nil) }
+
+ it 'does not overwrite CSS' do
+ guard.should_not_receive(:compile)
+ ::Guard::UI.should_receive(:info).with(/output would overwrite the original/)
+ guard.run(['yes/a.css'])
+ end
+ end
+
+ context 'when CSS is more recently modified than LESS' do
+ before do
+ write_stub_less_file('yes/a.less')
+ FileUtils.touch('yes/a.css')
+ end
+
+ it 'does not compile' do
+ guard.should_not_receive(:compile)
+ guard.run(['yes/a.less'])
+ end
+
+ it 'informs user of up-to-date skipped files' do
+ ::Guard::UI.should_receive(:info).with(/yes\/a.css is already up-to-date/)
+ guard.run(['yes/a.less'])
+ end
+
+ context 'but LESS file has an import more recently modified than CSS' do
+ before do
+ write_stub_less_file('yes/a.less', import=true)
+ # touch with :mtime option doesn't seem to work?
+ FileUtils.touch(['yes/a.css', 'yes/b.less'])
+ File.utime(Time.now - 5, Time.now - 5, 'yes/a.less')
+ File.utime(Time.now - 3, Time.now - 3, 'yes/a.css')
+ end
+
+ it 'compiles the importing LESS file' do
+ guard.should_receive(:compile)
+ guard.run(['yes/a.less'])
+ end
+ end
+ end
+
+ context 'when CSS is out of date' do
+ before do
+ stub_compilation_needed
+ guard.stub(:compile)
+ end
+
+ it 'compiles matching watched files' do
+ guard.should_receive(:compile).twice
+ guard.run(['yes/a.less', 'no/a.less', 'yes/b.less'])
+ end
+
+ it 'informs user of compiled files' do
+ ::Guard::UI.should_receive(:info).with(/yes\/a.less -> yes\/a.css/)
+ guard.run(['yes/a.less'])
+ end
+ end
+
+ context 'when compiling' do
+ before { stub_compilation_needed }
+
+ it 'produces CSS from LESS' do
+ write_stub_less_file('yes/a.less')
+ guard.run(['yes/a.less'])
+ File.read('yes/a.css').should match(/color: #4D926F;/i)
+ end
+
+ it 'produces CSS in same nested directory hierarchy as LESS' do
+ path = 'yes/we/can/have/nested/directories/a.less'
+ write_stub_less_file(path)
+ guard.run([path])
+ File.should exist('yes/we/can/have/nested/directories/a.css')
+ end
+
+ it 'includes directory of currently processing file in Less parser import paths' do
+ ::Less::Parser.should_receive(:new).with(:paths => ['yes'], :filename => 'yes/a.less')
+ guard.run(['yes/a.less'])
+ end
+
+ context 'using :import_paths option' do
+ let(:guard) do
+ Guard::Less.new([watcher], :import_paths => ['lib/styles'])
+ end
+
+ it 'also includes specified import paths for Less parser' do
+ ::Less::Parser.should_receive(:new).with(:paths => ['yes', 'lib/styles'], :filename => 'yes/a.less')
+ guard.run(['yes/a.less'])
+ end
+ end
+
+ context 'using :output option with custom directory' do
+ let(:watcher) { Guard::Watcher.new(%r{^yes/(.+\.less)$}) }
+ let(:guard) do
+ Guard::Less.new([watcher], :output => 'public/stylesheets')
+ end
+
+ it 'creates directories as needed to match source hierarchy' do
+ path = 'yes/we/can/have/nested/directories/a.less'
+ write_stub_less_file(path)
+ guard.run([path])
+ File.should exist('public/stylesheets/we/can/have/nested/directories/a.css')
+ end
+ end
+ end
+
+ end
+
+ private
+
+ def stub_compilation_needed
+ guard.stub(:mtime).and_return(Time.now - 1)
+ guard.stub(:mtime_including_imports).and_return(Time.now)
+ end
+
+ def write_stub_less_file(path, import=false)
+ FileUtils.mkdir_p(File.dirname(path))
+ File.open(path, 'w') do |out|
+ out.puts <<LESS
+@color: #4D926F;
+
+#header {
+ color: @color;
+}
+LESS
+
+ out << '@import "b";' if import
+ end
+ end
+end
+
View
21 spec/spec_helper.rb
@@ -0,0 +1,21 @@
+require 'rspec'
+require 'fakefs/spec_helpers'
+
+require 'guard/less'
+
+RSpec.configure do |config|
+ config.color_enabled = true
+ config.filter_run :focus => true
+ config.run_all_when_everything_filtered = true
+
+ config.before(:each) do
+ ENV["GUARD_ENV"] = 'test'
+ @project_path = Pathname.new(File.expand_path('../../', __FILE__))
+ end
+
+ config.after(:each) do
+ ENV["GUARD_ENV"] = nil
+ end
+
+end
+

0 comments on commit 8fe4ccc

Please sign in to comment.