Commit d702a2e3 authored by Shinya Maeda's avatar Shinya Maeda

Add rubocop rule for preventing change timezone

This commit adds rubocop rule for preventing change timezone
parent 3ded5e45
...@@ -200,6 +200,11 @@ Gitlab/ConstGetInheritFalse: ...@@ -200,6 +200,11 @@ Gitlab/ConstGetInheritFalse:
Exclude: Exclude:
- 'qa/bin/*' - 'qa/bin/*'
Gitlab/ChangeTimezone:
Enabled: true
Exclude:
- config/initializers/time_zone.rb
Gitlab/HTTParty: Gitlab/HTTParty:
Enabled: true Enabled: true
Exclude: Exclude:
......
# frozen_string_literal: true
module RuboCop
module Cop
module Gitlab
class ChangeTimezone < RuboCop::Cop::Cop
MSG = "Do not change timezone in the runtime (application or rspec), " \
"it could result in silently modifying other behavior.".freeze
def_node_matcher :changing_timezone?, <<~PATTERN
(send (const nil? :Time) :zone= ...)
PATTERN
def on_send(node)
changing_timezone?(node) { add_offense(node) }
end
end
end
end
end
...@@ -90,8 +90,11 @@ describe ApplicationHelper do ...@@ -90,8 +90,11 @@ describe ApplicationHelper do
end end
describe 'time_ago_with_tooltip' do describe 'time_ago_with_tooltip' do
around do |example|
Time.use_zone('UTC') { example.run }
end
def element(*arguments) def element(*arguments)
Time.zone = 'UTC'
@time = Time.zone.parse('2015-07-02 08:23') @time = Time.zone.parse('2015-07-02 08:23')
element = helper.time_ago_with_tooltip(@time, *arguments) element = helper.time_ago_with_tooltip(@time, *arguments)
......
# frozen_string_literal: true
require 'spec_helper'
require 'rubocop'
require 'rubocop/rspec/support'
require_relative '../../../../rubocop/cop/gitlab/change_timzone'
describe RuboCop::Cop::Gitlab::ChangeTimezone do
include CopHelper
subject(:cop) { described_class.new }
context 'Time.zone=' do
it 'registers an offense with no 2nd argument' do
expect_offense(<<~PATTERN.strip_indent)
Time.zone = 'Awkland'
^^^^^^^^^^^^^^^^^^^^^ Do not change timezone in the runtime (application or rspec), it could result in silently modifying other behavior.
PATTERN
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment