minitest-snapshots 0.3.0 → 1.0.1

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: ada852a8846eb38fdbb54bba6137d8e98526400a6aeb7c4f69c2738063f84ab3
4
- data.tar.gz: e6460d1edd5797c28d3f5646aeeaae97990224882fe8d66570be434bbc220766
3
+ metadata.gz: df1eab7b656ef41c119840132f47702d15dc99fc14eb88b4a47614046bf1694e
4
+ data.tar.gz: 57b540192a37e77075dac98a026bc2dcb864e50f7a33752dfac979be6c000a53
5
5
  SHA512:
6
- metadata.gz: 7be94c6b6a1aafccd033504c727dc7bd3ccc693e09f923f28dcf6604d3ef3e60aeb0e338567f34934bfb305ff1fd3602bf85768207c0c3cfa63a9d6367faaf05
7
- data.tar.gz: 42388f113aa86b0e8525a09fc25322a0e632003f1ee00d37a4bb9d4f00f9a57f5d94c183a799f52650f2b2e0d2fabecf568be0ac24a828a1770711505441262d
6
+ metadata.gz: 5b581de57a56296cce3378f59e9ba6d08a3708378068abd8f3004eff456775b78cf7435778c3505fb67ebc5a0d71c658e2b16d7d35013444ba95606dd161fa7a
7
+ data.tar.gz: f4458793636fdf47fbba8002250e6128ce90b0f326312ccad9ac23ab3b20f65924c088130cd1884be85dbd80c44ef4f361f3f456445882e4da568251ca3b6710
data/README.md CHANGED
@@ -1,6 +1,14 @@
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://img.shields.io/gem/v/minitest-snapshots)](https://rubygems.org/gems/minitest-snapshots)
4
+ [![Gem Downloads](https://img.shields.io/gem/dt/minitest-snapshots)](https://www.ruby-toolbox.com/projects/minitest-snapshots)
5
+ [![GitHub Workflow Status](https://img.shields.io/github/actions/workflow/status/mattbrictson/minitest-snapshots/ci.yml)](https://github.com/mattbrictson/minitest-snapshots/actions/workflows/ci.yml)
6
+
7
+ Simple minitest plugin gem implementing Jest-style snapshot testing. It's like VCR, but for any value.
8
+
9
+ ## Requirements
10
+
11
+ - Ruby 2.7 or later
4
12
 
5
13
  ## Usage
6
14
 
@@ -9,17 +17,22 @@ Instead of copying and pasting large segments of machine generated text into you
9
17
  Example:
10
18
 
11
19
  ```ruby
12
- class QueryCompilerText extends Minitest::Test
13
- def test_it_can_compile_a_query
14
- assert_matches_snapshot QueryCompiler.new.compile
15
- end
20
+ class QueryCompilerText < Minitest::Test
21
+ def test_it_can_compile_a_query
22
+ assert_matches_snapshot QueryCompiler.new.compile
23
+ end
16
24
  end
17
25
  ```
18
26
 
19
27
  ## Command line options
20
28
 
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.
29
+ - `-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.
30
+ - `-l` or `--lock-snapshots`: Prevents new snapshots from being written. Useful on CI to ensure all snapshots have been written by the developer.
31
+
32
+ For example, to update snapshots on a Rails project:
33
+
34
+ $ bin/rails test -u
35
+
23
36
  ## Installation
24
37
 
25
38
  Add this line to your application's Gemfile:
@@ -36,7 +49,6 @@ Or install it yourself as:
36
49
 
37
50
  $ gem install minitest-snapshots
38
51
 
39
-
40
52
  ## Development
41
53
 
42
54
  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 +57,16 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
45
57
 
46
58
  ## Contributing
47
59
 
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.
60
+ Bug reports and pull requests are welcome on GitHub at https://github.com/mattbrictson/minitest-snapshots.
49
61
 
50
62
  ## License
51
63
 
52
- The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
64
+ The gem is available as open source under the terms of the [MIT License](LICENSE.txt).
53
65
 
54
66
  ## Code of Conduct
55
67
 
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).
68
+ 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).
69
+
70
+ ## History
71
+
72
+ `minitest-snapshots` was created by Harry Brundage (@airhorns) and originally published to rubygems in 2019. Significant contributions were [added](https://github.com/mattbrictson/minitest-snapshots/pull/6) by @chocolateboy in 2020. In June 2023, ownership of the project was transferred to Matt Brictson (@mattbrictson).
@@ -1,22 +1,28 @@
1
+ require_relative "serializer"
1
2
  require "fileutils"
2
3
 
3
4
  module Minitest
4
5
  module Assertions
5
- def assert_matches_snapshot(string, snapshot_name = nil)
6
- snapshot_file = snapshot_path(self.class.name.underscore, 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)
7
9
 
8
10
  if !Minitest::Snapshots.force_updates && File.exist?(snapshot_file)
9
- 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
+ )
10
16
  else
11
17
  if Minitest::Snapshots.lock_snapshots
12
- assert false, "New snapshot tried to be created when snapshot writing is prevented with the --lock-snapshots option."
18
+ assert(
19
+ false,
20
+ "Attempt to create a snapshot failed because writing is prevented by the --lock-snapshots option"
21
+ )
13
22
  end
14
23
 
15
24
  FileUtils.mkdir_p(File.dirname(snapshot_file))
16
-
17
- File.open(snapshot_file, "w") do |file|
18
- file.write(string)
19
- end
25
+ File.write(snapshot_file, snapshot)
20
26
  end
21
27
  end
22
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
@@ -6,17 +6,25 @@ module Minitest
6
6
  def before_setup
7
7
  super
8
8
  @snapshot_assertion_counter = 0
9
- @snapshot_dir ||= if defined?(Rails)
10
- Rails.root.join("test", "snapshots").to_s
11
- else
12
- FileUtils.pwd
13
- end
9
+ @snapshot_dir ||= Minitest::Snapshots.default_snapshots_directory
14
10
  end
15
11
 
16
12
  private
17
13
 
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
+
18
24
  def snapshot_path(suite_name, snapshot_name)
19
- File.join(@snapshot_dir, suite_name, "#{name}__#{snapshot_name}.snap.txt")
25
+ filename = format("%s__%s.snap.yaml", sanitize(name), sanitize(snapshot_name))
26
+ subdir = sanitize(suite_name)
27
+ File.join(@snapshot_dir, subdir, filename)
20
28
  end
21
29
  end
22
30
  end
@@ -1,5 +1,5 @@
1
1
  module Minitest
2
2
  module Snapshots
3
- VERSION = "0.3.0"
3
+ VERSION = "1.0.1".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.3.0
4
+ version: 1.0.1
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-19 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-10-03 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.19
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.3.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