Skip to content

Instantly share code, notes, and snippets.

@thesp0nge
Created March 26, 2012 15:11
Show Gist options
  • Select an option

  • Save thesp0nge/2205758 to your computer and use it in GitHub Desktop.

Select an option

Save thesp0nge/2205758 to your computer and use it in GitHub Desktop.
This rspec doesn't help too much in debugging what's going wrong in Palco::Base. There are too few stories with too much matchers contained. Let's use KISS to break it down.
# Public: the base palco skelethon builder.
#
# Ideally it will never called directly but it will be subclassed by
# Palco::Extension and Palco::Application
#
# Examples:
#
# base = Palco::Base.new('a_test', [{:name=>'README', :file=>true}, {:name=>'lib', :file=>false}])
# base.generate
#
module Palco
class Base
# Public: creates a new base palco instance.
#
# The idea is that specialized classes extending Palco::Base will be aware
# about the list of files and directories to be created, meanwhile the
# Palco::Base takes this list without any clue about their meaning.
#
# name - the name of the Sinatra project
# items_list - an array containing a list of hashes with name of the file to
# be created and a second param telling the code code it the item is either
# a file or a directory.
#
# Example
#
# base = Palco::Base.new('test_one',[{:name=>'README', :file=>true}, {:name=>'lib', :file=>false}])
# Returns
# A new Palco::Base object
attr_reader :project_name
attr_reader :items_list
attr_reader :valid
attr_reader :generated
def initialize(name=nil, items_list=[])
@project_name = name
@items_list = items_list
@generated = false
if @project_name.nil? or @items_list.size==0
@valid = false
else
@valid = true
end
end
# Public: generates the skelethon
#
# Example
# base = Palco::Base.new('test_one',[{:name=>'README', :file=>true}, {:name=>'lib', :file=>false}])
# base.generate
#
# Returns
# True if every item in the item list has been created of false otherwise.
#
def generate
begin
root_dir = Dir.mkdir(@project_name, 0700)
Dir.chdir(@project_name)
@items_list.each do |item|
f= item[:file]
if f
File.touch(File.join(@project_name), item[:name])
else
Dir.mkdir(item[:name])
end
end
@generated = true
return true
rescue SystemCallError
return false
end
end
# Public: destroy the skelethon removing all files and all directories.
# Also the base directory has been removed as well.
#
# Please note that you're not supposed to call destroy for a non generated
# skelethon. The method returns false in this case.
#
# Example
# base = Palco::Base.new('test_one',[{:name=>'README', :file=>true}, {:name=>'lib', :file=>false}])
# base.generate
# base.destroy
#
# Returns
# True if all items in the item list has been removed or false otherwise.
#
def destroy
true
end
def valid?
return self.valid
end
def generate?
return self.generate
end
end
end
require 'spec_helper'
describe "Palco::Base package " do
before(:all) do
@base = Palco::Base.new('test_one', [{:name=>'README', :file=>true}, {:name=>'a_directory', :file=>false}])
end
it "creates a project named 'test_one'" do
@base.project_name.should == "test_one"
@base.valid?.should be_true
@base.generate.should be_true
File.directory?("test_one").should be_true
end
it "creates a README file in 'test_one' directory" do
File.exists?("test_one/README").should be_true
end
it "creates a directory called 'a_directory' inside 'test_one'" do
File.directory?("test_one/a_directory").should be_true
end
it "removes all if destroy method has been called" do
@base.destroy
@base.valid?.should be_false
File.directory?("test_one").should be_false
File.exists?("test_one/README").should be_false
File.directory?("test_one/a_directory").should be_false
end
it "cannot remove a skelethon that it has not been generated before" do
@base = Palco::Base.new('test_two', [{:name=>'README', :file=>true}, {:name=>'a_directory', :file=>false}])
File.directory?("test_one").should be_false
File.exists?("test_one/README").should be_false
File.directory?("test_one/a_directory").should be_false
@base.destroy.should be_false
end
it "must provide an handy generated? handler telling that everything is ok" do
@base = Palco::Base.new('test_three', [{:name=>'README', :file=>true}, {:name=>'a_directory', :file=>false}])
@base.generated?.should be_false
@base.generate
File.directory?("test_one").should be_true
File.exists?("test_one/README").should be_true
File.directory?("test_one/a_directory").should be_true
@base.generated?.should be_true
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment