rspec-snapshot 2.0.0 → 2.0.2

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: e9e969397a7cd054e4bd77820f0e91574d45556659fcded20178ca52f8bc077d
4
- data.tar.gz: ca056e2c01e588a69aa51bb86b88af081acff4abd580cf74b1b624f4e3d55822
3
+ metadata.gz: b55b1aac3c8f7485efb46cf9ae2d62099d074f8c57c41d8a0b649ee6d7040c7f
4
+ data.tar.gz: 0e6d00759c134fe74c0b627fcbb40757825e959adffdd9ffa3599e68584bc954
5
5
  SHA512:
6
- metadata.gz: 5e7415a66e8182852a55ac99d922bb03488114cc0c3cf97f56c49ea11ff419f2a53dfbb67bd6aa3cd5956a9f533d3a4c8933897e651a6dc5578296366be2df43
7
- data.tar.gz: 69d2418bbfd2c94096ce3aae0dcb6a3415d8c217d491ddd502aa5324e0dd6a799ce595cfa9b846c9a2c474679301ac2ab3c2bd49d41010f39c5fa14c4d55aa2a
6
+ metadata.gz: c2ff25eb3ebc46a01852c4f2147a862cb8d9b94c2277aaadc9d331608d27cf4d0348c74521196c0c36d234827a274533dce1445ac91ba6f8c478d81a5605c59f
7
+ data.tar.gz: 3bf7ba016e5e47aa87bec679e06e48b0e54cf03f68f67e794bb065f351fdd09f314563e2053dc8d97d5a5d406f57df85cb71206087fda4738d5b7581a7b666b0
@@ -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,39 @@
1
+ on:
2
+ push:
3
+
4
+ jobs:
5
+ lint:
6
+ runs-on: ubuntu-latest
7
+ strategy:
8
+ matrix:
9
+ ruby-version: ['3.2', '3.1', '3.0', '2.7']
10
+ defaults:
11
+ run:
12
+ shell: bash
13
+ steps:
14
+ - uses: actions/checkout@v4
15
+ - uses: ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1
16
+ with:
17
+ ruby-version: ${{ matrix.ruby-version }}
18
+ - run: gem install bundler:2.4.22
19
+ - run: bundle install
20
+ - run: bundle exec rubocop
21
+
22
+ test:
23
+ needs: lint
24
+ runs-on: ubuntu-latest
25
+ strategy:
26
+ matrix:
27
+ ruby-version: ['3.2', '3.1', '3.0', '2.7']
28
+ defaults:
29
+ run:
30
+ shell: bash
31
+ steps:
32
+ - uses: actions/checkout@v4
33
+ - uses: ruby/setup-ruby@ec02537da5712d66d4d50a0f33b7eb52773b5ed1
34
+ with:
35
+ ruby-version: ${{ matrix.ruby-version }}
36
+ - run: gem install bundler:2.4.22
37
+ - run: bundle install
38
+ - run: cat Gemfile.lock
39
+ - 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/github/docs/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
 
@@ -3,17 +3,15 @@
3
3
  module RSpec
4
4
  # rubocop:disable Style/Documentation
5
5
  module Snapshot
6
- # rubocop:disable Lint/EmptyClass
7
- class Configuration; end
8
- # rubocop:enable Lint/EmptyClass
6
+ class Configuration
7
+ def self.initialize_configuration(config)
8
+ config.add_setting :snapshot_dir, default: :relative
9
9
 
10
- def self.initialize_configuration(config)
11
- config.add_setting :snapshot_dir, default: :relative
12
-
13
- config.add_setting :snapshot_serializer, default: nil
10
+ config.add_setting :snapshot_serializer, default: nil
11
+ end
14
12
  end
15
13
 
16
- initialize_configuration RSpec.configuration
14
+ Configuration.initialize_configuration RSpec.configuration
17
15
  end
18
16
  # rubocop:enable Style/Documentation
19
17
  end
@@ -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,73 @@
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
+ create_snapshot_dir(snapshot_dir)
17
+ @snapshot_path = File.join(snapshot_dir, "#{snapshot_name}.snap")
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
+ FileUtils.mkdir_p(snapshot_dir)
30
+ end
31
+
32
+ # @return [String] The snapshot file contents.
33
+ def read
34
+ file = File.new(@snapshot_path)
35
+ value = file.read
36
+ file.close
37
+ value
38
+ end
39
+
40
+ # Writes the value to file, overwriting the file contents if either of the
41
+ # following is true:
42
+ # * The snapshot file does not already exist.
43
+ # * The UPDATE_SNAPSHOTS environment variable is set.
44
+ #
45
+ # TODO: Do not write to file if running in CI mode.
46
+ #
47
+ # @param [String] snapshot_name The snapshot name.
48
+ # @param [String] value The value to write to file.
49
+ def write(value)
50
+ return unless should_write?
51
+
52
+ file = File.new(@snapshot_path, 'w+')
53
+ file.write(value)
54
+ RSpec.configuration.reporter.message(
55
+ "Snapshot written: #{@snapshot_path}"
56
+ )
57
+ file.close
58
+ end
59
+
60
+ private def should_write?
61
+ file_does_not_exist? || update_snapshots?
62
+ end
63
+
64
+ private def update_snapshots?
65
+ !!ENV.fetch('UPDATE_SNAPSHOTS', nil)
66
+ end
67
+
68
+ private def file_does_not_exist?
69
+ !File.exist?(@snapshot_path)
70
+ end
71
+ end
72
+ end
73
+ end
@@ -1,52 +1,28 @@
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
9
6
  # RSpec matcher for snapshot testing
10
- class MatchSnapShot
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
 
@@ -55,39 +31,24 @@ module RSpec
55
31
 
56
32
  # === is the method called when matching an argument
57
33
  alias === matches?
34
+ alias match matches?
58
35
 
59
36
  private def serialize(value)
60
- if value.is_a?(String)
61
- value
62
- else
63
- @serializer.dump(value)
64
- end
65
- end
37
+ return value if value.is_a?(String)
66
38
 
67
- private def write_snapshot
68
- return unless should_write?
69
-
70
- RSpec.configuration.reporter.message(
71
- "Snapshot written: #{@snapshot_path}"
72
- )
73
- file = File.new(@snapshot_path, 'w+')
74
- file.write(@actual)
75
- file.close
39
+ @serializer.dump(value)
76
40
  end
77
41
 
78
- private def should_write?
79
- update_snapshots? || !File.exist?(@snapshot_path)
42
+ private def write_snapshot(value)
43
+ @file_operator.write(value)
80
44
  end
81
45
 
82
- private def update_snapshots?
83
- ENV['UPDATE_SNAPSHOTS']
46
+ private def read_snapshot
47
+ @file_operator.read
84
48
  end
85
49
 
86
- private def read_snapshot
87
- file = File.new(@snapshot_path)
88
- value = file.read
89
- file.close
90
- value
50
+ def description
51
+ "to match a snapshot containing: \"#{@expected}\""
91
52
  end
92
53
 
93
54
  def diffable?
@@ -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.0'
5
+ VERSION = '2.0.2'
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.17'
35
- spec.add_development_dependency 'rubocop-rspec', '~> 2.4'
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.0
4
+ version: 2.0.2
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-06-22 00:00:00.000000000 Z
11
+ date: 2023-12-15 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.17'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - "~>"
109
- - !ruby/object:Gem::Version
110
- version: '1.17'
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.4'
118
- type: :development
119
- prerelease: false
120
- version_requirements: !ruby/object:Gem::Requirement
121
- requirements:
122
- - - "~>"
123
- - !ruby/object:Gem::Version
124
- version: '2.4'
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,7 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.5.9
5
- - 2.6.7
6
- - 2.7.3
7
- before_install: gem install bundler:2.2.20