rspec-time-guard 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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 2236097d6377167a8f7a07f9d37e1d23ca71110aabdfcf0e6b79ca2d9783c8e5
4
+ data.tar.gz: d9b596f350b3efef6f8fe67513aa7dd03c933208196cbb31808b8d79e305411b
5
+ SHA512:
6
+ metadata.gz: 3f8aab7a4360a8226ca00e4501eedca92b7c2fdebffcffb89ae5f92e6e7a700b4e225d37445140e6888e8fa58643dea80be4c06d6ef849a02ddb0aa3297532e2
7
+ data.tar.gz: 006735ff24aa6e288d72d043f7efad35169d4af3c481de695ae3dcc2d5cfc868cc29c28139c7107badf77025fbee846a2e90885e6556d709822982711bf23e81
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.standard.yml ADDED
@@ -0,0 +1,3 @@
1
+ # For available configuration options, see:
2
+ # https://github.com/standardrb/standard
3
+ ruby_version: 3.0
data/CHANGELOG.md ADDED
@@ -0,0 +1,5 @@
1
+ ## [Unreleased]
2
+
3
+ ## [0.1.0] - 2025-03-11
4
+
5
+ - Initial release
@@ -0,0 +1,132 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ We as members, contributors, and leaders pledge to make participation in our
6
+ community a harassment-free experience for everyone, regardless of age, body
7
+ size, visible or invisible disability, ethnicity, sex characteristics, gender
8
+ identity and expression, level of experience, education, socio-economic status,
9
+ nationality, personal appearance, race, caste, color, religion, or sexual
10
+ identity and orientation.
11
+
12
+ We pledge to act and interact in ways that contribute to an open, welcoming,
13
+ diverse, inclusive, and healthy community.
14
+
15
+ ## Our Standards
16
+
17
+ Examples of behavior that contributes to a positive environment for our
18
+ community include:
19
+
20
+ * Demonstrating empathy and kindness toward other people
21
+ * Being respectful of differing opinions, viewpoints, and experiences
22
+ * Giving and gracefully accepting constructive feedback
23
+ * Accepting responsibility and apologizing to those affected by our mistakes,
24
+ and learning from the experience
25
+ * Focusing on what is best not just for us as individuals, but for the overall
26
+ community
27
+
28
+ Examples of unacceptable behavior include:
29
+
30
+ * The use of sexualized language or imagery, and sexual attention or advances of
31
+ any kind
32
+ * Trolling, insulting or derogatory comments, and personal or political attacks
33
+ * Public or private harassment
34
+ * Publishing others' private information, such as a physical or email address,
35
+ without their explicit permission
36
+ * Other conduct which could reasonably be considered inappropriate in a
37
+ professional setting
38
+
39
+ ## Enforcement Responsibilities
40
+
41
+ Community leaders are responsible for clarifying and enforcing our standards of
42
+ acceptable behavior and will take appropriate and fair corrective action in
43
+ response to any behavior that they deem inappropriate, threatening, offensive,
44
+ or harmful.
45
+
46
+ Community leaders have the right and responsibility to remove, edit, or reject
47
+ comments, commits, code, wiki edits, issues, and other contributions that are
48
+ not aligned to this Code of Conduct, and will communicate reasons for moderation
49
+ decisions when appropriate.
50
+
51
+ ## Scope
52
+
53
+ This Code of Conduct applies within all community spaces, and also applies when
54
+ an individual is officially representing the community in public spaces.
55
+ Examples of representing our community include using an official email address,
56
+ posting via an official social media account, or acting as an appointed
57
+ representative at an online or offline event.
58
+
59
+ ## Enforcement
60
+
61
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
62
+ reported to the community leaders responsible for enforcement at
63
+ [INSERT CONTACT METHOD].
64
+ All complaints will be reviewed and investigated promptly and fairly.
65
+
66
+ All community leaders are obligated to respect the privacy and security of the
67
+ reporter of any incident.
68
+
69
+ ## Enforcement Guidelines
70
+
71
+ Community leaders will follow these Community Impact Guidelines in determining
72
+ the consequences for any action they deem in violation of this Code of Conduct:
73
+
74
+ ### 1. Correction
75
+
76
+ **Community Impact**: Use of inappropriate language or other behavior deemed
77
+ unprofessional or unwelcome in the community.
78
+
79
+ **Consequence**: A private, written warning from community leaders, providing
80
+ clarity around the nature of the violation and an explanation of why the
81
+ behavior was inappropriate. A public apology may be requested.
82
+
83
+ ### 2. Warning
84
+
85
+ **Community Impact**: A violation through a single incident or series of
86
+ actions.
87
+
88
+ **Consequence**: A warning with consequences for continued behavior. No
89
+ interaction with the people involved, including unsolicited interaction with
90
+ those enforcing the Code of Conduct, for a specified period of time. This
91
+ includes avoiding interactions in community spaces as well as external channels
92
+ like social media. Violating these terms may lead to a temporary or permanent
93
+ ban.
94
+
95
+ ### 3. Temporary Ban
96
+
97
+ **Community Impact**: A serious violation of community standards, including
98
+ sustained inappropriate behavior.
99
+
100
+ **Consequence**: A temporary ban from any sort of interaction or public
101
+ communication with the community for a specified period of time. No public or
102
+ private interaction with the people involved, including unsolicited interaction
103
+ with those enforcing the Code of Conduct, is allowed during this period.
104
+ Violating these terms may lead to a permanent ban.
105
+
106
+ ### 4. Permanent Ban
107
+
108
+ **Community Impact**: Demonstrating a pattern of violation of community
109
+ standards, including sustained inappropriate behavior, harassment of an
110
+ individual, or aggression toward or disparagement of classes of individuals.
111
+
112
+ **Consequence**: A permanent ban from any sort of public interaction within the
113
+ community.
114
+
115
+ ## Attribution
116
+
117
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage],
118
+ version 2.1, available at
119
+ [https://www.contributor-covenant.org/version/2/1/code_of_conduct.html][v2.1].
120
+
121
+ Community Impact Guidelines were inspired by
122
+ [Mozilla's code of conduct enforcement ladder][Mozilla CoC].
123
+
124
+ For answers to common questions about this code of conduct, see the FAQ at
125
+ [https://www.contributor-covenant.org/faq][FAQ]. Translations are available at
126
+ [https://www.contributor-covenant.org/translations][translations].
127
+
128
+ [homepage]: https://www.contributor-covenant.org
129
+ [v2.1]: https://www.contributor-covenant.org/version/2/1/code_of_conduct.html
130
+ [Mozilla CoC]: https://github.com/mozilla/diversity
131
+ [FAQ]: https://www.contributor-covenant.org/faq
132
+ [translations]: https://www.contributor-covenant.org/translations
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2025 Lucas Montorio
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.
data/README.md ADDED
@@ -0,0 +1,182 @@
1
+ [//]: # (TODO: Add a public TODO list?)
2
+
3
+ # ⚠️ **_This project is a Work In Progress_** ⚠️
4
+
5
+ # RspecTimeGuard
6
+
7
+ `RspecTimeGuard` helps you identify and manage slow-running tests in your RSpec test suite by setting time limits on individual examples or globally across your test suite.
8
+
9
+
10
+ ## Installation
11
+
12
+ Add this line to your application's Gemfile:
13
+
14
+ ```ruby
15
+ gem 'rspec-time-guard'
16
+ ```
17
+
18
+ And then execute:
19
+
20
+ ```bash
21
+ $ bundle install
22
+ ```
23
+
24
+ Or install it globally:
25
+
26
+ ```bash
27
+ $ gem install rspec-time-guard
28
+ ```
29
+
30
+
31
+ ## Usage
32
+
33
+ ### Basic Setup
34
+
35
+ RSpec Time Guard integrates automatically with Rails applications in the test environment. For non-Rails projects, you'll need to manually require and set it up.
36
+
37
+ #### Rails Setup
38
+
39
+ The gem will automatically initialize itself when Rails loads in the test environment.
40
+
41
+ #### Manual Setup (for non-Rails projects)
42
+
43
+ In your `spec_helper.rb` or similar file:
44
+
45
+ ```ruby
46
+ require 'rspec_time_guard'
47
+
48
+ RspecTimeGuard.setup
49
+ ```
50
+
51
+ ### Configuration
52
+
53
+ Create an initializer at `config/initializers/rspec_time_guard.rb` (for Rails) or add to your spec configuration file:
54
+
55
+ ```ruby
56
+ RspecTimeGuard.configure do |config|
57
+ # Set a global time limit in seconds for all examples (nil = no global limit)
58
+ config.global_time_limit_seconds = 1.0
59
+
60
+ # Whether to continue running tests that exceed their time limit
61
+ # true = shows warning but allows test to complete
62
+ # false = raises TimeLimitExceededError and stops the test (default)
63
+ config.continue_on_timeout = false
64
+ end
65
+ ```
66
+
67
+ ### Setting Time Limits
68
+
69
+ #### Option 1: Global Time Limit
70
+
71
+ Set a global time limit that applies to all your tests through configuration:
72
+
73
+ ```ruby
74
+ RspecTimeGuard.configure do |config|
75
+ config.global_time_limit_seconds = 0.5 # 500 milliseconds
76
+ end
77
+ ```
78
+
79
+ #### Option 2: Per-Example Time Limits
80
+
81
+ Add a specific time limit to individual examples using metadata:
82
+
83
+ ```ruby
84
+ # Apply a 0.25 second time limit to this test
85
+ it "should do something quickly", time_limit_seconds: 0.25 do
86
+ # Your test code
87
+ end
88
+
89
+ # Apply a 5 second time limit to this group of tests
90
+ describe "operations that need more time", time_limit_seconds: 5 do
91
+ it "does a complex operation" do
92
+ # ...
93
+ end
94
+
95
+ it "does another complex operation" do
96
+ # ...
97
+ end
98
+ end
99
+ ```
100
+
101
+ ### Error Handling
102
+
103
+ When a test exceeds its time limit:
104
+
105
+ 1. If `continue_on_timeout` is `false` (default):
106
+ - The test will be interrupted
107
+ - A `RspecTimeGuard::TimeLimitExceededError` will be raised
108
+ - The test will be marked as failed
109
+
110
+ 2. If `continue_on_timeout` is `true`:
111
+ - A warning message will be displayed
112
+ - The test will continue running until completion
113
+ - The test will pass or fail based on its assertions, not its timing
114
+
115
+ ### Important Notes on Test Execution
116
+
117
+ #### Test Interruption
118
+
119
+ When a time limit is exceeded and `continue_on_timeout` is set to `false` (the default):
120
+
121
+ - Test execution is immediately interrupted at the time limit
122
+ - Any code after the point where the timeout occurs will not be executed
123
+ - Cleanup operations such as database transactions may not complete normally
124
+ - Any assertions or expectations after the timeout point won't be evaluated
125
+
126
+ This means if your test has important cleanup steps or assertions near the end, they might not run if the test times out earlier.
127
+ If you need to ensure all test code runs even when timing out, use the `continue_on_timeout` option.
128
+
129
+ #### Thread Safety Considerations
130
+
131
+ RSpec Time Guard uses threads for execution monitoring. While we take care to properly clean up these threads, be aware that:
132
+
133
+ 1. Tests with threading or process-spawning code might behave unexpectedly
134
+ 2. Thread-local variables could be lost when a test is interrupted
135
+ 3. Some Ruby extensions and libraries might not be fully thread-safe
136
+ ite
137
+
138
+
139
+ ## Examples
140
+
141
+ ### Basic Example
142
+
143
+ ```ruby
144
+ # spec/models/user_spec.rb
145
+ require 'rails_helper'
146
+
147
+ RSpec.describe User, type: :model do
148
+ it "validates email quickly", time_limit_seconds: 0.1 do
149
+ user = User.new(email: "invalid")
150
+ expect(user.valid?).to be false
151
+ end
152
+
153
+ # This test will use the global time limit if configured
154
+ it "can generate a profile" do
155
+ user = User.create(name: "John", email: "john@example.com")
156
+ expect(user.generate_profile).to include(name: "John")
157
+ end
158
+ end
159
+ ```
160
+
161
+
162
+ ## How It Works
163
+
164
+ RSpec Time Guard works by:
165
+
166
+ 1. Setting up an RSpec `around(:each)` hook
167
+ 2. Running your test in a separate thread
168
+ 3. Monitoring execution time
169
+ 4. Taking action if the time limit is exceeded
170
+
171
+
172
+ ## Contributing
173
+
174
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/rspec-time-guard. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/rspec-time-guard/blob/main/CODE_OF_CONDUCT.md).
175
+
176
+ ## License
177
+
178
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
179
+
180
+ ## Code of Conduct
181
+
182
+ Everyone interacting in the `RspecTimeGuard` project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/rspec-time-guard/blob/main/CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "bundler/gem_tasks"
4
+ require "rspec/core/rake_task"
5
+
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ require "standard/rake"
9
+
10
+ task default: %i[spec standard]
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_time_guard"
@@ -0,0 +1,12 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RspecTimeGuard
4
+ class Configuration
5
+ attr_accessor :global_time_limit_seconds, :continue_on_timeout
6
+
7
+ def initialize
8
+ @global_time_limit_seconds = nil
9
+ @continue_on_timeout = false
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RspecTimeGuard
4
+ class Railtie < Rails::Railtie
5
+ initializer "rspec_time_guard.configure" do
6
+ Rails.logger.info("INITIALIZER")
7
+
8
+ config.after_initialize do
9
+ Rails.logger.info("AFTER INITIALIZE")
10
+
11
+ if defined?(RSpec) && Rails.env.test?
12
+ require "rspec_time_guard"
13
+
14
+ RspecTimeGuard.setup if defined?(RSpec)
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RspecTimeGuard
4
+ VERSION = "0.1.0"
5
+ end
@@ -0,0 +1,65 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "rspec_time_guard/configuration"
4
+ require "rspec_time_guard/version"
5
+ require "rspec_time_guard/railtie" if defined?(Rails)
6
+
7
+ module RspecTimeGuard
8
+ class TimeLimitExceededError < StandardError; end
9
+
10
+ class << self
11
+ def configure
12
+ yield(configuration)
13
+ end
14
+
15
+ def configuration
16
+ @_configuration ||= RspecTimeGuard::Configuration.new
17
+ end
18
+
19
+ # TODO: Handle RSpec summary manually?
20
+ # TODO: Check that it doesn't break RSpec parallel runs in CI (if any)
21
+ # TODO: Run profiling on the whole test suite to check for performance issues
22
+ # TODO: add PR template
23
+ def setup
24
+ RSpec.configure do |config|
25
+ config.around(:each) do |example|
26
+ time_limit_seconds = example.metadata[:time_limit_seconds] || RspecTimeGuard.configuration.global_time_limit_seconds
27
+
28
+ next example.run unless time_limit_seconds
29
+
30
+ completed = false
31
+
32
+ # NOTE: We instantiate a monitoring thread, to allow the example to run in the main RSpec thread.
33
+ # This is required to keep the RSpec context.
34
+ monitor_thread = Thread.new do
35
+ Thread.current.report_on_exception = false
36
+
37
+ # NOTE: The following logic:
38
+ # - Waits for the duration of the time limit
39
+ # - If the main thread is still running at that stage, raises a TimeLimitExceededError
40
+ sleep time_limit_seconds
41
+
42
+ unless completed
43
+ message = "[RspecTimeGuard] Example exceeded timeout of #{time_limit_seconds} seconds"
44
+ if RspecTimeGuard.configuration.continue_on_timeout
45
+ warn "#{message} - Running the example anyway (:continue_on_timeout option set to TRUE)"
46
+ example.run
47
+ else
48
+ Thread.main.raise RspecTimeGuard::TimeLimitExceededError, message
49
+ end
50
+ end
51
+ end
52
+
53
+ # NOTE: Main RSpec thread execution
54
+ begin
55
+ example.run
56
+ completed = true
57
+ ensure
58
+ # NOTE: We explicitly clean up the monitoring thread in case the example completes before the time limit.
59
+ monitor_thread.kill if monitor_thread.alive?
60
+ end
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rspec-time-guard
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Lucas Montorio
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2025-04-03 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: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rubocop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: RSpec Time Guard helps you identify and manage slow-running tests in
42
+ your RSpec test suite by setting time limits on individual examples or globally
43
+ across your test suite.
44
+ email:
45
+ - lucas.montorio@lifen.fr
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - ".rspec"
51
+ - ".standard.yml"
52
+ - CHANGELOG.md
53
+ - CODE_OF_CONDUCT.md
54
+ - LICENSE.txt
55
+ - README.md
56
+ - Rakefile
57
+ - lib/rspec-time-guard.rb
58
+ - lib/rspec_time_guard.rb
59
+ - lib/rspec_time_guard/configuration.rb
60
+ - lib/rspec_time_guard/railtie.rb
61
+ - lib/rspec_time_guard/version.rb
62
+ homepage: https://github.com/LucasMontorio/rspec-time-guard
63
+ licenses:
64
+ - MIT
65
+ metadata:
66
+ homepage_uri: https://github.com/LucasMontorio/rspec-time-guard
67
+ source_code_uri: https://github.com/LucasMontorio/rspec-time-guard
68
+ changelog_uri: https://github.com/LucasMontorio/rspec-time-guard/blob/main/CHANGELOG.md
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - ">="
76
+ - !ruby/object:Gem::Version
77
+ version: 3.0.0
78
+ required_rubygems_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ requirements: []
84
+ rubygems_version: 3.5.11
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: A simple time keeper for RSpec tests.
88
+ test_files: []