From 148f1aa9ac7a704fd5f5d986be82bdefdd337e2c Mon Sep 17 00:00:00 2001 From: Emre Erkunt Date: Wed, 2 Oct 2024 17:50:52 +0100 Subject: [PATCH] `fix`: Deprecated UTC time usage (#742) * `fix`: Deprecated UTC time usage * `fix`: Update on codeql * `ref`: Update on general mocking --- .github/workflows/codeql-analysis.yml | 6 +++--- .../extensions/override_radish_utctime.py | 11 +++++++++++ terraform_compliance/main.py | 3 +++ .../terraform_compliance/common/test_bdd_tags.py | 15 +++++++++------ 4 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 terraform_compliance/extensions/override_radish_utctime.py diff --git a/.github/workflows/codeql-analysis.yml b/.github/workflows/codeql-analysis.yml index b43bbba1..975656d3 100644 --- a/.github/workflows/codeql-analysis.yml +++ b/.github/workflows/codeql-analysis.yml @@ -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. @@ -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 @@ -52,4 +52,4 @@ jobs: # make release - name: Perform CodeQL Analysis - uses: github/codeql-action/analyze@v1 + uses: github/codeql-action/analyze@v2 diff --git a/terraform_compliance/extensions/override_radish_utctime.py b/terraform_compliance/extensions/override_radish_utctime.py new file mode 100644 index 00000000..073e5d83 --- /dev/null +++ b/terraform_compliance/extensions/override_radish_utctime.py @@ -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 \ No newline at end of file diff --git a/terraform_compliance/main.py b/terraform_compliance/main.py index fef47051..503ad25d 100644 --- a/terraform_compliance/main.py +++ b/terraform_compliance/main.py @@ -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() ## # diff --git a/tests/terraform_compliance/common/test_bdd_tags.py b/tests/terraform_compliance/common/test_bdd_tags.py index 3ae87229..c510b808 100644 --- a/tests/terraform_compliance/common/test_bdd_tags.py +++ b/tests/terraform_compliance/common/test_bdd_tags.py @@ -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') \ No newline at end of file