Skip to content

Commit

Permalink
added rspec test for InterfaceLift::Installer
Browse files Browse the repository at this point in the history
  • Loading branch information
michel committed Oct 21, 2009
1 parent 8e19c8f commit 3838cd5
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 52 deletions.
3 changes: 2 additions & 1 deletion bin/interfacelift
Expand Up @@ -32,7 +32,8 @@ command :list do |c|
c.description = 'shows a list of all available themes' c.description = 'shows a list of all available themes'
c.action do |args, options| c.action do |args, options|
begin begin
InterfaceLift::Installer.list manager = InterfaceLift::ThemeManager.new
manager.available_themes
rescue Exception => e rescue Exception => e
puts e puts e
end end
Expand Down
46 changes: 46 additions & 0 deletions lib/installer.rb
@@ -0,0 +1,46 @@
module InterfaceLift
class Installer
attr_reader :path, :theme, :theme_path
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
SHARED = "#{APP_ROOT}/lib/templates/shared"


def initialize(path,theme)
@path = path
@theme = theme
@theme_path = "#{APP_ROOT}/lib/templates/#{theme}"

raise "Given path does not contain a rails app." unless File.directory? "#{@path}/public"
raise "Theme #{@theme} is not available." unless File.directory? @theme_path
end

def install!
install_images
install_stylesheets
install_layouts
install_javascript
install_shared
end

private
def install_images
FileUtils.cp_r "#{@theme_path}/images","#{path}/public" if File.directory? "#{@theme_path}/images"
end

def install_stylesheets
FileUtils.cp_r "#{@theme_path}/stylesheets","#{path}/public" if File.directory? "#{@theme_path}/stylesheets"
end

def install_layouts
FileUtils.cp_r "#{@theme_path}/layouts","#{path}/app/views" if File.directory? "#{@theme_path}/layouts"
end

def install_javascript
FileUtils.cp_r "#{@theme_path}/javascripts","#{path}/public" if File.directory? "#{@theme_path}/javascripts"
end

def install_shared
FileUtils.cp_r "#{SHARED}/icons","#{path}/public/images/" if File.directory? "#{@theme_path}/icons"
end
end
end
56 changes: 5 additions & 51 deletions lib/interfacelift.rb
@@ -1,52 +1,6 @@
module InterfaceLift module InterfaceLift
class Installer VERSION = "0.0.1"
attr_reader :path, :theme, :theme_path end
APP_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
SHARED = "#{APP_ROOT}/lib/templates/shared" require "installer"

require "theme_manager"

def initialize(path,theme)
@path = path
@theme = theme
raise "Not a rails app" unless File.directory? "#{path}/public"
@theme_path = "#{APP_ROOT}/lib/templates/#{theme}"
raise "Theme #{theme} not found" unless File.directory? theme_path
end


def self.list
Dir.foreach("#{APP_ROOT}/lib/templates") do |t|
puts t unless t == "shared" || t == ".." || t == "." || t == ".DS_Store"
end
end

def install!
install_images
install_stylesheets
install_layouts
install_javascript
install_shared
end

def install_images
FileUtils.cp_r "#{@theme_path}/images","#{path}/public" if File.directory? "#{@theme_path}/images"
end

def install_stylesheets
FileUtils.cp_r "#{@theme_path}/stylesheets","#{path}/public" if File.directory? "#{@theme_path}/stylesheets"
end

def install_layouts
FileUtils.cp_r "#{@theme_path}/layouts","#{path}/app/views" if File.directory? "#{@theme_path}/layouts"
end

def install_javascript
FileUtils.cp_r "#{@theme_path}/javascripts","#{path}/public" if File.directory? "#{@theme_path}/javascripts"
end

def install_shared
FileUtils.cp_r "#{SHARED}/icons","#{path}/public/images/" if File.directory? "#{@theme_path}/icons"
end

end
end
9 changes: 9 additions & 0 deletions lib/theme_manager.rb
@@ -0,0 +1,9 @@
module InterfaceLift
class ThemeManager
def available_themes
Dir.foreach("#{APP_ROOT}/lib/templates") do |t|
puts t unless t == "shared" || t == ".." || t == "." || t == ".DS_Store"
end
end
end
end
74 changes: 74 additions & 0 deletions spec/installer_spec.rb
@@ -0,0 +1,74 @@
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
include InterfaceLift
describe InterfaceLift::Installer do

before(:each) do
@path = "/tmp"
@theme = "test_theme"
@theme_path = "#{GEM_ROOT}/lib/templates/#{@theme}"
end

def valid_arguments
File.should_receive(:directory?).with("#{@path}/public").and_return(true)
File.should_receive(:directory?).with(@theme_path).and_return(true)
end

describe "Initialization" do

it "Should initialize with a theme name and a path" do
valid_arguments
installer = Installer.new(@path,@theme)
installer.theme.should == @theme
installer.path.should == @path
end

it "Should raise an error when there the theme is not available" do
File.should_receive(:directory?).with("#{@path}/public").and_return(true)
File.should_receive(:directory?).with(@theme_path).and_return(false)
lambda { Installer.new(@path,@theme) }.should raise_error("Theme #{@theme} is not available.")
end


it "Should raise an error when the given path does not contain a rails app " do
File.should_receive(:directory?).with("#{@path}/public").and_return(false)
lambda { Installer.new(@path,@theme) }.should raise_error("Given path does not contain a rails app.")
end
end

describe "Installing a theme" do
before(:each) do
#don't copy files over!
valid_arguments
@installer = Installer.new(@path,@theme)
File.stub!(:directory?).and_return(true)
FileUtils.stub!(:cp_r)
end

after(:each) do
@installer.install!
end

it "Should copy over existing images to the RAILS_ROOT/public folder" do
FileUtils.should_receive(:cp_r).with("#{@theme_path}/images","#{@path}/public")
end

it "Should copy over existing stylesheets to the RAILS_ROOT/public folder" do
FileUtils.should_receive(:cp_r).with("#{@theme_path}/stylesheets","#{@path}/public")
end

it "Should copy over templates to the RAILS_ROOT/app/views/layout folder" do
FileUtils.should_receive(:cp_r).with("#{@theme_path}/layouts","#{@path}/app/views")
end

it "Should copy over javascripts to the RAILS_ROOT/public folder" do
FileUtils.should_receive(:cp_r).with("#{@theme_path}/javascripts","#{@path}/public")
end

it "Should copy over shared resources to the RAILS_ROOT/public folder" do
FileUtils.should_receive(:cp_r).with("#{GEM_ROOT}/lib/templates/shared/icons","#{@path}/public/images/")
end

end


end
15 changes: 15 additions & 0 deletions spec/interfacelift_spec.rb
@@ -0,0 +1,15 @@
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')

describe InterfaceLift do
it "Should have a version" do
InterfaceLift::VERSION.should_not be_nil
end

it "Should have an Installer class" do
InterfaceLift::Installer.should_not be_nil
end

it "Should have an Theme_manager class" do
InterfaceLift::ThemeManager.should_not be_nil
end
end
6 changes: 6 additions & 0 deletions spec/spec.opts
@@ -0,0 +1,6 @@
--colour
--format
profile
--timeout
20
--diff
9 changes: 9 additions & 0 deletions spec/spec_helper.rb
@@ -0,0 +1,9 @@
SPEC_ROOT = File.expand_path(File.dirname(__FILE__))
$LOAD_PATH << SPEC_ROOT
GEM_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))

$LOAD_PATH << "#{GEM_ROOT}/lib"
require "interfacelift"

#stubbing and mocking
require 'mocha'

0 comments on commit 3838cd5

Please sign in to comment.