minitest-snapshots 0.2.0 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 411f63a5cccd1e1db3268528b288bf135842b81a00545445d56aceb0e8bafe23
4
- data.tar.gz: 4b12cb599785b458f68f545bd3cc4725f2870d0d35ae4397a921a436a7b16c6f
3
+ metadata.gz: 8bed43934d93c4818c27cfd55600fab8767b2d4198da07575b3f8404d9edd234
4
+ data.tar.gz: c025cd5b1feb65a29f5fe5829c8eef36de54aa6b116b96d99c919757c9c032f8
5
5
  SHA512:
6
- metadata.gz: 1080bbc13fb55cbe405def0c01f6a8236fef0f4b73dba4d29615df7a7f11a1bdd7eee212a3b05efa9c5a08b8df36e564e8acb48395893d1a7d232724124b3611
7
- data.tar.gz: 3fff24593022ebe0d75419c4ba03dbb760b4a221852bde666c4d6cb982373bc1e18d48c503766c0b68c8e027cb4a33b7b0342d5306e3e66084771ff555c8a353
6
+ metadata.gz: 7b5837b513ce6f00a104884224f0da6f90b0aa7ff07a3aed3ea3488e60728d701295ea11209ea5817896428a9dd95cb0caae2aa92ab01ae0d251a232cde25380
7
+ data.tar.gz: 8580ed083254bf074a32a5e035b552428896eb5cc4cc3127e79bf63f8388e8fbb466933819476b7d45cec78bf598184ae5eae1a6fcb15201f314d312f522379b
data/README.md CHANGED
@@ -1,6 +1,13 @@
1
1
  # Minitest::Snapshots
2
2
 
3
- Simple minitest plugin gem implementing Jest style snapshot testing. It's like VCR, but for any string.
3
+ [![Gem Version](https://badge.fury.io/rb/minitest-snapshots.svg)](https://rubygems.org/gems/minitest-snapshots)
4
+ [![CI](https://github.com/mattbrictson/gem/actions/workflows/ci.yml/badge.svg)](https://github.com/mattbrictson/gem/actions/workflows/ci.yml)
5
+
6
+ Simple minitest plugin gem implementing Jest-style snapshot testing. It's like VCR, but for any value.
7
+
8
+ ## Requirements
9
+
10
+ - Ruby 2.7 or later
4
11
 
5
12
  ## Usage
6
13
 
@@ -10,16 +17,21 @@ Example:
10
17
 
11
18
  ```ruby
12
19
  class QueryCompilerText extends Minitest::Test
13
- def test_it_can_compile_a_query
14
- assert_matches_snapshot QueryCompiler.new.compile
15
- end
20
+ def test_it_can_compile_a_query
21
+ assert_matches_snapshot QueryCompiler.new.compile
22
+ end
16
23
  end
17
24
  ```
18
25
 
19
26
  ## Command line options
20
27
 
21
- * `-u` or `--update-snapshots`: Update snapshots on disk to the new actual value when re-running the test. Useful when you know the new output of a test case is correct and the snapshot is out of date.
22
- * `-l` or `--lock-snapshots`: Prevents new snapshots from being written. Useful on CI to ensure all snapshots have been written by the developer.
28
+ - `-u` or `--update-snapshots`: Update snapshots on disk to the new actual value when re-running the test. Useful when you know the new output of a test case is correct and the snapshot is out of date.
29
+ - `-l` or `--lock-snapshots`: Prevents new snapshots from being written. Useful on CI to ensure all snapshots have been written by the developer.
30
+
31
+ For example, to update snapshots on a Rails project:
32
+
33
+ $ bin/rails test -u
34
+
23
35
  ## Installation
24
36
 
25
37
  Add this line to your application's Gemfile:
@@ -36,7 +48,6 @@ Or install it yourself as:
36
48
 
37
49
  $ gem install minitest-snapshots
38
50
 
39
-
40
51
  ## Development
41
52
 
42
53
  After checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
@@ -45,12 +56,12 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
45
56
 
46
57
  ## Contributing
47
58
 
48
- Bug reports and pull requests are welcome on GitHub at https://github.com/superpro-inc/minitest-snapshots. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mattbrictson/minitest-snapshots.
49
60
 
50
61
  ## License
51
62
 
52
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
63
+ The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
53
64
 
54
65
  ## Code of Conduct
55
66
 
56
- Everyone interacting in the Minitest::Snapshots project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/superpro-inc/minitest-snapshots/blob/master/CODE_OF_CONDUCT.md).
67
+ Everyone interacting in the this project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](CODE_OF_CONDUCT.md).
@@ -1,17 +1,28 @@
1
+ require_relative "serializer"
2
+ require "fileutils"
3
+
1
4
  module Minitest
2
5
  module Assertions
3
- def assert_matches_snapshot(string, snapshot_name = nil)
4
- snapshot_file = snapshot_path(snapshot_name || (@snapshot_assertion_counter += 1))
6
+ def assert_matches_snapshot(value, snapshot_name = nil)
7
+ snapshot_file = snapshot_path(self.class.name, snapshot_name || (@snapshot_assertion_counter += 1))
8
+ snapshot = Minitest::Snapshots::Serializer.serialize(value)
5
9
 
6
10
  if !Minitest::Snapshots.force_updates && File.exist?(snapshot_file)
7
- assert_equal File.read(snapshot_file), string, "The string does not match the snapshot (located at #{snapshot_file})"
11
+ assert_equal(
12
+ File.read(snapshot_file),
13
+ snapshot,
14
+ "The value does not match the snapshot (located at #{snapshot_file})"
15
+ )
8
16
  else
9
17
  if Minitest::Snapshots.lock_snapshots
10
- assert false, "New snapshot tried to be created when snapshot writing is prevented with the --lock-snapshots option."
11
- end
12
- File.open(snapshot_file, "w") do |file|
13
- file.write(string)
18
+ assert(
19
+ false,
20
+ "Attempt to create a snapshot failed because writing is prevented by the --lock-snapshots option"
21
+ )
14
22
  end
23
+
24
+ FileUtils.mkdir_p(File.dirname(snapshot_file))
25
+ File.write(snapshot_file, snapshot)
15
26
  end
16
27
  end
17
28
  end
@@ -0,0 +1,66 @@
1
+ require "set"
2
+ require "yaml"
3
+
4
+ module Minitest
5
+ module Snapshots
6
+ # The serializer translates values (objects) into strings. By default,
7
+ # it uses YAML (Psych). Can be overridden to implement custom
8
+ # serialization formats, e.g.:
9
+ #
10
+ # module Minitest::Snapshots::Serializer
11
+ # def self.serialize(value)
12
+ # Marshal.dump(value)
13
+ # end
14
+ # end
15
+ module Serializer
16
+ # The name of the method used to customize the output of +to_yaml+.
17
+ # Used to provide canonical representations for Hash and Set instances.
18
+ HOOK = :encode_with
19
+
20
+ # Used to ensure the addition/removal of hooks is atomic
21
+ @lock = Mutex.new
22
+
23
+ # Serialize the supplied value to YAML with hooks to canonicalize
24
+ # (i.e. sort) Hash keys and Set elements.
25
+ #
26
+ # h1 = { foo: "bar", baz: "quux" }
27
+ # h2 = { baz: "quux", foo: "bar" }
28
+ #
29
+ # h1.to_yaml == h2.to_yaml # => false
30
+ # Serializer.serialize(h1) == Serializer.serialize(h2) # => true
31
+ #
32
+ # The hooks are only installed for the duration of the method call
33
+ # and are not installed if custom hooks are already defined.
34
+ def self.serialize(value)
35
+ @lock.synchronize do
36
+ if (hook_hash = hook?(Hash))
37
+ Hash.define_method(HOOK) do |coder|
38
+ sorted = sort_by { |pair| pair.first.to_yaml }.to_h
39
+ coder.map = dup.clear.merge!(sorted)
40
+ end
41
+ end
42
+
43
+ if (hook_set = hook?(Set))
44
+ Set.define_method(HOOK) do |coder|
45
+ sorted = sort_by(&:to_yaml)
46
+ coder.seq = dup.clear.merge(sorted)
47
+ end
48
+ end
49
+
50
+ value.to_yaml
51
+ ensure
52
+ Hash.remove_method(HOOK) if hook_hash
53
+ Set.remove_method(HOOK) if hook_set
54
+ end
55
+ end
56
+
57
+ # Returns true if a class is hookable (doesn't already have a YAML
58
+ # serialization hook defined), false otherwise
59
+ def self.hook?(mod)
60
+ !(mod.method_defined?(HOOK) || mod.private_method_defined?(HOOK))
61
+ end
62
+
63
+ private_class_method :hook?
64
+ end
65
+ end
66
+ end
@@ -1,16 +1,30 @@
1
+ require "fileutils"
2
+
1
3
  module Minitest
2
4
  module Snapshots
3
5
  module TestExtensions
4
6
  def before_setup
5
7
  super
6
8
  @snapshot_assertion_counter = 0
7
- @snapshot_dir ||= Rails.root.join("test", "snapshots").to_s
9
+ @snapshot_dir ||= Minitest::Snapshots.default_snapshots_directory
8
10
  end
9
11
 
10
12
  private
11
13
 
12
- def snapshot_path(snapshot_name)
13
- File.join(@snapshot_dir, "#{name}__#{snapshot_name}.snap.txt")
14
+ def sanitize(name)
15
+ sanitized = name.to_s.downcase.gsub(/(?:\A[\W_]+)|(?:[\W_]+\z)/, "").gsub(/[\W_]+/, "_")
16
+
17
+ if sanitized.empty?
18
+ raise NameError, "Can't sanitize name: #{name.inspect}"
19
+ end
20
+
21
+ sanitized
22
+ end
23
+
24
+ def snapshot_path(suite_name, snapshot_name)
25
+ filename = format("%s__%s.snap.yaml", sanitize(name), sanitize(snapshot_name))
26
+ subdir = sanitize(suite_name)
27
+ File.join(@snapshot_dir, subdir, filename)
14
28
  end
15
29
  end
16
30
  end
@@ -1,5 +1,5 @@
1
1
  module Minitest
2
2
  module Snapshots
3
- VERSION = "0.2.0"
3
+ VERSION = "1.0.0".freeze
4
4
  end
5
5
  end
@@ -2,7 +2,18 @@ require "minitest"
2
2
 
3
3
  module Minitest::Snapshots
4
4
  class << self
5
- attr_accessor :force_updates
6
- attr_accessor :lock_snapshots
5
+ attr_accessor :force_updates, :lock_snapshots
6
+
7
+ def default_snapshots_directory
8
+ if defined?(Rails) && Rails.respond_to?(:root)
9
+ Rails.root.join("test", "snapshots").to_s
10
+ elsif Dir.exist?("test")
11
+ File.expand_path("test/snapshots")
12
+ elsif Dir.exist?("spec")
13
+ File.expand_path("spec/snapshots")
14
+ else
15
+ File.expand_path("snapshots")
16
+ end
17
+ end
7
18
  end
8
19
  end
@@ -1,7 +1,8 @@
1
- require "minitest/snapshots/version"
1
+ require_relative "snapshots"
2
+ require_relative "snapshots/version"
2
3
 
3
4
  module Minitest
4
- def self.plugin_snapshots_options(opts, options)
5
+ def self.plugin_snapshots_options(opts, _options)
5
6
  opts.on "-u", "--update-snapshots", "Update (overwrite) stored snapshots" do
6
7
  Minitest::Snapshots.force_updates = true
7
8
  end
@@ -11,10 +12,9 @@ module Minitest
11
12
  end
12
13
  end
13
14
 
14
- def self.plugin_snapshots_init(options)
15
- require "minitest/snapshots"
16
- require "minitest/snapshots/test_extensions"
17
- require "minitest/snapshots/assertion_extensions"
18
- Minitest::Test.send :include, Minitest::Snapshots::TestExtensions
15
+ def self.plugin_snapshots_init(_options)
16
+ require_relative "snapshots/test_extensions"
17
+ require_relative "snapshots/assertion_extensions"
18
+ Minitest::Test.include Minitest::Snapshots::TestExtensions
19
19
  end
20
20
  end
metadata CHANGED
@@ -1,70 +1,42 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: minitest-snapshots
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Harry Brundage
8
- autorequire:
8
+ - Matt Brictson
9
+ autorequire:
9
10
  bindir: exe
10
11
  cert_chain: []
11
- date: 2019-09-11 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
- description:
12
+ date: 2023-06-30 00:00:00.000000000 Z
13
+ dependencies: []
14
+ description:
42
15
  email:
43
16
  - harry.brundage@gmail.com
17
+ - opensource@mattbrictson.com
44
18
  executables: []
45
19
  extensions: []
46
20
  extra_rdoc_files: []
47
21
  files:
48
- - ".gitignore"
49
- - CODE_OF_CONDUCT.md
50
- - Gemfile
51
- - Gemfile.lock
52
22
  - LICENSE.txt
53
23
  - README.md
54
- - Rakefile
55
- - bin/console
56
- - bin/setup
57
24
  - lib/minitest/snapshots.rb
58
25
  - lib/minitest/snapshots/assertion_extensions.rb
26
+ - lib/minitest/snapshots/serializer.rb
59
27
  - lib/minitest/snapshots/test_extensions.rb
60
28
  - lib/minitest/snapshots/version.rb
61
29
  - lib/minitest/snapshots_plugin.rb
62
- - minitest-snapshots.gemspec
63
- homepage: https://github.com/superpro-inc/minitest-snapshots
30
+ homepage: https://github.com/mattbrictson/minitest-snapshots
64
31
  licenses:
65
32
  - MIT
66
- metadata: {}
67
- post_install_message:
33
+ metadata:
34
+ bug_tracker_uri: https://github.com/mattbrictson/minitest-snapshots/issues
35
+ changelog_uri: https://github.com/mattbrictson/minitest-snapshots/releases
36
+ source_code_uri: https://github.com/mattbrictson/minitest-snapshots
37
+ homepage_uri: https://github.com/mattbrictson/minitest-snapshots
38
+ rubygems_mfa_required: 'true'
39
+ post_install_message:
68
40
  rdoc_options: []
69
41
  require_paths:
70
42
  - lib
@@ -72,16 +44,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
72
44
  requirements:
73
45
  - - ">="
74
46
  - !ruby/object:Gem::Version
75
- version: '0'
47
+ version: '2.7'
76
48
  required_rubygems_version: !ruby/object:Gem::Requirement
77
49
  requirements:
78
50
  - - ">="
79
51
  - !ruby/object:Gem::Version
80
52
  version: '0'
81
53
  requirements: []
82
- rubygems_version: 3.0.3
83
- signing_key:
54
+ rubygems_version: 3.4.13
55
+ signing_key:
84
56
  specification_version: 4
85
- summary: Adds jest style snapshot testing asserting against long values stored on
86
- the filesystem.
57
+ summary: Minitest plugin implementing Jest-style snapshot testing
87
58
  test_files: []
data/.gitignore DELETED
@@ -1,8 +0,0 @@
1
- /.bundle/
2
- /.yardoc
3
- /_yardoc/
4
- /coverage/
5
- /doc/
6
- /pkg/
7
- /spec/reports/
8
- /tmp/
data/CODE_OF_CONDUCT.md DELETED
@@ -1,74 +0,0 @@
1
- # Contributor Covenant Code of Conduct
2
-
3
- ## Our Pledge
4
-
5
- In the interest of fostering an open and welcoming environment, we as
6
- contributors and maintainers pledge to making participation in our project and
7
- our community a harassment-free experience for everyone, regardless of age, body
8
- size, disability, ethnicity, gender identity and expression, level of experience,
9
- nationality, personal appearance, race, religion, or sexual identity and
10
- orientation.
11
-
12
- ## Our Standards
13
-
14
- Examples of behavior that contributes to creating a positive environment
15
- include:
16
-
17
- * Using welcoming and inclusive language
18
- * Being respectful of differing viewpoints and experiences
19
- * Gracefully accepting constructive criticism
20
- * Focusing on what is best for the community
21
- * Showing empathy towards other community members
22
-
23
- Examples of unacceptable behavior by participants include:
24
-
25
- * The use of sexualized language or imagery and unwelcome sexual attention or
26
- advances
27
- * Trolling, insulting/derogatory comments, and personal or political attacks
28
- * Public or private harassment
29
- * Publishing others' private information, such as a physical or electronic
30
- address, without explicit permission
31
- * Other conduct which could reasonably be considered inappropriate in a
32
- professional setting
33
-
34
- ## Our Responsibilities
35
-
36
- Project maintainers are responsible for clarifying the standards of acceptable
37
- behavior and are expected to take appropriate and fair corrective action in
38
- response to any instances of unacceptable behavior.
39
-
40
- Project maintainers have the right and responsibility to remove, edit, or
41
- reject comments, commits, code, wiki edits, issues, and other contributions
42
- that are not aligned to this Code of Conduct, or to ban temporarily or
43
- permanently any contributor for other behaviors that they deem inappropriate,
44
- threatening, offensive, or harmful.
45
-
46
- ## Scope
47
-
48
- This Code of Conduct applies both within project spaces and in public spaces
49
- when an individual is representing the project or its community. Examples of
50
- representing a project or community include using an official project e-mail
51
- address, posting via an official social media account, or acting as an appointed
52
- representative at an online or offline event. Representation of a project may be
53
- further defined and clarified by project maintainers.
54
-
55
- ## Enforcement
56
-
57
- Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
- reported by contacting the project team at harry.brundage@gmail.com. All
59
- complaints will be reviewed and investigated and will result in a response that
60
- is deemed necessary and appropriate to the circumstances. The project team is
61
- obligated to maintain confidentiality with regard to the reporter of an incident.
62
- Further details of specific enforcement policies may be posted separately.
63
-
64
- Project maintainers who do not follow or enforce the Code of Conduct in good
65
- faith may face temporary or permanent repercussions as determined by other
66
- members of the project's leadership.
67
-
68
- ## Attribution
69
-
70
- This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
- available at [http://contributor-covenant.org/version/1/4][version]
72
-
73
- [homepage]: http://contributor-covenant.org
74
- [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile DELETED
@@ -1,6 +0,0 @@
1
- source "https://rubygems.org"
2
-
3
- git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
-
5
- # Specify your gem's dependencies in minitest-snapshots.gemspec
6
- gemspec
data/Gemfile.lock DELETED
@@ -1,20 +0,0 @@
1
- PATH
2
- remote: .
3
- specs:
4
- minitest-snapshots (0.2.0)
5
-
6
- GEM
7
- remote: https://rubygems.org/
8
- specs:
9
- rake (10.5.0)
10
-
11
- PLATFORMS
12
- ruby
13
-
14
- DEPENDENCIES
15
- bundler (~> 1.17)
16
- minitest-snapshots!
17
- rake (~> 10.0)
18
-
19
- BUNDLED WITH
20
- 1.17.2
data/Rakefile DELETED
@@ -1,2 +0,0 @@
1
- require "bundler/gem_tasks"
2
- task :default => :spec
data/bin/console DELETED
@@ -1,14 +0,0 @@
1
- #!/usr/bin/env ruby
2
-
3
- require "bundler/setup"
4
- require "minitest/snapshots"
5
-
6
- # You can add fixtures and/or initialization code here to make experimenting
7
- # with your gem easier. You can also use a different console, if you like.
8
-
9
- # (If you use this, don't forget to add pry to your Gemfile!)
10
- # require "pry"
11
- # Pry.start
12
-
13
- require "irb"
14
- IRB.start(__FILE__)
data/bin/setup DELETED
@@ -1,8 +0,0 @@
1
- #!/usr/bin/env bash
2
- set -euo pipefail
3
- IFS=$'\n\t'
4
- set -vx
5
-
6
- bundle install
7
-
8
- # Do any other automated setup that you need to do here
@@ -1,27 +0,0 @@
1
-
2
- lib = File.expand_path("../lib", __FILE__)
3
- $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
- require "minitest/snapshots/version"
5
-
6
- Gem::Specification.new do |spec|
7
- spec.name = "minitest-snapshots"
8
- spec.version = Minitest::Snapshots::VERSION
9
- spec.authors = ["Harry Brundage"]
10
- spec.email = ["harry.brundage@gmail.com"]
11
-
12
- spec.summary = %q{Adds jest style snapshot testing asserting against long values stored on the filesystem.}
13
- spec.homepage = "https://github.com/superpro-inc/minitest-snapshots"
14
- spec.license = "MIT"
15
-
16
- # Specify which files should be added to the gem when it is released.
17
- # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
18
- spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
19
- `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
20
- end
21
- spec.bindir = "exe"
22
- spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
- spec.require_paths = ["lib"]
24
-
25
- spec.add_development_dependency "bundler", "~> 1.17"
26
- spec.add_development_dependency "rake", "~> 10.0"
27
- end