Repository: django/django
File: tests/admin_scripts/tests.py (Python)

Code before:
            if manage_py
            else os.path.join(
                os.path.dirname(conf.__file__), "project_template", "manage.py-tpl"
            )
        )
        test_manage_py = os.path.join(self.test_dir, "manage.py")
        shutil.copyfile(template_manage_py, test_manage_py)

        with open(test_manage_py) as fp:
            manage_py_contents = fp.read()
        manage_py_contents = manage_py_contents.replace(
            "{{ project_name }}", "test_project"
        )
        with open(test_manage_py, "w") as fp:
            fp.write(manage_py_contents)

        return self.run_test(["./manage.py", *args], settings_file)

    def assertInAfterFormatting(self, member, container, msg=None):
        if HAS_BLACK:
            import black

            # Black does not have a stable API, but this is still less
            # fragile than attempting to filter out containing paths.
            member = black.format_str(member, mode=black.FileMode())
            container = black.format_str(container, mode=black.FileMode())

        self.assertIn(member, container, msg=msg)

    def assertNoOutput(self, stream):
        "Utility assertion: assert that the given stream is empty"
        self.assertEqual(
            len(stream), 0, "Stream should be empty: actually contains '%s'" % stream
        )

    def assertOutput(self, stream, msg, regex=False):
        "Utility assertion: assert that the given message exists in the output"
        if regex:
            self.assertIsNotNone(
                re.search(msg, stream),
                "'%s' does not match actual output text '%s'" % (msg, stream),
            )
        else:
            self.assertIn(
                msg,
                stream,
                "'%s' does not match actual output text '%s'" % (msg, stream),
            )

    def assertNotInOutput(self, stream, msg):
        """

Review comment (bug):
My original suggestion is to only format the `member` since `container` is already formatted by the executing management command.

Moreover, I would rename this helper to `assertInOutput` and use it consistently to replace "most" `assertIn` calls, so the current (and future) failing tests, like `test_custom_project_template_context_variables`,  are brittle no more:
```
======================================================================
FAIL: test_custom_project_template_context_variables (admin_scripts.tests.StartProject.test_custom_project_template_context_variables)
Make sure template context variables are rendered with proper values
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/unittest/case.py", line 58, in testPartExecutor
    yield
  File "/opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/unittest/case.py", line 669, in run
    self._callTestMethod(testMethod)
    
  File "/opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/unittest/case.py", line 615, in _callTestMethod
    result = method()
    ^^^^^^^^^^^^^^^
  File "/home/runner/work/django/django/tests/admin_scripts/tests.py", line 2865, in test_custom_project_template_context_variables
    self.assertIn('project_directory = "%s"' % testproject_dir, content)
    ^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/unittest/case.py", line 1192, in assertIn
    self.fail(self._formatMessage(msg, standardMsg))
    ^^^^^^^^^^^
  File "/opt/hostedtoolcache/Python/3.14.0/x64/lib/python3.14/unittest/case.py", line 750, in fail
    raise self.failureException(msg)
    ^^^^^^^^^^^^^^^
AssertionError: 'project_directory = "/tmp/django_guy55sry/django_lmr2wqmu/tmp1ypnoyz2/test_project/project_dir"' not found in '# The manage.py of the another_project test project\n\n# template context:\nproject_name = "another_project"\nproject_directory = (\n    "/tmp/django_guy55sry/django_lmr2wqmu/tmp1ypnoyz2/test_project/project_dir"\n)\nsecret_key = "django-insecure-ld1&pwsm40q-$r$p*cf03(g#&0hwhpunw3dnv*$#_-qs85j0z9"\n'
```

Code after:
            if manage_py
            else os.path.join(
                os.path.dirname(conf.__file__), "project_template", "manage.py-tpl"
            )
        )
        test_manage_py = os.path.join(self.test_dir, "manage.py")
        shutil.copyfile(template_manage_py, test_manage_py)

        with open(test_manage_py) as fp:
            manage_py_contents = fp.read()
        manage_py_contents = manage_py_contents.replace(
            "{{ project_name }}", "test_project"
        )
        with open(test_manage_py, "w") as fp:
            fp.write(manage_py_contents)

        return self.run_test(["./manage.py", *args], settings_file)

    def assertInAfterFormatting(self, member, container, msg=None):
        if HAS_BLACK:
            import black

            # Black does not have a stable API, but this is still less fragile
            # than attempting to filter out all paths where it is available.
            member = black.format_str(member, mode=black.FileMode())

        self.assertIn(member, container, msg=msg)

    def assertNoOutput(self, stream):
        "Utility assertion: assert that the given stream is empty"
        self.assertEqual(
            len(stream), 0, "Stream should be empty: actually contains '%s'" % stream
        )

    def assertOutput(self, stream, msg, regex=False):
        "Utility assertion: assert that the given message exists in the output"
        if regex:
            self.assertIsNotNone(
                re.search(msg, stream),
                "'%s' does not match actual output text '%s'" % (msg, stream),
            )
        else:
            self.assertIn(
                msg,
                stream,
                "'%s' does not match actual output text '%s'" % (msg, stream),
            )

    def assertNotInOutput(self, stream, msg):
        """
        Utility assertion: assert that the given message doesn't exist in the