rspec-request_snapshot 0.6.2 → 0.7.0

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
  SHA1:
3
- metadata.gz: 064a2bd24422bf55dec9a6e2272481a14fbc8819
4
- data.tar.gz: ad9e88a9e83e8216135d7b57d6eb7eaefadd00fb
3
+ metadata.gz: 5ba15dd1a24c663735cff748399371edcd987160
4
+ data.tar.gz: 9c3c9529912b9fe6161c12174ee72ead76ddf7fd
5
5
  SHA512:
6
- metadata.gz: 6773e770587d2ad04ea6d3e049d0bca8bcc9663473801aebaeb6da9701df471b8e0f3b2fc0ed83147e449c57d8e3d3a5cf6219f027ba6ac186cf9db6a181c991
7
- data.tar.gz: 5676af26f46bf00e36b6f4edfe95b2a5490dc9a1e20cce010ee5ea7f06f2a25be9dedeff5aabf14507274a25aafc2c49327840d270be53abe2380db5007a2de4
6
+ metadata.gz: 1d4d0e31e051360a7b123cdd2f7356209291b90405d7ba3372a704d20febf9911ca46183d91250c6c81abe2e3be54ec209f0d2b8bdb76497d44bd45c23234149
7
+ data.tar.gz: f0b050f54ba17e8341ae4be7220e6a86f33db26e80a995b70eb344688bfae5726f7f3eed5cf7f9d6af721f8fa7a6fe610edd158bff0b20b04f1e32b9b6f08db5
@@ -1,5 +1,10 @@
1
1
  # Changelog
2
2
 
3
+ ## [v0.7.0]
4
+
5
+ ### Added
6
+ - Added CONSERVATIVE_UPDATE_SNAPSHOTS flag
7
+
3
8
  ## [v0.6.2]
4
9
 
5
10
  ### Fixed
@@ -1,12 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- rspec-request_snapshot (0.6.2)
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,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Rspec::RequestSnapshot::Updaters
4
+ class Base < Rspec::RequestSnapshot::Handlers::Base
5
+ end
6
+ 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
@@ -3,7 +3,7 @@
3
3
  # rubocop:disable Style/ClassAndModuleChildren
4
4
  module Rspec
5
5
  module RequestSnapshot
6
- VERSION = "0.6.2"
6
+ VERSION = "0.7.0"
7
7
  end
8
8
  end
9
9
  # rubocop:enable Style/ClassAndModuleChildren
@@ -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.6.2
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-20 00:00:00.000000000 Z
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