Skip to content

Commit

Permalink
Add support for Ruby 3.1 (#2635)
Browse files Browse the repository at this point in the history
* Add ci for Ruby 3.1

* Don't start SimpleCov more than once

In Ruby >= 3.1 it is an error to call `SimpleCov.start` more than once
(see simplecov-ruby/simplecov#1003).

This commit changes the shared configuration files to call
`SimpleCov.configure` instead of `SimpleCov.start`, to work around this
issue.

* Fix issue with dates in YAML config with Ruby >= 3.1

This commit fixes a issue #2614.

In middleman <= 4.4.3, if your YAML config file has something that looks
like an date (in ISO format), then you get the error

  YAML Exception ...: Tried to load unspecified class: Date

Including a properly formatted date in the frontmatter is a fairly
common use-case.

This happens Ruby 3.1 upgraded the included YAML library [Psych] to the
4.x branch, which changes `YAML.load` to call `Psych.safe_load` instead
of `Psych.unsafe_load`. This means it no longer parses dates unless it's
provided with `permitted_classes: [Date, ...]`.

The currently released versions of middleman (4.4.3 and 5.0.0-rc1) call
`YAML.load`, so the behavour depends on your ruby version.

This commit fixes things by using `YAML.safe_load` instead of
`YAML.load`, and allowing dates. Note that we have to do some
introspection of the arguments to YAML.safe_load to name the keyword
argument correctly, because in older versions of Ruby this argument had
a different name or wasn't a keyword argument [[1]], and we want to
retain support for those old versions.

[Psych]: https://github.com/ruby/psych

[1]: ruby/psych@682abf2
  • Loading branch information
lfdebrux committed Apr 12, 2023
1 parent 3812b03 commit df2b799
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ jobs:
name: ubuntu-ruby-${{ matrix.ruby-version }}
strategy:
matrix:
ruby-version: ["3.0", "2.7", "2.6", "2.5"]
ruby-version: ["3.1", "3.0", "2.7", "2.6", "2.5"]
steps:
- uses: actions/checkout@v2
- name: Set up Ruby
Expand Down
4 changes: 2 additions & 2 deletions middleman-cli/.simplecov
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SimpleCov.start do
SimpleCov.configure do
add_filter '/fixtures/'
add_filter '/features/'
add_filter '/spec/'
add_filter '/step_definitions/'
add_filter '/lib/vendored-middleman-deps/'
end
end
4 changes: 2 additions & 2 deletions middleman-core/.simplecov
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
SimpleCov.start do
SimpleCov.configure do
add_filter '/fixtures/'
add_filter '/features/'
add_filter '/spec/'
add_filter '/step_definitions/'
add_filter '/lib/vendored-middleman-deps/'
end
end
10 changes: 9 additions & 1 deletion middleman-core/lib/middleman-core/util/data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,17 @@ def build_regex(frontmatter_delims)
# @return [Hash]
Contract String, Pathname => Hash
def parse_yaml(content, full_path)
permitted_classes = [Date, Symbol]
c = begin
::Middleman::Util.instrument 'parse.yaml' do
::YAML.load(content)
allowed_parameters = ::YAML.method(:safe_load).parameters
if allowed_parameters.include? [:key, :permitted_classes]
::YAML.safe_load(content, permitted_classes: permitted_classes)
elsif allowed_parameters.include? [:key, :whitelist_classes]
::YAML.safe_load(content, whitelist_classes: permitted_classes)
else
::YAML.safe_load(content, permitted_classes)
end
end
rescue StandardError, ::Psych::SyntaxError => error
warn "YAML Exception parsing #{full_path}: #{error.message}"
Expand Down
4 changes: 2 additions & 2 deletions middleman/.simplecov
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
SimpleCov.start do
SimpleCov.configure do
add_filter '/fixtures/'
add_filter '/features/'
add_filter '/spec/'
end
end

0 comments on commit df2b799

Please sign in to comment.