Skip to content

Commit

Permalink
Fixes #37579 - Define correct timezone behavior in unittest
Browse files Browse the repository at this point in the history
  • Loading branch information
dosas authored and adamruzicka committed Jun 26, 2024
1 parent fcad10f commit 12d8edd
Showing 1 changed file with 43 additions and 3 deletions.
46 changes: 43 additions & 3 deletions test/unit/options/normalizers_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,16 @@

end

def mock_env(partial_env_hash)
old_env = ENV.to_hash
ENV.update partial_env_hash
begin
yield
ensure
ENV.replace old_env
end
end

describe 'datetime' do

let(:formatter) { HammerCLI::Options::Normalizers::DateTime.new }
Expand All @@ -429,15 +439,45 @@
end

it "should accept and parse iso8601" do
_(formatter.format("1986-01-01T08:30:20")).must_equal("1986-01-01T08:30:20+00:00")
mock_env("TZ" => "UTC") do
_(formatter.format("1986-01-01T08:30:20")).must_equal("1986-01-01T08:30:20+00:00")
end
end

it "should accept and parse YYYY-MM-DD HH:MM:SS" do
_(formatter.format("1986-01-01 08:30:20")).must_equal("1986-01-01T08:30:20+00:00")
mock_env("TZ" => "UTC") do
_(formatter.format("1986-01-01 08:30:20")).must_equal("1986-01-01T08:30:20+00:00")
end
end

it "should accept and parse YYYY/MM/DD HH:MM:SS" do
_(formatter.format("1986/01/01 08:30:20")).must_equal("1986-01-01T08:30:20+00:00")
mock_env("TZ" => "UTC") do
_(formatter.format("1986/01/01 08:30:20")).must_equal("1986-01-01T08:30:20+00:00")
end
end

it "should parse iso8601 compliant timezones" do
mock_env("TZ" => "UTC") do
_(formatter.format("1986-01-01 08:30:20 +04:00")).must_equal("1986-01-01T08:30:20+04:00")
end
end

it "should not parse non iso8601 formatted timezones" do
mock_env("TZ" => "Asia/Tokyo") do
_(formatter.format("1986-01-01 08:30:20 foo")).must_equal("1986-01-01T08:30:20+09:00")
end
end

it "should use system provided timezone" do
mock_env("TZ" => "Asia/Tokyo") do
_(formatter.format("1986-01-01 08:30:20")).must_equal("1986-01-01T08:30:20+09:00")
end
end

it "should overwrite default timezone" do
mock_env("TZ" => "Asia/Tokyo") do
_(formatter.format("1986-01-01 08:30:20 +04:00")).must_equal("1986-01-01T08:30:20+04:00")
end
end

end
Expand Down

0 comments on commit 12d8edd

Please sign in to comment.