From fea99995e100cfd7f0d7fc52bb0b7bd5b51525ff Mon Sep 17 00:00:00 2001 From: Emre Erkunt Date: Wed, 2 Oct 2024 16:12:52 +0100 Subject: [PATCH 1/3] `fix`: Deprecated UTC time usage --- terraform_compliance/extensions/override_radish_utctime.py | 4 ++++ terraform_compliance/main.py | 4 ++++ 2 files changed, 8 insertions(+) create mode 100644 terraform_compliance/extensions/override_radish_utctime.py diff --git a/terraform_compliance/extensions/override_radish_utctime.py b/terraform_compliance/extensions/override_radish_utctime.py new file mode 100644 index 00000000..0961a6ec --- /dev/null +++ b/terraform_compliance/extensions/override_radish_utctime.py @@ -0,0 +1,4 @@ +import datetime + +def current_utc_time(): + return datetime.datetime.now(datetime.timezone.utc) diff --git a/terraform_compliance/main.py b/terraform_compliance/main.py index fef47051..d288e92e 100644 --- a/terraform_compliance/main.py +++ b/terraform_compliance/main.py @@ -22,8 +22,12 @@ 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 current_utc_time from radish import errororacle +import radish.extensions.time_recorder + errororacle.handle_exception = handle_exception_override +radish.extensions.time_recorder.datetime.utcnow = current_utc_time ## # From 93a56466a1d90514a5fdc05329aebdbe6f36f72f Mon Sep 17 00:00:00 2001 From: Emre Erkunt Date: Wed, 2 Oct 2024 16:35:11 +0100 Subject: [PATCH 2/3] `fix`: Update on codeql --- .github/workflows/codeql-analysis.yml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 From e9e082014c83a4dbb69496c57d49efc43f93d952 Mon Sep 17 00:00:00 2001 From: Emre Erkunt Date: Wed, 2 Oct 2024 17:31:51 +0100 Subject: [PATCH 3/3] `ref`: Update on general mocking --- .../extensions/override_radish_utctime.py | 7 +++++++ terraform_compliance/main.py | 5 ++--- .../terraform_compliance/common/test_bdd_tags.py | 15 +++++++++------ 3 files changed, 18 insertions(+), 9 deletions(-) diff --git a/terraform_compliance/extensions/override_radish_utctime.py b/terraform_compliance/extensions/override_radish_utctime.py index 0961a6ec..073e5d83 100644 --- a/terraform_compliance/extensions/override_radish_utctime.py +++ b/terraform_compliance/extensions/override_radish_utctime.py @@ -1,4 +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 d288e92e..503ad25d 100644 --- a/terraform_compliance/main.py +++ b/terraform_compliance/main.py @@ -22,12 +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 current_utc_time +from terraform_compliance.extensions.override_radish_utctime import apply_utctime_patch from radish import errororacle -import radish.extensions.time_recorder errororacle.handle_exception = handle_exception_override -radish.extensions.time_recorder.datetime.utcnow = current_utc_time +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