Skip to content

Commit

Permalink
Improved tests for AbstractRepository cache and instantiation
Browse files Browse the repository at this point in the history
  • Loading branch information
Luca Guidi committed Jul 10, 2008
1 parent 68ce881 commit 436d1dd
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 16 deletions.
20 changes: 9 additions & 11 deletions lib/sashimi/repositories/abstract_repository.rb
Expand Up @@ -5,7 +5,7 @@ def initialize(plugin_name, message = nil)
end

def to_s
@message || @plugin_name + " isn't in the local repository."
@message || @plugin_name.to_s + " isn't in the local repository."
end
end

Expand Down Expand Up @@ -46,13 +46,11 @@ def copy_plugin_and_remove_hidden_folders

class << self
def instantiate_repository(plugin)
with_path local_repository_path do
unless plugin.name.nil?
instantiate_repository_by_cache(plugin)
else
instantiate_repository_by_url(plugin)
end.new(plugin)
end
unless plugin.name.nil?
instantiate_repository_by_cache(plugin)
else
instantiate_repository_by_url(plugin)
end.new(plugin)
end

# Return all installed plugin names and summary, formatted for stdout.
Expand Down Expand Up @@ -275,9 +273,9 @@ def remove_from_cache
# Write all the plugins into the cache
def write_to_cache(plugins)
with_path local_repository_path do
FileUtils.mv(cache_file, "#{cache_file}-backup")
File.open(cache_file, 'w'){|f| f.write(plugins.to_yaml)}
FileUtils.rm("#{cache_file}-backup")
File.atomic_write(cache_file) do |file|
file.write(plugins.to_yaml)
end
end
end

Expand Down
26 changes: 21 additions & 5 deletions test/test_helper.rb
Expand Up @@ -85,10 +85,12 @@ def create_plugin_directories
end

def create_plugin_directory(plugin, about = true)
FileUtils.mkdir(plugin) unless File.exists?(plugin)
File.open(File.join(plugin, 'about.yml'), 'w+') do |f|
f.write({'summary' => "Plugin summary"}.to_yaml)
end if about
with_path plugins_path do
FileUtils.mkdir(plugin) unless File.exists?(plugin)
File.open(File.join(plugin, 'about.yml'), 'w+') do |f|
f.write({'summary' => "Plugin summary"}.to_yaml)
end if about
end
end

def create_rails_app
Expand All @@ -100,7 +102,9 @@ def prepare_installation
end

def create_cache_file
File.open(cache_file, 'w+'){|f| f.write(cached_plugins.to_yaml)}
with_path plugins_path do
File.open(cache_file, 'w+'){|f| f.write(cached_plugins.to_yaml)}
end
end

def with_local_repository_path(&block)
Expand All @@ -117,12 +121,24 @@ def with_path(path, &block)
FileUtils.cd(old_path)
end
end

def with_clear_cache(&block)
AbstractRepository.expire_cache
yield
create_cache_file
AbstractRepository.expire_cache
end
end

module Sashimi
class AbstractRepository
@@plugins_path = 'sashimi_test/.rails/plugins'
cattr_accessor :plugins_path

def self.expire_cache #:nodoc:
@@cache_content = nil
end

public :add_to_cache, :cache_content, :cache_file,
:copy_plugin_to_rails_app, :local_repository_path, :path_to_rails_app,
:prepare_installation, :remove_from_cache, :rails_plugins_path,
Expand Down
116 changes: 116 additions & 0 deletions test/unit/repositories/abstract_repository_test.rb
Expand Up @@ -20,6 +20,22 @@ def test_plugins_names
assert_equal cached_plugins.keys.sort, repository.class.plugins_names
end

### INSTANTIATE

def test_instantiate_repository
assert_kind_of GitRepository, repository.class.instantiate_repository(plugin)
assert_kind_of GitRepository, repository.class.instantiate_repository(create_plugin('sashimi', plugin.url))
end

def test_should_instantiate_repository_by_url
assert_equal SvnRepository, AbstractRepository.instantiate_repository_by_url(create_plugin(nil, 'http://svn.com'))
assert_equal GitRepository, AbstractRepository.instantiate_repository_by_url(plugin)
end

def test_should_instantiate_repository_by_cache
assert repository.class.instantiate_repository_by_cache(create_plugin('sashimi', plugin.url))
end

### COMMANDS

# ADD
Expand Down Expand Up @@ -109,6 +125,18 @@ def test_local_repository_path
expected = [ repository.class.find_home, repository.class.plugins_path ].to_path
assert_equal expected, repository.local_repository_path
end

def test_find_home
flunk
end

def test_with_path
old_path = Dir.pwd
repository.with_path 'test' do
assert_equal [old_path, 'test'].to_path, Dir.pwd
end
assert_equal old_path, Dir.pwd
end
end

def test_path_to_rails_app
Expand All @@ -132,4 +160,92 @@ def test_plugin_path
expected = [ plugins_path, repository.plugin.name ].to_path
assert_equal expected, repository.plugin_path
end

### FILES

def test_copy_plugin_and_remove_hidden_folders
flunk
end

def test_update_rails_plugins
flunk
end

def test_update_unversioned_rails_plugins
flunk
end

def test_update_versioned_rails_plugins
flunk
end

def test_files_scheduled_for_add
flunk
end

def test_files_scheduled_for_remove
flunk
end

def test_remove_temp_folder
flunk
end

def test_prepare_installation
flunk
end

def test_copy_plugin_to_rails_app
flunk
end

def test_remove_hidden_folders
flunk
end

### CACHE

def test_cache_content
assert_equal cached_plugins, repository.class.cache_content
end

def test_should_add_a_new_plugin_to_cache
with_clear_cache do
create_plugin_directory('brand_new')
plugin = create_plugin(nil, 'git://github.com/jodosha/brand_new.git')
repository.add_to_cache(plugin)
assert_equal cached_plugins.merge(plugin.to_hash), repository.class.cache_content
end
end

def test_should_merge_an_existent_plugin_into_cache
with_clear_cache do
repository.add_to_cache(plugin)
assert_equal cached_plugins.merge(plugin.to_hash), repository.class.cache_content
end
end

def test_remove_from_cache
with_clear_cache do
repository.remove_from_cache
assert_not repository.class.plugins_names.include?(plugin.name)
end
end

def test_write_to_cache
with_clear_cache do
repository.write_to_cache(plugin.to_hash)
assert_equal plugin.to_hash, repository.class.cache_content
end
end

### OTHER

def test_about
flunk
end

def test_run_install_hook
flunk
end
end

0 comments on commit 436d1dd

Please sign in to comment.