derail_specs 0.2.0 → 0.5.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f70b380bfc02b15f03003667f83784ae59f24b110c5c2323a21c5ee02a73fddf
4
- data.tar.gz: 9a6e3daf75695be63ee86f3da37359de27087ecf95e000f5e7366359a120c72b
3
+ metadata.gz: 90fd1d641de71a5a41d231af09190c33987e21efb49cd973fbef9671a9ef8cdb
4
+ data.tar.gz: 6eb5f40e29be236de49bf2625e54d04f33647f22c74862e6dd4fdf6ee891771a
5
5
  SHA512:
6
- metadata.gz: 716f6b48003cba47b8d4b82643225fadae5309e6762e951540d6d4ea93f81b39340cdebf48108872f0dd557fa7d9a722348a96348c6aa737885065a3fa346805
7
- data.tar.gz: a3f0eb5f552897e6b9c0f64f3059ae074c6b1434c824aae9829c0dfcf36ddbdb0768a4472bb869ed20d630c4a0cfe543c98e51b4b73a83a15410a17c25f016d3
6
+ metadata.gz: ba977461d31563b4d7dd4da9cf9217e4bba40b34f113170852f623d1a4a02e24963dbb64e6a9c18c7e0911824330ffe21636233446ce12c5f2efc03ce2258e0b
7
+ data.tar.gz: 969d0a9fa3c125ed4bcb707c3a5907616de8bd4567d081e0b8f23fc53056390fab0bc61418b72c0a27a5c7f88729573a1cbe52530169db20258c863e03161c7c
data/.rubocop.yml CHANGED
@@ -1,10 +1,15 @@
1
1
  inherit_from: .rubocop_todo.yml
2
2
 
3
+ AllCops:
4
+ NewCops: enable
5
+ Exclude:
6
+ - 'example/db/schema.rb'
7
+ - 'example/bin/*'
8
+ - 'vendor/**/*'
9
+
3
10
  Style/StringLiterals:
4
11
  Enabled: false
5
12
 
6
- AllCops:
7
- NewCops: enable
8
13
 
9
14
  Style/Documentation:
10
15
  Enabled: false
data/.rubocop_todo.yml CHANGED
@@ -1,6 +1,6 @@
1
1
  # This configuration was generated by
2
2
  # `rubocop --auto-gen-config`
3
- # on 2021-09-15 06:26:41 UTC using RuboCop version 1.21.0.
3
+ # on 2021-09-16 08:00:52 UTC using RuboCop version 1.21.0.
4
4
  # The point is for the user to remove these configuration records
5
5
  # one by one as the offenses are removed from the code base.
6
6
  # Note that changes in the inspected code, or installation of new
@@ -18,30 +18,14 @@ Gemspec/RequiredRubyVersion:
18
18
  Metrics/AbcSize:
19
19
  Max: 34
20
20
 
21
- # Offense count: 1
22
- # Configuration parameters: IgnoredMethods.
23
- Metrics/CyclomaticComplexity:
24
- Max: 9
25
-
26
- # Offense count: 6
21
+ # Offense count: 4
27
22
  # Configuration parameters: CountComments, CountAsOne, ExcludedMethods, IgnoredMethods.
28
23
  Metrics/MethodLength:
29
24
  Max: 20
30
25
 
31
- # Offense count: 1
32
- # Configuration parameters: IgnoredMethods.
33
- Metrics/PerceivedComplexity:
34
- Max: 9
35
-
36
- # Offense count: 1
37
- # Cop supports --auto-correct.
38
- Style/IfUnlessModifier:
39
- Exclude:
40
- - 'example/bin/bundle'
41
-
42
- # Offense count: 4
26
+ # Offense count: 3
43
27
  # Cop supports --auto-correct.
44
28
  # Configuration parameters: AllowHeredoc, AllowURI, URISchemes, IgnoreCopDirectives, IgnoredPatterns.
45
29
  # URISchemes: http, https
46
30
  Layout/LineLength:
47
- Max: 198
31
+ Max: 127
data/CHANGELOG.md CHANGED
@@ -1,5 +1,23 @@
1
1
  ## [Unreleased]
2
2
 
3
+
4
+ ## [0.5.0] - 2021-09-17
5
+
6
+ - Add before_server_start and before_server_stop initializer hooks
7
+
8
+ ## [0.4.0] - 2021-09-16
9
+
10
+ - Add infinite server mode, which can be used by not setting a command in the config
11
+ -
12
+ ## [0.3.0] - 2021-09-16
13
+
14
+ - Add `/factory-bot/create` route to map to FactoryBot.create
15
+ - Run `rake derail_specs:run` in Rails test environment
16
+
17
+ ## [0.2.1] - 2021-09-15
18
+
19
+ - Fix Rails 5.2 missing method `ActiveRecord::TestFixtures.setup_shared_connection_pool`
20
+
3
21
  ## [0.2.0] - 2021-09-15
4
22
 
5
23
  - Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- derail_specs (0.2.0)
4
+ derail_specs (0.5.0)
5
5
  puma (>= 3.8.0)
6
6
  railties (>= 5.2.0)
7
7
 
data/README.md CHANGED
@@ -38,6 +38,12 @@ DerailSpecs.configure do |config|
38
38
  end
39
39
  ```
40
40
 
41
+ Alternatively, if you don't set a `config.command`, the server will boot in
42
+ *infinite* mode and will not stop until receiving an INT signal (IE Ctrl+c).
43
+ This is good for when your test command isn't available in a rails context,
44
+ like when you are running rails in docker, but have a test application running
45
+ on the host machine.
46
+
41
47
  ## Usage
42
48
 
43
49
  To run tests, run
@@ -63,6 +69,56 @@ beforeEach(async () => {
63
69
  });
64
70
  ```
65
71
 
72
+ ### FactoryBot.create
73
+
74
+ Post with a JSON array to `/factory-bot/create` with the following structure:
75
+
76
+ ```JSON
77
+ ["test", { "name": "Test Name", "created_at": "2011-11-11 11:11:11" }]
78
+ ````
79
+
80
+ which will run:
81
+
82
+ ```ruby
83
+ FactoryBot.create(:test, name: "Test Name", created_at: "2011-11-11 11:11:11")
84
+ ```
85
+
86
+ Example curl:
87
+
88
+ ```bash
89
+ curl -X POST \
90
+ -d '["test", { "name": "Test Name from Curl", "created_at": "2011-11-11 11:11:11" }]' \
91
+ -H 'Content-Type: application/json' \
92
+ 127.0.0.1:3001/factory-bot/create
93
+ ```
94
+
95
+ ### Initializer Hooks
96
+
97
+ By default, DerailSpecs doesn't run any fixtures. You can configure global fixture
98
+ loading with initializer hooks in `config/initializers/derail_specs.rb`:
99
+
100
+ ```
101
+ return unless Rails.env.test?
102
+
103
+ DerailSpecs.hooks.before_server_start do
104
+ # Add our fixtures before the resettable transaction is started
105
+ fixtures_dir = Rails.root.join("spec/fixtures")
106
+ fixture_files = Dir.glob(fixtures_dir.join("**/*.yml")).map do |f|
107
+ f[(fixtures_dir.to_s.size + 1)..-5]
108
+ end
109
+
110
+ @fixtures = ActiveRecord::FixtureSet.create_fixtures(
111
+ fixtures_dir,
112
+ fixture_files,
113
+ )
114
+ end
115
+
116
+ DerailSpecs.hooks.before_server_stop do
117
+ @fixtures.map(&:model_class).each(&:delete_all)
118
+ end
119
+ ```
120
+
121
+
66
122
  ## Development
67
123
 
68
124
  After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
data/bin/setup CHANGED
@@ -5,4 +5,7 @@ set -vx
5
5
 
6
6
  bundle install
7
7
 
8
+ cd example
9
+ bin/setup
10
+
8
11
  # Do any other automated setup that you need to do here
data/example/Gemfile CHANGED
@@ -11,5 +11,6 @@ gem 'sqlite3', '~> 1.4'
11
11
 
12
12
  group :development, :test do
13
13
  gem "derail_specs", path: '..'
14
+ gem 'factory_bot_rails'
14
15
  gem 'pry-rails'
15
16
  end
data/example/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- derail_specs (0.1.0)
4
+ derail_specs (0.5.0)
5
5
  puma (>= 3.8.0)
6
6
  railties (>= 5.2.0)
7
7
 
@@ -72,6 +72,11 @@ GEM
72
72
  concurrent-ruby (1.1.9)
73
73
  crass (1.0.6)
74
74
  erubi (1.10.0)
75
+ factory_bot (6.2.0)
76
+ activesupport (>= 5.0.0)
77
+ factory_bot_rails (6.2.0)
78
+ factory_bot (~> 6.2.0)
79
+ railties (>= 5.0.0)
75
80
  globalid (0.5.2)
76
81
  activesupport (>= 5.0)
77
82
  i18n (1.8.10)
@@ -147,6 +152,7 @@ PLATFORMS
147
152
 
148
153
  DEPENDENCIES
149
154
  derail_specs!
155
+ factory_bot_rails
150
156
  pry-rails
151
157
  puma (~> 5.0)
152
158
  rails (~> 6.1.4, >= 6.1.4.1)
@@ -0,0 +1,2 @@
1
+ class Test < ApplicationRecord
2
+ end
@@ -0,0 +1,9 @@
1
+ class CreateTests < ActiveRecord::Migration[6.1]
2
+ def change
3
+ create_table :tests do |t|
4
+ t.string :name
5
+
6
+ t.timestamps
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,21 @@
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
+ # This file is the source Rails uses to define your schema when running `bin/rails
6
+ # db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
7
+ # be faster and is potentially less error prone than running all of your
8
+ # migrations from scratch. Old migrations may fail to apply correctly if those
9
+ # migrations use external dependencies or application code.
10
+ #
11
+ # It's strongly recommended that you check this file into your version control system.
12
+
13
+ ActiveRecord::Schema.define(version: 2021_09_16_070010) do
14
+
15
+ create_table "tests", force: :cascade do |t|
16
+ t.string "name"
17
+ t.datetime "created_at", precision: 6, null: false
18
+ t.datetime "updated_at", precision: 6, null: false
19
+ end
20
+
21
+ end
@@ -0,0 +1,5 @@
1
+ FactoryBot.define do
2
+ factory :test do
3
+ sequence(:name) { |n| "Test #{n}" }
4
+ end
5
+ end
data/example/tests.sh CHANGED
@@ -1,4 +1,8 @@
1
1
  #!/usr/bin/env bash
2
2
 
3
- curl 127.0.0.1:3001
3
+ curl -s 127.0.0.1:3001 > /dev/null
4
4
  curl 127.0.0.1:3001/reset-transaction
5
+ curl -X POST \
6
+ -d '["test", { "name": "Test Name from Curl", "created_at": "2011-11-11 11:11:11" }]' \
7
+ -H 'Content-Type: application/json' \
8
+ 127.0.0.1:3001/factory-bot/create
@@ -3,21 +3,30 @@ require_relative 'server'
3
3
  module DerailSpecs
4
4
  class Boot
5
5
  def run
6
+ DerailSpecs.hooks.run(:before_server_start)
6
7
  Transaction.begin
7
8
  set_exit_hooks!
8
9
 
9
10
  Server.new.tap(&:boot)
10
11
 
11
- puts "Starting Tests…"
12
-
13
- system DerailSpecs.configuration.command
12
+ if command.present?
13
+ puts "Run: #{command}"
14
+ system command
15
+ else
16
+ loop { sleep 60 }
17
+ end
14
18
  end
15
19
 
16
20
  private
17
21
 
22
+ def command
23
+ DerailSpecs.configuration.command
24
+ end
25
+
18
26
  def set_exit_hooks!
19
27
  at_exit do
20
28
  Transaction.rollback
29
+ DerailSpecs.hooks.run(:before_server_stop)
21
30
  end
22
31
  Signal.trap("INT") do
23
32
  puts "Exiting derail_specs…"
@@ -0,0 +1,14 @@
1
+ begin
2
+ gem 'factory_bot_rails'
3
+ require 'factory_bot_rails'
4
+
5
+ module DerailSpecs
6
+ class FactoryBot
7
+ def self.create(factory, options = nil)
8
+ ::FactoryBot.create(factory, options)
9
+ end
10
+ end
11
+ end
12
+ rescue Gem::LoadError
13
+ require_relative 'factory_bot_stub'
14
+ end
@@ -0,0 +1,8 @@
1
+ module DerailSpecs
2
+ class FactoryBot
3
+ def self.create(*_)
4
+ puts "factory_bot_rails is not installed in group :test"
5
+ puts "Please add factory_bot_rails to your Gemfile to use this feature"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,42 @@
1
+ module DerailSpecs
2
+ def self.hooks
3
+ InitializerHooks.instance
4
+ end
5
+
6
+ class InitializerHooks
7
+ def self.instance
8
+ @instance ||= new
9
+ end
10
+
11
+ def before_server_start(&blk)
12
+ register(:before_server_start, blk)
13
+ end
14
+
15
+ def before_server_stop(&blk)
16
+ register(:before_server_stop, blk)
17
+ end
18
+
19
+ def reset!
20
+ @hooks = {}
21
+ end
22
+
23
+ def run(name)
24
+ return unless @hooks[name]
25
+ @hooks[name].each do |blk|
26
+ blk.call
27
+ end
28
+ end
29
+
30
+ private
31
+
32
+ def register(name, blk)
33
+ @hooks[name] ||= []
34
+ @hooks[name] << blk
35
+ end
36
+
37
+ def initialize
38
+ reset!
39
+ end
40
+ end
41
+ end
42
+
@@ -7,6 +7,14 @@ module DerailSpecs
7
7
  [202, { "Content-Type" => "text/plain" }, ["Accepted"]]
8
8
  }
9
9
  end
10
+ map "/factory-bot/create" do
11
+ run lambda { |env|
12
+ body = Rack::Request.new(env).body.gets
13
+ object = FactoryBot.create(*JSON.parse(body))
14
+
15
+ [202, { "Content-Type" => "application/json" }, [object.to_json]]
16
+ }
17
+ end
10
18
  map "/" do
11
19
  run Rails.application
12
20
  end
@@ -63,7 +63,7 @@ module DerailSpecs
63
63
  end
64
64
 
65
65
  def gather_connections
66
- setup_shared_connection_pool
66
+ setup_shared_connection_pool if ActiveRecord::VERSION::MAJOR >= 6
67
67
 
68
68
  ActiveRecord::Base.connection_handler.connection_pool_list.map(&:connection)
69
69
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DerailSpecs
4
- VERSION = "0.2.0"
4
+ VERSION = "0.5.0"
5
5
  end
data/lib/derail_specs.rb CHANGED
@@ -7,7 +7,12 @@ module DerailSpecs
7
7
 
8
8
  def self.configuration
9
9
  @configuration ||= Struct
10
- .new(:command, :host, :port, keyword_init: true)
10
+ .new(
11
+ :command,
12
+ :host,
13
+ :port,
14
+ keyword_init: true,
15
+ )
11
16
  .new(
12
17
  host: '127.0.0.1',
13
18
  port: 3001,
@@ -22,3 +27,5 @@ end
22
27
  require 'derail_specs/boot'
23
28
  require 'derail_specs/transaction'
24
29
  require 'derail_specs/railtie'
30
+ require 'derail_specs/factory_bot'
31
+ require 'derail_specs/initializer_hooks'
@@ -1,3 +1,5 @@
1
+ return unless Rails.test.env?
2
+
1
3
  DerailSpecs.configure do |config|
2
4
  config.command = './tests.sh'
3
5
  config.host = '127.0.0.1'
@@ -1,5 +1,6 @@
1
1
  namespace :derail_specs do
2
2
  task run: :environment do
3
+ Rails.env = "test"
3
4
  DerailSpecs::Boot.new.run
4
5
  end
5
6
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: derail_specs
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Alex Piechowski
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-09-16 00:00:00.000000000 Z
11
+ date: 2021-09-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puma
@@ -67,6 +67,7 @@ files:
67
67
  - example/Rakefile
68
68
  - example/app/controllers/application_controller.rb
69
69
  - example/app/models/application_record.rb
70
+ - example/app/models/test.rb
70
71
  - example/app/views/layouts/application.html.erb
71
72
  - example/bin/bundle
72
73
  - example/bin/rails
@@ -94,6 +95,9 @@ files:
94
95
  - example/config/locales/en.yml
95
96
  - example/config/puma.rb
96
97
  - example/config/routes.rb
98
+ - example/db/migrate/20210916070010_create_tests.rb
99
+ - example/db/schema.rb
100
+ - example/factories.rb
97
101
  - example/public/404.html
98
102
  - example/public/422.html
99
103
  - example/public/500.html
@@ -104,6 +108,9 @@ files:
104
108
  - example/tests.sh
105
109
  - lib/derail_specs.rb
106
110
  - lib/derail_specs/boot.rb
111
+ - lib/derail_specs/factory_bot.rb
112
+ - lib/derail_specs/factory_bot_stub.rb
113
+ - lib/derail_specs/initializer_hooks.rb
107
114
  - lib/derail_specs/railtie.rb
108
115
  - lib/derail_specs/server.rb
109
116
  - lib/derail_specs/server/app.rb