timecop icon indicating copy to clipboard operation
timecop copied to clipboard

Using timecop with psych 4.0.5, behavior differs between the test environment and other environments

Open sanfrecce-osaka opened this issue 2 years ago • 0 comments

Steps to reproduce

Under the following conditions,

  • psych version: 4.0.5
  • timecop version: 0.9.5
  • ruby version: 3.1.2
  • rails version: 6.1.6.1

run the following.

$ bin/rails c
irb(main):001:0> YAML.unsafe_load('foo: 2020-12-30')
=> {"foo"=>Wed, 30 Dec 2020}

This is the expected result.

Next, run the following.

$ bin/rails c -e test
irb(main):001:0> YAML.unsafe_load('foo: 2020-12-30')
=> {"foo"=>"2020-12-30"}

I was expecting that value is Date as well, but it turned out to be String.

Expected behavior

Value is Date.

Actual behavior

Value is String.

Reason

Since psych 4.0.5, Psych::ScalarScanner#tokenize passes Date::GREGORIAN as the third argument of Date.strptime.

cf. https://github.com/ruby/psych/pull/573

However, when using timecop, Date.strptime_with_mock_date(This is an alias of Date.strptime) only allows Date::ITALY as the third argument.

cf. https://github.com/travisjeffery/timecop/blob/v0.9.5/lib/timecop/time_extensions.rb#L47-L51

So ArgumentError is raised and Psych::ScalarScanner#tokenize returns String.

cf. https://github.com/ruby/psych/blob/v4.0.5/lib/psych/scalar_scanner.rb#L63-L69

sanfrecce-osaka avatar Sep 11 '22 09:09 sanfrecce-osaka