rspec-request_snapshot 0.6.2 → 0.7.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/Gemfile.lock +3 -1
- data/README.md +7 -0
- data/lib/rspec/request_snapshot.rb +2 -0
- data/lib/rspec/request_snapshot/matcher.rb +12 -0
- data/lib/rspec/request_snapshot/updaters/base.rb +6 -0
- data/lib/rspec/request_snapshot/updaters/json.rb +63 -0
- data/lib/rspec/request_snapshot/version.rb +1 -1
- data/rspec-request_snapshot.gemspec +1 -0
- metadata +18 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5ba15dd1a24c663735cff748399371edcd987160
|
4
|
+
data.tar.gz: 9c3c9529912b9fe6161c12174ee72ead76ddf7fd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 1d4d0e31e051360a7b123cdd2f7356209291b90405d7ba3372a704d20febf9911ca46183d91250c6c81abe2e3be54ec209f0d2b8bdb76497d44bd45c23234149
|
7
|
+
data.tar.gz: f0b050f54ba17e8341ae4be7220e6a86f33db26e80a995b70eb344688bfae5726f7f3eed5cf7f9d6af721f8fa7a6fe610edd158bff0b20b04f1e32b9b6f08db5
|
data/CHANGELOG.md
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,12 +1,13 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
rspec-request_snapshot (0.
|
4
|
+
rspec-request_snapshot (0.7.0)
|
5
5
|
rspec (~> 3.0)
|
6
6
|
|
7
7
|
GEM
|
8
8
|
remote: https://rubygems.org/
|
9
9
|
specs:
|
10
|
+
climate_control (0.1.0)
|
10
11
|
diff-lcs (1.3)
|
11
12
|
docile (1.3.1)
|
12
13
|
json (2.1.0)
|
@@ -35,6 +36,7 @@ PLATFORMS
|
|
35
36
|
|
36
37
|
DEPENDENCIES
|
37
38
|
bundler (~> 1.16)
|
39
|
+
climate_control (~> 0.1.0)
|
38
40
|
rake (~> 10.0)
|
39
41
|
rspec-request_snapshot!
|
40
42
|
simplecov (~> 0.16.1)
|
data/README.md
CHANGED
@@ -57,6 +57,13 @@ If you need to replace snapshots, run the specs with:
|
|
57
57
|
|
58
58
|
REPLACE_SNAPSHOTS=true bundle exec rspec
|
59
59
|
|
60
|
+
If you only need to add, remove or replace data without replacing the whole snapshot:
|
61
|
+
|
62
|
+
CONSERVATIVE_UPDATE_SNAPSHOTS=true bundle exec rspec
|
63
|
+
|
64
|
+
**Note:** Conservative update will not work along with ignore_order option. It should only be used when there is
|
65
|
+
no major changes in the snapshots that will be updated.
|
66
|
+
|
60
67
|
### Matcher
|
61
68
|
|
62
69
|
```ruby
|
@@ -7,3 +7,5 @@ require "rspec/request_snapshot/matcher"
|
|
7
7
|
require "rspec/request_snapshot/handlers/base"
|
8
8
|
require "rspec/request_snapshot/handlers/json"
|
9
9
|
require "rspec/request_snapshot/handlers/text"
|
10
|
+
require "rspec/request_snapshot/updaters/base"
|
11
|
+
require "rspec/request_snapshot/updaters/json"
|
@@ -14,6 +14,8 @@ module Rspec::RequestSnapshot
|
|
14
14
|
FileUtils.mkdir_p(File.dirname(snapshot_file_path)) unless Dir.exist?(File.dirname(snapshot_file_path))
|
15
15
|
|
16
16
|
if File.exist?(snapshot_file_path) && !(ENV["REPLACE_SNAPSHOTS"] == "true")
|
17
|
+
updater.update(actual, snapshot_file_path) if ENV["CONSERVATIVE_UPDATE_SNAPSHOTS"] == "true"
|
18
|
+
|
17
19
|
@actual = handler.comparable(actual)
|
18
20
|
@expected = handler.comparable(File.read(snapshot_file_path))
|
19
21
|
|
@@ -46,5 +48,15 @@ module Rspec::RequestSnapshot
|
|
46
48
|
handler_class.new(@options)
|
47
49
|
end
|
48
50
|
end
|
51
|
+
|
52
|
+
def updater
|
53
|
+
@updater ||= begin
|
54
|
+
convervative_updater_class = case format
|
55
|
+
when :json
|
56
|
+
Updaters::JSON
|
57
|
+
end
|
58
|
+
convervative_updater_class.new(@options)
|
59
|
+
end
|
60
|
+
end
|
49
61
|
end
|
50
62
|
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
class Rspec::RequestSnapshot::Updaters::JSON < Rspec::RequestSnapshot::Updaters::Base
|
4
|
+
def update(expected_json, snapshot_file_path)
|
5
|
+
expected_json = JSON.parse(expected_json)
|
6
|
+
stored_json = JSON.parse(File.read(snapshot_file_path))
|
7
|
+
|
8
|
+
deep_update(expected_json, stored_json)
|
9
|
+
|
10
|
+
File.write(snapshot_file_path, stored_json.to_json)
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def deep_update(expected_json, stored_json)
|
16
|
+
case expected_json
|
17
|
+
when Array
|
18
|
+
deep_update_array(expected_json, stored_json)
|
19
|
+
when Hash
|
20
|
+
deep_update_hash(expected_json, stored_json)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def deep_update_hash(expected_json, stored_json)
|
25
|
+
keys = expected_json.keys | stored_json.keys
|
26
|
+
keys.each do |key|
|
27
|
+
# If key present on expected and not stored, add it to stored
|
28
|
+
if stored_json[key].nil?
|
29
|
+
stored_json[key] = expected_json[key]
|
30
|
+
next
|
31
|
+
end
|
32
|
+
|
33
|
+
# If key only present on stored, remove it from stored
|
34
|
+
if expected_json[key].nil?
|
35
|
+
stored_json.delete(key)
|
36
|
+
next
|
37
|
+
end
|
38
|
+
|
39
|
+
# If key present on both, and not a dynamic attribute, update the stored one
|
40
|
+
unless dynamic_attributes.include?(key)
|
41
|
+
if expected_json[key].is_a?(Hash) || expected_json[key].is_a?(Array)
|
42
|
+
deep_update(expected_json[key], stored_json[key])
|
43
|
+
else
|
44
|
+
stored_json[key] = expected_json[key]
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
def deep_update_array(expected_json, stored_json)
|
51
|
+
max_size = [expected_json.size, stored_json.size].max
|
52
|
+
|
53
|
+
0.upto(max_size).each do |index|
|
54
|
+
# If element is only present on expected, add it to stored
|
55
|
+
stored_json[index] = expected_json[index] if !stored_json[index] && expected_json[index]
|
56
|
+
|
57
|
+
# If element is only present on stored, remove it from stored
|
58
|
+
stored_json.delete_at(index) if !expected_json[index] && stored_json[index]
|
59
|
+
|
60
|
+
deep_update(expected_json[index], stored_json[index])
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -27,4 +27,5 @@ Gem::Specification.new do |spec|
|
|
27
27
|
spec.add_development_dependency "bundler", "~> 1.16"
|
28
28
|
spec.add_development_dependency "rake", "~> 10.0"
|
29
29
|
spec.add_development_dependency "simplecov", "~> 0.16.1"
|
30
|
+
spec.add_development_dependency "climate_control", "~> 0.1.0"
|
30
31
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: rspec-request_snapshot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Campos
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-06-
|
11
|
+
date: 2019-06-24 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rspec
|
@@ -66,6 +66,20 @@ dependencies:
|
|
66
66
|
- - ~>
|
67
67
|
- !ruby/object:Gem::Version
|
68
68
|
version: 0.16.1
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: climate_control
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ~>
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 0.1.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.1.0
|
69
83
|
description: Make sure API behavior is not changing by taking and storing a snapshot
|
70
84
|
from an API response on a first run and check if they match on next spec runs.
|
71
85
|
email:
|
@@ -94,6 +108,8 @@ files:
|
|
94
108
|
- lib/rspec/request_snapshot/handlers/json.rb
|
95
109
|
- lib/rspec/request_snapshot/handlers/text.rb
|
96
110
|
- lib/rspec/request_snapshot/matcher.rb
|
111
|
+
- lib/rspec/request_snapshot/updaters/base.rb
|
112
|
+
- lib/rspec/request_snapshot/updaters/json.rb
|
97
113
|
- lib/rspec/request_snapshot/version.rb
|
98
114
|
- rspec-request_snapshot.gemspec
|
99
115
|
homepage: https://github.com/CareMessagePlatform/rspec-request_snapshot
|