Skip to content

Commit

Permalink
fix: Deprecated UTC time usage (#742)
Browse files Browse the repository at this point in the history
* `fix`: Deprecated UTC time usage

* `fix`: Update on codeql

* `ref`: Update on general mocking
  • Loading branch information
eerkunt authored Oct 2, 2024
1 parent 4a71a5b commit 148f1aa
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 9 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/codeql-analysis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:

# Initializes the CodeQL tools for scanning.
- name: Initialize CodeQL
uses: github/codeql-action/init@v1
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
# If you wish to specify custom queries, you can do so here or in a config file.
Expand All @@ -38,7 +38,7 @@ jobs:
# Autobuild attempts to build any compiled languages (C/C++, C#, or Java).
# If this step fails, then you should remove it and run the build manually (see below)
- name: Autobuild
uses: github/codeql-action/autobuild@v1
uses: github/codeql-action/autobuild@v2

# ℹ️ Command-line programs to run using the OS shell.
# 📚 https://git.io/JvXDl
Expand All @@ -52,4 +52,4 @@ jobs:
# make release

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v1
uses: github/codeql-action/analyze@v2
11 changes: 11 additions & 0 deletions terraform_compliance/extensions/override_radish_utctime.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import radish.extensions.time_recorder
import datetime
import mock

def current_utc_time():
return datetime.datetime.now(datetime.timezone.utc)

def apply_utctime_patch():
# Patch datetime specifically within radish.extensions.time_recorder
radish.extensions.time_recorder.datetime = mock.Mock()
radish.extensions.time_recorder.datetime.utcnow = current_utc_time
3 changes: 3 additions & 0 deletions terraform_compliance/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@
Step.run = StepOverride.run

from terraform_compliance.extensions.override_radish_hookerrors import handle_exception as handle_exception_override
from terraform_compliance.extensions.override_radish_utctime import apply_utctime_patch
from radish import errororacle

errororacle.handle_exception = handle_exception_override
apply_utctime_patch()
##
#

Expand Down
15 changes: 9 additions & 6 deletions tests/terraform_compliance/common/test_bdd_tags.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,23 @@
from unittest import TestCase
from unittest import TestCase, mock
from terraform_compliance.common.bdd_tags import look_for_bdd_tags
from terraform_compliance.common.exceptions import Failure
from terraform_compliance.extensions.override_radish_utctime import current_utc_time
from tests.mocks import MockedStep, MockedTags


class TestBddTags(TestCase):

def test_unchanged_step_object(self):
@mock.patch('radish.extensions.time_recorder.datetime')
def test_unchanged_step_object(self, mock_datetime):
mock_datetime.utcnow.side_effect = current_utc_time # Patches within radish
step = MockedStep()
look_for_bdd_tags(step)
self.assertFalse(step.context.no_failure)
self.assertIsNone(step.context.failure_class)

def test_warning_case(self):
@mock.patch('radish.extensions.time_recorder.datetime')
def test_warning_case(self, mock_datetime):
mock_datetime.utcnow.side_effect = current_utc_time # Patches within radish
step = MockedStep()
step.all_tags = [MockedTags(name='warning')]
look_for_bdd_tags(step)
self.assertTrue(step.context.no_failure)
self.assertEqual(step.context.failure_class, 'warning')
self.assertEqual(step.context.failure_class, 'warning')

0 comments on commit 148f1aa

Please sign in to comment.