Skip to content

Commit

Permalink
Updated extension installer + added spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Dimitrij Denissenko committed Dec 6, 2009
1 parent 819f974 commit 089ec8e
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 17 deletions.
48 changes: 31 additions & 17 deletions lib/retrospectiva/extension_manager/extension_installer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,27 @@
module Retrospectiva
module ExtensionManager
module ExtensionInstaller
include ActiveSupport::Memoizable
extend self

CONFIG_FILE = File.join(RAILS_ROOT, 'config', 'runtime', 'extensions.yml')

delegate :extension_path, :available_extensions, :to => :"Retrospectiva::ExtensionManager"

def install(extension)
return if installed_extension_names.include?(extension.name)

@installed_extension_names << extension.name
returning write_extension_table do
dump_schema
end
end

def uninstall(extension)
return unless installed_extension_names.include?(extension.name)

@installed_extension_names.delete(extension.name)
returning write_extension_table do
dump_schema
end
end

def download(uri)
name = File.basename(uri, '.git').split('.').last
if system("git clone --depth 1 #{uri} #{extension_path(name)}")
Expand All @@ -34,25 +36,41 @@ def download(uri)
end

def installed_extension_names
@installed_extension_names ||= if RAILS_ENV == 'test'
ENV['RETRO_EXT'].to_s.split(/[\s,]+/).reject(&:blank?)
else
YAML.load_configuration(CONFIG_FILE, [])
end
@installed_extension_names ||= read_extension_table
end

def reload
@installed_extension_names = nil
installed_extension_names
end

private

def config_file
Rails.root.join('config', 'runtime', 'extensions.yml')
end

def test_mode?
Rails.env.test?
end

def read_extension_table
if test_mode?
ENV['RETRO_EXT'].to_s.split(/[\s,]+/).reject(&:blank?)
else
YAML.load_configuration(config_file, [])
end
end

def write_extension_table
sanitize!
File.open(CONFIG_FILE, 'w') do |f|
File.open(config_file, 'w') do |f|
YAML.dump(installed_extension_names, f )
end
end

def sanitize!
valid_names = ExtensionManager.available_extensions.map(&:name)
@installed_extension_names = (installed_extension_names & valid_names).uniq
@installed_extension_names = (installed_extension_names & available_extensions.map(&:name)).uniq
end

def dump_schema
Expand All @@ -62,10 +80,6 @@ def dump_schema
end
end

def extension_path(name = nil)
ExtensionManager.extension_path(name)
end

end
end
end
Expand Down
3 changes: 3 additions & 0 deletions spec/fixtures/runtime/extensions.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
- retro_wiki
- agile_pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
require 'spec_helper'

describe Retrospectiva::ExtensionManager::ExtensionInstaller do
SOURCE = File.join(Spec::Runner.configuration.fixture_path, 'runtime', 'extensions.yml')
FIXTURE = File.join(RAILS_ROOT, 'tmp', 'spec', 'extensions.yml')

def installer
Retrospectiva::ExtensionManager::ExtensionInstaller
end

def installed_names
installer.installed_extension_names
end

def mock_extension(name)
mock('Extension', :name => name)
end

before :all do
FileUtils.mkdir_p(File.dirname(FIXTURE))
end

after :all do
installer.reload
end

before do
FileUtils.cp SOURCE, FIXTURE
installer.stub!(:config_file).and_return(FIXTURE)
installer.stub!(:test_mode?).and_return(false)
installer.stub!(:system).and_return(false)
installer.reload
end

after do
FileUtils.rm FIXTURE, :force => true
end

it 'should keep a list of installed extensions' do
installer.installed_extension_names.should == ['retro_wiki', 'agile_pm']
end

describe 'installing extensions' do

it 'should work correctly' do
installed_names.should have(2).items
installer.install(mock_extension('retro_blog'))
installed_names.should have(3).items
installed_names.should include('retro_blog')
end

it 'should only install if extension really exists' do
installed_names.should have(2).items
installer.install(mock_extension('_not_there_'))
installed_names.should have(2).items
end

it 'should only install if not already installed' do
installer.should_not_receive(:write_extension_table)
installer.install(mock_extension('retro_wiki'))
installed_names.should include('retro_wiki')
end

end

describe 'removing extensions' do

it 'should work correctly' do
installed_names.should have(2).items
installer.uninstall(mock_extension('retro_wiki'))
installed_names.should have(1).items
installed_names.should_not include('retro_wiki')
end

it 'should only remove if extension is really installed' do
installer.should_not_receive(:write_extension_table)
installer.uninstall(mock_extension('retro_blog'))
end

end

describe 'downloading extensions' do

it 'should use git to clone extension' do
target = Rails.root.join('extensions', 'openid_auth')
installer.should_receive(:system).
with("git clone --depth 1 git://github.com/dim/retrospectiva.openid_auth.git #{target}").
and_return(false)
installer.download('git://github.com/dim/retrospectiva.openid_auth.git')
end

end

end

0 comments on commit 089ec8e

Please sign in to comment.