├── .gitignore ├── .rspec ├── .rubocop.yml ├── .travis.yml ├── Changelog.md ├── Gemfile ├── README.md ├── Rakefile ├── fixtures ├── rails50_app │ ├── Gemfile │ ├── Rakefile │ ├── app │ │ └── models │ │ │ ├── application_record.rb │ │ │ └── foo.rb │ ├── bin │ │ ├── rake │ │ └── spring │ ├── config │ │ ├── application.rb │ │ ├── boot.rb │ │ ├── environment.rb │ │ ├── environments │ │ │ ├── development.rb │ │ │ ├── production.rb │ │ │ └── test.rb │ │ └── initializers │ │ │ └── filter_parameter_logging.rb │ ├── db │ │ ├── migrate │ │ │ └── 20140830065127_create_foos.rb │ │ └── schema.rb │ └── test │ │ ├── fixtures │ │ └── foos.yml │ │ ├── test_helper.rb │ │ └── unit │ │ └── foo_test.rb ├── rails51_app │ ├── Gemfile │ ├── Rakefile │ ├── app │ │ └── models │ │ │ ├── application_record.rb │ │ │ └── foo.rb │ ├── bin │ │ ├── rake │ │ └── spring │ ├── config │ │ ├── application.rb │ │ ├── boot.rb │ │ ├── environment.rb │ │ ├── environments │ │ │ ├── development.rb │ │ │ ├── production.rb │ │ │ └── test.rb │ │ └── initializers │ │ │ └── filter_parameter_logging.rb │ ├── db │ │ ├── migrate │ │ │ └── 20140830065127_create_foos.rb │ │ └── schema.rb │ └── test │ │ ├── fixtures │ │ └── foos.yml │ │ ├── test_helper.rb │ │ └── unit │ │ └── foo_test.rb └── rails52_app │ ├── Gemfile │ ├── Rakefile │ ├── app │ └── models │ │ ├── application_record.rb │ │ └── foo.rb │ ├── bin │ ├── rake │ └── spring │ ├── config │ ├── application.rb │ ├── boot.rb │ ├── environment.rb │ ├── environments │ │ ├── development.rb │ │ ├── production.rb │ │ └── test.rb │ └── initializers │ │ └── filter_parameter_logging.rb │ ├── db │ ├── migrate │ │ └── 20140830065127_create_foos.rb │ └── schema.rb │ └── test │ ├── fixtures │ └── foos.yml │ ├── models │ └── foo_test.rb │ └── test_helper.rb ├── lib ├── memory_test_fix.rb └── memory_test_fix │ ├── railtie.rb │ ├── schema_file_loader.rb │ └── schema_loader.rb ├── memory_test_fix.gemspec ├── spec ├── spec_helper.rb └── unit │ └── memory_test_fix │ └── schema_loader_spec.rb └── test ├── integration └── integration_test.rb └── test_helper.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .*.swp 3 | Gemfile.lock 4 | fixtures/rails*_app/Gemfile.lock 5 | fixtures/rails*_app/db/*.sqlite3 6 | fixtures/rails*_app/log/*.log 7 | fixtures/rails*_app/config/database.yml 8 | doc/ 9 | .yardoc/ 10 | .bundle/ 11 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --require spec_helper 3 | -------------------------------------------------------------------------------- /.rubocop.yml: -------------------------------------------------------------------------------- 1 | require: 2 | - rubocop-rspec 3 | 4 | AllCops: 5 | Exclude: 6 | - 'fixtures/**/*' 7 | TargetRubyVersion: 2.3 8 | 9 | # Dot at end of line makes it clearer that the line is not done 10 | Layout/DotPosition: 11 | EnforcedStyle: trailing 12 | 13 | # Test and spec describe blocks can be any size. 14 | # Gem specification can be any size. 15 | Metrics/BlockLength: 16 | Exclude: 17 | - 'spec/**/*' 18 | - 'test/**/*' 19 | - 'memory_test_fix.gemspec' 20 | 21 | # Require lines to fit in pull requests. 22 | Metrics/LineLength: 23 | Max: 92 24 | 25 | # Prefer parentheses in method definitions 26 | Style/MethodDefParentheses: 27 | EnforcedStyle: require_parentheses 28 | 29 | # Use older RuboCop default 30 | Style/PercentLiteralDelimiters: 31 | PreferredDelimiters: 32 | '%w': () 33 | 34 | # Always use raise to raise exceptions 35 | Style/SignalException: 36 | EnforcedStyle: only_raise 37 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: ruby 2 | rvm: 3 | - 2.3 4 | - 2.4 5 | - 2.5 6 | - 2.6 7 | - ruby-head 8 | - ruby-head-clang 9 | - jruby-9.1.15.0 10 | - jruby-9.2.5.0 11 | - jruby-head 12 | matrix: 13 | allow_failures: 14 | - rvm: jruby-9.1.15.0 15 | - rvm: jruby-9.2.5.0 16 | - rvm: jruby-head 17 | - rvm: ruby-head 18 | - rvm: ruby-head-clang 19 | 20 | branches: 21 | only: 22 | - master 23 | -------------------------------------------------------------------------------- /Changelog.md: -------------------------------------------------------------------------------- 1 | # Changelog 2 | 3 | ## Unreleased 4 | 5 | * Drop support for Ruby 2.2 6 | 7 | ## 1.5.1 / 2018-02-14 8 | 9 | * Fix support for Rails 5.2: Handle Rails 5.2's repeated connection 10 | establishment. 11 | 12 | ## 1.5.0 / 2018-02-14 13 | 14 | * Add support for Rails 5.2 15 | 16 | ## 1.4.2 / 2017-04-28 17 | 18 | * Update dependencies to allow use with Rails 5.1 19 | 20 | ## 1.4.1 / 2017-03-27 21 | 22 | * Stop silent schema loading dropping STDOUT completely 23 | 24 | ## 1.4.0 / 2016-07-06 25 | 26 | * Add support for Rails 5.0 27 | * Drop support for Rails 4.x 28 | 29 | ## 1.3.0 / 2015-09-07 30 | 31 | * Drop support for Rails 3.2 32 | * Support use of Spring 33 | 34 | ## 1.2.2 / 2014-09-09 35 | 36 | * Make dependency declaration match set of supported Rails versions 37 | * Document public API 38 | * Add integration test for upcoming Rails 4.2 39 | 40 | ## 1.2.1 / 2014-09-07 41 | 42 | * Ensure all files are available in the gem 43 | 44 | ## 1.2.0 / 2014-09-07 45 | 46 | * Drop support for Rails 3.0 and 3.1 47 | 48 | ## 1.1.0 / 2014-08-30 49 | 50 | * Officially add support for Rails 4.1 51 | * Add 'migrate' option (thanks, Stephan Zalewski) 52 | * Use railties to run init_schema at the appropriate moment 53 | (thanks, Stephan Zalewski) 54 | * Fix Rails 4.0 support by initializing the schema whenever an 55 | ActiveRecord connection is established 56 | 57 | ## 1.0.0 / 2014-04-07 58 | 59 | * Officially add support for Rails 4.0 60 | * Drop support for Rails 2.3 61 | 62 | ## 0.2.2 / 2013-02-02 63 | 64 | * Fix broken Rails 3.0 compatibility 65 | * Restore Rails 2.3 compatibility 66 | 67 | ## 0.2.1 / 2013-02-02 68 | 69 | * Add support for Rails 3.2 (thanks, Stephan Zalewski) 70 | 71 | ## 0.2.0 / 2011-01-06 72 | 73 | * Rails 3 compatibility (thanks, Greg Weber) 74 | * Only support Rails 3 75 | * Use Rails 3 plugin loading mechanism to avoid loading the schema 76 | before initialization is complete 77 | * Use 'verbosity' setting from the current environment, not from 78 | 'test' 79 | 80 | ## 0.1.3 / 2010-06-26 81 | 82 | * Support other environments besides 'test' (thanks, Erik Hanson & 83 | Matt Scilipoti) 84 | 85 | ## 0.1.1 / 2009-02-08 86 | 87 | * Fix broken gemspec that did not include any files, nor all 88 | authors. 89 | 90 | ## 0.1 / 2009-02-08 91 | 92 | * Initial release as a GemPlugin 93 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | source 'https://rubygems.org' 4 | 5 | gemspec 6 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # MemoryTestFix 2 | 3 | **This project is no longer being maintained.** 4 | 5 | A simple fix to run your Rails tests with sqlite. From the 6 | [example by Chris Roos](http://blog.seagul.co.uk/articles/2006/02/08/in-memory-sqlite-database-for-rails-testing). 7 | 8 | ## Status 9 | 10 | [![Dependency Status](https://gemnasium.com/mvz/memory_test_fix.png)](https://gemnasium.com/mvz/memory_test_fix) 11 | [![Build Status](https://travis-ci.org/mvz/memory_test_fix.png?branch=master)](https://travis-ci.org/mvz/memory_test_fix) 12 | [![Code Climate](https://codeclimate.com/github/mvz/memory_test_fix.png)](https://codeclimate.com/github/mvz/memory_test_fix) 13 | 14 | ## Usage 15 | 16 | Add the gem to your bundle by adding 17 | 18 | gem 'memory_test_fix' 19 | 20 | to your `Gemfile`. 21 | 22 | In your database.yml, use: 23 | 24 | test: 25 | adapter: sqlite3 26 | database: ":memory:" 27 | 28 | It runs much faster! 29 | 30 | You can also adjust the verbosity of the output: 31 | 32 | test: 33 | adapter: sqlite3 34 | database: ":memory:" 35 | verbosity: silent 36 | 37 | To use rails migrations instead of loading `db/schema.rb` 38 | 39 | test: 40 | adapter: sqlite3 41 | database: ":memory:" 42 | migrate: true 43 | 44 | You can also use this with other (testing) environments, not just 'test'. 45 | 46 | ## Rails Versions 47 | 48 | In general, Bundler should pick a compatible version for you if you don't 49 | specify one in your `Gemfile`. 50 | 51 | That said, the latest version of this gem is compatible with Rails 5.0, 5.1 and 52 | upcoming 5.2. 53 | If you're still on Rails 4, you can use version 1.3.0. Unfortunately, the hard 54 | requirement of Ruby 2.2 or above for Rails 5 means `memory_test_fix` cannot 55 | support Rails 4 and 5 at the same time. 56 | 57 | If you're using a version of Rails older than 4, your first priority should be 58 | upgrading Rails. If that's really not an option, you can use version 1.2.2 of 59 | this gem with Rails 3.2. If you're using Rails 3.1 or 3.0, use version 1.1.0. 60 | If you're using Rails 2.3, use version 0.2.2. If you're using an even older 61 | version of Rails, use version 0.1.3. 62 | 63 | ## Authors 64 | 65 | The [original 66 | hack](http://chrisroos.co.uk/blog/2006-02-08-in-memory-sqlite-database-for-rails-testing) 67 | this gem is based on was created by Chris Roos. 68 | 69 | The hack was adapted as a Rails plugin by [Geoffrey 70 | Grosenbach](http://nubyonrails.com). 71 | 72 | The following people have contributed: 73 | 74 | * Kakutani Shintaro 75 | * Matijs van Zuijlen 76 | * Erik Hanson & Matt Scilipoti 77 | * Greg Weber 78 | * Stephan Zalewski 79 | 80 | MemoryTestFix is maintained by [Matijs van Zuijlen](http://www.matijs.net/) 81 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'rake' 4 | require 'rake/testtask' 5 | require 'rspec/core/rake_task' 6 | require 'bundler/gem_tasks' 7 | require 'rake/clean' 8 | 9 | CLEAN.include 'fixtures/**/Gemfile.lock' 10 | CLOBBER.include 'pkg' 11 | 12 | desc 'Default: run tests.' 13 | task default: :test 14 | 15 | namespace :test do 16 | Rake::TestTask.new(:integration) do |t| 17 | t.libs += %w(lib test) 18 | t.pattern = 'test/integration/**/*_test.rb' 19 | t.verbose = true 20 | end 21 | end 22 | 23 | RSpec::Core::RakeTask.new(:spec) 24 | 25 | desc 'Test the memory_test_fix plugin.' 26 | task test: [:spec, 'test:integration'] 27 | -------------------------------------------------------------------------------- /fixtures/rails50_app/Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'rails', '~> 5.0.0' 4 | gem 'sqlite3', platform: :ruby 5 | gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby 6 | gem 'memory_test_fix', path: '../..' 7 | 8 | group :development, :test do 9 | gem 'spring' 10 | end 11 | -------------------------------------------------------------------------------- /fixtures/rails50_app/Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require_relative 'config/application' 5 | 6 | Rails.application.load_tasks 7 | -------------------------------------------------------------------------------- /fixtures/rails50_app/app/models/application_record.rb: -------------------------------------------------------------------------------- 1 | class ApplicationRecord < ActiveRecord::Base 2 | self.abstract_class = true 3 | end 4 | -------------------------------------------------------------------------------- /fixtures/rails50_app/app/models/foo.rb: -------------------------------------------------------------------------------- 1 | class Foo < ApplicationRecord 2 | end 3 | -------------------------------------------------------------------------------- /fixtures/rails50_app/bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | begin 3 | load File.expand_path('../spring', __FILE__) 4 | rescue LoadError => e 5 | raise unless e.message.include?('spring') 6 | end 7 | require_relative '../config/boot' 8 | require 'rake' 9 | Rake.application.run 10 | -------------------------------------------------------------------------------- /fixtures/rails50_app/bin/spring: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | # This file loads spring without using Bundler, in order to be fast. 4 | # It gets overwritten when you run the `spring binstub` command. 5 | 6 | unless defined?(Spring) 7 | require 'rubygems' 8 | require 'bundler' 9 | 10 | if (match = Bundler.default_lockfile.read.match(/^GEM$.*?^ (?: )*spring \((.*?)\)$.*?^$/m)) 11 | Gem.paths = { 'GEM_PATH' => [Bundler.bundle_path.to_s, *Gem.path].uniq.join(Gem.path_separator) } 12 | gem 'spring', match[1] 13 | require 'spring/binstub' 14 | end 15 | end 16 | -------------------------------------------------------------------------------- /fixtures/rails50_app/config/application.rb: -------------------------------------------------------------------------------- 1 | require_relative 'boot' 2 | 3 | require 'rails' 4 | require 'active_record/railtie' 5 | require 'rails/test_unit/railtie' 6 | 7 | Bundler.require(*Rails.groups) 8 | 9 | module RailsTestApp 10 | class Application < Rails::Application 11 | config.active_record.raise_in_transactional_callbacks = true 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /fixtures/rails50_app/config/boot.rb: -------------------------------------------------------------------------------- 1 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) 2 | 3 | require 'bundler/setup' # Set up gems listed in the Gemfile. 4 | -------------------------------------------------------------------------------- /fixtures/rails50_app/config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require_relative 'application' 3 | 4 | # Initialize the Rails application. 5 | Rails.application.initialize! 6 | -------------------------------------------------------------------------------- /fixtures/rails50_app/config/environments/development.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | config.eager_load = false 3 | end 4 | -------------------------------------------------------------------------------- /fixtures/rails50_app/config/environments/production.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | config.eager_load = true 3 | end 4 | -------------------------------------------------------------------------------- /fixtures/rails50_app/config/environments/test.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | config.eager_load = false 3 | config.active_support.test_order = :random 4 | config.active_support.deprecation = :stderr 5 | end 6 | -------------------------------------------------------------------------------- /fixtures/rails50_app/config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | Rails.application.config.filter_parameters += [:password] 2 | -------------------------------------------------------------------------------- /fixtures/rails50_app/db/migrate/20140830065127_create_foos.rb: -------------------------------------------------------------------------------- 1 | class CreateFoos < ActiveRecord::Migration 2 | def self.up 3 | create_table :foos do |t| 4 | t.string :name 5 | 6 | t.timestamps null: false 7 | end 8 | end 9 | 10 | def self.down 11 | drop_table :foos 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /fixtures/rails50_app/db/schema.rb: -------------------------------------------------------------------------------- 1 | # encoding: UTF-8 2 | # This file is auto-generated from the current state of the database. Instead 3 | # of editing this file, please use the migrations feature of Active Record to 4 | # incrementally modify your database, and then regenerate this schema definition. 5 | # 6 | # Note that this schema.rb definition is the authoritative source for your 7 | # database schema. If you need to create the application database on another 8 | # system, you should be using db:schema:load, not running all the migrations 9 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 10 | # you'll amass, the slower it'll run and the greater likelihood for issues). 11 | # 12 | # It's strongly recommended that you check this file into your version control system. 13 | 14 | ActiveRecord::Schema.define(version: 20140830065127) do 15 | 16 | create_table "foos", force: :cascade do |t| 17 | t.string "name" 18 | t.datetime "created_at", null: false 19 | t.datetime "updated_at", null: false 20 | end 21 | 22 | end 23 | -------------------------------------------------------------------------------- /fixtures/rails50_app/test/fixtures/foos.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html 2 | 3 | one: 4 | name: Foo one 5 | 6 | two: 7 | name: Foo two 8 | -------------------------------------------------------------------------------- /fixtures/rails50_app/test/test_helper.rb: -------------------------------------------------------------------------------- 1 | ENV['RAILS_ENV'] ||= 'test' 2 | require File.expand_path('../../config/environment', __FILE__) 3 | require 'rails/test_help' 4 | 5 | class ActiveSupport::TestCase 6 | # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. 7 | fixtures :all 8 | 9 | # Add more helper methods to be used by all tests here... 10 | end 11 | -------------------------------------------------------------------------------- /fixtures/rails50_app/test/unit/foo_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class FooTest < ActiveSupport::TestCase 4 | test "Foos have names" do 5 | assert_equal 'Foo one', foos(:one).name 6 | end 7 | 8 | test "Foos can be stored" do 9 | foo = Foo.new(name: 'A third') 10 | foo.save! 11 | assert_equal 'A third', Foo.find(foo.id).name 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /fixtures/rails51_app/Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'rails', '~> 5.1.0' 4 | gem 'sqlite3', platform: :ruby 5 | gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby 6 | gem 'memory_test_fix', path: '../..' 7 | 8 | group :development, :test do 9 | gem 'spring' 10 | end 11 | -------------------------------------------------------------------------------- /fixtures/rails51_app/Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require_relative 'config/application' 5 | 6 | Rails.application.load_tasks 7 | -------------------------------------------------------------------------------- /fixtures/rails51_app/app/models/application_record.rb: -------------------------------------------------------------------------------- 1 | class ApplicationRecord < ActiveRecord::Base 2 | self.abstract_class = true 3 | end 4 | -------------------------------------------------------------------------------- /fixtures/rails51_app/app/models/foo.rb: -------------------------------------------------------------------------------- 1 | class Foo < ApplicationRecord 2 | end 3 | -------------------------------------------------------------------------------- /fixtures/rails51_app/bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | begin 3 | load File.expand_path('../spring', __FILE__) 4 | rescue LoadError => e 5 | raise unless e.message.include?('spring') 6 | end 7 | require_relative '../config/boot' 8 | require 'rake' 9 | Rake.application.run 10 | -------------------------------------------------------------------------------- /fixtures/rails51_app/bin/spring: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | # This file loads spring without using Bundler, in order to be fast. 4 | # It gets overwritten when you run the `spring binstub` command. 5 | 6 | unless defined?(Spring) 7 | require 'rubygems' 8 | require 'bundler' 9 | 10 | lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read) 11 | spring = lockfile.specs.detect { |spec| spec.name == "spring" } 12 | if spring 13 | Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path 14 | gem 'spring', spring.version 15 | require 'spring/binstub' 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /fixtures/rails51_app/config/application.rb: -------------------------------------------------------------------------------- 1 | require_relative 'boot' 2 | 3 | require 'rails' 4 | require 'active_record/railtie' 5 | require 'rails/test_unit/railtie' 6 | 7 | Bundler.require(*Rails.groups) 8 | 9 | module RailsTestApp 10 | class Application < Rails::Application 11 | config.load_defaults 5.1 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /fixtures/rails51_app/config/boot.rb: -------------------------------------------------------------------------------- 1 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) 2 | 3 | require 'bundler/setup' # Set up gems listed in the Gemfile. 4 | -------------------------------------------------------------------------------- /fixtures/rails51_app/config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require_relative 'application' 3 | 4 | # Initialize the Rails application. 5 | Rails.application.initialize! 6 | -------------------------------------------------------------------------------- /fixtures/rails51_app/config/environments/development.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | config.eager_load = false 3 | end 4 | -------------------------------------------------------------------------------- /fixtures/rails51_app/config/environments/production.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | config.eager_load = true 3 | end 4 | -------------------------------------------------------------------------------- /fixtures/rails51_app/config/environments/test.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | config.eager_load = false 3 | config.active_support.test_order = :random 4 | config.active_support.deprecation = :stderr 5 | end 6 | -------------------------------------------------------------------------------- /fixtures/rails51_app/config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | Rails.application.config.filter_parameters += [:password] 2 | -------------------------------------------------------------------------------- /fixtures/rails51_app/db/migrate/20140830065127_create_foos.rb: -------------------------------------------------------------------------------- 1 | class CreateFoos < ActiveRecord::Migration[5.1] 2 | def self.up 3 | create_table :foos do |t| 4 | t.string :name 5 | 6 | t.timestamps null: false 7 | end 8 | end 9 | 10 | def self.down 11 | drop_table :foos 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /fixtures/rails51_app/db/schema.rb: -------------------------------------------------------------------------------- 1 | # encoding: UTF-8 2 | # This file is auto-generated from the current state of the database. Instead 3 | # of editing this file, please use the migrations feature of Active Record to 4 | # incrementally modify your database, and then regenerate this schema definition. 5 | # 6 | # Note that this schema.rb definition is the authoritative source for your 7 | # database schema. If you need to create the application database on another 8 | # system, you should be using db:schema:load, not running all the migrations 9 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 10 | # you'll amass, the slower it'll run and the greater likelihood for issues). 11 | # 12 | # It's strongly recommended that you check this file into your version control system. 13 | 14 | ActiveRecord::Schema.define(version: 20140830065127) do 15 | 16 | create_table "foos", force: :cascade do |t| 17 | t.string "name" 18 | t.datetime "created_at", null: false 19 | t.datetime "updated_at", null: false 20 | end 21 | 22 | end 23 | -------------------------------------------------------------------------------- /fixtures/rails51_app/test/fixtures/foos.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html 2 | 3 | one: 4 | name: Foo one 5 | 6 | two: 7 | name: Foo two 8 | -------------------------------------------------------------------------------- /fixtures/rails51_app/test/test_helper.rb: -------------------------------------------------------------------------------- 1 | require File.expand_path('../../config/environment', __FILE__) 2 | require 'rails/test_help' 3 | 4 | class ActiveSupport::TestCase 5 | # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. 6 | fixtures :all 7 | 8 | # Add more helper methods to be used by all tests here... 9 | end 10 | -------------------------------------------------------------------------------- /fixtures/rails51_app/test/unit/foo_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class FooTest < ActiveSupport::TestCase 4 | test "Foos have names" do 5 | assert_equal 'Foo one', foos(:one).name 6 | end 7 | 8 | test "Foos can be stored" do 9 | foo = Foo.new(name: 'A third') 10 | foo.save! 11 | assert_equal 'A third', Foo.find(foo.id).name 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /fixtures/rails52_app/Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | 3 | gem 'rails', '~> 5.2.0' 4 | gem 'sqlite3', platform: :ruby 5 | gem 'activerecord-jdbcsqlite3-adapter', platform: :jruby 6 | gem 'memory_test_fix', path: '../..' 7 | 8 | group :development do 9 | gem 'spring' 10 | end 11 | -------------------------------------------------------------------------------- /fixtures/rails52_app/Rakefile: -------------------------------------------------------------------------------- 1 | # Add your own tasks in files placed in lib/tasks ending in .rake, 2 | # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. 3 | 4 | require_relative 'config/application' 5 | 6 | Rails.application.load_tasks 7 | -------------------------------------------------------------------------------- /fixtures/rails52_app/app/models/application_record.rb: -------------------------------------------------------------------------------- 1 | class ApplicationRecord < ActiveRecord::Base 2 | self.abstract_class = true 3 | end 4 | -------------------------------------------------------------------------------- /fixtures/rails52_app/app/models/foo.rb: -------------------------------------------------------------------------------- 1 | class Foo < ApplicationRecord 2 | end 3 | -------------------------------------------------------------------------------- /fixtures/rails52_app/bin/rake: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | begin 3 | load File.expand_path('../spring', __FILE__) 4 | rescue LoadError => e 5 | raise unless e.message.include?('spring') 6 | end 7 | require_relative '../config/boot' 8 | require 'rake' 9 | Rake.application.run 10 | -------------------------------------------------------------------------------- /fixtures/rails52_app/bin/spring: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | 3 | # This file loads spring without using Bundler, in order to be fast. 4 | # It gets overwritten when you run the `spring binstub` command. 5 | 6 | unless defined?(Spring) 7 | require 'rubygems' 8 | require 'bundler' 9 | 10 | lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read) 11 | spring = lockfile.specs.detect { |spec| spec.name == "spring" } 12 | if spring 13 | Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path 14 | gem 'spring', spring.version 15 | require 'spring/binstub' 16 | end 17 | end 18 | -------------------------------------------------------------------------------- /fixtures/rails52_app/config/application.rb: -------------------------------------------------------------------------------- 1 | require_relative 'boot' 2 | 3 | require 'rails' 4 | require 'active_record/railtie' 5 | require 'rails/test_unit/railtie' 6 | 7 | Bundler.require(*Rails.groups) 8 | 9 | module RailsTestApp 10 | class Application < Rails::Application 11 | config.load_defaults 5.2 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /fixtures/rails52_app/config/boot.rb: -------------------------------------------------------------------------------- 1 | ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) 2 | 3 | require 'bundler/setup' # Set up gems listed in the Gemfile. 4 | -------------------------------------------------------------------------------- /fixtures/rails52_app/config/environment.rb: -------------------------------------------------------------------------------- 1 | # Load the Rails application. 2 | require_relative 'application' 3 | 4 | # Initialize the Rails application. 5 | Rails.application.initialize! 6 | -------------------------------------------------------------------------------- /fixtures/rails52_app/config/environments/development.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | config.eager_load = false 3 | end 4 | -------------------------------------------------------------------------------- /fixtures/rails52_app/config/environments/production.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | config.eager_load = true 3 | end 4 | -------------------------------------------------------------------------------- /fixtures/rails52_app/config/environments/test.rb: -------------------------------------------------------------------------------- 1 | Rails.application.configure do 2 | config.eager_load = false 3 | config.active_support.test_order = :random 4 | config.active_support.deprecation = :stderr 5 | end 6 | -------------------------------------------------------------------------------- /fixtures/rails52_app/config/initializers/filter_parameter_logging.rb: -------------------------------------------------------------------------------- 1 | Rails.application.config.filter_parameters += [:password] 2 | -------------------------------------------------------------------------------- /fixtures/rails52_app/db/migrate/20140830065127_create_foos.rb: -------------------------------------------------------------------------------- 1 | class CreateFoos < ActiveRecord::Migration[5.1] 2 | def self.up 3 | create_table :foos do |t| 4 | t.string :name 5 | 6 | t.timestamps null: false 7 | end 8 | end 9 | 10 | def self.down 11 | drop_table :foos 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /fixtures/rails52_app/db/schema.rb: -------------------------------------------------------------------------------- 1 | # This file is auto-generated from the current state of the database. Instead 2 | # of editing this file, please use the migrations feature of Active Record to 3 | # incrementally modify your database, and then regenerate this schema definition. 4 | # 5 | # Note that this schema.rb definition is the authoritative source for your 6 | # database schema. If you need to create the application database on another 7 | # system, you should be using db:schema:load, not running all the migrations 8 | # from scratch. The latter is a flawed and unsustainable approach (the more migrations 9 | # you'll amass, the slower it'll run and the greater likelihood for issues). 10 | # 11 | # It's strongly recommended that you check this file into your version control system. 12 | 13 | ActiveRecord::Schema.define(version: 2014_08_30_065127) do 14 | 15 | create_table "foos", force: :cascade do |t| 16 | t.string "name" 17 | t.datetime "created_at", null: false 18 | t.datetime "updated_at", null: false 19 | end 20 | 21 | end 22 | -------------------------------------------------------------------------------- /fixtures/rails52_app/test/fixtures/foos.yml: -------------------------------------------------------------------------------- 1 | # Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html 2 | 3 | one: 4 | name: Foo one 5 | 6 | two: 7 | name: Foo two 8 | -------------------------------------------------------------------------------- /fixtures/rails52_app/test/models/foo_test.rb: -------------------------------------------------------------------------------- 1 | require 'test_helper' 2 | 3 | class FooTest < ActiveSupport::TestCase 4 | test "Foos have names" do 5 | assert_equal 'Foo one', foos(:one).name 6 | end 7 | 8 | test "Foos can be stored" do 9 | foo = Foo.new(name: 'A third') 10 | foo.save! 11 | assert_equal 'A third', Foo.find(foo.id).name 12 | end 13 | end 14 | -------------------------------------------------------------------------------- /fixtures/rails52_app/test/test_helper.rb: -------------------------------------------------------------------------------- 1 | ENV['RAILS_ENV'] ||= 'test' 2 | require_relative '../config/environment' 3 | require 'rails/test_help' 4 | 5 | class ActiveSupport::TestCase 6 | # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. 7 | fixtures :all 8 | 9 | # Add more helper methods to be used by all tests here... 10 | end 11 | -------------------------------------------------------------------------------- /lib/memory_test_fix.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'memory_test_fix/schema_loader' 4 | 5 | if defined?(Rails) 6 | require 'memory_test_fix/railtie.rb' 7 | 8 | if Rails.version =~ /^5\.2\./ 9 | ActiveRecord::Base.class_eval do 10 | def self.establish_connection(*) 11 | super.tap do 12 | MemoryTestFix::SchemaLoader.init_schema 13 | end 14 | end 15 | end 16 | end 17 | end 18 | 19 | if defined?(Spring) 20 | Spring.after_fork do 21 | MemoryTestFix::SchemaLoader.init_schema 22 | end 23 | end 24 | -------------------------------------------------------------------------------- /lib/memory_test_fix/railtie.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module MemoryTestFix 4 | # Set up for use with Rails 5 | class Railtie < ::Rails::Railtie 6 | config.after_initialize do 7 | MemoryTestFix::SchemaLoader.init_schema 8 | end 9 | end 10 | end 11 | -------------------------------------------------------------------------------- /lib/memory_test_fix/schema_file_loader.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | module MemoryTestFix 4 | # Load Rails schema file into in-memory database. 5 | # @api private 6 | module SchemaFileLoader 7 | # Load the Rails schema file. 8 | def self.load_schema 9 | load "#{Rails.root}/db/schema.rb" 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /lib/memory_test_fix/schema_loader.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'memory_test_fix/schema_file_loader' 4 | require 'active_record' 5 | 6 | module MemoryTestFix 7 | # Set up database schema into in-memory database. 8 | class SchemaLoader 9 | # Initialize the schema for an in-memory database, if it is configured. See 10 | # the README for details on how to configure Rails to use an in-memory 11 | # database. 12 | def self.init_schema 13 | new.init_schema 14 | end 15 | 16 | # @param [Hash] options The options to configure this instance of SchemaLoader with. 17 | # @option options [Hash] :configuration The configuration of the database connection 18 | # @option options :migrator The migrator to use if configured to use migrations 19 | # @option options :loader The loader to use if configured to not use migrations 20 | # @api private 21 | def initialize(options = {}) 22 | @configuration = options[:configuration] || ActiveRecord::Base.connection_config 23 | @migrator = options[:migrator] || default_migrator 24 | @loader = options[:loader] || SchemaFileLoader 25 | end 26 | 27 | # Initialize the schema for the in-memoray database according to the 28 | # configuration passed to the constructor. 29 | # @api private 30 | def init_schema 31 | return unless in_memory_database? 32 | 33 | inform_using_in_memory unless silent? 34 | 35 | if silent? || quiet? 36 | load_schema_silently 37 | else 38 | load_schema 39 | end 40 | end 41 | 42 | private 43 | 44 | def in_memory_database? 45 | in_memory? && sqlite3? 46 | end 47 | 48 | def in_memory? 49 | @configuration[:database] == ':memory:' || @configuration[:dbfile] == ':memory:' 50 | end 51 | 52 | def sqlite3? 53 | @configuration[:adapter] == 'sqlite3' 54 | end 55 | 56 | def verbosity 57 | @configuration[:verbosity] 58 | end 59 | 60 | def silent? 61 | verbosity == 'silent' 62 | end 63 | 64 | def quiet? 65 | verbosity == 'quiet' 66 | end 67 | 68 | def migrate 69 | @configuration[:migrate] == true 70 | end 71 | 72 | def inform_using_in_memory 73 | puts 'Creating sqlite :memory: database' 74 | end 75 | 76 | def load_schema 77 | if migrate 78 | if rails_52? 79 | @migrator.up 80 | else 81 | @migrator.up('db/migrate') 82 | end 83 | else 84 | @loader.load_schema 85 | end 86 | end 87 | 88 | def load_schema_silently 89 | silently do 90 | load_schema 91 | end 92 | end 93 | 94 | def default_migrator 95 | if rails_52? 96 | ActiveRecord::Base.connection.migration_context 97 | else 98 | ActiveRecord::Migrator 99 | end 100 | end 101 | 102 | def rails_52? 103 | ActiveRecord.version.segments[0..1] == [5, 2] 104 | end 105 | 106 | def silently 107 | tmp_stream = StringIO.new 108 | 109 | original = $stdout 110 | $stdout = tmp_stream 111 | 112 | yield 113 | ensure 114 | $stdout = original 115 | end 116 | end 117 | end 118 | -------------------------------------------------------------------------------- /memory_test_fix.gemspec: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | Gem::Specification.new do |spec| 4 | spec.name = 'memory_test_fix' 5 | spec.version = '1.5.1' 6 | 7 | spec.authors = ['Matijs van Zuijlen', 8 | 'Chris Roos', 9 | 'Geoffrey Grosenbach', 10 | 'Kakutani Shintaro', 11 | 'Erik Hanson and Matt Scilipoti', 12 | 'Greg Weber', 13 | 'Stephan Zalewski'] 14 | spec.email = 'matijs@matijs.net' 15 | spec.homepage = 'http://wiki.github.com/mvz/memory_test_fix' 16 | 17 | spec.license = 'MIT' 18 | 19 | spec.summary = 'Use SQLite3 in-memory database for Rails tests.' 20 | spec.description = <<-TEXT 21 | Makes use of SQLite3 in-memory database possible for your 22 | Rails tests by preloading the schema. 23 | TEXT 24 | spec.required_ruby_version = '>= 2.3.0' 25 | 26 | spec.files = Dir['{lib,test,spec,fixtures}/**/*', 27 | '*.md', 28 | '*.gemspec', 29 | 'Rakefile', 30 | 'Gemfile'] & `git ls-files -z`.split("\0") 31 | 32 | spec.rdoc_options = ['--main', 'README.md'] 33 | spec.extra_rdoc_files = ['README.md'] 34 | 35 | spec.add_runtime_dependency('activerecord', '~> 5.0') 36 | spec.add_runtime_dependency('railties', '~> 5.0') 37 | spec.add_development_dependency('minitest', '~> 5.2') 38 | spec.add_development_dependency('rake', '~> 12.0') 39 | spec.add_development_dependency('rspec', '~> 3.1') 40 | end 41 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | # This file was generated by the `rspec --init` command. Conventionally, all 4 | # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. 5 | # The generated `.rspec` file contains `--require spec_helper` which will cause this 6 | # file to always be loaded, without a need to explicitly require it in any files. 7 | # 8 | # Given that it is always loaded, you are encouraged to keep this file as 9 | # light-weight as possible. Requiring heavyweight dependencies from this file 10 | # will add to the boot time of your test suite on EVERY test run, even for an 11 | # individual file that may not need all of that loaded. Instead, consider making 12 | # a separate helper file that requires the additional dependencies and performs 13 | # the additional setup, and require it from the spec files that actually need it. 14 | # 15 | # The `.rspec` file also contains a few flags that are not defaults but that 16 | # users commonly want. 17 | # 18 | # See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration 19 | RSpec.configure do |config| 20 | # rspec-expectations config goes here. You can use an alternate 21 | # assertion/expectation library such as wrong or the stdlib/minitest 22 | # assertions if you prefer. 23 | config.expect_with :rspec do |expectations| 24 | # This option will default to `true` in RSpec 4. It makes the `description` 25 | # and `failure_message` of custom matchers include text for helper methods 26 | # defined using `chain`, e.g.: 27 | # be_bigger_than(2).and_smaller_than(4).description 28 | # # => "be bigger than 2 and smaller than 4" 29 | # ...rather than: 30 | # # => "be bigger than 2" 31 | expectations.include_chain_clauses_in_custom_matcher_descriptions = true 32 | end 33 | 34 | # rspec-mocks config goes here. You can use an alternate test double 35 | # library (such as bogus or mocha) by changing the `mock_with` option here. 36 | config.mock_with :rspec do |mocks| 37 | # Prevents you from mocking or stubbing a method that does not exist on 38 | # a real object. This is generally recommended, and will default to 39 | # `true` in RSpec 4. 40 | mocks.verify_partial_doubles = true 41 | end 42 | 43 | # These two settings work together to allow you to limit a spec run 44 | # to individual examples or groups you care about by tagging them with 45 | # `:focus` metadata. When nothing is tagged with `:focus`, all examples 46 | # get run. 47 | config.filter_run :focus 48 | config.run_all_when_everything_filtered = true 49 | 50 | # Limits the available syntax to the non-monkey patched syntax that is recommended. 51 | # For more details, see: 52 | # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax 53 | # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ 54 | # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching 55 | config.disable_monkey_patching! 56 | 57 | # This setting enables warnings. It's recommended, but in some cases may 58 | # be too noisy due to issues in dependencies. 59 | config.warnings = true 60 | 61 | # Many RSpec users commonly either run the entire suite or an individual 62 | # file, and it's useful to allow more verbose output when running an 63 | # individual spec file. 64 | if config.files_to_run.one? 65 | # Use the documentation formatter for detailed output, 66 | # unless a formatter has already been configured 67 | # (e.g. via a command-line flag). 68 | config.default_formatter = 'doc' 69 | end 70 | 71 | # Print the 10 slowest examples and example groups at the 72 | # end of the spec run, to help surface which specs are running 73 | # particularly slow. 74 | config.profile_examples = 10 75 | 76 | # Run specs in random order to surface order dependencies. If you find an 77 | # order dependency and want to debug it, you can fix the order by providing 78 | # the seed, which is printed after each run. 79 | # --seed 1234 80 | config.order = :random 81 | 82 | # Seed global randomization in this process using the `--seed` CLI option. 83 | # Setting this allows you to use `--seed` to deterministically reproduce 84 | # test failures related to randomization by passing the same `--seed` value 85 | # as the one that triggered the failure. 86 | Kernel.srand config.seed 87 | end 88 | -------------------------------------------------------------------------------- /spec/unit/memory_test_fix/schema_loader_spec.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'spec_helper' 4 | 5 | require 'memory_test_fix/schema_loader' 6 | 7 | RSpec.describe MemoryTestFix::SchemaLoader do 8 | def silently 9 | tmp_stream = StringIO.new 10 | 11 | original = $stdout 12 | $stdout = tmp_stream 13 | 14 | yield 15 | ensure 16 | $stdout = original 17 | end 18 | 19 | describe '#init_schema' do 20 | let(:migrator) do 21 | if ActiveRecord::Migrator.respond_to? :up 22 | class_double(ActiveRecord::Migrator) 23 | else 24 | instance_double(ActiveRecord::MigrationContext) 25 | end 26 | end 27 | 28 | let(:loader) { class_double(MemoryTestFix::SchemaFileLoader) } 29 | let(:options) { { configuration: config, migrator: migrator, loader: loader } } 30 | let(:schema_loader) { described_class.new options } 31 | let(:base_config) { { database: ':memory:', adapter: 'sqlite3' } } 32 | 33 | before do 34 | allow(loader).to receive(:load_schema) { puts 'loading schema' } 35 | allow(migrator).to receive(:up) 36 | end 37 | 38 | context 'when no in-memory database is configured' do 39 | let(:config) { { database: 'some/file.sqlite3', adapter: 'sqlite3' } } 40 | 41 | it 'outputs nothing' do 42 | expect { schema_loader.init_schema }.not_to output.to_stdout 43 | end 44 | 45 | it 'does not load anything' do 46 | schema_loader.init_schema 47 | expect(loader).not_to have_received :load_schema 48 | end 49 | end 50 | 51 | context 'when configured not to use migrations' do 52 | let(:config) { base_config } 53 | 54 | it 'informs the user it is creating an in-memory database' do 55 | expect { schema_loader.init_schema }. 56 | to output(/Creating sqlite :memory: database/).to_stdout 57 | end 58 | 59 | it 'prints the output from the loader' do 60 | expect { schema_loader.init_schema }. 61 | to output(/loading schema/).to_stdout 62 | end 63 | 64 | it 'tells the loader to load the schema' do 65 | silently { schema_loader.init_schema } 66 | expect(loader).to have_received :load_schema 67 | end 68 | end 69 | 70 | context 'when configured to use migrations' do 71 | let(:config) { base_config.merge(migrate: true) } 72 | 73 | it 'informs the user it is creating an in-memory database' do 74 | expect { schema_loader.init_schema }. 75 | to output(/Creating sqlite :memory: database/).to_stdout 76 | end 77 | 78 | it "does not print 'loading schema'" do 79 | expect { schema_loader.init_schema }. 80 | not_to output(/loading schema/).to_stdout 81 | end 82 | 83 | it 'tells the migrator to run the migrations' do 84 | silently { schema_loader.init_schema } 85 | expect(migrator).to have_received :up 86 | end 87 | end 88 | 89 | context 'when running in quietly' do 90 | let(:config) { base_config.merge(verbosity: 'quiet') } 91 | 92 | it 'informs the user it is creating an in-memory database' do 93 | expect { schema_loader.init_schema }. 94 | to output(/Creating sqlite :memory: database/).to_stdout 95 | end 96 | 97 | it 'does not print the output from the loader' do 98 | expect { schema_loader.init_schema }. 99 | not_to output(/loading schema/).to_stdout 100 | end 101 | end 102 | 103 | context 'when running in silence' do 104 | let(:config) { base_config.merge(verbosity: 'silent') } 105 | 106 | it 'outputs nothing' do 107 | expect { schema_loader.init_schema }.not_to output.to_stdout 108 | end 109 | end 110 | end 111 | end 112 | -------------------------------------------------------------------------------- /test/integration/integration_test.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'test_helper' 4 | require 'yaml' 5 | 6 | def in_clean_bundler_environment(*args) 7 | system(*%w(/usr/bin/env -u RUBYOPT -u BUNDLE_BIN_PATH -u BUNDLE_GEMFILE) + args) 8 | end 9 | 10 | def update_bundle(label) 11 | return if in_clean_bundler_environment('bundle', 'update', '--quiet', '--local') 12 | 13 | puts "Starting remote update of the bundle for #{label}" 14 | return if in_clean_bundler_environment('bundle', 'update') 15 | 16 | raise "Unable to initialize test environment for #{label}" 17 | end 18 | 19 | def run_tests(command_array = %w(bundle exec rake)) 20 | result = false 21 | out, err = capture_subprocess_io do 22 | result = in_clean_bundler_environment(*command_array) 23 | end 24 | # If the command failed, make it print any error messages 25 | err.must_equal '' unless result 26 | out 27 | end 28 | 29 | def stop_spring 30 | capture_subprocess_io do 31 | in_clean_bundler_environment('bin/spring', 'stop') 32 | end 33 | end 34 | 35 | BASE_CONFIG = { 36 | 'development' => { 37 | 'adapter' => 'sqlite3', 38 | 'pool' => 5, 39 | 'timeout' => 5000, 40 | 'database' => 'db/development.sqlite3' 41 | }, 42 | 'test' => { 43 | 'adapter' => 'sqlite3', 44 | 'database' => ':memory:' 45 | } 46 | }.freeze 47 | 48 | MIGRATING_CONFIG = BASE_CONFIG.dup.tap do |config| 49 | config['test'] = config['test'].merge('migrate' => true) 50 | end 51 | 52 | def create_db_config_without_migrations 53 | File.open 'config/database.yml', 'w' do |f| 54 | f.puts YAML.dump(BASE_CONFIG) 55 | end 56 | end 57 | 58 | def create_db_config_with_migrations 59 | File.open 'config/database.yml', 'w' do |f| 60 | f.puts YAML.dump(MIGRATING_CONFIG) 61 | end 62 | end 63 | 64 | VERSIONS = [ 65 | ['Rails 5.0', 'rails50_app'], 66 | ['Rails 5.1', 'rails51_app'], 67 | ['Rails 5.2', 'rails52_app'] 68 | ].freeze 69 | 70 | VERSIONS.each do |label, appdir| 71 | Dir.chdir "fixtures/#{appdir}" do 72 | update_bundle label 73 | end 74 | 75 | describe "A #{label} app using memory_test_fix" do 76 | it 'can run its tests in-memory without migrations' do 77 | Dir.chdir "fixtures/#{appdir}" do 78 | create_db_config_without_migrations 79 | out = run_tests 80 | out.must_match(/Creating sqlite :memory: database/) 81 | out.wont_match(/migrating/) 82 | out.must_match(/2 runs, 2 assertions, 0 failures, 0 errors, 0 skips/) 83 | end 84 | end 85 | 86 | it 'can run its tests in-memory with migrations' do 87 | Dir.chdir "fixtures/#{appdir}" do 88 | create_db_config_with_migrations 89 | out = run_tests 90 | out.must_match(/Creating sqlite :memory: database/) 91 | out.must_match(/migrating/) 92 | out.must_match(/2 runs, 2 assertions, 0 failures, 0 errors, 0 skips/) 93 | end 94 | end 95 | 96 | describe 'when using spring' do 97 | let(:command_array) { %w(bin/rake) } 98 | it 'can run its tests in-memory without migrations' do 99 | Dir.chdir "fixtures/#{appdir}" do 100 | stop_spring 101 | create_db_config_without_migrations 102 | out = run_tests command_array 103 | out.must_match(/Creating sqlite :memory: database/) 104 | out.wont_match(/migrating/) 105 | out.must_match(/2 runs, 2 assertions, 0 failures, 0 errors, 0 skips/) 106 | end 107 | end 108 | 109 | it 'can run its tests in-memory with migrations' do 110 | Dir.chdir "fixtures/#{appdir}" do 111 | stop_spring 112 | create_db_config_with_migrations 113 | out = run_tests command_array 114 | out.must_match(/Creating sqlite :memory: database/) 115 | out.must_match(/migrating/) 116 | out.must_match(/2 runs, 2 assertions, 0 failures, 0 errors, 0 skips/) 117 | end 118 | end 119 | end 120 | end 121 | end 122 | -------------------------------------------------------------------------------- /test/test_helper.rb: -------------------------------------------------------------------------------- 1 | # frozen_string_literal: true 2 | 3 | require 'minitest/autorun' 4 | --------------------------------------------------------------------------------