Skip to content

Commit

Permalink
Update specs and correct some problems.
Browse files Browse the repository at this point in the history
  • Loading branch information
seancribbs committed Nov 11, 2009
1 parent 55b68f8 commit a705172
Show file tree
Hide file tree
Showing 17 changed files with 370 additions and 204 deletions.
114 changes: 106 additions & 8 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,15 +1,105 @@
# I think this is the one that should be moved to the extension Rakefile template

# In rails 1.2, plugins aren't available in the path until they're loaded.
# Check to see if the rspec plugin is installed first and require
# it if it is. If not, use the gem version.

# Determine where the RSpec plugin is by loading the boot
unless defined? RADIANT_ROOT
ENV["RAILS_ENV"] = "test"
case
when ENV["RADIANT_ENV_FILE"]
require File.dirname(ENV["RADIANT_ENV_FILE"]) + "/boot"
when File.dirname(__FILE__) =~ %r{vendor/radiant/vendor/extensions}
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../../../")}/config/boot"
else
require "#{File.expand_path(File.dirname(__FILE__) + "/../../../")}/config/boot"
end
end

require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'rake/testtask'

desc 'Default: run unit tests.'
task :default => :test
rspec_base = File.expand_path(RADIANT_ROOT + '/vendor/plugins/rspec/lib')
$LOAD_PATH.unshift(rspec_base) if File.exist?(rspec_base)
require 'spec/rake/spectask'
require 'cucumber'
require 'cucumber/rake/task'

desc 'Test the scheduler extension.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
# Cleanup the RADIANT_ROOT constant so specs will load the environment
Object.send(:remove_const, :RADIANT_ROOT)

extension_root = File.expand_path(File.dirname(__FILE__))

task :default => :spec
task :stats => "spec:statsetup"

desc "Run all specs in spec directory"
Spec::Rake::SpecTask.new(:spec) do |t|
t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb']
end

task :features => 'spec:integration'

namespace :spec do
desc "Run all specs in spec directory with RCov"
Spec::Rake::SpecTask.new(:rcov) do |t|
t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
t.spec_files = FileList['spec/**/*_spec.rb']
t.rcov = true
t.rcov_opts = ['--exclude', 'spec', '--rails']
end

desc "Print Specdoc for all specs"
Spec::Rake::SpecTask.new(:doc) do |t|
t.spec_opts = ["--format", "specdoc", "--dry-run"]
t.spec_files = FileList['spec/**/*_spec.rb']
end

[:models, :controllers, :views, :helpers].each do |sub|
desc "Run the specs under spec/#{sub}"
Spec::Rake::SpecTask.new(sub) do |t|
t.spec_opts = ['--options', "\"#{extension_root}/spec/spec.opts\""]
t.spec_files = FileList["spec/#{sub}/**/*_spec.rb"]
end
end

desc "Run the Cucumber features"
Cucumber::Rake::Task.new(:integration) do |t|
t.fork = true
t.cucumber_opts = ['--format', (ENV['CUCUMBER_FORMAT'] || 'pretty')]
# t.feature_pattern = "#{extension_root}/features/**/*.feature"
t.profile = "default"
end

# Setup specs for stats
task :statsetup do
require 'code_statistics'
::STATS_DIRECTORIES << %w(Model\ specs spec/models)
::STATS_DIRECTORIES << %w(View\ specs spec/views)
::STATS_DIRECTORIES << %w(Controller\ specs spec/controllers)
::STATS_DIRECTORIES << %w(Helper\ specs spec/views)
::CodeStatistics::TEST_TYPES << "Model specs"
::CodeStatistics::TEST_TYPES << "View specs"
::CodeStatistics::TEST_TYPES << "Controller specs"
::CodeStatistics::TEST_TYPES << "Helper specs"
::STATS_DIRECTORIES.delete_if {|a| a[0] =~ /test/}
end

namespace :db do
namespace :fixtures do
desc "Load fixtures (from spec/fixtures) into the current environment's database. Load specific fixtures using FIXTURES=x,y"
task :load => :environment do
require 'active_record/fixtures'
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
(ENV['FIXTURES'] ? ENV['FIXTURES'].split(/,/) : Dir.glob(File.join(RAILS_ROOT, 'spec', 'fixtures', '*.{yml,csv}'))).each do |fixture_file|
Fixtures.create_fixtures('spec/fixtures', File.basename(fixture_file, '.*'))
end
end
end
end
end

desc 'Generate documentation for the scheduler extension.'
Expand All @@ -21,5 +111,13 @@ Rake::RDocTask.new(:rdoc) do |rdoc|
rdoc.rdoc_files.include('lib/**/*.rb')
end

# For extensions that are in transition
desc 'Test the scheduler extension.'
Rake::TestTask.new(:test) do |t|
t.libs << 'lib'
t.pattern = 'test/**/*_test.rb'
t.verbose = true
end

# Load any custom rakefiles for extension
Dir[File.dirname(__FILE__) + '/tasks/*.rake'].sort.each { |f| require f }
1 change: 1 addition & 0 deletions cucumber.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
default: --format progress features --tags ~@proposed,~@in_progress
16 changes: 16 additions & 0 deletions features/support/env.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Sets up the Rails environment for Cucumber
ENV["RAILS_ENV"] = "test"
# Extension root
extension_env = File.expand_path(File.dirname(__FILE__) + '/../../../../../config/environment')
require extension_env+'.rb'

Dir.glob(File.join(RADIANT_ROOT, "features", "**", "*.rb")).each {|step| require step}

Cucumber::Rails::World.class_eval do
include Dataset
datasets_directory "#{RADIANT_ROOT}/spec/datasets"
Dataset::Resolver.default = Dataset::DirectoryResolver.new("#{RADIANT_ROOT}/spec/datasets", File.dirname(__FILE__) + '/../../spec/datasets', File.dirname(__FILE__) + '/../datasets')
self.datasets_database_dump_path = "#{Rails.root}/tmp/dataset"

# dataset :scheduler
end
14 changes: 14 additions & 0 deletions features/support/paths.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def path_to(page_name)
case page_name

when /the homepage/i
root_path

when /login/i
login_path
# Add more page name => path mappings here

else
raise "Can't find mapping from \"#{page_name}\" to a path."
end
end
13 changes: 7 additions & 6 deletions lib/scheduler/controller_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
module Scheduler::ControllerExtensions
def self.included(base)
base.class_eval { alias_method_chain :process_page, :scheduling }
base.class_eval { around_filter :filter_with_scheduler }
end

def process_page_with_scheduling(page)

protected
def filter_with_scheduler
if live?
Page.with_published_only { process_page_without_scheduling(page) }
Page.with_published_only { yield }
else
process_page_without_scheduling(page)
yield
end
end
end
end
34 changes: 19 additions & 15 deletions lib/scheduler/page_extensions.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Scheduler::PageExtensions
include Radiant::Taggable

def self.included(base)
base.extend ClassMethods
class << base
Expand All @@ -18,37 +18,41 @@ def find_by_url_with_scheduling(url, live=true)
find_by_url_without_scheduling(url, live)
end
end
def with_published_only(&block)
raise ArgumentError, "Block required!" unless block_given?
# Let's not duplicate the scope, but also not obliterate any incoming scope
unless @with_published

def with_published_only
if @with_published

This comment has been minimized.

Copy link
@jomz

jomz Jul 14, 2017

Member

@seancribbs surprise! somebody is still using that code you wrote 8 years ago :)
Every so many requests, the scope is not applied. I first thought it had to do with the Date.today not being lambda'd and server processes older than the expires_on date, but after patching that the problem persists.
Seems to happen when the server is busier... It looks like @with_published can not be false again before the block is yielded, yet that seems to be what's happening...
I don't understand why the instance variable was introduced in the first place... do you happen to remember how things could go bad if I was to strip out the whole if clause? Any other thoughts?

yield
else
@with_published = true
results = with_scope(:find => {:conditions => ["((appears_on IS NULL AND expires_on IS NULL) OR (appears_on IS NULL AND ? <= expires_on) OR (expires_on IS NULL AND ? >= appears_on) OR (? BETWEEN appears_on AND expires_on))", Date.today, Date.today, Date.today]}, &block)
result = with_scope(:find => {:conditions => ["(appears_on IS NULL OR appears_on <= ?) AND (expires_on IS NULL OR expires_on > ?)", Date.today, Date.today]}) do
yield
end
@with_published = false
results
else
block.call
result
end
end
end

def visible?
published? && appeared? && !expired?
end

def appeared?
appears_on.blank? || appears_on <= Date.today
end

def expired?
!expires_on.blank? && self.expires_on < Date.today
end

tag 'children' do |tag|
tag.locals.children = tag.locals.page.children
Page.with_published_only do
if dev?(tag.globals.page.request)
tag.expand
else
Page.with_published_only do
tag.expand
end
end
end
end
19 changes: 11 additions & 8 deletions lib/tasks/scheduler_extension_tasks.rake
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,18 @@ namespace :radiant do
SchedulerExtension.migrator.migrate
end
end

desc "Copies Scheduler extension assets to the public directory."
desc "Copies public assets of the Scheduler to the instance public/ directory."
task :update => :environment do
ext_js_dir = SchedulerExtension.root + "/public/javascripts"
Dir[ext_js_dir + "/*.js"].each do |file|
puts "Copying #{File.basename(file)}..."
cp file, RAILS_ROOT + "/public/javascripts"
is_svn_or_dir = proc {|path| path =~ /\.svn/ || File.directory?(path) }
puts "Copying assets from SchedulerExtension"
Dir[SchedulerExtension.root + "/public/**/*"].reject(&is_svn_or_dir).each do |file|
path = file.sub(SchedulerExtension.root, '')
directory = File.dirname(path)
mkdir_p RAILS_ROOT + directory, :verbose => false
cp file, RAILS_ROOT + path, :verbose => false
end
end
end
end
end
end
end
17 changes: 17 additions & 0 deletions spec/controllers/controller_extensions_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
require File.dirname(__FILE__) + "/../spec_helper"

describe "Scheduler::ControllerExtensions", :type => :controller do
dataset :pages_with_scheduling
controller_name :site

it "should not render invisible pages in live mode" do
get :show_page, :url => ['expired']
response.should_not be_success
end

it "should render invisible pages in dev mode" do
request.host = "dev.example.com"
get :show_page, :url => ['expired']
response.should be_success
end
end
14 changes: 14 additions & 0 deletions spec/datasets/pages_with_scheduling_dataset.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class PagesWithSchedulingDataset < Dataset::Base
uses :pages

def load
create_page "Expired", :appears_on => 2.days.ago.to_date, :expires_on => Date.yesterday
create_page "Expired blank start", :expires_on => Date.yesterday
create_page "Blank schedule"
create_page "Visible blank start", :expires_on => Date.tomorrow
create_page "Visible", :appears_on => Date.yesterday, :expires_on => Date.tomorrow
create_page "Visible blank end", :appears_on => Date.yesterday
create_page "Unappeared blank end", :appears_on => Date.tomorrow
create_page "Unappeared", :appears_on => Date.tomorrow, :expires_on => 2.days.from_now.to_date
end
end
Loading

0 comments on commit a705172

Please sign in to comment.