To check out this repository please hg clone the following URL, or open the URL using EasyMercurial or your preferred Mercurial client.
root / vendor / plugins / engines / lib / engines / testing.rb @ 442:753f1380d6bc
History | View | Annotate | Download (4.83 KB)
| 1 |
# Contains the enhancements to assist in testing plugins. See Engines::Testing
|
|---|---|
| 2 |
# for more details.
|
| 3 |
|
| 4 |
require 'test/unit'
|
| 5 |
|
| 6 |
require 'tmpdir'
|
| 7 |
require 'fileutils'
|
| 8 |
|
| 9 |
# In most cases, Rails' own plugin testing mechanisms are sufficient. However, there
|
| 10 |
# are cases where plugins can be given a helping hand in the testing arena. This module
|
| 11 |
# contains some methods to assist when testing plugins that contain fixtures.
|
| 12 |
#
|
| 13 |
# == Fixtures and plugins
|
| 14 |
#
|
| 15 |
# Since Rails' own fixtures method is fairly strict about where files can be loaded from,
|
| 16 |
# the simplest approach when running plugin tests with fixtures is to simply copy all
|
| 17 |
# fixtures into a single temporary location and inform the standard Rails mechanism to
|
| 18 |
# use this directory, rather than RAILS_ROOT/test/fixtures.
|
| 19 |
#
|
| 20 |
# The Engines::Testing#setup_plugin_fixtures method does this, copying all plugin fixtures
|
| 21 |
# into the temporary location before and tests are performed. This behaviour is invoked
|
| 22 |
# the the rake tasks provided by the Engines plugin, in the "test:plugins" namespace. If
|
| 23 |
# necessary, you can invoke the task manually.
|
| 24 |
#
|
| 25 |
# If you wish to take advantage of this, add a call to the Engines::Testing.set_fixture_path
|
| 26 |
# method somewhere before your tests (in a test_helper file, or above the TestCase itself).
|
| 27 |
#
|
| 28 |
# = Testing plugins
|
| 29 |
#
|
| 30 |
# Normally testing a plugin will require that Rails is loaded, unless you are including
|
| 31 |
# a skeleton Rails environment or set of mocks within your plugin tests. If you require
|
| 32 |
# the Rails environment to be started, you must ensure that this actually happens; while
|
| 33 |
# it's not obvious, your tests do not automatically run with Rails loaded.
|
| 34 |
#
|
| 35 |
# The simplest way to setup plugin tests is to include a test helper with the following
|
| 36 |
# contents:
|
| 37 |
#
|
| 38 |
# # Load the normal Rails helper. This ensures the environment is loaded
|
| 39 |
# require File.expand_path(File.dirname(__FILE__) + '/../../../../test/test_helper')
|
| 40 |
# # Ensure that we are using the temporary fixture path
|
| 41 |
# Engines::Testing.set_fixture_path
|
| 42 |
#
|
| 43 |
# Then run tests using the provided tasks (<tt>test:plugins</tt>, or the tasks that the engines
|
| 44 |
# plugin provides - <tt>test:plugins:units</tt>, etc.).
|
| 45 |
#
|
| 46 |
# Alternatively, you can explicitly load the environment by adpating the contents of the
|
| 47 |
# default <tt>test_helper</tt>:
|
| 48 |
#
|
| 49 |
# ENV["RAILS_ENV"] = "test"
|
| 50 |
# # Note that we are requiring config/environment from the root of the enclosing application.
|
| 51 |
# require File.expand_path(File.dirname(__FILE__) + "/../../../../config/environment")
|
| 52 |
# require 'test_help'
|
| 53 |
#
|
| 54 |
module Engines::Testing |
| 55 |
mattr_accessor :temporary_fixtures_directory
|
| 56 |
self.temporary_fixtures_directory = FileUtils.mkdir_p(File.join(Dir.tmpdir, "rails_fixtures")) |
| 57 |
|
| 58 |
# Copies fixtures from plugins and the application into a temporary directory
|
| 59 |
# (Engines::Testing.temporary_fixtures_directory).
|
| 60 |
#
|
| 61 |
# If a set of plugins is not given, fixtures are copied from all plugins in order
|
| 62 |
# of precedence, meaning that plugins can 'overwrite' the fixtures of others if they are
|
| 63 |
# loaded later; the application's fixtures are copied last, allowing any custom fixtures
|
| 64 |
# to override those in the plugins. If no argument is given, plugins are loaded via
|
| 65 |
# PluginList#by_precedence.
|
| 66 |
#
|
| 67 |
# This method is called by the engines-supplied plugin testing rake tasks
|
| 68 |
def self.setup_plugin_fixtures(plugins = Engines.plugins.by_precedence) |
| 69 |
|
| 70 |
# First, clear the directory
|
| 71 |
Dir.glob("#{self.temporary_fixtures_directory}/*.yml").each{|fixture| File.delete(fixture)} |
| 72 |
|
| 73 |
# Copy all plugin fixtures, and then the application fixtures, into this directory
|
| 74 |
plugins.each do |plugin|
|
| 75 |
plugin_fixtures_directory = File.join(plugin.directory, "test", "fixtures") |
| 76 |
plugin_app_directory = File.join(plugin.directory, "app") |
| 77 |
if File.directory?(plugin_app_directory) && File.directory?(plugin_fixtures_directory) |
| 78 |
Engines.mirror_files_from(plugin_fixtures_directory, self.temporary_fixtures_directory) |
| 79 |
end
|
| 80 |
end
|
| 81 |
Engines.mirror_files_from(File.join(RAILS_ROOT, "test", "fixtures"), |
| 82 |
self.temporary_fixtures_directory)
|
| 83 |
end
|
| 84 |
|
| 85 |
# Sets the fixture path used by Test::Unit::TestCase to the temporary
|
| 86 |
# directory which contains all plugin fixtures.
|
| 87 |
def self.set_fixture_path |
| 88 |
ActiveSupport::TestCase.fixture_path = self.temporary_fixtures_directory |
| 89 |
$LOAD_PATH.unshift self.temporary_fixtures_directory |
| 90 |
end
|
| 91 |
|
| 92 |
# overridden test should be in test/{unit,functional,integration}/{plugin_name}/{test_name}
|
| 93 |
def self.override_tests_from_app |
| 94 |
filename = caller.first.split(":").first
|
| 95 |
plugin_name = filename.split("/")[-4] |
| 96 |
test_kind = filename.split("/")[-2] |
| 97 |
override_file = File.expand_path(File.join(File.dirname(filename), "..", "..", "..", "..", "..", "test", |
| 98 |
test_kind, plugin_name, File.basename(filename)))
|
| 99 |
load(override_file) if File.exist?(override_file) |
| 100 |
end
|
| 101 |
end
|