Repository navigation
Bring the Rake task into Rubytest #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
HISTORY.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
demo/06_rake_task.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| ## Rake task | ||
|
|
||
| `rubytest/rake` provides a task for running tests from a Rakefile. | ||
|
|
||
| require 'rubytest/rake' | ||
| require 'tmpdir' | ||
|
|
||
| project = Dir.mktmpdir('rubytest-rake') | ||
| Dir.mkdir(File.join(project, 'test')) | ||
|
|
||
| probe = lambda do |label, body| | ||
| <<-RUBY | ||
| probe = Object.new | ||
| probe.define_singleton_method(:call) { #{body} } | ||
| probe.define_singleton_method(:to_s) { #{label.inspect} } | ||
| $TEST_SUITE << probe | ||
| RUBY | ||
| end | ||
|
|
||
| File.write(File.join(project, 'test', 'good_test.rb'), probe.call('good', 'true')) | ||
| File.write(File.join(project, 'bad.rb'), probe.call('bad', "raise Assertion, 'wrong value'")) | ||
|
|
||
| status_of = lambda do |task| | ||
| Dir.chdir(project) do | ||
| begin | ||
| task.run | ||
| 0 | ||
| rescue SystemExit => exit | ||
| exit.status | ||
| end | ||
| end | ||
| end | ||
|
|
||
| The block configures the run, as with `Test.configure`. The task is an | ||
| ordinary Rake task. | ||
|
|
||
| passing = Test::Rake::TestTask.new('demo:pass') do |run| | ||
| run.files << 'test/good_test.rb' | ||
| run.format = 'test' | ||
| end | ||
|
|
||
| Rake::Task.task_defined?('demo:pass').assert == true | ||
| status_of.call(passing).assert == 0 | ||
|
|
||
| A run that fails makes the task exit with status 1, so that `rake` fails too. | ||
|
|
||
| failing = Test::Rake::TestTask.new('demo:fail') do |run| | ||
| run.files << 'bad.rb' | ||
| run.format = 'test' | ||
| end | ||
|
|
||
| status_of.call(failing).assert == 1 | ||
|
|
||
| Given no files, the task looks under `test/` for files named `test_*.rb`, | ||
| `*_test.rb`, `case_*.rb` or `*_case.rb`, and runs whichever of those it | ||
| finds. It also puts `lib` on the load path. | ||
|
|
||
| defaults = Test::Rake::TestTask.new('demo:defaults') { |run| run.format = 'test' } | ||
|
|
||
| status_of.call(defaults).assert == 0 | ||
| defaults.config.files.assert == ['test/**/*_test.rb'] | ||
| defaults.config.loadpath.assert == ['lib'] | ||
|
|
||
| If it finds none, it says so. | ||
|
|
||
| empty = Dir.mktmpdir('rubytest-rake-empty') | ||
| nothing = Test::Rake::TestTask.new('demo:nothing') { |run| run.format = 'test' } | ||
| missing = begin | ||
| Dir.chdir(empty) { nothing.run } | ||
| nil | ||
| rescue ArgumentError => error | ||
| error | ||
| end | ||
|
|
||
| missing.message.assert.start_with?('no test files found') | ||
|
|
||
| The `TEST` environment variable selects the tests for one invocation, in | ||
| place of the task's own. | ||
|
|
||
| previous = ENV['TEST'] | ||
| begin | ||
| ENV['TEST'] = 'bad.rb' | ||
| status_of.call(passing).assert == 1 | ||
| passing.config.files.assert == ['bad.rb'] | ||
| ensure | ||
| ENV['TEST'] = previous | ||
| end |
128 changes: 128 additions & 0 deletions
lib/rubytest/rake.rb
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,128 @@ | ||
| require 'rubytest' | ||
| require 'rake' | ||
| require 'rake/tasklib' | ||
|
|
||
| module Test | ||
|
|
||
| ## | ||
| # Rake subspace. | ||
| # | ||
| module Rake | ||
|
|
||
| ## | ||
| # Define a rake task that runs tests. | ||
| # | ||
| # require 'rubytest/rake' | ||
| # | ||
| # Test::Rake::TestTask.new do |run| | ||
| # run.loadpath 'lib' | ||
| # run.files << 'test/*_test.rb' | ||
| # end | ||
| # | ||
| # The block is given the task's Test::Config, the same object that | ||
| # `Test.configure` yields. Without any files, the task runs whichever | ||
| # of DEFAULT_TESTS match, and without a load path it adds `lib`. | ||
| # | ||
| # The `TEST` environment variable selects the tests to run for one | ||
| # invocation, in place of the task's own: `rake test TEST=test/foo_test.rb`. | ||
| # Separate several files or globs with `:` or `;`. | ||
| # | ||
| # Where the platform can fork, tests run in a child process, so that | ||
| # what they load doesn't stay in Rake's. If the run is unsuccessful the | ||
| # task exits with status 1. | ||
| # | ||
| class TestTask < ::Rake::TaskLib | ||
|
|
||
| # Globs used to find test files when the task is given none. | ||
| DEFAULT_TESTS = [ | ||
| 'test/**/case_*.rb', | ||
| 'test/**/*_case.rb', | ||
| 'test/**/test_*.rb', | ||
| 'test/**/*_test.rb' | ||
| ] | ||
|
|
||
| # Name of the task. | ||
| # | ||
| # @return [String] | ||
| attr :name | ||
|
|
||
| # Test run configuration. | ||
| # | ||
| # @return [Config] | ||
| attr :config | ||
|
|
||
| # Initialize new Rake::TestTask instance. | ||
| # | ||
| def initialize(name='test', desc='run tests', &block) | ||
| @name = (name || 'test').to_s | ||
| @desc = desc | ||
|
|
||
| @config = Test::Config.new | ||
|
|
||
| block.call(@config) if block | ||
|
|
||
| define_task | ||
| end | ||
|
|
||
| # Define rake task for testing. | ||
| # | ||
| # @return nothing | ||
| def define_task | ||
| desc @desc | ||
| task @name do | ||
| run | ||
| end | ||
| end | ||
|
|
||
| # Run tests, in a child process if possible, otherwise in this one. | ||
| # Exits with status 1 if the run is unsuccessful. | ||
| # | ||
| # @return nothing | ||
| def run | ||
| prepare | ||
| success = Process.respond_to?(:fork) ? run_in_fork : run_here | ||
| exit 1 unless success | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # Settle the files and load path the run will use. | ||
| def prepare | ||
| if ENV['TEST'] | ||
| config.files = ENV['TEST'].split(/[:;]/) | ||
| elsif config.files.empty? | ||
| config.files = default_tests | ||
| end | ||
| config.loadpath << 'lib' if config.loadpath.empty? | ||
| end | ||
|
|
||
| # The default globs that match at least one file. The runner fails a | ||
| # glob that matches nothing, so the others are left out. | ||
| # | ||
| # @return [Array<String>] | ||
| def default_tests | ||
| base = config.chdir || Dir.pwd | ||
| globs = DEFAULT_TESTS.reject{ |glob| Dir.glob(glob, base: base).empty? } | ||
| if globs.empty? | ||
| raise ArgumentError, "no test files found; looked for #{DEFAULT_TESTS.join(', ')}" | ||
| end | ||
| globs | ||
| end | ||
|
|
||
| # @return [Boolean] Whether the run was successful. | ||
| def run_here | ||
| Test::Runner.new(config).run | ||
| end | ||
|
|
||
| # @return [Boolean] Whether the run was successful. | ||
| def run_in_fork | ||
| pid = fork { exit(run_here ? 0 : 1) } | ||
| Process.wait(pid) | ||
| $?.success? | ||
| end | ||
|
|
||
| end | ||
|
|
||
| end | ||
|
|
||
| end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.