rspec-snapshot 2.0.1 → 2.0.3

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: '099c9c3b45ca0936bb35a90dc10263dc0634cfbf307e32eab60eb6154cd0d94c'
4
- data.tar.gz: 862c34e9f5693afe80ca9384048d03d67e78a6e09300b1906a1e8c929315b263
3
+ metadata.gz: 4668f539ce7c95d76e77eab406e9b7dd1a8a5cf50265b60309b8da6d3c2564a1
4
+ data.tar.gz: ecf7a28db05aa850e7fec2b413e9b13dfe9c917eee5747c68fb22eab3bf10471
5
5
  SHA512:
6
- metadata.gz: a25332c1f83a33d982f16fdf31284d68800db12c1f49a08320e3eca1515a614bd7ba1cb0a5028953d8d32c0cade632c8970a86d1c9fdf941c9b41c9d71476c55
7
- data.tar.gz: 3c952f9f7da76ac1c464a9e35c69e8b280e09aa6066c9e763eba97e4ad7b4ded481516b68bdbecf35d235010a51e832a15955538e56d02d355c84299c4e42409
6
+ metadata.gz: 5fa621abf5030844e59efe1e5b3c2846debaaf9905bc4506c5d4be6481e9f549b3b4df84365867e2ba7c021fcc56d3262f374c9d0fcd5cc2f0d53d7e4ed925d9
7
+ data.tar.gz: 2b8425fad9b2f564d05b1d9ffbce831368d279321ccfb8747e25fce6f4c4e45a2e7ce44c79ab9b4ca803a4a20c8438f6e2c3d44555b458cb65789cda0fd1570d
@@ -0,0 +1,11 @@
1
+ # To get started with Dependabot version updates, you'll need to specify which
2
+ # package ecosystems to update and where the package manifests are located.
3
+ # Please see the documentation for all configuration options:
4
+ # https://docs.github.com/github/administering-a-repository/configuration-options-for-dependency-updates
5
+
6
+ version: 2
7
+ updates:
8
+ - package-ecosystem: "bundler" # See documentation for possible values
9
+ directory: "/" # Location of package manifests
10
+ schedule:
11
+ interval: "weekly"
@@ -0,0 +1,40 @@
1
+ on:
2
+ push:
3
+ pull_request:
4
+
5
+ jobs:
6
+ lint:
7
+ runs-on: ubuntu-latest
8
+ strategy:
9
+ matrix:
10
+ ruby-version: ['3.3', '3.2', '3.1', '3.0', '2.7']
11
+ defaults:
12
+ run:
13
+ shell: bash
14
+ steps:
15
+ - uses: actions/checkout@v4
16
+ - uses: ruby/setup-ruby@360dc864d5da99d54fcb8e9148c14a84b90d3e88
17
+ with:
18
+ ruby-version: ${{ matrix.ruby-version }}
19
+ - run: gem install bundler:2.4.22
20
+ - run: bundle install
21
+ - run: bundle exec rubocop
22
+
23
+ test:
24
+ needs: lint
25
+ runs-on: ubuntu-latest
26
+ strategy:
27
+ matrix:
28
+ ruby-version: ['3.3', '3.2', '3.1', '3.0', '2.7']
29
+ defaults:
30
+ run:
31
+ shell: bash
32
+ steps:
33
+ - uses: actions/checkout@v4
34
+ - uses: ruby/setup-ruby@360dc864d5da99d54fcb8e9148c14a84b90d3e88
35
+ with:
36
+ ruby-version: ${{ matrix.ruby-version }}
37
+ - run: gem install bundler:2.4.22
38
+ - run: bundle install
39
+ - run: cat Gemfile.lock
40
+ - run: bundle exec rspec
data/.rubocop.yml CHANGED
@@ -1,4 +1,5 @@
1
1
  require:
2
+ - rubocop-rake
2
3
  - rubocop-rspec
3
4
 
4
5
  AllCops:
@@ -47,7 +48,7 @@ RSpec/ExpectInHook:
47
48
  RSpec/FilePath:
48
49
  Enabled: true
49
50
  RSpec/InstanceVariable:
50
- Enabled: false
51
+ Enabled: true
51
52
  RSpec/LeakyConstantDeclaration:
52
53
  Enabled: true
53
54
  RSpec/LetSetup:
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 2.7.3
1
+ 3.2.2
data/Gemfile CHANGED
@@ -2,3 +2,13 @@ source 'https://rubygems.org'
2
2
 
3
3
  # Specify your gem's dependencies in rspec-snapshot.gemspec
4
4
  gemspec
5
+
6
+ # Development dependencies
7
+ gem 'bundler', '~> 2.3'
8
+ gem 'byebug'
9
+ gem 'pry-byebug'
10
+ gem 'rake', '~> 13.0'
11
+ gem 'rubocop', '~> 1.59'
12
+ gem 'rubocop-rake', '~> 0.6.0'
13
+ gem 'rubocop-rspec', '~> 2.16'
14
+ gem 'simplecov'
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # RSpec::Snapshot [![Build Status](https://travis-ci.com/levinmr/rspec-snapshot.svg?branch=master)](https://travis-ci.com/levinmr/rspec-snapshot)
1
+ # RSpec::Snapshot ![Build Status](https://github.com/levinmr/rspec-snapshot/actions/workflows/ci.yml/badge.svg?branch=master)
2
2
 
3
3
  Adds snapshot testing to RSpec, inspired by [Jest](https://jestjs.io/).
4
4
 
@@ -154,12 +154,19 @@ Install a current version of ruby (> 2.5) and bundler. Then install gems
154
154
 
155
155
  $ bin/console
156
156
 
157
- ### Rake commands
157
+ ### Installing the gem locally
158
158
 
159
- To install this gem onto your local machine, run `bundle exec rake install`. To
160
- release a new version, update the version number in `version.rb`, and then run
161
- `bundle exec rake release`, which will create a git tag for the version, push
162
- git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
159
+ $ bundle exec rake install
160
+
161
+ ### Publishing a new gem version
162
+
163
+ * Update the version number in `version.rb`
164
+ * Ensure the changes to be published are merged to the master branch
165
+ * Checkout the master branch locally
166
+ * Run `bundle exec rake release`, which will:
167
+ * create a git tag for the version
168
+ * push git commits and tags
169
+ * push the `.gem` file to [rubygems.org](https://rubygems.org).
163
170
 
164
171
  ## Contributing
165
172
 
@@ -7,6 +7,8 @@ module RSpec
7
7
  # Serializes values in a human readable way for snapshots using the
8
8
  # awesome_print gem
9
9
  class DefaultSerializer
10
+ # @param [*] value The value to serialize.
11
+ # @return [String] The serialized value.
10
12
  def dump(value)
11
13
  value.ai(plain: true, indent: 2)
12
14
  end
@@ -0,0 +1,75 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'fileutils'
4
+
5
+ module RSpec
6
+ module Snapshot
7
+ # Handles File IO for snapshots
8
+ class FileOperator
9
+ # Initializes the class instance, and creates the snapshot directory for
10
+ # the current test if needed.
11
+ #
12
+ # @param [String] snapshot_name The name of the snapshot to read/write.
13
+ # @param [Hash] metadata The RSpec metadata for the current test.
14
+ def initialize(snapshot_name, metadata)
15
+ snapshot_dir = snapshot_dir(metadata)
16
+ @snapshot_path = File.join(snapshot_dir, "#{snapshot_name}.snap")
17
+ create_snapshot_dir(@snapshot_path)
18
+ end
19
+
20
+ private def snapshot_dir(metadata)
21
+ if RSpec.configuration.snapshot_dir == :relative
22
+ File.dirname(metadata[:file_path]) << '/__snapshots__'
23
+ else
24
+ RSpec.configuration.snapshot_dir
25
+ end
26
+ end
27
+
28
+ private def create_snapshot_dir(snapshot_dir)
29
+ return if Dir.exist?(File.dirname(snapshot_dir))
30
+
31
+ FileUtils.mkdir_p(File.dirname(snapshot_dir))
32
+ end
33
+
34
+ # @return [String] The snapshot file contents.
35
+ def read
36
+ file = File.new(@snapshot_path)
37
+ value = file.read
38
+ file.close
39
+ value
40
+ end
41
+
42
+ # Writes the value to file, overwriting the file contents if either of the
43
+ # following is true:
44
+ # * The snapshot file does not already exist.
45
+ # * The UPDATE_SNAPSHOTS environment variable is set.
46
+ #
47
+ # TODO: Do not write to file if running in CI mode.
48
+ #
49
+ # @param [String] snapshot_name The snapshot name.
50
+ # @param [String] value The value to write to file.
51
+ def write(value)
52
+ return unless should_write?
53
+
54
+ file = File.new(@snapshot_path, 'w+')
55
+ file.write(value)
56
+ RSpec.configuration.reporter.message(
57
+ "Snapshot written: #{@snapshot_path}"
58
+ )
59
+ file.close
60
+ end
61
+
62
+ private def should_write?
63
+ file_does_not_exist? || update_snapshots?
64
+ end
65
+
66
+ private def update_snapshots?
67
+ !!ENV.fetch('UPDATE_SNAPSHOTS', nil)
68
+ end
69
+
70
+ private def file_does_not_exist?
71
+ !File.exist?(@snapshot_path)
72
+ end
73
+ end
74
+ end
75
+ end
@@ -1,8 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'fileutils'
4
- require 'rspec/snapshot/default_serializer'
5
-
6
3
  module RSpec
7
4
  module Snapshot
8
5
  module Matchers
@@ -10,43 +7,22 @@ module RSpec
10
7
  class MatchSnapshot
11
8
  attr_reader :actual, :expected
12
9
 
13
- def initialize(metadata, snapshot_name, config)
14
- @metadata = metadata
15
- @snapshot_name = snapshot_name
16
- @config = config
17
- @serializer = serializer_class.new
18
- @snapshot_path = File.join(snapshot_dir, "#{@snapshot_name}.snap")
19
- create_snapshot_dir
20
- end
21
-
22
- private def serializer_class
23
- if @config[:snapshot_serializer]
24
- @config[:snapshot_serializer]
25
- elsif RSpec.configuration.snapshot_serializer
26
- RSpec.configuration.snapshot_serializer
27
- else
28
- DefaultSerializer
29
- end
30
- end
31
-
32
- private def snapshot_dir
33
- if RSpec.configuration.snapshot_dir.to_s == 'relative'
34
- File.dirname(@metadata[:file_path]) << '/__snapshots__'
35
- else
36
- RSpec.configuration.snapshot_dir
37
- end
38
- end
39
-
40
- private def create_snapshot_dir
41
- return if Dir.exist?(File.dirname(@snapshot_path))
42
-
43
- FileUtils.mkdir_p(File.dirname(@snapshot_path))
10
+ # @param [#dump] serializer A class instance which responds to #dump to
11
+ # convert test values to string for writing to snapshot files.
12
+ # @param [FileOperator] file_operator Handles reading and writing the
13
+ # snapshot file contents.
14
+ def initialize(serializer, file_operator)
15
+ @serializer = serializer
16
+ @file_operator = file_operator
44
17
  end
45
18
 
19
+ # @param [*] actual The received test value to compare to a snapshot.
20
+ # @return [Boolean] True if the serialized actual value matches the
21
+ # snapshot contents, false otherwise.
46
22
  def matches?(actual)
47
23
  @actual = serialize(actual)
48
24
 
49
- write_snapshot
25
+ write_snapshot(@actual)
50
26
 
51
27
  @expected = read_snapshot
52
28
 
@@ -63,30 +39,12 @@ module RSpec
63
39
  @serializer.dump(value)
64
40
  end
65
41
 
66
- private def write_snapshot
67
- return unless should_write?
68
-
69
- RSpec.configuration.reporter.message(
70
- "Snapshot written: #{@snapshot_path}"
71
- )
72
- file = File.new(@snapshot_path, 'w+')
73
- file.write(@actual)
74
- file.close
75
- end
76
-
77
- private def should_write?
78
- update_snapshots? || !File.exist?(@snapshot_path)
79
- end
80
-
81
- private def update_snapshots?
82
- ENV['UPDATE_SNAPSHOTS']
42
+ private def write_snapshot(value)
43
+ @file_operator.write(value)
83
44
  end
84
45
 
85
46
  private def read_snapshot
86
- file = File.new(@snapshot_path)
87
- value = file.read
88
- file.close
89
- value
47
+ @file_operator.read
90
48
  end
91
49
 
92
50
  def description
@@ -1,15 +1,17 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'rspec/snapshot/matchers/match_snapshot'
4
+ require 'rspec/snapshot/file_operator'
5
+ require 'rspec/snapshot/serializer_factory'
4
6
 
5
7
  module RSpec
6
8
  module Snapshot
7
9
  # rubocop:disable Style/Documentation
8
10
  module Matchers
9
11
  def match_snapshot(snapshot_name, config = {})
10
- MatchSnapshot.new(RSpec.current_example.metadata,
11
- snapshot_name,
12
- config)
12
+ MatchSnapshot.new(SerializerFactory.new(config).create,
13
+ FileOperator.new(snapshot_name,
14
+ RSpec.current_example.metadata))
13
15
  end
14
16
 
15
17
  alias snapshot match_snapshot
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rspec/snapshot/default_serializer'
4
+
5
+ module RSpec
6
+ module Snapshot
7
+ # Uses the factory pattern to initialize a snapshot serializer.
8
+ class SerializerFactory
9
+ def initialize(config = {})
10
+ @config = config
11
+ end
12
+
13
+ # @returns [#dump] A serializer object which implements #dump to convert
14
+ # any value to string.
15
+ def create
16
+ serializer_class.new
17
+ end
18
+
19
+ private def serializer_class
20
+ if @config[:snapshot_serializer]
21
+ @config[:snapshot_serializer]
22
+ elsif RSpec.configuration.snapshot_serializer
23
+ RSpec.configuration.snapshot_serializer
24
+ else
25
+ DefaultSerializer
26
+ end
27
+ end
28
+ end
29
+ end
30
+ end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module RSpec
4
4
  module Snapshot
5
- VERSION = '2.0.1'
5
+ VERSION = '2.0.3'
6
6
  end
7
7
  end
@@ -26,12 +26,5 @@ Gem::Specification.new do |spec|
26
26
 
27
27
  spec.add_dependency 'awesome_print', '> 1.0.0'
28
28
  spec.add_dependency 'rspec', '> 3.0.0'
29
-
30
- spec.add_development_dependency 'bundler', '~> 2.2'
31
- spec.add_development_dependency 'byebug'
32
- spec.add_development_dependency 'pry-byebug'
33
- spec.add_development_dependency 'rake', '~> 13.0'
34
- spec.add_development_dependency 'rubocop', '~> 1.22'
35
- spec.add_development_dependency 'rubocop-rspec', '~> 2.5'
36
- spec.add_development_dependency 'simplecov'
29
+ spec.metadata['rubygems_mfa_required'] = 'true'
37
30
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rspec-snapshot
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.1
4
+ version: 2.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Mike Levin
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-10-15 00:00:00.000000000 Z
11
+ date: 2024-04-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: awesome_print
@@ -38,104 +38,6 @@ dependencies:
38
38
  - - ">"
39
39
  - !ruby/object:Gem::Version
40
40
  version: 3.0.0
41
- - !ruby/object:Gem::Dependency
42
- name: bundler
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: '2.2'
48
- type: :development
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: '2.2'
55
- - !ruby/object:Gem::Dependency
56
- name: byebug
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: pry-byebug
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: rake
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - "~>"
88
- - !ruby/object:Gem::Version
89
- version: '13.0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - "~>"
95
- - !ruby/object:Gem::Version
96
- version: '13.0'
97
- - !ruby/object:Gem::Dependency
98
- name: rubocop
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - "~>"
102
- - !ruby/object:Gem::Version
103
- version: '1.22'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: '1.22'
111
- - !ruby/object:Gem::Dependency
112
- name: rubocop-rspec
113
- requirement: !ruby/object:Gem::Requirement
114
- requirements:
115
- - - "~>"
116
- - !ruby/object:Gem::Version
117
- version: '2.5'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - "~>"
123
- - !ruby/object:Gem::Version
124
- version: '2.5'
125
- - !ruby/object:Gem::Dependency
126
- name: simplecov
127
- requirement: !ruby/object:Gem::Requirement
128
- requirements:
129
- - - ">="
130
- - !ruby/object:Gem::Version
131
- version: '0'
132
- type: :development
133
- prerelease: false
134
- version_requirements: !ruby/object:Gem::Requirement
135
- requirements:
136
- - - ">="
137
- - !ruby/object:Gem::Version
138
- version: '0'
139
41
  description: Adding snapshot testing to RSpec
140
42
  email:
141
43
  - michael_r_levin@yahoo.com
@@ -143,11 +45,12 @@ executables: []
143
45
  extensions: []
144
46
  extra_rdoc_files: []
145
47
  files:
48
+ - ".github/dependabot.yml"
49
+ - ".github/workflows/ci.yml"
146
50
  - ".gitignore"
147
51
  - ".rspec"
148
52
  - ".rubocop.yml"
149
53
  - ".ruby-version"
150
- - ".travis.yml"
151
54
  - Gemfile
152
55
  - LICENSE.md
153
56
  - README.md
@@ -156,14 +59,17 @@ files:
156
59
  - lib/rspec/snapshot.rb
157
60
  - lib/rspec/snapshot/configuration.rb
158
61
  - lib/rspec/snapshot/default_serializer.rb
62
+ - lib/rspec/snapshot/file_operator.rb
159
63
  - lib/rspec/snapshot/matchers.rb
160
64
  - lib/rspec/snapshot/matchers/match_snapshot.rb
65
+ - lib/rspec/snapshot/serializer_factory.rb
161
66
  - lib/rspec/snapshot/version.rb
162
67
  - rspec-snapshot.gemspec
163
68
  homepage: https://github.com/levinmr/rspec-snapshot
164
69
  licenses:
165
70
  - MIT
166
- metadata: {}
71
+ metadata:
72
+ rubygems_mfa_required: 'true'
167
73
  post_install_message:
168
74
  rdoc_options: []
169
75
  require_paths:
@@ -179,7 +85,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
179
85
  - !ruby/object:Gem::Version
180
86
  version: '0'
181
87
  requirements: []
182
- rubygems_version: 3.1.6
88
+ rubygems_version: 3.4.10
183
89
  signing_key:
184
90
  specification_version: 4
185
91
  summary: RSpec Snapshot Matcher
data/.travis.yml DELETED
@@ -1,9 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- dist: focal
4
- rvm:
5
- - 2.5.9
6
- - 2.6.8
7
- - 2.7.4
8
- - 3.0.2
9
- before_install: gem install bundler:2.2.22