From d279e4ea203a76c5df3ef306fa9c056090630b16 Mon Sep 17 00:00:00 2001 From: Brent O'Connor Date: Sat, 2 Nov 2024 18:13:00 -0500 Subject: [PATCH] Switch to using a tuple for the version in an assertion --- tests/test_new_command.py | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/tests/test_new_command.py b/tests/test_new_command.py index 01a6ae0..afec6fc 100644 --- a/tests/test_new_command.py +++ b/tests/test_new_command.py @@ -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", @@ -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()], } @@ -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 @@ -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}"