snapshot_inspector 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (43) hide show
  1. checksums.yaml +7 -0
  2. data/MIT-LICENSE +20 -0
  3. data/README.md +227 -0
  4. data/Rakefile +8 -0
  5. data/app/assets/config/snapshot_inspector/manifest.js +2 -0
  6. data/app/assets/javascripts/snapshot_inspector/application.js +1 -0
  7. data/app/assets/stylesheets/snapshot_inspector/application.css +33 -0
  8. data/app/assets/stylesheets/snapshot_inspector/snapshots/mail.css +48 -0
  9. data/app/assets/stylesheets/snapshot_inspector/snapshots/not_found.css +15 -0
  10. data/app/assets/stylesheets/snapshot_inspector/snapshots/response.css +9 -0
  11. data/app/assets/stylesheets/snapshot_inspector/snapshots.css +73 -0
  12. data/app/controllers/snapshot_inspector/application_controller.rb +14 -0
  13. data/app/controllers/snapshot_inspector/snapshots/mail_controller.rb +57 -0
  14. data/app/controllers/snapshot_inspector/snapshots/response_controller.rb +15 -0
  15. data/app/controllers/snapshot_inspector/snapshots_controller.rb +7 -0
  16. data/app/helpers/snapshot_inspector/application_helper.rb +15 -0
  17. data/app/helpers/snapshot_inspector/snapshots_helper.rb +37 -0
  18. data/app/mailers/snapshot_inspector/application_mailer.rb +6 -0
  19. data/app/models/snapshot_inspector/snapshot/context.rb +49 -0
  20. data/app/models/snapshot_inspector/snapshot/mail_type.rb +35 -0
  21. data/app/models/snapshot_inspector/snapshot/response_type.rb +19 -0
  22. data/app/models/snapshot_inspector/snapshot/rspec_context.rb +52 -0
  23. data/app/models/snapshot_inspector/snapshot/test_unit_context.rb +44 -0
  24. data/app/models/snapshot_inspector/snapshot/type.rb +52 -0
  25. data/app/models/snapshot_inspector/snapshot.rb +86 -0
  26. data/app/views/layouts/snapshot_inspector/application.html.erb +18 -0
  27. data/app/views/snapshot_inspector/snapshots/index.html.erb +29 -0
  28. data/app/views/snapshot_inspector/snapshots/mail/show.html.erb +107 -0
  29. data/app/views/snapshot_inspector/snapshots/not_found.html.erb +8 -0
  30. data/app/views/snapshot_inspector/snapshots/response/raw.html.erb +1 -0
  31. data/app/views/snapshot_inspector/snapshots/response/show.html.erb +1 -0
  32. data/config/importmap.rb +12 -0
  33. data/config/routes.rb +9 -0
  34. data/lib/minitest/snapshot_inspector_plugin.rb +28 -0
  35. data/lib/snapshot_inspector/engine.rb +67 -0
  36. data/lib/snapshot_inspector/storage.rb +60 -0
  37. data/lib/snapshot_inspector/test/action_mailer_headers.rb +18 -0
  38. data/lib/snapshot_inspector/test/rspec_helpers.rb +45 -0
  39. data/lib/snapshot_inspector/test/test_unit_helpers.rb +48 -0
  40. data/lib/snapshot_inspector/version.rb +3 -0
  41. data/lib/snapshot_inspector.rb +42 -0
  42. data/lib/tasks/tmp.rake +10 -0
  43. metadata +159 -0
@@ -0,0 +1,45 @@
1
+ module SnapshotInspector
2
+ module Test
3
+ module RSpecHelpers
4
+ extend ActiveSupport::Concern
5
+
6
+ # Takes a snapshot of a given +snapshotee+.
7
+ #
8
+ # A +snapshotee+ can be an instance of +ActionDispatch::TestResponse+ or +ActionMailer::MessageDelivery+.
9
+ #
10
+ # +take_snapshot+ needs to be called after the +snapshotee+ object becomes available
11
+ # for inspection in the lifecycle of the spec (e.g. request or mailer spec). You can take one or
12
+ # more snapshots in a single spec.
13
+ #
14
+ # Snapshots are taken only when explicitly enabled with an environment variable +TAKE_SNAPSHOTS=1+.
15
+ #
16
+ # E.g. +bin/rspec TAKE_SNAPSHOTS=1+
17
+ #
18
+ # @param snapshotee [ActionDispatch::TestResponse, ActionMailer::MessageDelivery]
19
+ # @return SnapshotInspector::Snapshot
20
+ def take_snapshot(snapshotee)
21
+ return unless SnapshotInspector.configuration.snapshot_taking_enabled
22
+
23
+ increment_take_snapshot_counter_scoped_by_test
24
+
25
+ SnapshotInspector::Snapshot.persist(
26
+ snapshotee: snapshotee,
27
+ context: {
28
+ test_framework: :rspec,
29
+ example: RSpec.current_example.metadata.except(:execution_result).as_json.with_indifferent_access,
30
+ take_snapshot_index: _take_snapshot_counter - 1
31
+ }
32
+ )
33
+ end
34
+
35
+ private
36
+
37
+ attr_reader :_take_snapshot_counter
38
+
39
+ def increment_take_snapshot_counter_scoped_by_test
40
+ @_take_snapshot_counter ||= 0
41
+ @_take_snapshot_counter += 1
42
+ end
43
+ end
44
+ end
45
+ end
@@ -0,0 +1,48 @@
1
+ module SnapshotInspector
2
+ module Test
3
+ module TestUnitHelpers
4
+ extend ActiveSupport::Concern
5
+
6
+ # Takes a snapshot of a given +snapshotee+.
7
+ #
8
+ # A +snapshotee+ can be an instance of +ActionDispatch::TestResponse+ or +ActionMailer::MessageDelivery+.
9
+ #
10
+ # +take_snapshot+ needs to be called after the +snapshotee+ object becomes available
11
+ # for inspection in the lifecycle of the test (e.g. integration or mailer test). You can take one or
12
+ # more snapshots in a single test case.
13
+ #
14
+ # Snapshots are taken only when explicitly enabled with a flag +--take-snapshots+
15
+ # or when an environment variable +TAKE_SNAPSHOTS=1+ is set.
16
+ #
17
+ # E.g. +bin/rails test --take-snapshots+
18
+ #
19
+ # @param snapshotee [ActionDispatch::TestResponse, ActionMailer::MessageDelivery]
20
+ # @return SnapshotInspector::Snapshot
21
+ def take_snapshot(snapshotee)
22
+ return unless SnapshotInspector.configuration.snapshot_taking_enabled
23
+
24
+ increment_take_snapshot_counter_scoped_by_test
25
+
26
+ SnapshotInspector::Snapshot.persist(
27
+ snapshotee: snapshotee,
28
+ context: {
29
+ test_framework: :test_unit,
30
+ method_name: method_name,
31
+ source_location: method(method_name).source_location,
32
+ test_case_name: self.class.to_s,
33
+ take_snapshot_index: _take_snapshot_counter - 1
34
+ }
35
+ )
36
+ end
37
+
38
+ private
39
+
40
+ attr_reader :_take_snapshot_counter
41
+
42
+ def increment_take_snapshot_counter_scoped_by_test
43
+ @_take_snapshot_counter ||= 0
44
+ @_take_snapshot_counter += 1
45
+ end
46
+ end
47
+ end
48
+ end
@@ -0,0 +1,3 @@
1
+ module SnapshotInspector
2
+ VERSION = "0.1.1"
3
+ end
@@ -0,0 +1,42 @@
1
+ abort "`snapshot_inspector` is only meant to be loaded in the `development` and `test` environments. Your current environment is `#{Rails.env}`. Move the gem into the `group :development, :test` block in your Gemfile." unless Rails.env.development? || Rails.env.test?
2
+
3
+ require "snapshot_inspector/version"
4
+ require "snapshot_inspector/engine"
5
+ require "minitest/snapshot_inspector_plugin"
6
+ require "importmap-rails"
7
+
8
+ module SnapshotInspector
9
+ module Test
10
+ autoload :TestUnitHelpers, "snapshot_inspector/test/test_unit_helpers"
11
+ autoload :RSpecHelpers, "snapshot_inspector/test/rspec_helpers"
12
+ end
13
+
14
+ STORAGE_DIRECTORY = "tmp/snapshot_inspector"
15
+
16
+ class << self
17
+ attr_accessor :configuration
18
+ end
19
+
20
+ class Configuration
21
+ attr_accessor :importmap, :snapshot_taking_enabled, :storage_directory, :host, :route_path
22
+
23
+ def initialize
24
+ @importmap = Importmap::Map.new
25
+ @snapshot_taking_enabled = ENV.fetch("TAKE_SNAPSHOTS", nil) == "1"
26
+ @storage_directory = nil
27
+ @host = "http://localhost:3000"
28
+ @route_path = "/rails/snapshots"
29
+ end
30
+ end
31
+
32
+ def self.initialize_configuration
33
+ self.configuration ||= Configuration.new
34
+ end
35
+
36
+ def self.configure
37
+ initialize_configuration
38
+ yield(configuration)
39
+ end
40
+ end
41
+
42
+ SnapshotInspector.initialize_configuration
@@ -0,0 +1,10 @@
1
+ require "snapshot_inspector/storage"
2
+
3
+ namespace :tmp do
4
+ namespace :snapshots do
5
+ desc "Clear all files in the snapshots storage directory (e.g. tmp/snapshots)"
6
+ task :clear do
7
+ SnapshotInspector::Storage.clear
8
+ end
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,159 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: snapshot_inspector
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Tomaz Zlender
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-03-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rails
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: 7.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 7.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: stimulus-rails
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 1.0.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: 1.0.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: importmap-rails
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: 1.0.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: 1.0.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: sprockets-rails
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 3.3.0
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 3.3.0
69
+ - !ruby/object:Gem::Dependency
70
+ name: nokogiri
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: 1.11.0
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 1.11.0
83
+ description: Take snapshots of responses and mail messages while testing, and inspect
84
+ them in a browser. A Ruby on Rails engine.
85
+ email:
86
+ - tomaz@zlender.se
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - MIT-LICENSE
92
+ - README.md
93
+ - Rakefile
94
+ - app/assets/config/snapshot_inspector/manifest.js
95
+ - app/assets/javascripts/snapshot_inspector/application.js
96
+ - app/assets/stylesheets/snapshot_inspector/application.css
97
+ - app/assets/stylesheets/snapshot_inspector/snapshots.css
98
+ - app/assets/stylesheets/snapshot_inspector/snapshots/mail.css
99
+ - app/assets/stylesheets/snapshot_inspector/snapshots/not_found.css
100
+ - app/assets/stylesheets/snapshot_inspector/snapshots/response.css
101
+ - app/controllers/snapshot_inspector/application_controller.rb
102
+ - app/controllers/snapshot_inspector/snapshots/mail_controller.rb
103
+ - app/controllers/snapshot_inspector/snapshots/response_controller.rb
104
+ - app/controllers/snapshot_inspector/snapshots_controller.rb
105
+ - app/helpers/snapshot_inspector/application_helper.rb
106
+ - app/helpers/snapshot_inspector/snapshots_helper.rb
107
+ - app/mailers/snapshot_inspector/application_mailer.rb
108
+ - app/models/snapshot_inspector/snapshot.rb
109
+ - app/models/snapshot_inspector/snapshot/context.rb
110
+ - app/models/snapshot_inspector/snapshot/mail_type.rb
111
+ - app/models/snapshot_inspector/snapshot/response_type.rb
112
+ - app/models/snapshot_inspector/snapshot/rspec_context.rb
113
+ - app/models/snapshot_inspector/snapshot/test_unit_context.rb
114
+ - app/models/snapshot_inspector/snapshot/type.rb
115
+ - app/views/layouts/snapshot_inspector/application.html.erb
116
+ - app/views/snapshot_inspector/snapshots/index.html.erb
117
+ - app/views/snapshot_inspector/snapshots/mail/show.html.erb
118
+ - app/views/snapshot_inspector/snapshots/not_found.html.erb
119
+ - app/views/snapshot_inspector/snapshots/response/raw.html.erb
120
+ - app/views/snapshot_inspector/snapshots/response/show.html.erb
121
+ - config/importmap.rb
122
+ - config/routes.rb
123
+ - lib/minitest/snapshot_inspector_plugin.rb
124
+ - lib/snapshot_inspector.rb
125
+ - lib/snapshot_inspector/engine.rb
126
+ - lib/snapshot_inspector/storage.rb
127
+ - lib/snapshot_inspector/test/action_mailer_headers.rb
128
+ - lib/snapshot_inspector/test/rspec_helpers.rb
129
+ - lib/snapshot_inspector/test/test_unit_helpers.rb
130
+ - lib/snapshot_inspector/version.rb
131
+ - lib/tasks/tmp.rake
132
+ homepage: https://github.com/tomazzlender/snapshot_inspector
133
+ licenses:
134
+ - MIT
135
+ metadata:
136
+ homepage_uri: https://github.com/tomazzlender/snapshot_inspector
137
+ source_code_uri: https://github.com/tomazzlender/snapshot_inspector
138
+ changelog_uri: https://github.com/tomazzlender/snapshot_inspector/blob/main/CHANGELOG.md
139
+ post_install_message:
140
+ rdoc_options: []
141
+ require_paths:
142
+ - lib
143
+ required_ruby_version: !ruby/object:Gem::Requirement
144
+ requirements:
145
+ - - ">="
146
+ - !ruby/object:Gem::Version
147
+ version: '0'
148
+ required_rubygems_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ requirements: []
154
+ rubygems_version: 3.4.7
155
+ signing_key:
156
+ specification_version: 4
157
+ summary: Take snapshots of responses and mail messages while testing, and inspect
158
+ them in a browser. A Ruby on Rails engine.
159
+ test_files: []