derail_specs 0.2.1 → 0.5.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 19ad2edfa41b68ea23a7e62cffc7f9265a7f303ca4e3440f3c37f16ec3d57f6a
4
- data.tar.gz: a36a37269421c0ac564046c6e05c6897997dea27bbd1d5d2c49198a92f728fba
3
+ metadata.gz: 3cf9a4363bbaa67b3fce2b01191b7bb8f7a11bdea4b1d710702e47b303cc379f
4
+ data.tar.gz: 94cad7e3e96d35edb0c67dc153f366f0252e1aa5e8dec31cc182d77b3757b969
5
5
  SHA512:
6
- metadata.gz: 6b2120eec627732dce1e88bb601d37a40b531cd72c373081f7d911555ff092549dfe4630b70e7f67c49031d1f7399d87412b3905e3684175004ddbf97079e131
7
- data.tar.gz: '02726830b503acba20c9ab92c0e1b6207451ba1af925914cd56c915270b11d5280b06f4bbf65ed30f9a38576b62c1442b5728b4963ff4c6b0fd056365037591d'
6
+ metadata.gz: 5da35b8957275ab2eb84d7985e941b3800e6cd08af6090b98db477237f9d2d1e350083178abefe297e94accbdadb6b1dac8d15f28f58daab26812bf5398df70d
7
+ data.tar.gz: 015ef7e97de920756eeb547391f11204ca3011d5a6842d20b0ecc63c9b39cd1ea71cca72c5d3b851098e31208e29de6295412d547bad513b95ab08b62b1e46b4
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,8 +1,25 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.5.1] - 2021-09-23
4
+
5
+ - Fixed a bug with Ruby 2.6 and Rails >= 6 where #bind_call (Ruby 2.7) was used instead of #bind#call
6
+
7
+ ## [0.5.0] - 2021-09-17
8
+
9
+ - Add before_server_start and before_server_stop initializer hooks
10
+
11
+ ## [0.4.0] - 2021-09-16
12
+
13
+ - Add infinite server mode, which can be used by not setting a command in the config
14
+
15
+ ## [0.3.0] - 2021-09-16
16
+
17
+ - Add `/factory-bot/create` route to map to FactoryBot.create
18
+ - Run `rake derail_specs:run` in Rails test environment
19
+
3
20
  ## [0.2.1] - 2021-09-15
4
21
 
5
- - Fix Rails 5.2 missing method ActiveRecord::TestFixtures.setup_shared_connection_pool
22
+ - Fix Rails 5.2 missing method `ActiveRecord::TestFixtures.setup_shared_connection_pool`
6
23
 
7
24
  ## [0.2.0] - 2021-09-15
8
25
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- derail_specs (0.2.1)
4
+ derail_specs (0.5.1)
5
5
  puma (>= 3.8.0)
6
6
  railties (>= 5.2.0)
7
7
 
@@ -49,7 +49,7 @@ GEM
49
49
  parallel (1.21.0)
50
50
  parser (3.0.2.0)
51
51
  ast (~> 2.4.1)
52
- puma (5.4.0)
52
+ puma (5.5.0)
53
53
  nio4r (~> 2.0)
54
54
  racc (1.5.2)
55
55
  rack (2.2.3)
data/README.md CHANGED
@@ -32,12 +32,18 @@ to include your test command:
32
32
 
33
33
  ```ruby
34
34
  DerailSpecs.configure do |config|
35
- config.command = './tests.sh' # <-- your test command here
35
+ config.command = './tests.sh' # <-- your test command here, or blank for infinite mode
36
36
  config.host = '127.0.0.1'
37
37
  config.port = 3001
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
+ ```ruby
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.2.1)
4
+ derail_specs (0.5.1)
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,40 @@
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
+
26
+ @hooks[name].each(&:call)
27
+ end
28
+
29
+ private
30
+
31
+ def register(name, blk)
32
+ @hooks[name] ||= []
33
+ @hooks[name] << blk
34
+ end
35
+
36
+ def initialize
37
+ reset!
38
+ end
39
+ end
40
+ end
@@ -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
@@ -78,7 +78,10 @@ module DerailSpecs
78
78
  @legacy_saved_pool_configs ||= Hash.new { |hash, key| hash[key] = {} }
79
79
  @saved_pool_configs ||= Hash.new { |hash, key| hash[key] = {} }
80
80
 
81
- ActiveRecord::TestFixtures.instance_method(:setup_shared_connection_pool).bind_call(self)
81
+ ActiveRecord::TestFixtures
82
+ .instance_method(:setup_shared_connection_pool)
83
+ .bind(self)
84
+ .call
82
85
  end
83
86
  end
84
87
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module DerailSpecs
4
- VERSION = "0.2.1"
4
+ VERSION = "0.5.1"
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,5 +1,7 @@
1
+ return unless Rails.test.env?
2
+
1
3
  DerailSpecs.configure do |config|
2
- config.command = './tests.sh'
4
+ config.command = './tests.sh' # <-- delete this line for infinite mode
3
5
  config.host = '127.0.0.1'
4
6
  config.port = 3001
5
7
  end
@@ -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.1
4
+ version: 0.5.1
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-23 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