sanic-org / sanic-testing

Test clients for Sanic
https://sanic.dev/en/plugins/sanic-testing/getting-started.html
MIT License
31 stars 19 forks source link

test_mode automatically enabled if the package "sanic-testing" is present #40

Closed cnicodeme closed 1 year ago

cnicodeme commented 2 years ago

Describe the bug I was wondering why my code was returning True for testing when I wasn't testing it (it behave differently when testing, such as not really sending emails).

After looking, I found out that Sanic/app.py loop over the SANIC_PACKAGES list, which includes "sanic-testing", and if present, import them.

When importing Sanic testing, this package change the test_mode to True, which is not true until the Sanic Testing package has been instantiated. IMHO, it should only change the state to true when enabled, not when imported

Code snippet

app.py, line 1836

            for package_name in SANIC_PACKAGES:
                module_name = package_name.replace("-", "_")
                try:
                    module = import_module(module_name)
                    packages.append(f"{package_name}=={module.__version__}")
                except ImportError:
                    ...

Expected behavior Importing "sanic-testing" shouldn't change the state of Sanic (test_mode). Only when instantiating it.

Environment (please complete the following information):

cnicodeme commented 2 years ago

Disabling the motd avoid loading the package, and can be used as a quick and temporary fix.

app.run(motd=False)
ahopkins commented 2 years ago

Good catch. Moving the issue to where it should really be located.