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