minitest-verify 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: d86d8652f970d9903ab8669a82deeb240cf98e921142b16478c0b1f3e63f1f74
4
+ data.tar.gz: b412d10611d8e75e217287896e03fd86d9e34c704aed74c05fbd50526da1a5eb
5
+ SHA512:
6
+ metadata.gz: a2734af789cb9c1019f5fe1fa18382dcab38f5773647ba4149ca10cb2d39eb57a03f840adb84ced9fa751b69517ea6dcd86a7dd1bfb67067cc0836318143aec7
7
+ data.tar.gz: 8dc9ca7b532ddfce192c3ed5e7241967dd3f58be678aef9576e087bbf66159ded64de6aa1c37630ec88eebb19cdc0319e27c23fae5a5c43cf79b5bca4d5c0c36
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Thomas Marshall
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,74 @@
1
+ # Minitest::Verify
2
+
3
+ Avoid false-positive tests by verifying they fail when key setup is removed.
4
+
5
+ ## Installation
6
+
7
+ TODO: Replace `UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG` with your gem name right after releasing it to RubyGems.org. Please do not do it earlier due to security reasons. Alternatively, replace this section with instructions to install your gem from git if you don't plan to release to RubyGems.org.
8
+
9
+ Install the gem and add to the application's Gemfile by executing:
10
+
11
+ $ bundle add UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
12
+
13
+ If bundler is not being used to manage dependencies, install the gem by executing:
14
+
15
+ $ gem install UPDATE_WITH_YOUR_GEM_NAME_IMMEDIATELY_AFTER_RELEASE_TO_RUBYGEMS_ORG
16
+
17
+ ## Usage
18
+
19
+ This is a false-positive test. It always passes because `post` and `comment` are completely unrelated: there's no reason `post.comments` would ever include `comment`.
20
+
21
+ ```rb
22
+ require "minitest/autorun"
23
+
24
+ class PostTest < Minitest::Test
25
+ def test_comments_excludes_hidden_comments
26
+ post = create(:post)
27
+ comment = create(:comment, hidden: true)
28
+
29
+ refute_includes post.comments, comment
30
+ end
31
+ end
32
+ ```
33
+
34
+ We can pull out the key setup and wrap it with `verify_fails_without`:
35
+
36
+ ```rb
37
+ require "minitest/autorun"
38
+ require "minitest/verify"
39
+
40
+ class PostTest < Minitest::Test
41
+ include Minitest::Verify
42
+
43
+ def test_comments_excludes_hidden_comments
44
+ post = create(:post)
45
+ comment = create(:comment)
46
+
47
+ verify_fails_without { comment.update!(hidden: true) }
48
+
49
+ refute_includes post.comments, comment
50
+ end
51
+ end
52
+ ```
53
+
54
+ Now run the test with the `--verify` argument:
55
+
56
+ ```
57
+ $ ruby post_test.rb --verify
58
+ ```
59
+
60
+ This will cause the test to run twice. First it runs _with_ the contents of the `verify_fails_without` block evaluated (normal run). Then it runs _without_ the contents of the `verify_fails_without` block evaluated (verification run). If the test still passes without having evaluated the code inside the block, it's a false positive and you'll see a verification failure in your test output:
61
+
62
+ ```
63
+ # Running:
64
+
65
+ V
66
+
67
+ Finished in 0.000380s, 2631.5783 runs/s, 5263.1565 assertions/s.
68
+
69
+ 1) Verification Failure:
70
+ PostTest#test_comments_excludes_hidden_comments [post_test.rb:11]:
71
+ Expected at least one assertion to fail.
72
+
73
+ 1 runs, 2 assertions, 0 failures, 0 errors, 0 skips
74
+ ```
data/Rakefile ADDED
@@ -0,0 +1,4 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ task default: %i[]
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Minitest
4
+ module Verify
5
+ VERSION = "0.1.0"
6
+ end
7
+ end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "verify/version"
4
+
5
+ module Minitest
6
+ module Verify
7
+ class VerificationFailedError < StandardError; end
8
+
9
+ class VerificationFailure < Minitest::Assertion
10
+ def result_label
11
+ "Verification Failure"
12
+ end
13
+ end
14
+
15
+ class VerificationError < Minitest::UnexpectedError
16
+ def result_label
17
+ "Verification Error"
18
+ end
19
+ end
20
+
21
+ class << self
22
+ attr_accessor :enabled
23
+ end
24
+
25
+ def verify_fails_without(&block)
26
+ if @current_caller
27
+ block.call unless caller[0] == @current_caller[0]
28
+ else
29
+ callers << caller
30
+ block.call
31
+ end
32
+ end
33
+
34
+ def run
35
+ super
36
+
37
+ return Result.from(self) unless Verify.enabled
38
+
39
+ @normal_failures = failures.dup
40
+ failures.clear
41
+
42
+ begin
43
+ while @current_caller = callers.shift
44
+ with_verification { super }
45
+ end
46
+ rescue VerificationFailedError
47
+ callers.clear
48
+ end
49
+
50
+ Result.from(self)
51
+ end
52
+
53
+ private
54
+
55
+ def callers
56
+ @callers ||= []
57
+ end
58
+
59
+ def with_verification
60
+ yield
61
+
62
+ assertions = failures.select { |f| f.class == Minitest::Assertion }
63
+ unexpected_errors = failures.select { |f| f.class == Minitest::UnexpectedError }
64
+
65
+ failures.clear
66
+ failures.concat(@normal_failures)
67
+
68
+ if unexpected_errors.any?
69
+ unexpected_errors.each do |unexpected_error|
70
+ failures << VerificationError.new(unexpected_error.error)
71
+ end
72
+
73
+ raise VerificationFailedError
74
+ end
75
+
76
+ if assertions.empty?
77
+ exception = VerificationFailure.new("Expected at least one assertion to fail.")
78
+ exception.set_backtrace(@current_caller)
79
+ failures << exception
80
+
81
+ raise VerificationFailedError
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,13 @@
1
+ require "minitest/verify"
2
+
3
+ module Minitest
4
+ def self.plugin_verify_options(opts, options)
5
+ opts.on "--verify", "Verify tests sometimes fail." do
6
+ options[:enabled] = true
7
+ end
8
+ end
9
+
10
+ def self.plugin_verify_init(options)
11
+ Verify.enabled = options.fetch(:enabled, false)
12
+ end
13
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: minitest-verify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Thomas Marshall
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2024-04-30 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ description:
28
+ email:
29
+ - thomas@thomasmarshall.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - LICENSE
35
+ - README.md
36
+ - Rakefile
37
+ - lib/minitest/verify.rb
38
+ - lib/minitest/verify/version.rb
39
+ - lib/minitest/verify_plugin.rb
40
+ homepage: https://github.com/thomasmarshall/minitest-verify
41
+ licenses: []
42
+ metadata:
43
+ homepage_uri: https://github.com/thomasmarshall/minitest-verify
44
+ source_code_uri: https://github.com/thomasmarshall/minitest-verify
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 3.0.0
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.5.3
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: A minitest plugin to prevent false positive tests.
64
+ test_files: []