Patch-Source: https://github.com/envygeeks/pathutil/pull/7 (updated) -- From cd4d3694ef515fe1c184e1d00d51c1bfdc85aa44 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sun, 3 Apr 2022 20:20:44 +0200 Subject: [PATCH 1/2] Fix usage of YAML.safe_load for compatibility with Ruby >=2.4 YAML.safe_load in Ruby 3.1.1 doesn't accept deprecated positional parameters anymore: 1) Pathutil::Helpers#load_yaml should be able to parse YAML Failure/Error: YAML.safe_load( data, whitelist_classes, whitelist_symbols, aliases ) ArgumentError: wrong number of arguments (given 4, expected 1) # ./lib/pathutil/helpers.rb:44:in `load_yaml' # ./spec/tests/lib/pathutil/helpers_spec.rb:25:in `block (3 levels) in ' --- lib/pathutil/helpers.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/pathutil/helpers.rb b/lib/pathutil/helpers.rb index 7398008..e777c0d 100644 --- a/lib/pathutil/helpers.rb +++ b/lib/pathutil/helpers.rb @@ -43,9 +43,9 @@ class Pathutil else YAML.safe_load( data, - whitelist_classes, - whitelist_symbols, - aliases + permitted_classes: whitelist_classes, + permitted_symbols: whitelist_symbols, + aliases: aliases ) end end -- 2.16.4 From 96fe1d9812f82f3bfd6f6a9fdd5b5f2e41e899f9 Mon Sep 17 00:00:00 2001 From: Jakub Jirutka Date: Sun, 3 Apr 2022 20:35:50 +0200 Subject: [PATCH 2/2] Remove deprecated SafeYAML support SafeYAML has been deprecated a very long time ago. --- Gemfile | 1 - README.md | 2 +- benchmark/yaml.rb | 16 ---------- lib/pathutil/helpers.rb | 38 +++++------------------ spec/tests/lib/pathutil/helpers_spec.rb | 53 --------------------------------- 5 files changed, 8 insertions(+), 102 deletions(-) delete mode 100644 benchmark/yaml.rb diff --git a/Gemfile b/Gemfile index 60ea36c..d9e73ed 100644 --- a/Gemfile +++ b/Gemfile @@ -7,7 +7,6 @@ gem "rake", :require => false gemspec group :test do - gem "safe_yaml", :require => false gem "luna-rspec-formatters", :require => false gem "simplecov", :require => false end diff --git a/README.md b/README.md index 774f708..933fa93 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ but only if they originate from the given root. - `>=`, `>` - Check if a file is in but ahead of a path: `Pathutil.new("/tmp/hello") > "/tmp" # => true` - `in_path?` - Check if a file is within a given path: `Pathutil.new("/tmp/hello").in_path?("/tmp") # => true` - `<=`, `<` - Check if a file is in but below a path: `Pathutil.new("/tmp") < "/tmp/hello" # => true` -- `read_yaml` - a wrapper around `Yaml.safe_load` and `SafeYAML` to make reading `YAML` easy. +- `read_yaml` - a wrapper around `Yaml.safe_load` to make reading `YAML` easy. - `children` - behaves like Pathname, except it accepts a block to work on the path. - `safe_copy` - Copy files, disallowing symlinks unless `in_path?` - `enforce_root` - Force a root if not already in that root. diff --git a/benchmark/yaml.rb b/benchmark/yaml.rb deleted file mode 100644 index d6b0812..0000000 --- a/benchmark/yaml.rb +++ /dev/null @@ -1,16 +0,0 @@ -# Frozen-string-literal: true -# Copyright: 2015 - 2017 Jordon Bedwell - MIT License -# Encoding: utf-8 - -require "bundler/setup" -require "safe_yaml/load" -require "benchmark/ips" -require "pathutil" - -data = "hello: world\nworld: hello" -Benchmark.ips :quiet => true do |x| - x.json! "benchmark.json" - x.report("A:SafeYAML.load") { SafeYAML.load(data) } - x.report("B:Pathutil::Helpers.load_yaml") { Pathutil::Helpers.load_yaml(data) } - x.compare! -end diff --git a/lib/pathutil/helpers.rb b/lib/pathutil/helpers.rb index e777c0d..8734690 100644 --- a/lib/pathutil/helpers.rb +++ b/lib/pathutil/helpers.rb @@ -20,7 +20,7 @@ class Pathutil end # -- - # Wraps around YAML and SafeYAML to provide alternatives to Rubies. + # Wraps around YAMLto provide alternatives to Rubies. # @note We default aliases to yes so we can detect if you explicit true. # @return Hash # -- @@ -34,20 +34,12 @@ class Pathutil ) end - if !YAML.respond_to?(:safe_load) - setup_safe_yaml whitelist_classes, aliases - SafeYAML.load( - data - ) - - else - YAML.safe_load( - data, - permitted_classes: whitelist_classes, - permitted_symbols: whitelist_symbols, - aliases: aliases - ) - end + YAML.safe_load( + data, + permitted_classes: whitelist_classes, + permitted_symbols: whitelist_symbols, + aliases: aliases + ) end # -- @@ -109,21 +101,5 @@ class Pathutil prefix, ext || "" ] end - - # -- - # Wrap around, cleanup, deprecate and use SafeYAML. - # rubocop:enable Style/ParallelAssignment - # -- - private - def setup_safe_yaml(whitelist_classes, aliases) - warn "WARN: SafeYAML does not support disabling of aliases." if aliases && aliases != :yes - warn "WARN: SafeYAML will be removed when Ruby 2.0 goes EOL." - require "safe_yaml/load" - - SafeYAML.restore_defaults! - whitelist_classes.map(&SafeYAML.method( - :whitelist_class! - )) - end end end diff --git a/spec/tests/lib/pathutil/helpers_spec.rb b/spec/tests/lib/pathutil/helpers_spec.rb index 0dfbc00..d82f0ac 100644 --- a/spec/tests/lib/pathutil/helpers_spec.rb +++ b/spec/tests/lib/pathutil/helpers_spec.rb @@ -35,59 +35,6 @@ describe Pathutil::Helpers do Psych::DisallowedClass ) end - - # - - context "when using SafeYAML" do - before do - allow(YAML).to receive(:respond_to?).with(:safe_load).and_return(false) - expect_any_instance_of(described_class).to receive(:warn).and_return( - nil - ) - end - - # - - context do - it "should warn it's deprecated" do - expect(described_class).to receive(:warn).and_return( - nil - ) - end - - # - - after do - described_class.load_yaml( - ":hello: :world" - ) - end - end - - # - - context "when trying to disable aliases" do - it "should warn that you cannot disable them in SafeYAML" do - expect(described_class).to receive(:warn).exactly(2).times.and_return( - nil - ) - end - - # - - after do - described_class.load_yaml("hello: world", aliases: true) - end - end - - # - - it "should parse with SafeYAML" do - expect(described_class.load_yaml(":hello: :world")).to eq({ - ":hello" => ":world" - }) - end - end end # -- 2.16.4