Skip to content

Commit

Permalink
Switch to using a tuple for the version in an assertion
Browse files Browse the repository at this point in the history
  • Loading branch information
epicserve committed Nov 2, 2024
1 parent a9e41ce commit d279e4e
Showing 1 changed file with 9 additions and 6 deletions.
15 changes: 9 additions & 6 deletions tests/test_new_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@
'SECRET_KEY="{{ secret_key }}"',
"ALLOWED_HOSTS=",
"DATABASE_URL=sqlite:///{{ project_dir }}/db.sqlite3",
"{% 5.1 %}" + SQLITE_OPTIONS_ENV,
# If Django version is 5.1 or higher, the following option should be present in the DATABASE_URL
("5.1", SQLITE_OPTIONS_ENV),
],
"config/settings.py": [
"from environs import Env",
Expand All @@ -79,7 +80,8 @@
],
}
NO_ENV_ASSERTIONS = {
"config/settings.py": ["{% 5.1 %}" + option for option in SQLITE_OPTIONS.splitlines()],
# If Django version is 5.1 or higher, the following option should be present in the DATABASE_URL
"config/settings.py": [("5.1", option) for option in SQLITE_OPTIONS.splitlines()],
}


Expand Down Expand Up @@ -142,6 +144,11 @@ def assert_files_are_correct(
with open(file) as f:
content = f.read()
for assertion_pattern in assertions:
version_str = None
if isinstance(assertion_pattern, list | tuple) is True:
version_str, assertion_pattern = assertion_pattern
if version_str and Version(template_context["django_version"]) < Version(version_str):
continue
if (
assertion_pattern.startswith("SECRET_KEY =")
and initialize_env is True
Expand All @@ -151,10 +158,6 @@ def assert_files_are_correct(
if re.match(r".*{{\s[_a-z]+\s}}.*", assertion_pattern):
formatted_assertion = assertion_pattern.replace("{{ ", "{").replace(" }}", "}")
assertion = formatted_assertion.format_map(SafeDict(assertion_context))
elif rematch := re.match(r".*{%\s(.*)\s%}.*", assertion_pattern):
if Version(template_context["django_version"]) < Version(rematch.group(1)):
continue
assertion = assertion_pattern.replace(f"{{% {rematch.group(1)} %}}", "")
else:
assertion = assertion_pattern
assert assertion in content, f"Assertion failed for {relative_path}: {assertion}"
Expand Down

0 comments on commit d279e4e

Please sign in to comment.