rspec-golden-files 0.1.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 +7 -0
- data/.gitignore +51 -0
- data/.rspec +3 -0
- data/Gemfile +6 -0
- data/LICENSE +21 -0
- data/README.md +39 -0
- data/lib/golden_files.rb +124 -0
- data/rspec-golden-files.gemspec +42 -0
- metadata +97 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: b1c393bc388b86d3741692619898b67ddffd186fa167a4d5caf453efe8830dd5
|
|
4
|
+
data.tar.gz: 23132b6702d2da10b1604eb1713e822543f0a3e3d4553b6396c1fe86be40cc4e
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: fb60190f4c79728c99d75b57d42f304015fbb3b87dc356ab1096cbec0b0bf74b45624df5af920a7890dc90548e491c5d6e42a577bee083f74b9de16f86c598a0
|
|
7
|
+
data.tar.gz: 7c679d6bf09b62be815736e9083eb35726f42c8e0febdd97543534fba32c91d555f1f71115ba4035ea38b1809ff8f8087f408f5004defee61d0b56bd79b447b6
|
data/.gitignore
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
.idea
|
|
2
|
+
*.gem
|
|
3
|
+
*.rbc
|
|
4
|
+
/.config
|
|
5
|
+
/coverage/
|
|
6
|
+
/InstalledFiles
|
|
7
|
+
/pkg/
|
|
8
|
+
/spec/reports/
|
|
9
|
+
/spec/examples.txt
|
|
10
|
+
/test/tmp/
|
|
11
|
+
/test/version_tmp/
|
|
12
|
+
/tmp/
|
|
13
|
+
|
|
14
|
+
# Used by dotenv library to load environment variables.
|
|
15
|
+
# .env
|
|
16
|
+
|
|
17
|
+
## Specific to RubyMotion:
|
|
18
|
+
.dat*
|
|
19
|
+
.repl_history
|
|
20
|
+
build/
|
|
21
|
+
*.bridgesupport
|
|
22
|
+
build-iPhoneOS/
|
|
23
|
+
build-iPhoneSimulator/
|
|
24
|
+
|
|
25
|
+
## Specific to RubyMotion (use of CocoaPods):
|
|
26
|
+
#
|
|
27
|
+
# We recommend against adding the Pods directory to your .gitignore. However
|
|
28
|
+
# you should judge for yourself, the pros and cons are mentioned at:
|
|
29
|
+
# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
|
|
30
|
+
#
|
|
31
|
+
# vendor/Pods/
|
|
32
|
+
|
|
33
|
+
## Documentation cache and generated files:
|
|
34
|
+
/.yardoc/
|
|
35
|
+
/_yardoc/
|
|
36
|
+
/doc/
|
|
37
|
+
/rdoc/
|
|
38
|
+
|
|
39
|
+
## Environment normalization:
|
|
40
|
+
/.bundle/
|
|
41
|
+
/vendor/bundle
|
|
42
|
+
/lib/bundler/man/
|
|
43
|
+
|
|
44
|
+
# for a library or gem, you might want to ignore these files since the code is
|
|
45
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
46
|
+
# Gemfile.lock
|
|
47
|
+
# .ruby-version
|
|
48
|
+
# .ruby-gemset
|
|
49
|
+
|
|
50
|
+
# unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
|
|
51
|
+
.rvmrc
|
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2018 Anton Antonov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# RSpec::Matchers::GoldenFiles
|
|
2
|
+
|
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/rspec/golden/files`. To experiment with that code, run `bin/console` for an interactive prompt.
|
|
4
|
+
|
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
|
6
|
+
|
|
7
|
+
## Installation
|
|
8
|
+
|
|
9
|
+
Add this line to your application's Gemfile:
|
|
10
|
+
|
|
11
|
+
```ruby
|
|
12
|
+
gem 'rspec-golden-files'
|
|
13
|
+
```
|
|
14
|
+
|
|
15
|
+
And then execute:
|
|
16
|
+
|
|
17
|
+
$ bundle
|
|
18
|
+
|
|
19
|
+
Or install it yourself as:
|
|
20
|
+
|
|
21
|
+
$ gem install rspec-golden-files
|
|
22
|
+
|
|
23
|
+
## Usage
|
|
24
|
+
|
|
25
|
+
TODO: Write usage instructions here
|
|
26
|
+
|
|
27
|
+
## Development
|
|
28
|
+
|
|
29
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
30
|
+
|
|
31
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
32
|
+
|
|
33
|
+
## Contributing
|
|
34
|
+
|
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rspec-golden-files.
|
|
36
|
+
|
|
37
|
+
## License
|
|
38
|
+
|
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/golden_files.rb
ADDED
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
require 'tempfile'
|
|
4
|
+
require 'erb'
|
|
5
|
+
require 'ostruct'
|
|
6
|
+
|
|
7
|
+
module RSpec
|
|
8
|
+
module Matchers
|
|
9
|
+
module GoldenFiles
|
|
10
|
+
VERSION = '0.1.0'.freeze
|
|
11
|
+
|
|
12
|
+
class SimpleFileReader
|
|
13
|
+
def initialize(filename)
|
|
14
|
+
@filename = filename
|
|
15
|
+
@file_contents = File.read(@filename)
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
def filename
|
|
19
|
+
@filename
|
|
20
|
+
end
|
|
21
|
+
|
|
22
|
+
def expected_value
|
|
23
|
+
@file_contents
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
def close; end
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
class ErbFileReader < SimpleFileReader
|
|
30
|
+
# HACK: Make hash keys usable in templates
|
|
31
|
+
# due to ERB's poor `#result_with_hash` usefulness.
|
|
32
|
+
class TemplateStruct < OpenStruct
|
|
33
|
+
def render(template)
|
|
34
|
+
template.result(binding)
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def initialize(filename, template_vars)
|
|
39
|
+
plain_file_contents = File.read(filename)
|
|
40
|
+
template = ERB.new(plain_file_contents)
|
|
41
|
+
template_struct = TemplateStruct.new(template_vars)
|
|
42
|
+
|
|
43
|
+
@file_contents = template_struct.render(template)
|
|
44
|
+
|
|
45
|
+
@tmpfile = Tempfile.new('expected')
|
|
46
|
+
@tmpfile.write(@file_contents)
|
|
47
|
+
@tmpfile.close(false)
|
|
48
|
+
|
|
49
|
+
@filename = @tmpfile.path
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
def close
|
|
53
|
+
@tmpfile.delete
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
class GoldenFilesMatcher
|
|
58
|
+
def initialize(filename, file_reader_type, template_vars)
|
|
59
|
+
@filename = filename
|
|
60
|
+
@file_reader_type = file_reader_type
|
|
61
|
+
@template_vars = template_vars
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def matches?(value)
|
|
65
|
+
unless File.exist?(@filename)
|
|
66
|
+
raise ArgumentError "golden file '#{@filename}' not found"
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
@value = value
|
|
70
|
+
|
|
71
|
+
actual_tmpfile = Tempfile.new('actual')
|
|
72
|
+
actual_tmpfile.write(value.to_s)
|
|
73
|
+
actual_tmpfile.close(false)
|
|
74
|
+
|
|
75
|
+
file_reader = case @file_reader_type.to_sym
|
|
76
|
+
when :simple
|
|
77
|
+
SimpleFileReader.new(@filename)
|
|
78
|
+
when :erb
|
|
79
|
+
ErbFileReader.new(@filename, @template_vars)
|
|
80
|
+
else
|
|
81
|
+
raise ArgumentError "unknown file reader type #{@file_reader_type}. `simple` and `erb` are supported"
|
|
82
|
+
end
|
|
83
|
+
|
|
84
|
+
@expected_value = file_reader.expected_value
|
|
85
|
+
system("diff #{actual_tmpfile.path} #{file_reader.filename} > /dev/null")
|
|
86
|
+
is_successful = $CHILD_STATUS.to_i.zero?
|
|
87
|
+
|
|
88
|
+
file_reader.close
|
|
89
|
+
actual_tmpfile.delete
|
|
90
|
+
|
|
91
|
+
is_successful
|
|
92
|
+
end
|
|
93
|
+
|
|
94
|
+
def actual
|
|
95
|
+
@value
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
def expected
|
|
99
|
+
@expected_value
|
|
100
|
+
end
|
|
101
|
+
|
|
102
|
+
def description
|
|
103
|
+
"be matching golden file with name #{@filename}"
|
|
104
|
+
end
|
|
105
|
+
|
|
106
|
+
def diffable?
|
|
107
|
+
true
|
|
108
|
+
end
|
|
109
|
+
|
|
110
|
+
def failure_message
|
|
111
|
+
"expected #{@value.inspect} does not match golden file '#{@filename}'"
|
|
112
|
+
end
|
|
113
|
+
|
|
114
|
+
def failure_message_when_negated
|
|
115
|
+
"expected #{@value.inspect} matches golden file '#{@filename}'"
|
|
116
|
+
end
|
|
117
|
+
end
|
|
118
|
+
end
|
|
119
|
+
|
|
120
|
+
def match_golden_file(filename, file_reader_type: 'simple', template_vars: {})
|
|
121
|
+
RSpec::Matchers::GoldenFiles::GoldenFilesMatcher.new(filename, file_reader_type, template_vars)
|
|
122
|
+
end
|
|
123
|
+
end
|
|
124
|
+
end
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
|
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require_relative 'lib/golden_files.rb'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "rspec-golden-files"
|
|
8
|
+
spec.version = RSpec::Matchers::GoldenFiles::VERSION
|
|
9
|
+
spec.authors = ["Anton Antonov"]
|
|
10
|
+
spec.email = ["anton.synd.antonov@gmail.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = 'rspec matcher for golden files'
|
|
13
|
+
spec.description = 'rspec matcher for golden files'
|
|
14
|
+
spec.homepage = 'https://github.com/syndbg/rspec-golden-files'
|
|
15
|
+
spec.license = 'MIT'
|
|
16
|
+
|
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
|
19
|
+
if spec.respond_to?(:metadata)
|
|
20
|
+
spec.metadata["allowed_push_host"] = 'https://rubygems.org'
|
|
21
|
+
|
|
22
|
+
spec.metadata["homepage_uri"] = spec.homepage
|
|
23
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
|
24
|
+
spec.metadata["changelog_uri"] = spec.homepage
|
|
25
|
+
else
|
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
|
27
|
+
"public gem pushes."
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
|
34
|
+
end
|
|
35
|
+
spec.bindir = "exe"
|
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
|
37
|
+
spec.require_paths = ["lib"]
|
|
38
|
+
|
|
39
|
+
spec.add_development_dependency "bundler", "~> 1.17"
|
|
40
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
41
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
42
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: rspec-golden-files
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Anton Antonov
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: exe
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2018-11-18 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.17'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.17'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
description: rspec matcher for golden files
|
|
56
|
+
email:
|
|
57
|
+
- anton.synd.antonov@gmail.com
|
|
58
|
+
executables: []
|
|
59
|
+
extensions: []
|
|
60
|
+
extra_rdoc_files: []
|
|
61
|
+
files:
|
|
62
|
+
- ".gitignore"
|
|
63
|
+
- ".rspec"
|
|
64
|
+
- Gemfile
|
|
65
|
+
- LICENSE
|
|
66
|
+
- README.md
|
|
67
|
+
- lib/golden_files.rb
|
|
68
|
+
- rspec-golden-files.gemspec
|
|
69
|
+
homepage: https://github.com/syndbg/rspec-golden-files
|
|
70
|
+
licenses:
|
|
71
|
+
- MIT
|
|
72
|
+
metadata:
|
|
73
|
+
allowed_push_host: https://rubygems.org
|
|
74
|
+
homepage_uri: https://github.com/syndbg/rspec-golden-files
|
|
75
|
+
source_code_uri: https://github.com/syndbg/rspec-golden-files
|
|
76
|
+
changelog_uri: https://github.com/syndbg/rspec-golden-files
|
|
77
|
+
post_install_message:
|
|
78
|
+
rdoc_options: []
|
|
79
|
+
require_paths:
|
|
80
|
+
- lib
|
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
82
|
+
requirements:
|
|
83
|
+
- - ">="
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '0'
|
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
- - ">="
|
|
89
|
+
- !ruby/object:Gem::Version
|
|
90
|
+
version: '0'
|
|
91
|
+
requirements: []
|
|
92
|
+
rubyforge_project:
|
|
93
|
+
rubygems_version: 2.7.6
|
|
94
|
+
signing_key:
|
|
95
|
+
specification_version: 4
|
|
96
|
+
summary: rspec matcher for golden files
|
|
97
|
+
test_files: []
|