derail_specs 0.3.0 → 0.6.0

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: b3cb7b4de2c0df3488fd8d13cea48a9a432eb4a93c9154348373d19ffb2f5faf
4
- data.tar.gz: 6640217cd89418908ad6cf8bf23d4acaf85aa75baf8f2495bdf661ff157d7161
3
+ metadata.gz: f41316c2b4b33b60d06fb9a0a0b8fc647f93455e3a1a8978fd16f27b7c118b37
4
+ data.tar.gz: 4c3d831d2f467eb42fac527d07ff5edcb943b8ed36fc358ff239d5045acd3f6c
5
5
  SHA512:
6
- metadata.gz: 3587e23efa379f03cdab789814e0ed81c026688d1013dc7e216625e69a320445631b3f5d6dec3a51ad58ac2f5a0d4fdc07e121ba93326ed2d4a9e32958752958
7
- data.tar.gz: cfd6c514732a6b153115038a04aa979232d0d0306cbfb93d1915410b13b405d77a31382ec7b27f9989529c81cebec9edb1497f18d299cee26c43d8c166678601
6
+ metadata.gz: b92ec87ddeabfc801dfaae6dab6f064ddf9b98dd4677bcef61041da677534944fcefc038f676f7b2f99dfec8e5c62b29ada587bd5c9aba366cad89994dbb32d9
7
+ data.tar.gz: e304ee8306d696348c44427febd0090912942cc4c07fc5334e1cea6d5aaf5521b8f72db997aa55fb6cdfbb5c991e71a7eddec9bbe251dfbf2a52f12ad596b836
data/.rubocop.yml CHANGED
@@ -5,6 +5,7 @@ AllCops:
5
5
  Exclude:
6
6
  - 'example/db/schema.rb'
7
7
  - 'example/bin/*'
8
+ - 'vendor/**/*'
8
9
 
9
10
  Style/StringLiterals:
10
11
  Enabled: false
data/CHANGELOG.md CHANGED
@@ -1,5 +1,20 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.6.0] - 2021-09-23
4
+
5
+ - Add /factory-bot/create-list to map to FactoryBot.create_list
6
+
7
+ ## [0.5.1] - 2021-09-23
8
+
9
+ - Fixed a bug with Ruby 2.6 and Rails >= 6 where #bind_call (Ruby 2.7) was used instead of #bind#call
10
+
11
+ ## [0.5.0] - 2021-09-17
12
+
13
+ - Add before_server_start and before_server_stop initializer hooks
14
+
15
+ ## [0.4.0] - 2021-09-16
16
+
17
+ - Add infinite server mode, which can be used by not setting a command in the config
3
18
 
4
19
  ## [0.3.0] - 2021-09-16
5
20
 
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- derail_specs (0.3.0)
4
+ derail_specs (0.6.0)
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
@@ -86,6 +92,33 @@ curl -X POST \
86
92
  127.0.0.1:3001/factory-bot/create
87
93
  ```
88
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
+
89
122
  ## Development
90
123
 
91
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.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- derail_specs (0.3.0)
4
+ derail_specs (0.6.0)
5
5
  puma (>= 3.8.0)
6
6
  railties (>= 5.2.0)
7
7
 
data/example/tests.sh CHANGED
@@ -6,3 +6,8 @@ curl -X POST \
6
6
  -d '["test", { "name": "Test Name from Curl", "created_at": "2011-11-11 11:11:11" }]' \
7
7
  -H 'Content-Type: application/json' \
8
8
  127.0.0.1:3001/factory-bot/create
9
+
10
+ curl -X POST \
11
+ -d '["test", 3, { "name": "Test Name from Curl", "created_at": "2011-11-11 11:11:11" }]' \
12
+ -H 'Content-Type: application/json' \
13
+ 127.0.0.1:3001/factory-bot/create-list
@@ -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…"
@@ -7,6 +7,10 @@ begin
7
7
  def self.create(factory, options = nil)
8
8
  ::FactoryBot.create(factory, options)
9
9
  end
10
+
11
+ def self.create_list(factory, count, options = nil)
12
+ ::FactoryBot.create_list(factory, count, options)
13
+ end
10
14
  end
11
15
  end
12
16
  rescue Gem::LoadError
@@ -1,6 +1,14 @@
1
1
  module DerailSpecs
2
2
  class FactoryBot
3
3
  def self.create(*_)
4
+ not_installed_message
5
+ end
6
+
7
+ def self.create_list(*_)
8
+ not_installed_message
9
+ end
10
+
11
+ def self.not_installed_message
4
12
  puts "factory_bot_rails is not installed in group :test"
5
13
  puts "Please add factory_bot_rails to your Gemfile to use this feature"
6
14
  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
@@ -15,6 +15,14 @@ module DerailSpecs
15
15
  [202, { "Content-Type" => "application/json" }, [object.to_json]]
16
16
  }
17
17
  end
18
+ map "/factory-bot/create-list" do
19
+ run lambda { |env|
20
+ body = Rack::Request.new(env).body.gets
21
+ object = FactoryBot.create_list(*JSON.parse(body))
22
+
23
+ [202, { "Content-Type" => "application/json" }, [object.to_json]]
24
+ }
25
+ end
18
26
  map "/" do
19
27
  run Rails.application
20
28
  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.3.0"
4
+ VERSION = "0.6.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,
@@ -23,3 +28,4 @@ require 'derail_specs/boot'
23
28
  require 'derail_specs/transaction'
24
29
  require 'derail_specs/railtie'
25
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
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.3.0
4
+ version: 0.6.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-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: puma
@@ -110,6 +110,7 @@ files:
110
110
  - lib/derail_specs/boot.rb
111
111
  - lib/derail_specs/factory_bot.rb
112
112
  - lib/derail_specs/factory_bot_stub.rb
113
+ - lib/derail_specs/initializer_hooks.rb
113
114
  - lib/derail_specs/railtie.rb
114
115
  - lib/derail_specs/server.rb
115
116
  - lib/derail_specs/server/app.rb