From 78290719747fd46387386902e99996cfabbc6163 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Tue, 15 Apr 2025 22:44:46 +0200 Subject: [PATCH 01/10] Update Rubys and Gems --- .github/workflows/ci.yml | 4 ++-- Gemfile.lock | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 14a1962..d17390d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,10 +15,10 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - ruby-version: ['2.7', '3.0', '3.1'] + ruby-version: ['3.2', '3.3', '3.4'] steps: - - uses: actions/checkout@v3 + - uses: actions/checkout@v4 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: diff --git a/Gemfile.lock b/Gemfile.lock index 90136a8..7fdf1e1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,10 +6,10 @@ PATH GEM remote: https://rubygems.org/ specs: - minitest (5.16.3) - power_assert (2.0.1) - rake (13.0.6) - test-unit (3.5.5) + minitest (5.25.5) + power_assert (2.0.5) + rake (13.2.1) + test-unit (3.6.8) power_assert PLATFORMS @@ -23,4 +23,4 @@ DEPENDENCIES test-unit (~> 3.0) BUNDLED WITH - 2.3.7 + 2.6.2 From 7449fe07b8e7ae228e4d6e0974278be3fb982605 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Tue, 15 Apr 2025 22:51:25 +0200 Subject: [PATCH 02/10] Fix main branch in CI config --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index d17390d..793fd85 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,9 +2,9 @@ name: CI on: push: - branches: [ "master" ] + branches: [ "main" ] pull_request: - branches: [ "master" ] + branches: [ "main" ] permissions: contents: read From ba48b00b82cf86034f874aa01b95279252329739 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Tue, 15 Apr 2025 22:55:42 +0200 Subject: [PATCH 03/10] Fix tests for Ruby 3.4 --- test/structured_warnings_test.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/test/structured_warnings_test.rb b/test/structured_warnings_test.rb index 86b98ed..5b1852f 100644 --- a/test/structured_warnings_test.rb +++ b/test/structured_warnings_test.rb @@ -213,7 +213,7 @@ def test_formatting_of_warn expected_warning = "#{__FILE__}:#{__LINE__ - 4}:" + - "in `block in test_formatting_of_warn': " + + "in 'block in StructuredWarningsTest#test_formatting_of_warn': " + "do not blink " + "(StructuredWarnings::StandardWarning)\n" @@ -236,7 +236,7 @@ def test_formatting_of_warn_with_uplevel if RUBY_VERSION < '2.3' "in `call': " else - "in `block in test_formatting_of_warn_with_uplevel': " + "in 'block in StructuredWarningsTest#test_formatting_of_warn_with_uplevel': " end expected_warning += @@ -261,7 +261,7 @@ def name expected_warning = "#{__FILE__}:#{__LINE__ - 7}:" + - "in `singleton class': " + + "in 'singleton class': " + "method redefined; discarding old name " + "(StructuredWarnings::BuiltInWarning)\n" From 00facc91292d363c8f7cffd0bff5267597bdee77 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Tue, 15 Apr 2025 23:00:18 +0200 Subject: [PATCH 04/10] Fix tests for Ruby < 3.4 --- test/structured_warnings_test.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/test/structured_warnings_test.rb b/test/structured_warnings_test.rb index 5b1852f..20bf76c 100644 --- a/test/structured_warnings_test.rb +++ b/test/structured_warnings_test.rb @@ -25,6 +25,22 @@ def method_using_incompatible_warn_api end end + def opening_quote + if RUBY_VERSION < '3.4' + '`' + else + "'" + end + end + + def classname_in_message + if RUBY_VERSION < '3.4' + '' + else + 'StructuredWarningsTest#' + end + end + def test_fork_in_thread return unless supports_fork? @@ -213,7 +229,7 @@ def test_formatting_of_warn expected_warning = "#{__FILE__}:#{__LINE__ - 4}:" + - "in 'block in StructuredWarningsTest#test_formatting_of_warn': " + + "in #{opening_quote}block in #{classname_in_message}test_formatting_of_warn': " + "do not blink " + "(StructuredWarnings::StandardWarning)\n" @@ -236,7 +252,7 @@ def test_formatting_of_warn_with_uplevel if RUBY_VERSION < '2.3' "in `call': " else - "in 'block in StructuredWarningsTest#test_formatting_of_warn_with_uplevel': " + "in #{opening_quote}block in #{classname_in_message}test_formatting_of_warn_with_uplevel': " end expected_warning += @@ -261,7 +277,7 @@ def name expected_warning = "#{__FILE__}:#{__LINE__ - 7}:" + - "in 'singleton class': " + + "in #{opening_quote}singleton class': " + "method redefined; discarding old name " + "(StructuredWarnings::BuiltInWarning)\n" From 5659510fe5b46312955bc2a9a192e63c0c1a49ae Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Tue, 15 Apr 2025 23:04:20 +0200 Subject: [PATCH 05/10] Cleanup support for older Ruby versions pre-3.0 --- test/structured_warnings_test.rb | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/test/structured_warnings_test.rb b/test/structured_warnings_test.rb index 20bf76c..0419c54 100644 --- a/test/structured_warnings_test.rb +++ b/test/structured_warnings_test.rb @@ -246,16 +246,8 @@ def test_formatting_of_warn_with_uplevel end expected_warning = - "#{__FILE__}:#{__LINE__ - 4}:" - - expected_warning += - if RUBY_VERSION < '2.3' - "in `call': " - else - "in #{opening_quote}block in #{classname_in_message}test_formatting_of_warn_with_uplevel': " - end - - expected_warning += + "#{__FILE__}:#{__LINE__ - 4}:" + + "in #{opening_quote}block in #{classname_in_message}test_formatting_of_warn_with_uplevel': " + "do not blink " + "(StructuredWarnings::StandardWarning)\n" From cf5eca5cfaf61ba8503dc3c9750d6f573b4d0a0b Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Tue, 15 Apr 2025 23:09:18 +0200 Subject: [PATCH 06/10] Reduce expected compatibility --- README.md | 21 +++------------------ structured_warnings.gemspec | 2 ++ 2 files changed, 5 insertions(+), 18 deletions(-) diff --git a/README.md b/README.md index 4c46449..e5264c5 100644 --- a/README.md +++ b/README.md @@ -33,24 +33,9 @@ Or install it yourself as: ## Compatibility -`structured_warnings` aims to work with all Ruby interpreters. Please file a bug -for any incompatibilities. - - -Versions of `structured_warnings` before `v0.3.0` are incompatible with Ruby -2.4+. Please upgrade accordingly, if you need Ruby 2.4 compatibility. Please -note on the otherhand, that many class names changed in an incompatible way -with `structured_warnings` `v0.3.0`. This was done to avoid future name clashes. - -Here's a table which should ease upgrading. - -| v0.2.0 and before | v0.3.0 and after | -|------------------------------|--------------------------------------------------| -| `Warning` | `StructuredWarnings::Base` | -| `StandardWarning` | `StructuredWarnings::StandardWarning` | -| `DeprecationWarning` | `StructuredWarnings::DeprecationWarning` | -| `DeprecatedMethodWarning` | `StructuredWarnings::DeprecatedMethodWarning` | -| `DeprecatedSignatureWarning` | `StructuredWarnings::DeprecatedSignatureWarning` | +`structured_warnings` aims to work with all stable, maintained Ruby versions. At +the time of this writing this is Ruby 3.2, 3.3, and 3.4. Please file a bug for +any incompatibilities. ### Test framework support diff --git a/structured_warnings.gemspec b/structured_warnings.gemspec index 684ad85..fca07c1 100644 --- a/structured_warnings.gemspec +++ b/structured_warnings.gemspec @@ -18,6 +18,8 @@ Gem::Specification.new do |spec| spec.require_paths = ['lib'] + spec.required_ruby_version = '>= 3.2.0' + spec.add_development_dependency 'bundler', '~> 2.1' spec.add_development_dependency 'rake', '~> 13.0' spec.add_development_dependency 'minitest', '~> 5.0' From 18a972d2d5b0419d6c88d6457b8c36c9f2d80506 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Tue, 15 Apr 2025 23:16:56 +0200 Subject: [PATCH 07/10] Remove old compat layer --- lib/structured_warnings.rb | 3 --- lib/warning.rb | 9 --------- 2 files changed, 12 deletions(-) delete mode 100644 lib/warning.rb diff --git a/lib/structured_warnings.rb b/lib/structured_warnings.rb index ad70348..12c8a92 100644 --- a/lib/structured_warnings.rb +++ b/lib/structured_warnings.rb @@ -1,8 +1,5 @@ require 'structured_warnings/version' -# Compatibility layer -require 'warning' unless defined? ::Warning - require 'dynamic' module StructuredWarnings diff --git a/lib/warning.rb b/lib/warning.rb deleted file mode 100644 index 9eab49b..0000000 --- a/lib/warning.rb +++ /dev/null @@ -1,9 +0,0 @@ -module Warning - KERNEL_WARN = Kernel.instance_method(:warn).bind(self) - - def warn(*args) - KERNEL_WARN.call(*args) - end - - extend self -end From b3a48e599de5c852b6f1e7b6507eb2395f843b81 Mon Sep 17 00:00:00 2001 From: Gregor Schmidt Date: Tue, 15 Apr 2025 23:20:57 +0200 Subject: [PATCH 08/10] All supported Ruby versions support core warnings now --- test/structured_warnings_test.rb | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test/structured_warnings_test.rb b/test/structured_warnings_test.rb index 0419c54..8008a9d 100644 --- a/test/structured_warnings_test.rb +++ b/test/structured_warnings_test.rb @@ -144,8 +144,6 @@ def test_base_is_default_warning end def test_builtin_warnings - return unless supports_core_warnings? - with_verbose_warnings do assert_warn(StructuredWarnings::BuiltInWarning, /method redefined; discarding old name/) do class << Object.new @@ -255,8 +253,6 @@ def test_formatting_of_warn_with_uplevel end def test_formatting_of_builtin_warn - return unless supports_core_warnings? - actual_warning = capture_strderr do class << Object.new attr_accessor :name @@ -288,10 +284,6 @@ def supports_fork? false end - def supports_core_warnings? - Warning.instance_method(:warn).source_location.nil? - end - def with_verbose_warnings verbose, $VERBOSE = $VERBOSE, true From a9e78269b88af6e8fa6b0eb65be84dc0c4d5422c Mon Sep 17 00:00:00 2001 From: Jan Sandbrink Date: Tue, 15 Apr 2025 08:50:23 +0200 Subject: [PATCH 09/10] Improve compatibility with newer Rubies In old Ruby versions named parameters were just a fancy way to append a hash to the list of positional arguments. Newer Ruby versions treat named parameters as something more special and they are not forwarded by `*args` anymore. --- lib/structured_warnings/kernel.rb | 4 ++-- lib/structured_warnings/warning.rb | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/structured_warnings/kernel.rb b/lib/structured_warnings/kernel.rb index 1419977..359c505 100644 --- a/lib/structured_warnings/kernel.rb +++ b/lib/structured_warnings/kernel.rb @@ -1,6 +1,6 @@ module StructuredWarnings::Kernel - def warn(*args) - Warning.warn(*args) + def warn(*args, **opts) + Warning.warn(*args, **opts) end end diff --git a/lib/structured_warnings/warning.rb b/lib/structured_warnings/warning.rb index 591114f..70d7065 100644 --- a/lib/structured_warnings/warning.rb +++ b/lib/structured_warnings/warning.rb @@ -36,7 +36,7 @@ module StructuredWarnings::Warning # # warn StructuredWarnings::Base.new("The least specific warning you can get") # - def warn(*args) + def warn(*args, **options) first = args.shift if first.is_a? Class and first <= StructuredWarnings::Base warning = first @@ -55,8 +55,6 @@ def warn(*args) message = first.to_s.split(':', 4).last[1..-2] end - options = args.first.is_a?(Hash) ? args.shift : {} - # If args is not empty, user passed an incompatible set of arguments. # Maybe somebody else is overriding warn as well and knows, what to do. # Better do nothing in this case. See #5 From 19a03945839277dc491d5b3c8a004bdc6809008a Mon Sep 17 00:00:00 2001 From: Jan Sandbrink Date: Tue, 15 Apr 2025 09:28:34 +0200 Subject: [PATCH 10/10] Fix missing characters for manual Warning.warn calls When calling Warning.warn manually the first and last character would be trimmed from the warning message. Since the intention seems to have been to remove unneeded whitespace, we are now using strip instead of manually making a substring. --- lib/structured_warnings/warning.rb | 2 +- test/structured_warnings_test.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/structured_warnings/warning.rb b/lib/structured_warnings/warning.rb index 70d7065..06e5c2a 100644 --- a/lib/structured_warnings/warning.rb +++ b/lib/structured_warnings/warning.rb @@ -52,7 +52,7 @@ def warn(*args, **options) else warning = StructuredWarnings::BuiltInWarning - message = first.to_s.split(':', 4).last[1..-2] + message = first.to_s.split(':', 4).last.strip end # If args is not empty, user passed an incompatible set of arguments. diff --git a/test/structured_warnings_test.rb b/test/structured_warnings_test.rb index 8008a9d..d663b81 100644 --- a/test/structured_warnings_test.rb +++ b/test/structured_warnings_test.rb @@ -272,6 +272,20 @@ def name assert_equal expected_warning, actual_warning end + def test_formatting_of_manual_warn + actual_warning = capture_strderr do + Warning.warn("This is a test warning.") + end + + expected_warning = + "#{__FILE__}:#{__LINE__ - 4}:" + + "in #{opening_quote}block in #{classname_in_message}test_formatting_of_manual_warn': " + + "This is a test warning. " + + "(StructuredWarnings::BuiltInWarning)\n" + + assert_equal expected_warning, actual_warning + end + protected def supports_fork?