Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: skip date validation when env variable not set #144

Merged
merged 2 commits into from
Jul 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions env.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,19 @@ def get_int_env_var(env_var_name: str) -> int | None:
def validate_date_format(env_var_name: str) -> str:
"""Validate the date format of the environment variable.

Does nothing if the environment variable is not set.

Args:
env_var_name: The name of the environment variable to retrieve.

Returns:
The value of the environment variable as a string.
"""
date_to_validate = os.getenv(env_var_name, "")

if not date_to_validate:
return date_to_validate

pattern = "%Y-%m-%d"
try:
datetime.datetime.strptime(date_to_validate, pattern)
Expand Down
49 changes: 49 additions & 0 deletions test_env.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,55 @@ def test_get_env_vars_invalid_start_date(self):
"START_DATE environment variable not in the format YYYY-MM-DD",
)

@patch.dict(
os.environ,
{
"ORGANIZATION": "org",
"REPOSITORY": "repo,repo2",
"GH_APP_ID": "",
"GH_APP_INSTALLATION_ID": "",
"GH_APP_PRIVATE_KEY": "",
"GH_TOKEN": "token",
"GH_ENTERPRISE_URL": "",
"START_DATE": "",
"END_DATE": "",
"SPONSOR_INFO": "False",
"LINK_TO_PROFILE": "True",
},
clear=True,
)
def test_get_env_vars_no_dates(self):
"""
Test the get_env_vars function when all environment variables are set correctly
and start_date and end_date are not set.
"""

(
organization,
repository_list,
gh_app_id,
gh_app_installation_id,
gh_app_private_key_bytes,
token,
ghe,
start_date,
end_date,
sponsor_info,
link_to_profile,
) = env.get_env_vars()

self.assertEqual(organization, "org")
self.assertEqual(repository_list, ["repo", "repo2"])
self.assertIsNone(gh_app_id)
self.assertIsNone(gh_app_installation_id)
self.assertEqual(gh_app_private_key_bytes, b"")
self.assertEqual(token, "token")
self.assertEqual(ghe, "")
self.assertEqual(start_date, "")
self.assertEqual(end_date, "")
self.assertFalse(sponsor_info)
self.assertTrue(link_to_profile)


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