Skip to content

Commit fd622be

Browse files
committed
feat: allow deprecation behavior env var
Add support for a GIT_DEPRECATION_BEHAVIOR environment variable that configures the ActiveSupport deprecation behavior when the gem is loaded. Any behavior name accepted by the installed ActiveSupport version is valid (e.g. 'silence', 'log', 'raise', 'report'). If the variable is set to an unrecognised value, loading the gem raises ArgumentError with the list of accepted values. Also marks Git::Deprecation as @api public since users need to reference it to configure deprecation handling in code.
1 parent 52059c6 commit fd622be

4 files changed

Lines changed: 67 additions & 3 deletions

File tree

‎README.md‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -589,6 +589,18 @@ You can silence deprecation warnings by adding this line to your source code:
589589
Git::Deprecation.behavior = :silence
590590
```
591591

592+
Or by setting this environment variable before loading the gem:
593+
594+
```sh
595+
GIT_DEPRECATION_BEHAVIOR=silence
596+
```
597+
598+
Accepted environment variable values are the behavior names supported by your
599+
installed ActiveSupport version.
600+
601+
If `GIT_DEPRECATION_BEHAVIOR` is set to an unsupported value, loading the gem
602+
raises `ArgumentError` with the accepted behavior names.
603+
592604
See [the Active Support Deprecation
593605
documentation](https://api.rubyonrails.org/classes/ActiveSupport/Deprecation.html)
594606
for more details.

‎lib/git.rb‎

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,29 @@
11
# frozen_string_literal: true
22

3-
require 'active_support'
43
require 'active_support/deprecation'
54

5+
# Define Git::Deprecation before requiring the rest of the library to ensure that
6+
# any deprecation warnings emitted during the loading of the library are properly
7+
# configured according to the GIT_DEPRECATION_BEHAVIOR environment variable.
8+
#
69
module Git
10+
# The deprecation instance used to emit deprecation warnings for the Git gem
11+
#
12+
# @api public
713
Deprecation = ActiveSupport::Deprecation.new('5.0.0', 'Git')
14+
15+
if (behavior = ENV.fetch('GIT_DEPRECATION_BEHAVIOR', nil))
16+
behavior = behavior.strip
17+
allowed_behaviors = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS.keys.map(&:to_s)
18+
19+
unless allowed_behaviors.include?(behavior)
20+
raise ArgumentError,
21+
"Invalid GIT_DEPRECATION_BEHAVIOR=#{behavior.inspect}; " \
22+
"expected one of: #{allowed_behaviors.join(', ')}"
23+
end
24+
25+
Deprecation.behavior = behavior.to_sym
26+
end
827
end
928

1029
require 'git/author'

‎lib/git/status.rb‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
# frozen_string_literal: true
22

3-
# These would be required by the main `git.rb` file
4-
53
module Git
64
# The Status class gets the status of a git repository. It identifies which
75
# files have been modified, added, or deleted, including untracked files.

‎tests/units/test_deprecations.rb‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# frozen_string_literal: true
22

3+
require 'open3'
4+
require 'rbconfig'
35
require_relative '../test_helper'
46

57
# Consolidated deprecation tests to ensure all deprecated entry points emit
@@ -214,3 +216,36 @@ def test_repository_path_accessor_deprecation
214216
@git.repo.path
215217
end
216218
end
219+
220+
# Tests for the GIT_DEPRECATION_BEHAVIOR environment variable
221+
class TestGitDeprecationBehaviorEnvVar < Test::Unit::TestCase
222+
GEM_ROOT = File.expand_path('../..', __dir__)
223+
RUBY_EXE = RbConfig.ruby
224+
225+
DEPRECATION_SCRIPT = <<~RUBY
226+
require 'git'
227+
Git::Deprecation.warn('deprecated message')
228+
RUBY
229+
230+
LOAD_SCRIPT = "require 'git'"
231+
232+
def run_with_env(env_vars, script)
233+
Open3.capture3(env_vars, RUBY_EXE, '-Ilib', '-e', script, chdir: GEM_ROOT)
234+
end
235+
236+
def test_silence_behavior_suppresses_warnings
237+
_out, err, status = run_with_env({ 'GIT_DEPRECATION_BEHAVIOR' => 'silence' }, DEPRECATION_SCRIPT)
238+
239+
assert status.success?, "Script failed unexpectedly:\n#{err}"
240+
assert_no_match(/DEPRECATION WARNING: deprecated message/, err)
241+
end
242+
243+
def test_invalid_behavior_raises_argument_error
244+
allowed_behaviors = ActiveSupport::Deprecation::DEFAULT_BEHAVIORS.keys.map(&:to_s)
245+
_out, err, status = run_with_env({ 'GIT_DEPRECATION_BEHAVIOR' => 'silent' }, LOAD_SCRIPT)
246+
247+
assert !status.success?, 'Expected script to fail but it succeeded'
248+
assert_match(/Invalid GIT_DEPRECATION_BEHAVIOR="silent"/, err)
249+
assert_match(/expected one of: #{Regexp.escape(allowed_behaviors.join(', '))}/, err)
250+
end
251+
end

0 commit comments

Comments
 (0)