Skip to content

Commit

Permalink
Added a few basic specs, mostly to work out how to go about it.
Browse files Browse the repository at this point in the history
  • Loading branch information
notahat committed Jun 24, 2008
1 parent 30a4d05 commit 40d2556
Show file tree
Hide file tree
Showing 11 changed files with 188 additions and 4 deletions.
1 change: 0 additions & 1 deletion README.rdoc
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ Giki (pronounced "geeky") is a wiki built on Git (and Rails).
Each project is stored as a directory within db/projects/#{RAILS_ENV}, and Each project is stored as a directory within db/projects/#{RAILS_ENV}, and
is simply a git repository containing a *.markdown file for each page. is simply a git repository containing a *.markdown file for each page.



== Requirements == Requirements


- Rails 2.1 - Rails 2.1
Expand Down
4 changes: 4 additions & 0 deletions script/spec
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/usr/bin/env ruby
$LOAD_PATH.unshift(File.expand_path(File.dirname(__FILE__) + "/../vendor/plugins/rspec/lib"))
require 'spec'
exit ::Spec::Runner::CommandLine.run(::Spec::Runner::OptionParser.parse(ARGV, STDERR, STDOUT))
116 changes: 116 additions & 0 deletions script/spec_server
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,116 @@
#!/usr/bin/env ruby
$LOAD_PATH.unshift File.dirname(__FILE__) + '/../vendor/plugins/rspec/lib' # For rspec installed as plugin
require 'rubygems'
require 'drb/drb'
require 'rbconfig'
require 'spec'
require 'optparse'

# This is based on Florian Weber's TDDMate
module Spec
module Runner
class RailsSpecServer
def run(argv, stderr, stdout)
$stdout = stdout
$stderr = stderr

base = ActiveRecord::Base
def base.clear_reloadable_connections!
active_connections.each do |name, conn|
if conn.requires_reloading?
conn.disconnect!
active_connections.delete(name)
end
end
end

if ActionController.const_defined?(:Dispatcher)
dispatcher = ::ActionController::Dispatcher.new($stdout)
dispatcher.cleanup_application
elsif ::Dispatcher.respond_to?(:reset_application!)
::Dispatcher.reset_application!
else
raise "Application reloading failed"
end
if Object.const_defined?(:Fixtures) && Fixtures.respond_to?(:reset_cache)
Fixtures.reset_cache
end

::Dependencies.mechanism = :load
require_dependency('application.rb') unless Object.const_defined?(:ApplicationController)
load File.dirname(__FILE__) + '/../spec/spec_helper.rb'

if in_memory_database?
load "#{RAILS_ROOT}/db/schema.rb" # use db agnostic schema by default
ActiveRecord::Migrator.up('db/migrate') # use migrations
end

::Spec::Runner::CommandLine.run(
::Spec::Runner::OptionParser.parse(
argv,
$stderr,
$stdout
)
)
end

def in_memory_database?
ENV["RAILS_ENV"] == "test" and
::ActiveRecord::Base.connection.class.to_s == "ActiveRecord::ConnectionAdapters::SQLite3Adapter" and
::Rails::Configuration.new.database_configuration['test']['database'] == ':memory:'
end
end
end
end
puts "Loading Rails environment"

ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'dispatcher'

def restart_test_server
puts "restarting"
config = ::Config::CONFIG
ruby = File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT']
command_line = [ruby, $0, ARGV].flatten.join(' ')
exec(command_line)
end

def daemonize(pid_file = nil)
return yield if $DEBUG
pid = Process.fork{
Process.setsid
Dir.chdir(RAILS_ROOT)
trap("SIGINT"){ exit! 0 }
trap("SIGTERM"){ exit! 0 }
trap("SIGHUP"){ restart_test_server }
File.open("/dev/null"){|f|
STDERR.reopen f
STDIN.reopen f
STDOUT.reopen f
}
yield
}
puts "spec_server launched. (PID: %d)" % pid
File.open(pid_file,"w"){|f| f.puts pid } if pid_file
exit! 0
end

options = Hash.new
opts = OptionParser.new
opts.on("-d", "--daemon"){|v| options[:daemon] = true }
opts.on("-p", "--pid PIDFILE"){|v| options[:pid] = v }
opts.parse!(ARGV)

puts "Ready"
exec_server = lambda {
trap("USR2") { restart_test_server } if Signal.list.has_key?("USR2")
DRb.start_service("druby://127.0.0.1:8989", Spec::Runner::RailsSpecServer.new)
DRb.thread.join
}

if options[:daemon]
daemonize(options[:pid], &exec_server)
else
exec_server.call
end
20 changes: 20 additions & 0 deletions spec/models/page_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,20 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Page do
before do
clear_projects
create_test_project
end

it "should create a page" do
create_test_page
@project.pages.all.length.should == 1
@page.path.should == "#{Project.projects_root}/test_project/test_page.markdown"
File.exist?(@page.path).should be_true
end

def create_test_page
@page = @project.pages.build(:name => "test_page")
@page.save
end
end
15 changes: 15 additions & 0 deletions spec/models/project_spec.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,15 @@
require File.dirname(__FILE__) + '/../spec_helper'

describe Project do
before do
clear_projects
end

it "should create a project" do
create_test_project
Project.all.length.should == 1
@project.path.should == "#{Project.projects_root}/test_project"
File.exist?(@project.path).should be_true
File.exist?("#{@project.path}/.git").should be_true
end
end
2 changes: 2 additions & 0 deletions spec/rcov.opts
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,2 @@
--exclude "spec/*,gems/*"
--rails
4 changes: 4 additions & 0 deletions spec/spec.opts
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
--colour
--format progress
--loadby mtime
--reverse
17 changes: 17 additions & 0 deletions spec/spec_helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,17 @@
# This file is copied to ~/spec when you run 'ruby script/generate rspec'
# from the project root directory.
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'spec'
require 'spec/rails'

Spec::Runner.configure do |config|
def clear_projects
`rm -rf #{Project.projects_root}`
end

def create_test_project
@project = Project.new(:name => "test_project")
@project.save
end
end
4 changes: 4 additions & 0 deletions stories/all.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,4 @@
dir = File.dirname(__FILE__)
Dir[File.expand_path("#{dir}/**/*.rb")].uniq.each do |file|
require file
end
3 changes: 3 additions & 0 deletions stories/helper.rb
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,3 @@
ENV["RAILS_ENV"] = "test"
require File.expand_path(File.dirname(__FILE__) + "/../config/environment")
require 'spec/rails/story_adapter'
6 changes: 3 additions & 3 deletions test/test_helper.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -19,20 +19,20 @@ class Test::Unit::TestCase
# The only drawback to using transactional fixtures is when you actually # The only drawback to using transactional fixtures is when you actually
# need to test transactions. Since your test is bracketed by a transaction, # need to test transactions. Since your test is bracketed by a transaction,
# any transactions started in your code will be automatically rolled back. # any transactions started in your code will be automatically rolled back.
self.use_transactional_fixtures = true # self.use_transactional_fixtures = true


# Instantiated fixtures are slow, but give you @david where otherwise you # Instantiated fixtures are slow, but give you @david where otherwise you
# would need people(:david). If you don't want to migrate your existing # would need people(:david). If you don't want to migrate your existing
# test cases which use the @david style and don't mind the speed hit (each # test cases which use the @david style and don't mind the speed hit (each
# instantiated fixtures translates to a database query per test method), # instantiated fixtures translates to a database query per test method),
# then set this back to true. # then set this back to true.
self.use_instantiated_fixtures = false # self.use_instantiated_fixtures = false


# Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order. # Setup all fixtures in test/fixtures/*.(yml|csv) for all tests in alphabetical order.
# #
# Note: You'll currently still have to declare fixtures explicitly in integration tests # Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting # -- they do not yet inherit this setting
fixtures :all # fixtures :all


# Add more helper methods to be used by all tests here... # Add more helper methods to be used by all tests here...
end end

0 comments on commit 40d2556

Please sign in to comment.