rspec-snapshot 2.0.1 → 2.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: '099c9c3b45ca0936bb35a90dc10263dc0634cfbf307e32eab60eb6154cd0d94c'
4
- data.tar.gz: 862c34e9f5693afe80ca9384048d03d67e78a6e09300b1906a1e8c929315b263
3
+ metadata.gz: b55b1aac3c8f7485efb46cf9ae2d62099d074f8c57c41d8a0b649ee6d7040c7f
4
+ data.tar.gz: 0e6d00759c134fe74c0b627fcbb40757825e959adffdd9ffa3599e68584bc954
5
5
  SHA512:
6
- metadata.gz: a25332c1f83a33d982f16fdf31284d68800db12c1f49a08320e3eca1515a614bd7ba1cb0a5028953d8d32c0cade632c8970a86d1c9fdf941c9b41c9d71476c55
7
- data.tar.gz: 3c952f9f7da76ac1c464a9e35c69e8b280e09aa6066c9e763eba97e4ad7b4ded481516b68bdbecf35d235010a51e832a15955538e56d02d355c84299c4e42409
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
 
@@ -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,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.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.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.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-10-15 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.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