Skip to content

Commit 3bba07b

Browse files
committed
fix: ensure created_after_date is string before date comparison
Fixes #92 This will handle ensure the value is never None. If the string does not match %Y-%m-%d format it will throw a ValueError as expected. - [x] add tests to specifically cover case with string is 2020-01-01 Signed-off-by: jmeridth <[email protected]>
1 parent 8a2ec26 commit 3bba07b

File tree

3 files changed

+72
-15
lines changed

3 files changed

+72
-15
lines changed

env.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ def get_env_vars() -> tuple[
5454
str,
5555
str,
5656
str,
57-
str | None,
57+
str,
5858
bool,
5959
str,
6060
str | None,
@@ -184,7 +184,7 @@ def get_env_vars() -> tuple[
184184
else:
185185
commit_message = "Create dependabot.yaml"
186186

187-
created_after_date = os.getenv("CREATED_AFTER_DATE")
187+
created_after_date = os.getenv("CREATED_AFTER_DATE", "")
188188
# make sure that created_after_date is a date in the format YYYY-MM-DD
189189
if created_after_date and len(created_after_date) != 10:
190190
raise ValueError("CREATED_AFTER_DATE environment variable not in YYYY-MM-DD")

test_env.py

Lines changed: 48 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ def test_get_env_vars_optional_values(self):
140140
"Dependabot could be enabled for this repository. \
141141
Please enable it by merging this pull request so that \
142142
we can keep our dependencies up to date and secure.",
143-
None,
143+
"",
144144
False,
145145
"Create dependabot.yaml",
146146
None,
@@ -192,7 +192,7 @@ def test_get_env_vars_auth_with_github_app_installation(self):
192192
"Dependabot could be enabled for this repository. Please enable it by merging "
193193
"this pull request so that we can keep our dependencies up to date and "
194194
"secure.",
195-
None,
195+
"",
196196
False,
197197
"Create dependabot.yaml",
198198
None,
@@ -252,7 +252,7 @@ def test_get_env_vars_with_repos_no_dry_run(self):
252252
"Dependabot could be enabled for this repository. \
253253
Please enable it by merging this pull request so that \
254254
we can keep our dependencies up to date and secure.",
255-
None,
255+
"",
256256
False,
257257
"Create dependabot.yaml",
258258
None,
@@ -290,7 +290,7 @@ def test_get_env_vars_with_repos_disabled_security_updates(self):
290290
"Dependabot could be enabled for this repository. \
291291
Please enable it by merging this pull request so that \
292292
we can keep our dependencies up to date and secure.",
293-
None,
293+
"",
294294
False,
295295
"Create dependabot.yaml",
296296
None,
@@ -329,7 +329,7 @@ def test_get_env_vars_with_repos_filter_visibility_multiple_values(self):
329329
"Dependabot could be enabled for this repository. \
330330
Please enable it by merging this pull request so that \
331331
we can keep our dependencies up to date and secure.",
332-
None,
332+
"",
333333
False,
334334
"Create dependabot.yaml",
335335
None,
@@ -368,7 +368,7 @@ def test_get_env_vars_with_repos_filter_visibility_single_value(self):
368368
"Dependabot could be enabled for this repository. \
369369
Please enable it by merging this pull request so that \
370370
we can keep our dependencies up to date and secure.",
371-
None,
371+
"",
372372
False,
373373
"Create dependabot.yaml",
374374
None,
@@ -437,7 +437,7 @@ def test_get_env_vars_with_repos_filter_visibility_no_duplicates(self):
437437
"Dependabot could be enabled for this repository. \
438438
Please enable it by merging this pull request so that \
439439
we can keep our dependencies up to date and secure.",
440-
None,
440+
"",
441441
False,
442442
"Create dependabot.yaml",
443443
None,
@@ -477,7 +477,7 @@ def test_get_env_vars_with_repos_exempt_ecosystems(self):
477477
"Dependabot could be enabled for this repository. \
478478
Please enable it by merging this pull request so that \
479479
we can keep our dependencies up to date and secure.",
480-
None,
480+
"",
481481
False,
482482
"Create dependabot.yaml",
483483
None,
@@ -516,7 +516,7 @@ def test_get_env_vars_with_no_batch_size(self):
516516
"Dependabot could be enabled for this repository. \
517517
Please enable it by merging this pull request so that \
518518
we can keep our dependencies up to date and secure.",
519-
None,
519+
"",
520520
False,
521521
"Create dependabot.yaml",
522522
None,
@@ -556,7 +556,7 @@ def test_get_env_vars_with_batch_size(self):
556556
"Dependabot could be enabled for this repository. \
557557
Please enable it by merging this pull request so that \
558558
we can keep our dependencies up to date and secure.",
559-
None,
559+
"",
560560
False,
561561
"Create dependabot.yaml",
562562
None,
@@ -601,6 +601,44 @@ def test_get_env_vars_with_invalid_batch_size_str(self):
601601
with self.assertRaises(ValueError):
602602
get_env_vars()
603603

604+
@patch.dict(
605+
os.environ,
606+
{
607+
"ORGANIZATION": "my_organization",
608+
"GH_TOKEN": "my_token",
609+
"CREATED_AFTER_DATE": "2020-01-01",
610+
},
611+
clear=True,
612+
)
613+
def test_get_env_vars_with_created_after_date_of_2020_01_01(self):
614+
"""Test that filter_visibility is set correctly when there are duplicate values"""
615+
expected_result = (
616+
"my_organization",
617+
[],
618+
None,
619+
None,
620+
b"",
621+
"my_token",
622+
"",
623+
[],
624+
"pull",
625+
"Enable Dependabot",
626+
"Dependabot could be enabled for this repository. \
627+
Please enable it by merging this pull request so that \
628+
we can keep our dependencies up to date and secure.",
629+
"2020-01-01",
630+
False,
631+
"Create dependabot.yaml",
632+
None,
633+
False,
634+
["internal", "private", "public"],
635+
None,
636+
True,
637+
[],
638+
)
639+
result = get_env_vars()
640+
self.assertEqual(result, expected_result)
641+
604642

605643
if __name__ == "__main__":
606644
unittest.main()

test_evergreen.py

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ class TestIsRepoCreateDateBeforeCreatedAfterDate(unittest.TestCase):
584584
"""Test the is_repo_create_date_before_created_after_date function in evergreen.py"""
585585

586586
def test_is_repo_create_date_before_created_after_date(self):
587-
"""Test the repo.created_at date is before created_after_date."""
587+
"""Test the repo.created_at date is before created_after_date and has timezone."""
588588
repo_created_at = "2020-01-01T05:00:00Z"
589589
created_after_date = "2021-01-01"
590590

@@ -593,7 +593,7 @@ def test_is_repo_create_date_before_created_after_date(self):
593593
self.assertTrue(result)
594594

595595
def test_is_repo_create_date_is_after_created_after_date(self):
596-
"""Test the repo.created_at date is after created_after_date."""
596+
"""Test the repo.created_at date is after created_after_date and has timezone."""
597597
repo_created_at = "2022-01-01T05:00:00Z"
598598
created_after_date = "2021-01-01"
599599

@@ -602,7 +602,7 @@ def test_is_repo_create_date_is_after_created_after_date(self):
602602
self.assertFalse(result)
603603

604604
def test_is_repo_created_date_has_no_time_zone(self):
605-
"""Test the repo.created_at date is after created_after_date."""
605+
"""Test the repo.created_at date is before created_after_date with no timezone."""
606606
repo_created_at = "2020-01-01"
607607
created_after_date = "2021-01-01"
608608

@@ -619,6 +619,25 @@ def test_is_created_after_date_is_empty_string(self):
619619

620620
self.assertFalse(result)
621621

622+
def test_is_repo_created_date_is_before_created_after_date_without_timezene_again(
623+
self,
624+
):
625+
"""Test the repo.created_at date is before created_after_date without timezone again."""
626+
repo_created_at = "2018-01-01"
627+
created_after_date = "2020-01-01"
628+
629+
result = is_repo_created_date_before(repo_created_at, created_after_date)
630+
631+
self.assertTrue(result)
632+
633+
def test_is_repo_created_date_and_created_after_date_is_not_a_date(self):
634+
"""Test the repo.created_at date and the created_after_date argument is not a date."""
635+
repo_created_at = "2018-01-01"
636+
created_after_date = "Not a date"
637+
638+
with self.assertRaises(ValueError):
639+
is_repo_created_date_before(repo_created_at, created_after_date)
640+
622641

623642
if __name__ == "__main__":
624643
unittest.main()

0 commit comments

Comments
 (0)