minitest-around-0.4.1/0000755000004100000410000000000013305251116014663 5ustar www-datawww-dataminitest-around-0.4.1/.travis.yml0000644000004100000410000000060413305251116016774 0ustar www-datawww-datalanguage: ruby sudo: false cache: bundler rvm: - 2.0 - 2.1 - 2.2 - 2.3 - 2.4 - ruby-head - jruby - jruby-head - rbx-2 env: global: - CODECLIMATE_REPO_TOKEN=e2647eb4bb8263157b9d99d9f79a49f8262a7e5ed11b07bdfbd312afdb43c2fa - JRUBY_OPTS='--dev -J-Xmx1024M' matrix: allow_failures: - rvm: ruby-head - rvm: jruby-head - rvm: rbx-2 fast_finish: true minitest-around-0.4.1/test/0000755000004100000410000000000013305251116015642 5ustar www-datawww-dataminitest-around-0.4.1/test/around_test.rb0000644000004100000410000000052713305251116020522 0ustar www-datawww-datarequire_relative 'helper' require 'minitest/around/unit' class TestWithoutAround < Minitest::Test def test_no_around_defined assert true end end class TestWithoutArgs < Minitest::Test def around $before = true yield $before = false # hard to test? end def test_runs_around assert_equal true, $before end end minitest-around-0.4.1/test/helper.rb0000644000004100000410000000026413305251116017450 0ustar www-datawww-dataif ENV['CODECLIMATE_REPO_TOKEN'] require "codeclimate-test-reporter" CodeClimate::TestReporter.start end require 'bundler/setup' require 'tempfile' require 'minitest/autorun' minitest-around-0.4.1/test/nested_spec.rb0000644000004100000410000000111213305251116020456 0ustar www-datawww-datarequire_relative 'helper' require 'minitest/around' describe 'Outer' do let(:var) { [] } before do var << :before end after do var << :after var.must_equal [:before, :begin, :ibefore, :ibegin, :during, :iend, :iafter, :end, :after] end around do |test| var << :begin test.call var << :end end describe 'Inner' do before do var << :ibefore end after do var << :iafter end around do |test| var << :ibegin test.call var << :iend end it 'testing' do var << :during end end end minitest-around-0.4.1/test/nested_test.rb0000644000004100000410000000113013305251116020503 0ustar www-datawww-datarequire_relative 'helper' require 'minitest/around/unit' class OuterNestedTest < Minitest::Test @@var = [] def setup @@var << :before end def teardown @@var << :after @@var.must_equal [:before, :ibefore, :begin, :ibegin, :during, :iend, :end, :iafter, :after] end def around @@var << :begin yield @@var << :end end end class InnerNestedTest < OuterNestedTest def setup @@var << :ibefore end def teardown @@var << :iafter end def around @@var << :ibegin yield @@var << :iend end def test_nesting @@var << :during end end minitest-around-0.4.1/test/around_spec.rb0000644000004100000410000000353713305251116020501 0ustar www-datawww-datarequire_relative 'helper' require 'minitest/around/spec' describe "Minitest Around" do describe "without around" do it "works w/o defining parameters" do assert true end end describe "simple" do around do |test| $before = true test.call $before = false end it "runs around" do $before.must_equal true end end describe "context" do before { @x = 1 } around do |test| @x = 2 test.call assert_equal 2, @x end it "stays in context" do @x.must_equal 2 end end describe "nested fun" do let(:list) { [] } before { list << 1 } before { list << 2 } after do if @xxx == 1 list.must_equal [1, 2, 3, 4, 5, 9, 8, 7, 6] elsif @xxx == 2 list.must_equal [1, 2, 3, 4, 5, 51, 9, 8, 7, 6] else raise end end after { list << 6 } around { |t| list << 3; t.call; list << 7 } before { list << 4 } around { |t| list << 5; t.call; list << 8 } after { list << 9 } it "orders" do @xxx = 1 list.must_equal [1, 2, 3, 4, 5] end describe "more nesting fun" do before { list << 51 } it "orders" do @xxx = 2 list.must_equal [1, 2, 3, 4, 5, 51] end end end describe "fail" do it "does not fail with fiber error" do output = spawn_test <<-RUBY describe "x" do around { raise ArgumentError } after { puts "AFTER" } it("x") { } end RUBY output.must_include "ArgumentError: ArgumentError" output.wont_include "FiberError" end end end def spawn_test(code) Tempfile.open("XX") do |f| f.write <<-RUBY require "#{File.expand_path("../helper", __FILE__)}" require 'minitest/around/spec' #{code} RUBY f.close `ruby #{f.path}` end end minitest-around-0.4.1/README.md0000644000004100000410000000575213305251116016153 0ustar www-datawww-data[github]: https://github.com/splattael/minitest-around [doc]: http://rubydoc.info/github/splattael/minitest-around/master/file/README.md [gem]: https://rubygems.org/gems/minitest-around [travis]: https://travis-ci.org/splattael/minitest-around [codeclimate]: https://codeclimate.com/github/splattael/minitest-around [inchpages]: https://inch-ci.org/github/splattael/minitest-around # minitest-around [![Travis](https://img.shields.io/travis/splattael/minitest-around.svg?branch=master)][travis] [![Gem Version](https://img.shields.io/gem/v/minitest-around.svg)][gem] [![Code Climate](https://img.shields.io/codeclimate/github/splattael/minitest-around.svg)][codeclimate] [![Test Coverage](https://codeclimate.com/github/splattael/minitest-around/badges/coverage.svg)][codeclimate] [![Inline docs](https://inch-ci.org/github/splattael/minitest-around.svg?branch=master&style=flat)][inchpages] [Gem][gem] | [Source][github] | [Documentation][doc] Around block for minitest 5.X. Alternative for setup/teardown dance. ## Installation ```Bash gem install minitest-around ``` ## Usage ### Unit tests ```Ruby require 'minitest/autorun' require 'minitest/around/unit' require 'thread' class MutexTest < Minitest::Test def around(&block) Mutex.new.synchronize(&block) end def test_synchronized # ... end end ``` ### Spec ```Ruby require 'minitest/autorun' require 'minitest/around/spec' require 'tmpdir' describe "inside new directory" do around do |test| Dir.mktmpdir do |dir| @dir = dir Dir.chdir(dir) do test.call end end end it "is in new directory" do assert_equal @dir, Dir.pwd.sub("/private/var/", "/var/") end end ``` ## Multiple before/after blocks Minitest-around also enables the use of multiple before/after blocks, which normally don't work in minitest. ## Caveats - Test bodies won't be run if you don't test.call inside +around+. - around runs inside a Fiber, so use `Thread.get_thread_local` / `set_thread_local` instead of `Thread.current.[]` ### Minitest 5.X only `minitest-around` currently supports only `minitest` 5.X. Please see the [mt4](https://github.com/splattael/minitest-around/tree/mt4) branch for `minitest` 4.7.X support. ## License [MIT License](http://www.opensource.org/licenses/mit-license.php) ## Authors * [Peter Leitzen](https://github.com/splattael) ## [Contributors](https://github.com/splattael/minitest-around/graphs/contributors) * [Michael Grosser](https://github.com/grosser) * [Hendra Uzia](https://github.com/hendrauzia) * [Rick Martínez](https://github.com/rickmzp) * [Philip Nelson](https://github.com/pnelson) ## Contributing 1. Fork it 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Added some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create new Pull Request ## Test ```Bash bundle exec rake test ``` ## Release ```Bash edit lib/minitest/around/version.rb git commit rake release ``` minitest-around-0.4.1/.gitignore0000644000004100000410000000005313305251116016651 0ustar www-datawww-data*.gem .bundle Gemfile.lock pkg rdoc .rvmrc minitest-around-0.4.1/LICENSE0000644000004100000410000000213313305251116015667 0ustar www-datawww-dataCopyright (c) 2012 Peter Suschlik Copyright (c) 2018 Peter Leitzen (Suschlik) MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. minitest-around-0.4.1/examples/0000755000004100000410000000000013305251116016501 5ustar www-datawww-dataminitest-around-0.4.1/examples/chdir_spec.rb0000644000004100000410000000017313305251116021132 0ustar www-datawww-datacode = File.read("README.md")[%r{(.*)}m].split("\n")[2..-3].join("\n") puts code eval code minitest-around-0.4.1/Rakefile0000644000004100000410000000223013305251116016325 0ustar www-datawww-datarequire 'bundler/setup' require 'bundler/gem_tasks' require 'cucumber/rake/task' desc 'Default: run unit tests.' task :default => [:test, :"test:isolated", :features] # Test TEST_FILES = FileList.new('test/*_{test,spec}.rb') desc "Run all tests" task :test do TEST_FILES.each do |test_file| sh "bundle", "exec", "rake", "test:isolated", "TEST=#{test_file}" end end require 'rake/testtask' Rake::TestTask.new(:"test:isolated") do |test| test.test_files = TEST_FILES test.libs << 'test' test.verbose = true test.warning = true end # Examples EXAMPLES = FileList["examples/*.rb"] desc "Run all examples" task :"test:examples" do EXAMPLES.each do |example| sh "bundle", "exec", "ruby", example end end # Features Cucumber::Rake::Task.new(:features) do |t| skip_tags = %w[rspec todo].map { |tag| "--tag ~@#{tag}" }.join(" ") t.cucumber_opts = "features #{skip_tags}" end # RDoc require 'rdoc/task' RDoc::Task.new do |rdoc| rdoc.rdoc_dir = 'rdoc' rdoc.title = 'mintest-around' rdoc.main = 'README.md' rdoc.rdoc_files.include('README.md', 'LICENSE', 'lib/**/*.rb') rdoc.options << "--all" rdoc.options << "--markup markdown" end minitest-around-0.4.1/lib/0000755000004100000410000000000013305251116015431 5ustar www-datawww-dataminitest-around-0.4.1/lib/minitest/0000755000004100000410000000000013305251116017265 5ustar www-datawww-dataminitest-around-0.4.1/lib/minitest/around.rb0000644000004100000410000000033113305251116021077 0ustar www-datawww-datarequire 'minitest/around/version' require 'minitest/around/spec' require 'minitest/around/unit' module Minitest module Around # Ugly hack for cucumber/mt5 combo! VERSION = MinitestAround::VERSION end end minitest-around-0.4.1/lib/minitest/around/0000755000004100000410000000000013305251116020555 5ustar www-datawww-dataminitest-around-0.4.1/lib/minitest/around/version.rb0000644000004100000410000000005613305251116022570 0ustar www-datawww-datamodule MinitestAround VERSION = '0.4.1' end minitest-around-0.4.1/lib/minitest/around/spec.rb0000644000004100000410000000163613305251116022042 0ustar www-datawww-datarequire 'minitest/spec' require 'minitest/around/version' require 'minitest/around/unit' Minitest::Spec::DSL.class_eval do # - resume to call first part # - execute test # - resume fiber to execute last part def around(*args, &block) fib = nil before do fib = Fiber.new do |context, resume| begin context.instance_exec(resume, &block) rescue Object fib = :failed raise end end fib.resume(self, lambda { Fiber.yield }) end after { fib.resume unless fib == :failed } end # Minitest does not support multiple before/after blocks remove_method :before def before(type=nil, &block) include Module.new { define_method(:setup) { super(); instance_exec(&block) } } end remove_method :after def after(type=nil, &block) include Module.new { define_method(:teardown) { instance_exec(&block); super() } } end end minitest-around-0.4.1/lib/minitest/around/unit.rb0000644000004100000410000000047213305251116022064 0ustar www-datawww-datarequire 'minitest/test' require 'minitest/around/version' Minitest::Test.class_eval do alias_method :run_without_around, :run def run(*args) if defined?(around) result = nil around { result = run_without_around(*args) } result else run_without_around(*args) end end end minitest-around-0.4.1/minitest-around.gemspec0000644000004100000410000000115513305251116021354 0ustar www-datawww-datarequire './lib/minitest/around/version' Gem::Specification.new "minitest-around", MinitestAround::VERSION do |s| s.authors = ["Peter Leitzen"] s.email = ["peter-minitest-around@suschlik.de"] s.homepage = "https://github.com/splattael/minitest-around" s.summary = "Around block for minitest." s.description = "Alternative for setup/teardown dance." s.license = 'MIT' s.files = `git ls-files`.split("\n") s.add_dependency 'minitest', '~> 5.0' s.add_development_dependency 'rdoc' s.add_development_dependency 'rake' s.add_development_dependency 'cucumber', '~> 2.4.0' end minitest-around-0.4.1/config/0000755000004100000410000000000013305251116016130 5ustar www-datawww-dataminitest-around-0.4.1/config/cucumber.yml0000644000004100000410000000004513305251116020457 0ustar www-datawww-datadefault: --tags ~@ignore -f progress minitest-around-0.4.1/Gemfile0000644000004100000410000000031513305251116016155 0ustar www-datawww-datasource "https://rubygems.org" gemspec # Specify dependencies in minitest-around.gemspec if ENV['CODECLIMATE_REPO_TOKEN'] gem "codeclimate-test-reporter", "~> 0.0", :group => :test, :require => nil end minitest-around-0.4.1/features/0000755000004100000410000000000013305251116016501 5ustar www-datawww-dataminitest-around-0.4.1/features/step_definitions/0000755000004100000410000000000013305251116022047 5ustar www-datawww-dataminitest-around-0.4.1/features/step_definitions/around_steps.rb0000644000004100000410000000141313305251116025101 0ustar www-datawww-datarequire 'tempfile' Given(/^a file named "(.*?)" with:$/) do |filename, content| write_test_file(filename, content) end When(/^I run `rspec.*?(\S+)`$/) do |filename| path = path_for(filename) @output = `ruby #{path}` end Then(/^the output should contain:$/) do |content| assert_includes @output, content, @output end Then(/^the output should contain "(.*?)"$/) do |content| # 1 runs, 0 assertions, 0 failures, 0 errors, 0 skips runs = $1 if content =~ /(\d+) examples?/ errors = $1 if content =~ /(\d+) errors?/ failures = $1 if content =~ /(\d+) failures?/ skips = $1 if content =~ /(\d+) pending/ content = /#{runs} runs, \d+ assertions, #{failures || 0} failures, #{errors || 0} errors, #{skips || 0} skips/ assert_match content, @output, @output end minitest-around-0.4.1/features/support/0000755000004100000410000000000013305251116020215 5ustar www-datawww-dataminitest-around-0.4.1/features/support/env.rb0000644000004100000410000000177013305251116021337 0ustar www-datawww-datarequire 'minitest/spec' class MyWorld include Minitest::Assertions attr_accessor :assertions def initialize self.assertions = 0 @temp_pathes = {} end def write_test_file(filename, content) # RSpec.describe -> describe content.gsub!("RSpec.describe", "describe") # example.run -> example.call content.gsub!("example.run", "example.call") # expect(..).to .. -> expect(..).must_equal .. content.gsub!("to eq", "must_equal") content = <<-RUBY + content require 'minitest/autorun' require '#{File.expand_path("../../../lib/minitest/around/spec", __FILE__)}' RUBY write_file(filename, content) end def write_file(filename, content) Tempfile.open(filename) do |file| file.write content assoc_tempfile filename, file.path end end def path_for(filename) @temp_pathes.fetch(filename) end private def assoc_tempfile(filename, temp_path) @temp_pathes[filename] = temp_path end end World do MyWorld.new end minitest-around-0.4.1/features/around_spec.feature0000644000004100000410000002456113305251116022370 0ustar www-datawww-dataFeature: `around` hooks `around` hooks receive the example as a block argument, extended to behave as a proc. This lets you define code that should be executed before and after the example. Of course, you can do the same thing with `before` and `after` hooks; and it's often cleaner to do so. Where `around` hooks shine is when you want to run an example within a block. For instance, if your database library offers a transaction method that receives a block, you can use an `around` to cleanly open and close the transaction around the example. **WARNING:** `around` hooks do not share state with the example the way `before` and `after` hooks do. This means that you cannot share instance variables between `around` hooks and examples. **WARNING:** Mock frameworks are set up and torn down within the context of running the example. You cannot interact with them directly in `around` hooks. Scenario: Use the example as a proc within the block passed to `around()` Given a file named "example_spec.rb" with: """ruby class Database def self.transaction puts "open transaction" yield puts "close transaction" end end RSpec.describe "around filter" do around(:example) do |example| Database.transaction(&example) end it "gets run in order" do puts "run the example" end end """ When I run `rspec example_spec.rb` Then the output should contain: """ open transaction run the example close transaction """ Scenario: Invoke the example using `run()` Given a file named "example_spec.rb" with: """ruby RSpec.describe "around hook" do around(:example) do |example| puts "around example before" example.run puts "around example after" end it "gets run in order" do puts "in the example" end end """ When I run `rspec example_spec.rb` Then the output should contain: """ around example before in the example around example after """ @rspec Scenario: Access the example metadata Given a file named "example_spec.rb" with: """ruby RSpec.describe "something" do around(:example) do |example| puts example.metadata[:foo] example.run end it "does something", :foo => "this should show up in the output" do end end """ When I run `rspec example_spec.rb` Then the output should contain "this should show up in the output" Scenario: An around hook continues to run even if the example throws an exception Given a file named "example_spec.rb" with: """ruby RSpec.describe "something" do around(:example) do |example| puts "around example setup" example.run puts "around example cleanup" end it "still executes the entire around hook" do fail "the example blows up" end end """ When I run `rspec example_spec.rb` Then the output should contain "1 example, 1 error" And the output should contain: """ around example setup around example cleanup """ @rspec Scenario: Define a global `around` hook Given a file named "example_spec.rb" with: """ruby RSpec.configure do |c| c.around(:example) do |example| puts "around example before" example.run puts "around example after" end end RSpec.describe "around filter" do it "gets run in order" do puts "in the example" end end """ When I run `rspec example_spec.rb` Then the output should contain: """ around example before in the example around example after """ Scenario: Per example hooks are wrapped by the `around` hook Given a file named "example_spec.rb" with: """ruby RSpec.describe "around filter" do around(:example) do |example| puts "around example before" example.run puts "around example after" end before(:example) do puts "before example" end after(:example) do puts "after example" end it "gets run in order" do puts "in the example" end end """ When I run `rspec example_spec.rb` Then the output should contain: """ around example before before example in the example after example around example after """ @todo Scenario: Context hooks are NOT wrapped by the `around` hook Given a file named "example_spec.rb" with: """ruby RSpec.describe "around filter" do around(:example) do |example| puts "around example before" example.run puts "around example after" end before(:context) do puts "before context" end after(:context) do puts "after context" end it "gets run in order" do puts "in the example" end end """ When I run `rspec --format progress example_spec.rb` Then the output should contain: """ before context around example before in the example around example after .after context """ @rspec Scenario: Examples run by an `around` block are run in the configured context Given a file named "example_spec.rb" with: """ruby module IncludedInConfigureBlock def included_in_configure_block; true; end end RSpec.configure do |c| c.include IncludedInConfigureBlock end RSpec.describe "around filter" do around(:example) do |example| example.run end it "runs the example in the correct context" do expect(included_in_configure_block).to be_truthy end end """ When I run `rspec example_spec.rb` Then the output should contain "1 example, 0 failure" @rspec Scenario: Implicitly pending examples are detected as Not yet implemented Given a file named "example_spec.rb" with: """ruby RSpec.describe "implicit pending example" do around(:example) do |example| example.run end it "should be detected as Not yet implemented" end """ When I run `rspec example_spec.rb` Then the output should contain "1 example, 0 failures, 1 pending" And the output should contain: """ Pending: (Failures listed here are expected and do not affect your suite's status) 1) implicit pending example should be detected as Not yet implemented # Not yet implemented # ./example_spec.rb:6 """ @rspec Scenario: Explicitly pending examples are detected as pending Given a file named "example_spec.rb" with: """ruby RSpec.describe "explicit pending example" do around(:example) do |example| example.run end it "should be detected as pending" do pending fail end end """ When I run `rspec example_spec.rb` Then the output should contain "1 example, 0 failures, 1 pending" And the output should contain: """ Pending: (Failures listed here are expected and do not affect your suite's status) 1) explicit pending example should be detected as pending # No reason given """ Scenario: Multiple `around` hooks in the same scope Given a file named "example_spec.rb" with: """ruby RSpec.describe "if there are multiple around hooks in the same scope" do around(:example) do |example| puts "first around hook before" example.run puts "first around hook after" end around(:example) do |example| puts "second around hook before" example.run puts "second around hook after" end it "they should all be run" do puts "in the example" expect(1).to eq(1) end end """ When I run `rspec example_spec.rb` Then the output should contain "1 example, 0 failure" And the output should contain: """ first around hook before second around hook before in the example second around hook after first around hook after """ Scenario: `around` hooks in multiple scopes Given a file named "example_spec.rb" with: """ruby RSpec.describe "if there are around hooks in an outer scope" do around(:example) do |example| puts "first outermost around hook before" example.run puts "first outermost around hook after" end around(:example) do |example| puts "second outermost around hook before" example.run puts "second outermost around hook after" end describe "outer scope" do around(:example) do |example| puts "first outer around hook before" example.run puts "first outer around hook after" end around(:example) do |example| puts "second outer around hook before" example.run puts "second outer around hook after" end describe "inner scope" do around(:example) do |example| puts "first inner around hook before" example.run puts "first inner around hook after" end around(:example) do |example| puts "second inner around hook before" example.run puts "second inner around hook after" end it "they should all be run" do puts "in the example" end end end end """ When I run `rspec example_spec.rb` Then the output should contain "1 example, 0 failure" And the output should contain: """ first outermost around hook before second outermost around hook before first outer around hook before second outer around hook before first inner around hook before second inner around hook before in the example second inner around hook after first inner around hook after second outer around hook after first outer around hook after second outermost around hook after first outermost around hook after """