derail_specs 0.2.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.
Files changed (73) hide show
  1. checksums.yaml +7 -0
  2. data/.rspec +3 -0
  3. data/.rubocop.yml +25 -0
  4. data/.rubocop_todo.yml +47 -0
  5. data/.tool-versions +1 -0
  6. data/CHANGELOG.md +5 -0
  7. data/CODE_OF_CONDUCT.md +84 -0
  8. data/Gemfile +13 -0
  9. data/Gemfile.lock +122 -0
  10. data/LICENSE.txt +21 -0
  11. data/README.md +85 -0
  12. data/Rakefile +12 -0
  13. data/bin/console +15 -0
  14. data/bin/setup +8 -0
  15. data/derail_specs.gemspec +34 -0
  16. data/example/.gitignore +23 -0
  17. data/example/.ruby-version +1 -0
  18. data/example/Gemfile +15 -0
  19. data/example/Gemfile.lock +159 -0
  20. data/example/README.md +24 -0
  21. data/example/Rakefile +8 -0
  22. data/example/app/controllers/application_controller.rb +4 -0
  23. data/example/app/models/application_record.rb +5 -0
  24. data/example/app/views/layouts/application.html.erb +15 -0
  25. data/example/bin/bundle +118 -0
  26. data/example/bin/rails +6 -0
  27. data/example/bin/rake +6 -0
  28. data/example/bin/setup +35 -0
  29. data/example/config/application.rb +40 -0
  30. data/example/config/boot.rb +5 -0
  31. data/example/config/credentials.yml.enc +1 -0
  32. data/example/config/database.yml +25 -0
  33. data/example/config/environment.rb +7 -0
  34. data/example/config/environments/development.rb +62 -0
  35. data/example/config/environments/production.rb +98 -0
  36. data/example/config/environments/test.rb +51 -0
  37. data/example/config/initializers/application_controller_renderer.rb +9 -0
  38. data/example/config/initializers/backtrace_silencers.rb +10 -0
  39. data/example/config/initializers/content_security_policy.rb +29 -0
  40. data/example/config/initializers/cookies_serializer.rb +7 -0
  41. data/example/config/initializers/derail_specs.rb +3 -0
  42. data/example/config/initializers/filter_parameter_logging.rb +8 -0
  43. data/example/config/initializers/inflections.rb +17 -0
  44. data/example/config/initializers/mime_types.rb +5 -0
  45. data/example/config/initializers/permissions_policy.rb +12 -0
  46. data/example/config/initializers/wrap_parameters.rb +16 -0
  47. data/example/config/locales/en.yml +33 -0
  48. data/example/config/puma.rb +45 -0
  49. data/example/config/routes.rb +5 -0
  50. data/example/config.ru +8 -0
  51. data/example/public/404.html +67 -0
  52. data/example/public/422.html +67 -0
  53. data/example/public/500.html +66 -0
  54. data/example/public/apple-touch-icon-precomposed.png +0 -0
  55. data/example/public/apple-touch-icon.png +0 -0
  56. data/example/public/favicon.ico +0 -0
  57. data/example/public/robots.txt +1 -0
  58. data/example/tests.sh +4 -0
  59. data/lib/derail_specs/boot.rb +28 -0
  60. data/lib/derail_specs/railtie.rb +7 -0
  61. data/lib/derail_specs/server/app.rb +15 -0
  62. data/lib/derail_specs/server/checker.rb +43 -0
  63. data/lib/derail_specs/server/middleware.rb +67 -0
  64. data/lib/derail_specs/server/puma.rb +32 -0
  65. data/lib/derail_specs/server/timer.rb +20 -0
  66. data/lib/derail_specs/server.rb +117 -0
  67. data/lib/derail_specs/transaction.rb +84 -0
  68. data/lib/derail_specs/version.rb +5 -0
  69. data/lib/derail_specs.rb +24 -0
  70. data/lib/generators/derail_specs/install_generator.rb +16 -0
  71. data/lib/generators/templates/config/initializers/derail_specs.rb +5 -0
  72. data/lib/tasks/derail_specs.rake +5 -0
  73. metadata +145 -0
@@ -0,0 +1,16 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Be sure to restart your server when you modify this file.
4
+
5
+ # This file contains settings for ActionController::ParamsWrapper which
6
+ # is enabled by default.
7
+
8
+ # Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array.
9
+ ActiveSupport.on_load(:action_controller) do
10
+ wrap_parameters format: [:json]
11
+ end
12
+
13
+ # To enable root element in JSON for ActiveRecord objects.
14
+ # ActiveSupport.on_load(:active_record) do
15
+ # self.include_root_in_json = true
16
+ # end
@@ -0,0 +1,33 @@
1
+ # Files in the config/locales directory are used for internationalization
2
+ # and are automatically loaded by Rails. If you want to use locales other
3
+ # than English, add the necessary files in this directory.
4
+ #
5
+ # To use the locales, use `I18n.t`:
6
+ #
7
+ # I18n.t 'hello'
8
+ #
9
+ # In views, this is aliased to just `t`:
10
+ #
11
+ # <%= t('hello') %>
12
+ #
13
+ # To use a different locale, set it with `I18n.locale`:
14
+ #
15
+ # I18n.locale = :es
16
+ #
17
+ # This would use the information in config/locales/es.yml.
18
+ #
19
+ # The following keys must be escaped otherwise they will not be retrieved by
20
+ # the default I18n backend:
21
+ #
22
+ # true, false, on, off, yes, no
23
+ #
24
+ # Instead, surround them with single quotes.
25
+ #
26
+ # en:
27
+ # 'true': 'foo'
28
+ #
29
+ # To learn more, please read the Rails Internationalization guide
30
+ # available at https://guides.rubyonrails.org/i18n.html.
31
+
32
+ en:
33
+ hello: "Hello world"
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Puma can serve each request in a thread from an internal thread pool.
4
+ # The `threads` method setting takes two numbers: a minimum and maximum.
5
+ # Any libraries that use thread pools should be configured to match
6
+ # the maximum value specified for Puma. Default is set to 5 threads for minimum
7
+ # and maximum; this matches the default thread size of Active Record.
8
+ #
9
+ max_threads_count = ENV.fetch("RAILS_MAX_THREADS", 5)
10
+ min_threads_count = ENV.fetch("RAILS_MIN_THREADS") { max_threads_count }
11
+ threads min_threads_count, max_threads_count
12
+
13
+ # Specifies the `worker_timeout` threshold that Puma will use to wait before
14
+ # terminating a worker in development environments.
15
+ #
16
+ worker_timeout 3600 if ENV.fetch("RAILS_ENV", "development") == "development"
17
+
18
+ # Specifies the `port` that Puma will listen on to receive requests; default is 3000.
19
+ #
20
+ port ENV.fetch("PORT", 3000)
21
+
22
+ # Specifies the `environment` that Puma will run in.
23
+ #
24
+ environment ENV.fetch("RAILS_ENV", "development")
25
+
26
+ # Specifies the `pidfile` that Puma will use.
27
+ pidfile ENV.fetch("PIDFILE", "tmp/pids/server.pid")
28
+
29
+ # Specifies the number of `workers` to boot in clustered mode.
30
+ # Workers are forked web server processes. If using threads and workers together
31
+ # the concurrency of the application would be max `threads` * `workers`.
32
+ # Workers do not work on JRuby or Windows (both of which do not support
33
+ # processes).
34
+ #
35
+ # workers ENV.fetch("WEB_CONCURRENCY") { 2 }
36
+
37
+ # Use the `preload_app!` method when specifying a `workers` number.
38
+ # This directive tells Puma to first boot the application and load code
39
+ # before forking the application. This takes advantage of Copy On Write
40
+ # process behavior so workers use less memory.
41
+ #
42
+ # preload_app!
43
+
44
+ # Allow puma to be restarted by `rails restart` command.
45
+ plugin :tmp_restart
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ Rails.application.routes.draw do
4
+ # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
5
+ end
data/example/config.ru ADDED
@@ -0,0 +1,8 @@
1
+ # frozen_string_literal: true
2
+
3
+ # This file is used by Rack-based servers to start the application.
4
+
5
+ require_relative "config/environment"
6
+
7
+ run Rails.application
8
+ Rails.application.load_server
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The page you were looking for doesn't exist (404)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ .rails-default-error-page {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ .rails-default-error-page div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ .rails-default-error-page div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ .rails-default-error-page h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ .rails-default-error-page div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body class="rails-default-error-page">
58
+ <!-- This file lives in public/404.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The page you were looking for doesn't exist.</h1>
62
+ <p>You may have mistyped the address or the page may have moved.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,67 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>The change you wanted was rejected (422)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ .rails-default-error-page {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ .rails-default-error-page div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ .rails-default-error-page div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ .rails-default-error-page h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ .rails-default-error-page div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body class="rails-default-error-page">
58
+ <!-- This file lives in public/422.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>The change you wanted was rejected.</h1>
62
+ <p>Maybe you tried to change something you didn't have access to.</p>
63
+ </div>
64
+ <p>If you are the application owner check the logs for more information.</p>
65
+ </div>
66
+ </body>
67
+ </html>
@@ -0,0 +1,66 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>We're sorry, but something went wrong (500)</title>
5
+ <meta name="viewport" content="width=device-width,initial-scale=1">
6
+ <style>
7
+ .rails-default-error-page {
8
+ background-color: #EFEFEF;
9
+ color: #2E2F30;
10
+ text-align: center;
11
+ font-family: arial, sans-serif;
12
+ margin: 0;
13
+ }
14
+
15
+ .rails-default-error-page div.dialog {
16
+ width: 95%;
17
+ max-width: 33em;
18
+ margin: 4em auto 0;
19
+ }
20
+
21
+ .rails-default-error-page div.dialog > div {
22
+ border: 1px solid #CCC;
23
+ border-right-color: #999;
24
+ border-left-color: #999;
25
+ border-bottom-color: #BBB;
26
+ border-top: #B00100 solid 4px;
27
+ border-top-left-radius: 9px;
28
+ border-top-right-radius: 9px;
29
+ background-color: white;
30
+ padding: 7px 12% 0;
31
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
32
+ }
33
+
34
+ .rails-default-error-page h1 {
35
+ font-size: 100%;
36
+ color: #730E15;
37
+ line-height: 1.5em;
38
+ }
39
+
40
+ .rails-default-error-page div.dialog > p {
41
+ margin: 0 0 1em;
42
+ padding: 1em;
43
+ background-color: #F7F7F7;
44
+ border: 1px solid #CCC;
45
+ border-right-color: #999;
46
+ border-left-color: #999;
47
+ border-bottom-color: #999;
48
+ border-bottom-left-radius: 4px;
49
+ border-bottom-right-radius: 4px;
50
+ border-top-color: #DADADA;
51
+ color: #666;
52
+ box-shadow: 0 3px 8px rgba(50, 50, 50, 0.17);
53
+ }
54
+ </style>
55
+ </head>
56
+
57
+ <body class="rails-default-error-page">
58
+ <!-- This file lives in public/500.html -->
59
+ <div class="dialog">
60
+ <div>
61
+ <h1>We're sorry, but something went wrong.</h1>
62
+ </div>
63
+ <p>If you are the application owner check the logs for more information.</p>
64
+ </div>
65
+ </body>
66
+ </html>
File without changes
File without changes
File without changes
@@ -0,0 +1 @@
1
+ # See https://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file
data/example/tests.sh ADDED
@@ -0,0 +1,4 @@
1
+ #!/usr/bin/env bash
2
+
3
+ curl 127.0.0.1:3001
4
+ curl 127.0.0.1:3001/reset-transaction
@@ -0,0 +1,28 @@
1
+ require_relative 'server'
2
+
3
+ module DerailSpecs
4
+ class Boot
5
+ def run
6
+ Transaction.begin
7
+ set_exit_hooks!
8
+
9
+ Server.new.tap(&:boot)
10
+
11
+ puts "Starting Tests…"
12
+
13
+ system DerailSpecs.configuration.command
14
+ end
15
+
16
+ private
17
+
18
+ def set_exit_hooks!
19
+ at_exit do
20
+ Transaction.rollback
21
+ end
22
+ Signal.trap("INT") do
23
+ puts "Exiting derail_specs…"
24
+ exit
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,7 @@
1
+ module DerailSpecs
2
+ require 'rails'
3
+
4
+ class Railtie < Rails::Railtie
5
+ rake_tasks { load 'tasks/derail_specs.rake' }
6
+ end
7
+ end
@@ -0,0 +1,15 @@
1
+ module DerailSpecs
2
+ class Server
3
+ App = Rack::Builder.new do
4
+ map "/reset-transaction" do
5
+ run lambda { |_env|
6
+ Transaction.reset
7
+ [202, { "Content-Type" => "text/plain" }, ["Accepted"]]
8
+ }
9
+ end
10
+ map "/" do
11
+ run Rails.application
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,43 @@
1
+ module DerailSpecs
2
+ class Server
3
+ class Checker
4
+ TRY_HTTPS_ERRORS = [EOFError, Net::ReadTimeout, Errno::ECONNRESET].freeze
5
+
6
+ def initialize(host, port)
7
+ @host = host
8
+ @port = port
9
+ @ssl = false
10
+ end
11
+
12
+ def request(&block)
13
+ ssl? ? https_request(&block) : http_request(&block)
14
+ rescue *TRY_HTTPS_ERRORS
15
+ res = https_request(&block)
16
+ @ssl = true
17
+ res
18
+ end
19
+
20
+ def ssl?
21
+ @ssl
22
+ end
23
+
24
+ private
25
+
26
+ def http_request(&block)
27
+ make_request(read_timeout: 2, &block)
28
+ end
29
+
30
+ def https_request(&block)
31
+ make_request(**ssl_options, &block)
32
+ end
33
+
34
+ def make_request(**options, &block)
35
+ Net::HTTP.start(@host, @port, options.merge(max_retries: 0), &block)
36
+ end
37
+
38
+ def ssl_options
39
+ { use_ssl: true, verify_mode: OpenSSL::SSL::VERIFY_NONE }
40
+ end
41
+ end
42
+ end
43
+ end
@@ -0,0 +1,67 @@
1
+ module DerailSpecs
2
+ class Server
3
+ class Middleware
4
+ class Counter
5
+ def initialize
6
+ @value = []
7
+ @mutex = Mutex.new
8
+ end
9
+
10
+ def increment(uri)
11
+ @mutex.synchronize { @value.push(uri) }
12
+ end
13
+
14
+ def decrement(uri)
15
+ @mutex.synchronize { @value.delete_at(@value.index(uri) || @value.length) }
16
+ end
17
+
18
+ def positive?
19
+ @mutex.synchronize { @value.length.positive? }
20
+ end
21
+
22
+ def value
23
+ @mutex.synchronize { @value.dup }
24
+ end
25
+ end
26
+
27
+ attr_reader :error
28
+
29
+ def initialize(app, server_errors, extra_middleware = [])
30
+ @app = app
31
+ @extended_app = extra_middleware.inject(@app) do |ex_app, klass|
32
+ klass.new(ex_app)
33
+ end
34
+ @counter = Counter.new
35
+ @server_errors = server_errors
36
+ end
37
+
38
+ def pending_requests
39
+ @counter.value
40
+ end
41
+
42
+ def pending_requests?
43
+ @counter.positive?
44
+ end
45
+
46
+ def clear_error
47
+ @error = nil
48
+ end
49
+
50
+ def call(env)
51
+ if env["PATH_INFO"] == "/__identify__"
52
+ [200, {}, [@app.object_id.to_s]]
53
+ else
54
+ @counter.increment(env["REQUEST_URI"])
55
+ begin
56
+ @extended_app.call(env)
57
+ rescue *@server_errors => e
58
+ @error ||= e
59
+ raise e
60
+ ensure
61
+ @counter.decrement(env["REQUEST_URI"])
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end