From 23e99ffb72361161ef8cff4d0d79efca83326c80 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Thu, 15 Nov 2012 12:09:25 +0000 Subject: Factor out method for getting a mail object from a fixture file. --- spec/spec_helper.rb | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'spec/spec_helper.rb') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 248dff70e..e6d81540b 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -208,6 +208,10 @@ def load_raw_emails_data end end +def get_fixture_mail(filename) + MailHandler.mail_from_raw_email(load_file_fixture(filename)) +end + def parse_all_incoming_messages IncomingMessage.find(:all).each{|x| x.parse_raw_email!} end -- cgit v1.2.3 From ffbd12d868eb1803631194b5b4e4ffbb3ac0d812 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 21 Nov 2012 18:50:29 +0000 Subject: Add function to clone a clean copy of the xapian index with fixtures loaded into it. --- spec/spec_helper.rb | 56 ++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 38 insertions(+), 18 deletions(-) (limited to 'spec/spec_helper.rb') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index e6d81540b..d4dad591d 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -23,7 +23,6 @@ FakeWeb.register_uri(:purge, %r|varnish.localdomain|, :body => "OK") # Use test-specific translations FastGettext.add_text_domain 'app', :path => File.join(File.dirname(__FILE__), 'fixtures', 'locale'), :type => :po FastGettext.default_text_domain = 'app' - Spec::Runner.configure do |config| # If you're not using ActiveRecord you should remove these # lines, delete config/database.yml and disable :active_record @@ -47,6 +46,7 @@ Spec::Runner.configure do |config| :holidays, :track_things_sent_emails + # == Fixtures # # You can declare fixtures for each example_group like this: @@ -99,6 +99,19 @@ def load_file_fixture(file_name) return content end +def parse_all_incoming_messages + IncomingMessage.find(:all).each{ |x| x.parse_raw_email! } +end + +def load_raw_emails_data + raw_emails_yml = File.join(Spec::Runner.configuration.fixture_path, "raw_emails.yml") + for raw_email_id in YAML::load_file(raw_emails_yml).map{|k,v| v["id"]} do + raw_email = RawEmail.find(raw_email_id) + raw_email.data = load_file_fixture("raw_emails/%d.email" % [raw_email_id]) + end +end + +# Rebuild the current xapian index def rebuild_xapian_index(terms = true, values = true, texts = true, dropfirst = true) if dropfirst begin @@ -110,16 +123,35 @@ def rebuild_xapian_index(terms = true, values = true, texts = true, dropfirst = ActsAsXapian.writable_db.close end parse_all_incoming_messages - verbose = false # safe_rebuild=true, which involves forking to avoid memory leaks, doesn't work well with rspec. # unsafe is significantly faster, and we can afford possible memory leaks while testing. - safe_rebuild = false - ActsAsXapian.rebuild_index(["PublicBody", "User", "InfoRequestEvent"].map{|m| m.constantize}, verbose, terms, values, texts, safe_rebuild) + models = [PublicBody, User, InfoRequestEvent] + ActsAsXapian.rebuild_index(models, verbose=false, terms, values, texts, safe_rebuild=false) +end + +# Create a clean xapian index based on the fixture files and the raw_email data. +def create_fixtures_xapian_index + load_raw_emails_data + rebuild_xapian_index end def update_xapian_index - verbose = false - ActsAsXapian.update_index(flush_to_disk=false, verbose) + ActsAsXapian.update_index(flush_to_disk=false, verbose=false) +end + +# Copy the xapian index created in create_fixtures_xapian_index to a temporary +# copy at the same level and point xapian at the copy +def get_fixtures_xapian_index() + # Create a base index for the fixtures if not already created + $existing_xapian_db ||= create_fixtures_xapian_index + # Store whatever the xapian db path is originally + $original_xapian_path ||= ActsAsXapian.db_path + path_array = $original_xapian_path.split(File::Separator) + path_array.pop + temp_path = File.join(path_array, 'test.temp') + FileUtils.remove_entry_secure(temp_path, force=true) + FileUtils.cp_r($original_xapian_path, temp_path) + ActsAsXapian.db_path = temp_path end # Validate an entire HTML page @@ -200,22 +232,10 @@ def safe_mock_model(model, args = {}) mock end -def load_raw_emails_data - raw_emails_yml = File.join(Spec::Runner.configuration.fixture_path, "raw_emails.yml") - for raw_email_id in YAML::load_file(raw_emails_yml).map{|k,v| v["id"]} do - raw_email = RawEmail.find(raw_email_id) - raw_email.data = load_file_fixture("raw_emails/%d.email" % [raw_email_id]) - end -end - def get_fixture_mail(filename) MailHandler.mail_from_raw_email(load_file_fixture(filename)) end -def parse_all_incoming_messages - IncomingMessage.find(:all).each{|x| x.parse_raw_email!} -end - def load_test_categories PublicBodyCategories.add(:en, [ "Local and regional", -- cgit v1.2.3 From 9af56ac5901790fb265a6492531eecbe5dd32f73 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Wed, 14 Nov 2012 13:10:51 +0000 Subject: Add option to load_file_fixture to specify that the file contents should just be loaded as binary. --- spec/spec_helper.rb | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'spec/spec_helper.rb') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d4dad591d..9621211f5 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -93,9 +93,14 @@ def file_fixture_name(file_name) return File.join(Spec::Runner.configuration.fixture_path, "files", file_name) end -def load_file_fixture(file_name) +def load_file_fixture(file_name, as_binary=false) file_name = file_fixture_name(file_name) - content = File.read(file_name) + content = File.open(file_name, 'r') do |file| + if as_binary + file.set_encoding(Encoding::BINARY) if file.respond_to?(:set_encoding) + end + file.read + end return content end -- cgit v1.2.3 From 308d338a172b11230bafa784f96fcf1eb5e6e981 Mon Sep 17 00:00:00 2001 From: Henare Degan Date: Sat, 8 Dec 2012 16:44:10 +1100 Subject: Only trigger garbage collection every 4 seconds - speeds up the whole test suite by ~35% on my machine Via https://37signals.com/svn/posts/2742-the-road-to-faster-tests --- spec/spec_helper.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'spec/spec_helper.rb') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index d4dad591d..839e6125e 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -46,6 +46,20 @@ Spec::Runner.configure do |config| :holidays, :track_things_sent_emails + # This section makes the garbage collector run less often to speed up tests + last_gc_run = Time.now + + config.before(:each) do + GC.disable + end + + config.after(:each) do + if Time.now - last_gc_run > 4 + GC.enable + GC.start + last_gc_run = Time.now + end + end # == Fixtures # -- cgit v1.2.3 From e898190fe61a4369ee83e94a24327ff437435b07 Mon Sep 17 00:00:00 2001 From: Louise Crow Date: Tue, 11 Dec 2012 14:07:54 +0000 Subject: Wrap specs on the extraction of RFC-822 headers in code that sets the ENV timezone. TMail renders headers using localtime, which is not ideal, but we're migrating away from it anyway, so I'm not sure it's worth delving into the internals of TMail to fix it. --- spec/spec_helper.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'spec/spec_helper.rb') diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 4cf376624..561a75da6 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -275,3 +275,18 @@ class ApplicationController < ActionController::Base @popup_banner = nil end end + + +def with_env_tz(new_tz = 'US/Eastern') + old_tz, ENV['TZ'] = ENV['TZ'], new_tz + yield +ensure + old_tz ? ENV['TZ'] = old_tz : ENV.delete('TZ') +end + +def with_active_record_default_timezone(zone) + old_zone, ActiveRecord::Base.default_timezone = ActiveRecord::Base.default_timezone, zone + yield +ensure + ActiveRecord::Base.default_timezone = old_zone +end -- cgit v1.2.3