diff --git a/.travis.yml b/.travis.yml index a8c96ed..fd6bd72 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,8 @@ language: ruby rvm: - - 1.9.3 - - 2.0.0 - - 2.1.3 + - 2.3 + - 2.4 + - 2.5 -script: bundle exec rake +notifications: + email: false diff --git a/Gemfile b/Gemfile index 4315b63..58f21e5 100644 --- a/Gemfile +++ b/Gemfile @@ -1,7 +1,15 @@ +# frozen_string_literal: true + source 'https://rubygems.org' gem 'coveralls', require: false +group :development, :test do + gem 'faraday', '~> 0.15.3' + gem 'mocha', '~> 1.7' + gem 'webmock', '~> 3.4', '>= 3.4.2' + gem 'yard', '~> 0.9.16', require: false +end + # Specify your gem's dependencies in minitest-vcr.gemspec gemspec - diff --git a/README.md b/README.md index 2941cd8..9d4ea23 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,10 @@ minitest-vcr |------------------------ | ----------------- | | Gem name | minitest-vcr | | License | [MIT](LICENSE.txt) | -| Version | [![Gem Version](https://badge.fury.io/rb/minitest-vcr.png)](http://badge.fury.io/rb/minitest-vcr) | -| Continuous Integration | [![Build Status](https://travis-ci.org/mfpiccolo/minitest-vcr.png?branch=master)](https://travis-ci.org/mfpiccolo/minitest-vcr) -| Test Coverage | [![Coverage Status](https://coveralls.io/repos/mfpiccolo/minitest-vcr/badge.png?branch=master)](https://coveralls.io/r/mfpiccolo/minitest-vcr?branch=coveralls) -| Grade | [![Code Climate](https://codeclimate.com/github/mfpiccolo/minitest-vcr.png)](https://codeclimate.com/github/mfpiccolo/minitest-vcr) -| Dependencies | [![Dependency Status](https://gemnasium.com/mfpiccolo/minitest-vcr.png)](https://gemnasium.com/mfpiccolo/minitest-vcr) +| Version | [![Gem Version](https://badge.fury.io/rb/minitest-vcr.svg)](https://badge.fury.io/rb/minitest-vcr) | +| Continuous Integration | [![Build Status](https://travis-ci.org/mfpiccolo/minitest-vcr.svg?branch=master)](https://travis-ci.org/mfpiccolo/minitest-vcr) +| Test Coverage | [![Coverage Status](https://coveralls.io/repos/github/mfpiccolo/minitest-vcr/badge.svg?branch=master)](https://coveralls.io/github/mfpiccolo/minitest-vcr?branch=master) +| Grade | [![Maintainability](https://api.codeclimate.com/v1/badges/9627118e4e89447cb51a/maintainability)](https://codeclimate.com/github/mfpiccolo/minitest-vcr/maintainability) | Homepage | [http://mfpiccolo.github.io/minitest-vcr][homepage] | | Documentation | [http://rdoc.info/github/mfpiccolo/minitest-vcr/frames][documentation] | | Issues | [https://github.com/mfpiccolo/minitest-vcr/issues][issues] | diff --git a/Rakefile b/Rakefile index bf578f7..80acc87 100644 --- a/Rakefile +++ b/Rakefile @@ -1,39 +1,17 @@ -# encoding: utf-8 - -require 'rubygems' - -begin - require 'bundler' -rescue LoadError => e - warn e.message - warn "Run `gem install bundler` to install Bundler." - exit -1 -end - -begin - Bundler.setup(:development) -rescue Bundler::BundlerError => e - warn e.message - warn "Run `bundle install` to install missing gems." - exit e.status_code -end - -require 'rake' - -require 'rubygems/tasks' -Gem::Tasks.new +# frozen_string_literal: true +require 'bundler/gem_tasks' require 'rake/testtask' -Rake::TestTask.new do |test| - test.libs << 'test' - test.pattern = 'test/**/*_test.rb' - test.verbose = true +Rake::TestTask.new(:test) do |t| + t.libs << 'test' + t.libs << 'lib' + t.test_files = FileList['test/**/*_test.rb'] + t.verbose = true end +task default: :test + require 'yard' YARD::Rake::YardocTask.new -task :doc => :yard - -task :default => :test - +task doc: :yard diff --git a/lib/minitest-vcr.rb b/lib/minitest-vcr.rb index 0ca0673..c72366d 100644 --- a/lib/minitest-vcr.rb +++ b/lib/minitest-vcr.rb @@ -1,5 +1,6 @@ require "minitest-vcr/version" require "minitest-vcr/spec" +require "minitest-vcr/active_support/test_case" module MinitestVcr def self.included(base) diff --git a/lib/minitest-vcr/active_support/test_case.rb b/lib/minitest-vcr/active_support/test_case.rb new file mode 100644 index 0000000..1302f54 --- /dev/null +++ b/lib/minitest-vcr/active_support/test_case.rb @@ -0,0 +1,35 @@ +# frozen_string_literal: true + +require 'vcr' +require 'active_support/testing/metadata' + +module MinitestVcr + module ActiveSupport + module TestCase + def before_setup + super + + return unless metadata.key?(:vcr) + + base_path = self.class.name.split('::').map do |p| + p.underscore.tr(' ', '_') + end + + base_path = File.join(*base_path) + + file_name = name.gsub(/^test_.\d+_/, ' ').strip.tr(' ', '_') + path = File.join(base_path, file_name) + + options = metadata[:vcr].is_a?(Hash) ? metadata[:vcr] : {} + + ::VCR.insert_cassette(path, options) + end + + def after_teardown + ::VCR.eject_cassette if metadata.key?(:vcr) + + super + end + end + end +end diff --git a/lib/minitest-vcr/spec.rb b/lib/minitest-vcr/spec.rb index d6457bb..f202a05 100644 --- a/lib/minitest-vcr/spec.rb +++ b/lib/minitest-vcr/spec.rb @@ -3,23 +3,24 @@ module MinitestVcr module Spec - - def self.configure! - run_before = lambda do |example| + module SetupAndTeardown + def setup + super if metadata[:vcr] options = metadata[:vcr].is_a?(Hash) ? metadata[:vcr] : {} - VCR.insert_cassette StringHelpers.vcr_path(example), options + VCR.insert_cassette StringHelpers.vcr_path(self), options end end - run_after = lambda do |example| + def teardown + super ::VCR.eject_cassette if metadata[:vcr] end - - ::MiniTest::Spec.before :each, &run_before - ::MiniTest::Spec.after :each, &run_after end + def self.configure! + ::MiniTest::Spec.send(:include, SetupAndTeardown) + end end # Spec module StringHelpers @@ -55,25 +56,3 @@ def self.extract_example_description(example) end end # MinitestVcr - -if defined? ActiveSupport::TestCase - class ActiveSupport::TestCase - def before_setup - super - if self.class.name.match("::vcr::") - base_path = self.class.name.split("::") - .map {|p| p.underscore.gsub(" ", "_") unless p == "vcr" }.join("/") - - file_name = name.gsub(/^test_.\d+_/, " ").strip.gsub(" ", "_") - VCR.insert_cassette(base_path + "/" + file_name) - end - end - - def after_teardown - if self.class.name.match("::vcr::") - ::VCR.eject_cassette - end - super - end - end -end diff --git a/minitest-vcr.gemspec b/minitest-vcr.gemspec index d109e62..66de117 100644 --- a/minitest-vcr.gemspec +++ b/minitest-vcr.gemspec @@ -1,5 +1,6 @@ -# coding: utf-8 -lib = File.expand_path('../lib', __FILE__) +# frozen_string_literal: true + +lib = File.expand_path('lib', __dir__) $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib) require 'minitest-vcr/version' @@ -13,20 +14,20 @@ Gem::Specification.new do |spec| spec.homepage = "" spec.license = "MIT" - spec.files = `git ls-files`.split($/) - spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) } - spec.test_files = spec.files.grep(%r{^(test|spec|features)/}) + # Specify which files should be added to the gem when it is released. + # The `git ls-files -z` loads the files in the RubyGem that have been added into git. + spec.files = Dir.chdir(File.expand_path(__dir__)) do + `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) } + end + spec.bindir = "exe" + spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) } spec.require_paths = ["lib"] + spec.add_dependency "activesupport-testing-metadata", "~> 0.1.0" + spec.add_dependency "minispec-metadata", "~> 3.0" + spec.add_dependency "minitest", ">= 5.0" spec.add_dependency "vcr", ">= 2.9" - spec.add_dependency "minitest", ">= 4.7.5" - spec.add_dependency "minispec-metadata", "~> 2.0" - spec.add_development_dependency "bundler", "~> 1.5" - spec.add_development_dependency "rake", "~> 10.0" - spec.add_development_dependency "mocha", "~> 1.0" - spec.add_development_dependency "faraday", "~> 0.9" - spec.add_development_dependency "rubygems-tasks", "~> 0.2" - spec.add_development_dependency "yard", "~> 0.8" - spec.add_development_dependency "webmock", "~> 1.17" + spec.add_development_dependency "bundler", "~> 1.16" + spec.add_development_dependency "rake", "~> 10.0" end diff --git a/test/cassettes/MinitestVcr_Spec/an_it_with_metadata/with_a_nested_example_group/can_supply_metadata.yml b/test/cassettes/MinitestVcr_Spec/an_it_with_metadata/with_a_nested_example_group/can_supply_metadata.yml index b3c7439..b9adf63 100644 --- a/test/cassettes/MinitestVcr_Spec/an_it_with_metadata/with_a_nested_example_group/can_supply_metadata.yml +++ b/test/cassettes/MinitestVcr_Spec/an_it_with_metadata/with_a_nested_example_group/can_supply_metadata.yml @@ -8,7 +8,7 @@ http_interactions: string: '' headers: User-Agent: - - Faraday v0.9.1 + - Faraday v0.15.3 Accept-Encoding: - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 Accept: @@ -23,25 +23,25 @@ http_interactions: Cache-Control: - max-age=604800 Content-Type: - - text/html + - text/html; charset=UTF-8 Date: - - Tue, 31 Mar 2015 20:03:52 GMT + - Sat, 13 Oct 2018 15:37:43 GMT Etag: - - '"359670651"' + - '"1541025663+ident"' Expires: - - Tue, 07 Apr 2015 20:03:52 GMT + - Sat, 20 Oct 2018 15:37:43 GMT Last-Modified: - Fri, 09 Aug 2013 23:54:35 GMT Server: - - ECS (sjc/16A4) + - ECS (ord/4CD5) + Vary: + - Accept-Encoding X-Cache: - HIT - X-Ec-Custom-Error: - - '1' Content-Length: - - '1270' + - '606' body: - encoding: UTF-8 + encoding: ASCII-8BIT string: "\n\n\n Example Domain\n\n \ \n \n \n

More information...

\n\n\n\n" http_version: - recorded_at: Tue, 31 Mar 2015 20:03:52 GMT -recorded_with: VCR 2.9.3 + recorded_at: Sat, 13 Oct 2018 15:37:44 GMT +recorded_with: VCR 4.0.0 diff --git a/test/cassettes/minitest_vcr/active_support/test_case_test/test_create_a_cassette_name_with_the_proper_path.yml b/test/cassettes/minitest_vcr/active_support/test_case_test/test_create_a_cassette_name_with_the_proper_path.yml new file mode 100644 index 0000000..6594926 --- /dev/null +++ b/test/cassettes/minitest_vcr/active_support/test_case_test/test_create_a_cassette_name_with_the_proper_path.yml @@ -0,0 +1,64 @@ +--- +http_interactions: +- request: + method: get + uri: http://example.com/ + body: + encoding: US-ASCII + string: '' + headers: + User-Agent: + - Faraday v0.15.2 + Accept-Encoding: + - gzip;q=1.0,deflate;q=0.6,identity;q=0.3 + Accept: + - "*/*" + response: + status: + code: 200 + message: OK + headers: + Accept-Ranges: + - bytes + Cache-Control: + - max-age=604800 + Content-Type: + - text/html; charset=UTF-8 + Date: + - Sun, 09 Sep 2018 01:24:50 GMT + Etag: + - '"1541025663+gzip"' + Expires: + - Sun, 16 Sep 2018 01:24:50 GMT + Last-Modified: + - Fri, 09 Aug 2013 23:54:35 GMT + Server: + - ECS (ord/573A) + Vary: + - Accept-Encoding + X-Cache: + - HIT + Content-Length: + - '606' + body: + encoding: ASCII-8BIT + string: "\n\n\n Example Domain\n\n + \ \n \n \n \n\n\n\n
\n

Example Domain

\n + \

This domain is established to be used for illustrative examples in + documents. You may use this\n domain in examples without prior coordination + or asking for permission.

\n

More + information...

\n
\n\n\n" + http_version: + recorded_at: Sun, 09 Sep 2018 01:24:50 GMT +recorded_with: VCR 4.0.0 diff --git a/test/helper.rb b/test/helper.rb index 66b0010..71181dd 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -19,6 +19,7 @@ require "minitest/autorun" require "minispec-metadata" +require "active_support/testing/metadata" require "vcr" require "minitest-vcr" require "webmock" diff --git a/test/minitest-vcr/active_support/test_case_test.rb b/test/minitest-vcr/active_support/test_case_test.rb new file mode 100644 index 0000000..7c07817 --- /dev/null +++ b/test/minitest-vcr/active_support/test_case_test.rb @@ -0,0 +1,17 @@ +require "helper" + +class ActiveSupport::TestCase + include MinitestVcr::ActiveSupport::TestCase +end + +class MinitestVcr::ActiveSupport::TestCaseTest < ActiveSupport::TestCase + test 'create a cassette name with the proper path', :vcr do + conn = Faraday.new + @response = conn.get 'http://example.com' + + expected = File.join(self.class.name.underscore, name) + actual = VCR.current_cassette.name + + assert_equal expected, actual + end +end diff --git a/test/minitest-vcr/spec_test.rb b/test/minitest-vcr/spec_test.rb index 75f27ef..9773c94 100644 --- a/test/minitest-vcr/spec_test.rb +++ b/test/minitest-vcr/spec_test.rb @@ -43,13 +43,9 @@ describe "#configure!", :vcr do - before do - ::MiniTest::Spec.expects(:before).with(:each) - ::MiniTest::Spec.expects(:after).with(:each) - end - - it "should call before and after with proper args and block" do + it "should include setup and teardown module into Minitest::Spec" do MinitestVcr::Spec.configure! + Minitest::Spec.included_modules.must_include(MinitestVcr::Spec::SetupAndTeardown) end end end