|
| 1 | +require 'rubytest' |
| 2 | +require 'rake' |
| 3 | +require 'rake/tasklib' |
| 4 | + |
| 5 | +module Test |
| 6 | + |
| 7 | + ## |
| 8 | + # Rake subspace. |
| 9 | + # |
| 10 | + module Rake |
| 11 | + |
| 12 | + ## |
| 13 | + # Define a rake task that runs tests. |
| 14 | + # |
| 15 | + # require 'rubytest/rake' |
| 16 | + # |
| 17 | + # Test::Rake::TestTask.new do |run| |
| 18 | + # run.loadpath 'lib' |
| 19 | + # run.files << 'test/*_test.rb' |
| 20 | + # end |
| 21 | + # |
| 22 | + # The block is given the task's Test::Config, the same object that |
| 23 | + # `Test.configure` yields. Without any files, the task runs whichever |
| 24 | + # of DEFAULT_TESTS match, and without a load path it adds `lib`. |
| 25 | + # |
| 26 | + # The `TEST` environment variable selects the tests to run for one |
| 27 | + # invocation, in place of the task's own: `rake test TEST=test/foo_test.rb`. |
| 28 | + # Separate several files or globs with `:` or `;`. |
| 29 | + # |
| 30 | + # Where the platform can fork, tests run in a child process, so that |
| 31 | + # what they load doesn't stay in Rake's. If the run is unsuccessful the |
| 32 | + # task exits with status 1. |
| 33 | + # |
| 34 | + class TestTask < ::Rake::TaskLib |
| 35 | + |
| 36 | + # Globs used to find test files when the task is given none. |
| 37 | + DEFAULT_TESTS = [ |
| 38 | + 'test/**/case_*.rb', |
| 39 | + 'test/**/*_case.rb', |
| 40 | + 'test/**/test_*.rb', |
| 41 | + 'test/**/*_test.rb' |
| 42 | + ] |
| 43 | + |
| 44 | + # Name of the task. |
| 45 | + # |
| 46 | + # @return [String] |
| 47 | + attr :name |
| 48 | + |
| 49 | + # Test run configuration. |
| 50 | + # |
| 51 | + # @return [Config] |
| 52 | + attr :config |
| 53 | + |
| 54 | + # Initialize new Rake::TestTask instance. |
| 55 | + # |
| 56 | + def initialize(name='test', desc='run tests', &block) |
| 57 | + @name = (name || 'test').to_s |
| 58 | + @desc = desc |
| 59 | + |
| 60 | + @config = Test::Config.new |
| 61 | + |
| 62 | + block.call(@config) if block |
| 63 | + |
| 64 | + define_task |
| 65 | + end |
| 66 | + |
| 67 | + # Define rake task for testing. |
| 68 | + # |
| 69 | + # @return nothing |
| 70 | + def define_task |
| 71 | + desc @desc |
| 72 | + task @name do |
| 73 | + run |
| 74 | + end |
| 75 | + end |
| 76 | + |
| 77 | + # Run tests, in a child process if possible, otherwise in this one. |
| 78 | + # Exits with status 1 if the run is unsuccessful. |
| 79 | + # |
| 80 | + # @return nothing |
| 81 | + def run |
| 82 | + prepare |
| 83 | + success = Process.respond_to?(:fork) ? run_in_fork : run_here |
| 84 | + exit 1 unless success |
| 85 | + end |
| 86 | + |
| 87 | + private |
| 88 | + |
| 89 | + # Settle the files and load path the run will use. |
| 90 | + def prepare |
| 91 | + if ENV['TEST'] |
| 92 | + config.files = ENV['TEST'].split(/[:;]/) |
| 93 | + elsif config.files.empty? |
| 94 | + config.files = default_tests |
| 95 | + end |
| 96 | + config.loadpath << 'lib' if config.loadpath.empty? |
| 97 | + end |
| 98 | + |
| 99 | + # The default globs that match at least one file. The runner fails a |
| 100 | + # glob that matches nothing, so the others are left out. |
| 101 | + # |
| 102 | + # @return [Array<String>] |
| 103 | + def default_tests |
| 104 | + base = config.chdir || Dir.pwd |
| 105 | + globs = DEFAULT_TESTS.reject{ |glob| Dir.glob(glob, base: base).empty? } |
| 106 | + if globs.empty? |
| 107 | + raise ArgumentError, "no test files found; looked for #{DEFAULT_TESTS.join(', ')}" |
| 108 | + end |
| 109 | + globs |
| 110 | + end |
| 111 | + |
| 112 | + # @return [Boolean] Whether the run was successful. |
| 113 | + def run_here |
| 114 | + Test::Runner.new(config).run |
| 115 | + end |
| 116 | + |
| 117 | + # @return [Boolean] Whether the run was successful. |
| 118 | + def run_in_fork |
| 119 | + pid = fork { exit(run_here ? 0 : 1) } |
| 120 | + Process.wait(pid) |
| 121 | + $?.success? |
| 122 | + end |
| 123 | + |
| 124 | + end |
| 125 | + |
| 126 | + end |
| 127 | + |
| 128 | +end |
0 commit comments