Skip to content

Commit

Permalink
fix: ensure created_after_date is string before date comparison
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
jmeridth committed Apr 11, 2024
1 parent 8a2ec26 commit 47d9dcb
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 15 deletions.
4 changes: 2 additions & 2 deletions env.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_env_vars() -> tuple[
str,
str,
str,
str | None,
str,
bool,
str,
str | None,
Expand Down Expand Up @@ -184,7 +184,7 @@ def get_env_vars() -> tuple[
else:
commit_message = "Create dependabot.yaml"

created_after_date = os.getenv("CREATED_AFTER_DATE")
created_after_date = os.getenv("CREATED_AFTER_DATE", "")
# make sure that created_after_date is a date in the format YYYY-MM-DD
if created_after_date and len(created_after_date) != 10:
raise ValueError("CREATED_AFTER_DATE environment variable not in YYYY-MM-DD")
Expand Down
57 changes: 47 additions & 10 deletions test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_get_env_vars_optional_values(self):
"Dependabot could be enabled for this repository. \
Please enable it by merging this pull request so that \
we can keep our dependencies up to date and secure.",
None,
"",
False,
"Create dependabot.yaml",
None,
Expand Down Expand Up @@ -192,7 +192,7 @@ def test_get_env_vars_auth_with_github_app_installation(self):
"Dependabot could be enabled for this repository. Please enable it by merging "
"this pull request so that we can keep our dependencies up to date and "
"secure.",
None,
"",
False,
"Create dependabot.yaml",
None,
Expand Down Expand Up @@ -252,7 +252,7 @@ def test_get_env_vars_with_repos_no_dry_run(self):
"Dependabot could be enabled for this repository. \
Please enable it by merging this pull request so that \
we can keep our dependencies up to date and secure.",
None,
"",
False,
"Create dependabot.yaml",
None,
Expand Down Expand Up @@ -290,7 +290,7 @@ def test_get_env_vars_with_repos_disabled_security_updates(self):
"Dependabot could be enabled for this repository. \
Please enable it by merging this pull request so that \
we can keep our dependencies up to date and secure.",
None,
"",
False,
"Create dependabot.yaml",
None,
Expand Down Expand Up @@ -329,7 +329,7 @@ def test_get_env_vars_with_repos_filter_visibility_multiple_values(self):
"Dependabot could be enabled for this repository. \
Please enable it by merging this pull request so that \
we can keep our dependencies up to date and secure.",
None,
"",
False,
"Create dependabot.yaml",
None,
Expand Down Expand Up @@ -368,7 +368,7 @@ def test_get_env_vars_with_repos_filter_visibility_single_value(self):
"Dependabot could be enabled for this repository. \
Please enable it by merging this pull request so that \
we can keep our dependencies up to date and secure.",
None,
"",
False,
"Create dependabot.yaml",
None,
Expand Down Expand Up @@ -437,7 +437,7 @@ def test_get_env_vars_with_repos_filter_visibility_no_duplicates(self):
"Dependabot could be enabled for this repository. \
Please enable it by merging this pull request so that \
we can keep our dependencies up to date and secure.",
None,
"",
False,
"Create dependabot.yaml",
None,
Expand Down Expand Up @@ -477,7 +477,7 @@ def test_get_env_vars_with_repos_exempt_ecosystems(self):
"Dependabot could be enabled for this repository. \
Please enable it by merging this pull request so that \
we can keep our dependencies up to date and secure.",
None,
"",
False,
"Create dependabot.yaml",
None,
Expand Down Expand Up @@ -516,7 +516,7 @@ def test_get_env_vars_with_no_batch_size(self):
"Dependabot could be enabled for this repository. \
Please enable it by merging this pull request so that \
we can keep our dependencies up to date and secure.",
None,
"",
False,
"Create dependabot.yaml",
None,
Expand Down Expand Up @@ -556,7 +556,7 @@ def test_get_env_vars_with_batch_size(self):
"Dependabot could be enabled for this repository. \
Please enable it by merging this pull request so that \
we can keep our dependencies up to date and secure.",
None,
"",
False,
"Create dependabot.yaml",
None,
Expand Down Expand Up @@ -601,6 +601,43 @@ def test_get_env_vars_with_invalid_batch_size_str(self):
with self.assertRaises(ValueError):
get_env_vars()

@patch.dict(
os.environ,
{
"ORGANIZATION": "my_organization",
"GH_TOKEN": "my_token",
"CREATED_AFTER_DATE": "2020-01-01",
},
clear=True,
)
def test_get_env_vars_with_invalid_batch_size_str(self):
"""Test that filter_visibility is set correctly when there are duplicate values"""
expected_result = (
"my_organization",
[],
None,
None,
b"",
"my_token",
"",
[],
"pull",
"Enable Dependabot",
"Dependabot could be enabled for this repository. \
Please enable it by merging this pull request so that \
we can keep our dependencies up to date and secure.",
"2020-01-01",
False,
"Create dependabot.yaml",
None,
False,
["internal", "private", "public"],
None,
True,
[],
)
result = get_env_vars()
self.assertEqual(result, expected_result)

if __name__ == "__main__":
unittest.main()
23 changes: 20 additions & 3 deletions test_evergreen.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ class TestIsRepoCreateDateBeforeCreatedAfterDate(unittest.TestCase):
"""Test the is_repo_create_date_before_created_after_date function in evergreen.py"""

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

Expand All @@ -593,7 +593,7 @@ def test_is_repo_create_date_before_created_after_date(self):
self.assertTrue(result)

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

Expand All @@ -602,7 +602,7 @@ def test_is_repo_create_date_is_after_created_after_date(self):
self.assertFalse(result)

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

Expand All @@ -619,6 +619,23 @@ def test_is_created_after_date_is_empty_string(self):

self.assertFalse(result)

def test_is_repo_created_date_is_before_created_after_date_without_timezene_again(self):
"""Test the repo.created_at date is before created_after_date without timezone again."""
repo_created_at = "2018-01-01"
created_after_date = "2020-01-01"

result = is_repo_created_date_before(repo_created_at, created_after_date)

self.assertTrue(result)

def test_is_repo_created_date_and_created_after_date_is_not_a_date(self):
"""Test the repo.created_at date and the created_after_date argument is not a date."""
repo_created_at = "2018-01-01"
created_after_date = "Not a date"

with self.assertRaises(ValueError):
is_repo_created_date_before(repo_created_at, created_after_date)


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

0 comments on commit 47d9dcb

Please sign in to comment.