Module: Sprout::TestHelper
- Includes:
- Gem::DefaultUserInteraction
- Defined in:
- lib/sprout/test_helper.rb
Overview
A collection of custom assertions and helper methods to take some of the suck out of testing functionality that is based on Sprout features.
Include this module into your test cases to make testing Sprout tools easier.
require 'sprout/test/sprout_test_helper'
class SomeTestCase < Test::Unit::TestCase
include Sprout::TestHelper
def setup
super
# do something
end
def teardown
super
# do something
end
def test_something
assert_file File.join(fixtures, 'some_file') do |f|
assert_matches /Fred/, f
end
end
end
Constant Summary
- FIXTURES_NAME =
The name of the folder that should contain fixture data.
'fixtures'
Instance Method Summary (collapse)
-
- (Sprout::System::OSXSystem) as_a_mac_system {|Sprout::System::OSXSystem| ... }
protected
Execute a block as a OSXSystem.
-
- (Sprout::System::UnixSystem) as_a_unix_system {|Sprout::System::UnixSystem| ... }
protected
Execute a block as a UnixSystem.
-
- (Sprout::System::WinNixSystem) as_a_win_nix_system {|Sprout::System::WinNixSystem| ... }
protected
Execute a block as a WinNixSystem.
-
- (Sprout::System::WinSystem) as_a_windows_system {|Sprout::System::WinSystem| ... }
protected
Execute a block as a WinSystem.
-
- (Sprout::System::BaseSystem) as_each_system {|Sprout::System::BaseSystem| ... }
protected
Execute a block as each available Sprout::System, any code within this block that calls Sprout.current_user will receive the currently active Sprout::System.
-
- (Object) assert_directory(path, message = nil)
protected
Assert that a directory exists at path and display message if it does not.
-
- (Object) assert_file(path, message = nil) {|String| ... }
protected
Assert that a file exists at path and display message if it does not.
-
- (Object) assert_matches(expression, string, message = '')
protected
Assert that an expression matches the provided string.
-
- (Object) assert_not_empty(path, message = nil)
protected
Assert that a file exists at path and is not empty.
-
- (Object) clear_tasks
protected
Clear all registered Rake tasks.
-
- (File) create_file(path)
protected
Create an empty file at path.
-
- (Dir) fixtures(from = nil)
the text case that calls this method.
-
- (Rake::Task) get_task(name)
protected
Retrieve a registered Rake task by name.
-
- (Dir) make_temp_folder(from)
protected
Create a temporary folder relative to the provided path.
-
- (Object) remove_file(path = nil)
protected
Remove a file if it exists.
-
- (Rake::Task) run_task(name)
protected
Invoke a Rake task by name.
-
- (Object) setup
Override the setup method in order to record the working directory before the test method runs.
- - (Object) skip(message = "")
-
- (Object) teardown
Override the teardown method in order to perform systemic cleanup work like, clearing lingering rake tasks, and removing temporary folders.
-
- (Dir) temp_cache
protected
Create and/or return sprout/cache directory relative to the fixtures folder nearest the file that calls this method.
-
- (Dir) temp_path
protected
Create a temporary folder relative to the test case that calls this method.
Instance Method Details
- (Sprout::System::OSXSystem) as_a_mac_system {|Sprout::System::OSXSystem| ... } (protected)
Execute a block as a OSXSystem.
as_a_mac_system do
puts ">> Sprout.home: #{Sprout.home}"
end
312 313 314 315 316 317 318 319 320 321 |
# File 'lib/sprout/test_helper.rb', line 312 def as_a_mac_system sys = Sprout::System::OSXSystem.new Sprout::System.stubs(:create).returns sys yield sys if block_given? # Ugh - This is way too greedy... We're killing all mocks in here # Doing it anyway b/c we need to get Windows support in place... # TODO: Implement this feature without clobbering all stubs/mocks #Mocha::Mockery.instance.teardown sys end |
- (Sprout::System::UnixSystem) as_a_unix_system {|Sprout::System::UnixSystem| ... } (protected)
Execute a block as a UnixSystem.
as_a_unix_system do
puts ">> Sprout.home: #{Sprout.home}"
end
292 293 294 295 296 297 298 299 300 301 |
# File 'lib/sprout/test_helper.rb', line 292 def as_a_unix_system sys = Sprout::System::UnixSystem.new expectation = Sprout::System.stubs(:create).returns sys yield sys if block_given? # Ugh - This is way too greedy... We're killing all mocks in here # Doing it anyway b/c we need to get Windows support in place... # TODO: Implement this feature without clobbering all stubs/mocks #Mocha::Mockery.instance.teardown sys end |
- (Sprout::System::WinNixSystem) as_a_win_nix_system {|Sprout::System::WinNixSystem| ... } (protected)
Execute a block as a WinNixSystem.
as_a_win_nix_system do
puts ">> Sprout.home: #{Sprout.home}"
end
352 353 354 355 356 357 358 359 360 361 |
# File 'lib/sprout/test_helper.rb', line 352 def as_a_win_nix_system sys = Sprout::System::WinNixSystem.new Sprout::System.stubs(:create).returns sys yield sys if block_given? # Ugh - This is way too greedy... We're killing all mocks in here # Doing it anyway b/c we need to get Windows support in place... # TODO: Implement this feature without clobbering all stubs/mocks #Mocha::Mockery.instance.teardown sys end |
- (Sprout::System::WinSystem) as_a_windows_system {|Sprout::System::WinSystem| ... } (protected)
Execute a block as a WinSystem.
as_a_windows_system do
puts ">> Sprout.home: #{Sprout.home}"
end
332 333 334 335 336 337 338 339 340 341 |
# File 'lib/sprout/test_helper.rb', line 332 def as_a_windows_system sys = Sprout::System::WinSystem.new Sprout::System.stubs(:create).returns sys yield sys if block_given? # Ugh - This is way too greedy... We're killing all mocks in here # Doing it anyway b/c we need to get Windows support in place... # TODO: Implement this feature without clobbering all stubs/mocks #Mocha::Mockery.instance.teardown sys end |
- (Sprout::System::BaseSystem) as_each_system {|Sprout::System::BaseSystem| ... } (protected)
Execute a block as each available Sprout::System, any code within this block that calls Sprout.current_user will receive the currently active Sprout::System.
as_each_system do
puts ">> Sprout.home: #{Sprout.home}"
end
This method is primarily used to ensure that we create system-appropriate paths and processes.
NOTE: This process automatically calls Mocha::Mockery.instance.teardown after the yield. This means that any mocks that have been created will no longer be available after the provided block is complete.
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/sprout/test_helper.rb', line 264 def as_each_system [ Sprout::System::VistaSystem.new, Sprout::System::WinNixSystem.new, Sprout::System::WinSystem.new, Sprout::System::JavaSystem.new, Sprout::System::OSXSystem.new, Sprout::System::UnixSystem.new ].each do |sys| expectation = Sprout::System.stubs(:create).returns sys yield sys if block_given? # Ugh - This is way too greedy... We're killing all mocks in here # Doing it anyway b/c we need to get Windows support in place... # TODO: Implement this feature without clobbering all stubs/mocks Mocha::Mockery.instance.teardown sys end end |
- (Object) assert_directory(path, message = nil) (protected)
Assert that a directory exists at path and display message if it does not.
198 199 200 201 |
# File 'lib/sprout/test_helper.rb', line 198 def assert_directory(path, =nil) ||= "Expected directory not found at #{path}" assert(File.directory?(path), ) end |
- (Object) assert_file(path, message = nil) {|String| ... } (protected)
Assert that a file exists at path and display message if it does not.
This method yields the file contents so that you can write readable tests like:
assert_file File.join(fixtures, 'my_file') do |f|
assert_matches /Johnny/, f
end
183 184 185 186 187 |
# File 'lib/sprout/test_helper.rb', line 183 def assert_file(path, =nil) ||= "Expected file not found at #{path}" assert(File.exists?(path), ) yield File.read(path) if block_given? end |
- (Object) assert_matches(expression, string, message = '') (protected)
Assert that an expression matches the provided string.
This helper mainly makes tests more readable and provides simpler failure messages without extra work.
assert_matches /Fred/, 'Bill, Fred, Bob'
227 228 229 230 231 232 233 234 |
# File 'lib/sprout/test_helper.rb', line 227 def assert_matches(expression, string, ='') if(expression.is_a?(String)) expresion = /#{expression}/ end if(!string.match(expression)) fail "#{} - '#{string}' should include '#{expression}'" end end |
- (Object) assert_not_empty(path, message = nil) (protected)
Assert that a file exists at path and is not empty. Display message if the file does not exist or if it is empty.
212 213 214 215 216 217 |
# File 'lib/sprout/test_helper.rb', line 212 def assert_not_empty(path, =nil) assert_file path, files = FileList["#{path}/*"] ||= " - Expected #{path} to not be empty, but it was" assert files.size > 0, end |
- (Object) clear_tasks (protected)
Clear all registered Rake tasks.
140 141 142 143 144 |
# File 'lib/sprout/test_helper.rb', line 140 def clear_tasks CLEAN.delete_if {|a| true } Rake::Task.clear Rake.application.clear end |
- (File) create_file(path) (protected)
Create an empty file at path
151 152 153 154 155 |
# File 'lib/sprout/test_helper.rb', line 151 def create_file path dir = File.dirname path FileUtils.mkdir_p dir FileUtils.touch path end |
- (Dir) fixtures(from = nil)
the text case that calls this method.
56 57 58 |
# File 'lib/sprout/test_helper.rb', line 56 def fixtures from=nil @fixtures ||= find_fixtures(from || Sprout.file_from_caller(caller.first)) end |
- (Rake::Task) get_task(name) (protected)
Retrieve a registered Rake task by name.
134 135 136 |
# File 'lib/sprout/test_helper.rb', line 134 def get_task(name) return Rake.application[name] end |
- (Dir) make_temp_folder(from) (protected)
Create a temporary folder relative to the provided path.
114 115 116 117 118 |
# File 'lib/sprout/test_helper.rb', line 114 def make_temp_folder from dir = File.join(fixtures(from), 'tmp') FileUtils.mkdir_p dir dir end |
- (Object) remove_file(path = nil) (protected)
Remove a file if it exists. If no file exists, do nothing.
162 163 164 165 166 |
# File 'lib/sprout/test_helper.rb', line 162 def remove_file(path=nil) if(path && File.exists?(path)) FileUtils.rm_rf(path) end end |
- (Rake::Task) run_task(name) (protected)
Invoke a Rake task by name.
124 125 126 127 128 |
# File 'lib/sprout/test_helper.rb', line 124 def run_task(name) t = Rake.application[name] t.invoke return t end |
- (Object) setup
Override the setup method in order to record the working directory before the test method runs.
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/sprout/test_helper.rb', line 63 def setup super # Prevent log messages from interrupting test output, # and create a pipe that test cases can read to ensure # user output is provided properly. Sprout.stdout = Sprout::OutputBuffer.new Sprout.stderr = Sprout::OutputBuffer.new @start_path = Dir.pwd end |
- (Object) skip(message = "")
47 48 49 50 |
# File 'lib/sprout/test_helper.rb', line 47 def skip ="" puts puts ">> SproutTestCase.skip called from: #{caller[0]} ( #{} )" end |
- (Object) teardown
Override the teardown method in order to perform systemic cleanup work like, clearing lingering rake tasks, and removing temporary folders.
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/sprout/test_helper.rb', line 79 def teardown super clear_tasks remove_file @temp_path remove_file @temp_cache @temp_path = nil @temp_cache = nil if(@start_path && Dir.pwd != @start_path) puts "[WARNING] >> SproutTestCase changing dir from #{Dir.pwd} back to: #{@start_path} - Did you mean to leave your working directory in a new place?" Dir.chdir @start_path end end |
- (Dir) temp_cache (protected)
Create and/or return sprout/cache directory relative to the fixtures folder nearest the file that calls this method.
240 241 242 243 |
# File 'lib/sprout/test_helper.rb', line 240 def temp_cache dir = File.dirname(Sprout.file_from_caller(caller.first)) @temp_cache ||= File.join(fixtures(dir), 'sprout', 'cache') end |
- (Dir) temp_path (protected)
Create a temporary folder relative to the test case that calls this method.
102 103 104 105 |
# File 'lib/sprout/test_helper.rb', line 102 def temp_path caller_file = Sprout.file_from_caller caller.first @temp_path ||= make_temp_folder File.dirname(caller_file) end |