noncommittal 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '029ebe87976f291a4998cefd198ee1003f0fcca977072621851247164496d25b'
4
+ data.tar.gz: 17cf95e7f5e844d8fe226154092575cc8ec85089c1fac25e2dd2531ceb99b496
5
+ SHA512:
6
+ metadata.gz: 183e8f4fcddcdf2673c72707df77d961b42a62002961d06dd2e8d6510e9cb7c27b47b30332bc0e5d23d33397f0e0b9cd7f87b44338e41e3649beb70c4fe95056
7
+ data.tar.gz: 27343c72bb0e819f7f6801866347dacdeeafce9d61c0844158bafe2e1f34dc0360c618f86605974e0abe960356f92862500916af5f55a703015e3bbb10ba7bee
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
@@ -0,0 +1,6 @@
1
+ ---
2
+ language: ruby
3
+ cache: bundler
4
+ rvm:
5
+ - 2.7.1
6
+ before_install: gem install bundler -v 2.1.4
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source "https://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in noncommittal.gemspec
4
+ gemspec
5
+
6
+ gem "rake", "~> 12.0"
7
+ gem "minitest", "~> 5.0"
8
+ gem "standard"
@@ -0,0 +1,48 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ noncommittal (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ ast (2.4.1)
10
+ minitest (5.14.2)
11
+ parallel (1.20.1)
12
+ parser (2.7.2.0)
13
+ ast (~> 2.4.1)
14
+ rainbow (3.0.0)
15
+ rake (12.3.3)
16
+ regexp_parser (2.0.0)
17
+ rexml (3.2.4)
18
+ rubocop (1.4.2)
19
+ parallel (~> 1.10)
20
+ parser (>= 2.7.1.5)
21
+ rainbow (>= 2.2.2, < 4.0)
22
+ regexp_parser (>= 1.8)
23
+ rexml
24
+ rubocop-ast (>= 1.1.1)
25
+ ruby-progressbar (~> 1.7)
26
+ unicode-display_width (>= 1.4.0, < 2.0)
27
+ rubocop-ast (1.3.0)
28
+ parser (>= 2.7.1.5)
29
+ rubocop-performance (1.9.1)
30
+ rubocop (>= 0.90.0, < 2.0)
31
+ rubocop-ast (>= 0.4.0)
32
+ ruby-progressbar (1.10.1)
33
+ standard (0.10.2)
34
+ rubocop (= 1.4.2)
35
+ rubocop-performance (= 1.9.1)
36
+ unicode-display_width (1.7.0)
37
+
38
+ PLATFORMS
39
+ ruby
40
+
41
+ DEPENDENCIES
42
+ minitest (~> 5.0)
43
+ noncommittal!
44
+ rake (~> 12.0)
45
+ standard
46
+
47
+ BUNDLED WITH
48
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Justin Searls
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
13
+ all 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
21
+ THE SOFTWARE.
@@ -0,0 +1,95 @@
1
+ # Noncommittal
2
+
3
+ This gem helps you avoid test pollution in your Rails test suite by preventing
4
+ tests from committing records to your Postgres database.
5
+
6
+ ## How to avoid commitment
7
+
8
+ Add this to your Gemfile, probably grouped with other test gems:
9
+
10
+ ``` ruby
11
+ group :test do
12
+ gem "noncommittal"
13
+ end
14
+ ```
15
+
16
+ Then, in your `test_helper.rb` (or similar), after you require your Rails
17
+ environment, just drop this in:
18
+
19
+ ```ruby
20
+ Noncommittal.start!(tables: [:users, :posts])
21
+ ```
22
+
23
+ This will create an empty table called `noncommittal_no_rows_allowed` and, for
24
+ each of your models, a deferred foreign key constraint that will effectively
25
+ prevent any records from being committed outside the test transaction.
26
+
27
+ ## Do you have commitment issues?
28
+
29
+ By default, Ruby on Rails tests run each test [in a
30
+ transaction](https://guides.rubyonrails.org/testing.html#testing-parallel-transactions)
31
+ that is rolled back at the end of each test. This is a performant way to create
32
+ proper isolation, preventing tests that save records to the database from
33
+ affecting the environment of your other tests.
34
+
35
+ However, Rails can only enforce this constraint when the test-scoped code
36
+ connects to the database via the framework and within that transaction. If a
37
+ test were to extend `Minitest::Test` instead of `ActiveSupport::TestCase`, that
38
+ test would not benefit from this rollback-only transaction protection. And if
39
+ the [subject under
40
+ test](https://github.com/testdouble/contributing-tests/wiki/Subject) contains
41
+ transaction logic itself, or creates its own database connections, or spawns
42
+ child processes, then it's entirely possible that some of your tests will
43
+ erroneously commit records to the database, potentially causing [test
44
+ pollution](https://github.com/testdouble/test-smells/tree/master/smells/unreliable/litter-bugs).
45
+
46
+ Over the years, Rubyists have taken several approaches to mitigate this risk,
47
+ but most popular solutions have drawbacks:
48
+
49
+ * Gems like
50
+ [database_cleaner](https://github.com/DatabaseCleaner/database_cleaner) that
51
+ purge your database between tests introduce a per-test runtime cost that is
52
+ much higher than relying on transaction rollbacks
53
+ * Bisecting your test suite to identify which test that violates transaction
54
+ safety catches test pollution only after it causes a problem and is separately
55
+ time-consuming
56
+ * Adding a before-hook that runs before each test to ensure tables are empty
57
+ won't give you a stack trace to the test that managed to commit inserted
58
+ records
59
+
60
+ So that's why the noncommittal gem exists! It's fast, will catch this problem as
61
+ soon as it's introduced, and will give you an accurate stack trace to the
62
+ offending test.
63
+
64
+ ## What if I want to commit to certain tables?
65
+
66
+ By default, noncommittal will gather the table names of all your models that
67
+ descend from `ActiveRecord::Base`, but this may not be what you want (you might
68
+ want to exclude certain models or include additional tables). To override this
69
+ behavior, you can pass an array of table names to a `tables` keyword argument,
70
+ like so:
71
+
72
+ ```ruby
73
+ Noncommittal.start!(tables: [:users, :posts])
74
+ ```
75
+
76
+ ## Limitations
77
+
78
+ This only works with Postgres currently. PRs welcome if you can accomplish the
79
+ same behavior with other database adapters!
80
+
81
+ ## Acknowledgements
82
+
83
+ This gem is a codification of [this
84
+ tweet](https://twitter.com/searls/status/1336729988498862085?s=20), which itself
85
+ was the brainchild of [Matthew Draper](https://github.com/matthewd).
86
+
87
+ ## Code of Conduct
88
+
89
+ This project follows Test Double's [code of
90
+ conduct](https://testdouble.com/code-of-conduct) for all community interactions,
91
+ including (but not limited to) one-on-one communications, public posts/comments,
92
+ code reviews, pull requests, and GitHub issues. If violations occur, Test Double
93
+ will take any action they deem appropriate for the infraction, up to and
94
+ including blocking a user from the organization's repositories.
95
+
@@ -0,0 +1,11 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+ require "standard/rake"
4
+
5
+ Rake::TestTask.new(:test) do |t|
6
+ t.libs << "test"
7
+ t.libs << "lib"
8
+ t.test_files = FileList["test/**/*_test.rb"]
9
+ end
10
+
11
+ task default: [:test, "standard:fix"]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "noncommittal"
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__)
@@ -0,0 +1,8 @@
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
@@ -0,0 +1,18 @@
1
+ require "noncommittal/version"
2
+
3
+ module Noncommittal
4
+ def self.start!(tables: nil)
5
+ tables ||= ActiveRecord::Base.descendants.map(&:table_name).compact.uniq - ["ar_internal_metadata", "schema_migrations"]
6
+
7
+ ActiveRecord::Base.connection.execute <<~SQL
8
+ create table if not exists noncommittal_no_rows_allowed (id bigint unique);
9
+ #{tables.map { |table_name|
10
+ constraint_name = "noncommittal_#{table_name}"
11
+ <<~SQL
12
+ alter table #{table_name} drop constraint if exists #{constraint_name};
13
+ alter table #{table_name} add constraint #{constraint_name} foreign key (id) references noncommittal_no_rows_allowed (id) deferrable initially deferred;
14
+ SQL
15
+ }.join("\n")}
16
+ SQL
17
+ end
18
+ end
@@ -0,0 +1,3 @@
1
+ module Noncommittal
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,23 @@
1
+ require_relative "lib/noncommittal/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "noncommittal"
5
+ spec.version = Noncommittal::VERSION
6
+ spec.authors = ["Justin Searls"]
7
+ spec.email = ["searls@gmail.com"]
8
+
9
+ spec.summary = "Ensures test isolation by preventing your Rails tests from committing to the database"
10
+ spec.license = "MIT"
11
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")
12
+
13
+ spec.metadata["homepage_uri"] = "https://github.com/testdouble/noncommittal"
14
+
15
+ # Specify which files should be added to the gem when it is released.
16
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
17
+ spec.files = Dir.chdir(File.expand_path("..", __FILE__)) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+ end
metadata ADDED
@@ -0,0 +1,57 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: noncommittal
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Justin Searls
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2020-12-10 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description:
14
+ email:
15
+ - searls@gmail.com
16
+ executables: []
17
+ extensions: []
18
+ extra_rdoc_files: []
19
+ files:
20
+ - ".gitignore"
21
+ - ".travis.yml"
22
+ - Gemfile
23
+ - Gemfile.lock
24
+ - LICENSE.txt
25
+ - README.md
26
+ - Rakefile
27
+ - bin/console
28
+ - bin/setup
29
+ - lib/noncommittal.rb
30
+ - lib/noncommittal/version.rb
31
+ - noncommittal.gemspec
32
+ homepage:
33
+ licenses:
34
+ - MIT
35
+ metadata:
36
+ homepage_uri: https://github.com/testdouble/noncommittal
37
+ post_install_message:
38
+ rdoc_options: []
39
+ require_paths:
40
+ - lib
41
+ required_ruby_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: 2.3.0
46
+ required_rubygems_version: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ version: '0'
51
+ requirements: []
52
+ rubygems_version: 3.1.2
53
+ signing_key:
54
+ specification_version: 4
55
+ summary: Ensures test isolation by preventing your Rails tests from committing to
56
+ the database
57
+ test_files: []