brightway-lca / brightway2-data

Tools for the management of inventory databases and impact assessment methods. Part of the Brightway LCA framework.
https://docs.brightway.dev/
BSD 3-Clause "New" or "Revised" License
11 stars 24 forks source link

Current test decoraters or base test class pollute project directory #148

Open cmutel opened 1 year ago

cmutel commented 1 year ago

All classes should create projects in temporary directories, but this isn't happening for some reason.

jsvgoncalves commented 5 months ago

Came here to create this exactly issue, the issue is contained here:

# bw2data/project.py
projects = ProjectManager()
# bw2data/tests.py

from .project import projects

@wrapt.decorator
def bw2test(wrapped, instance, args, kwargs):
    # …
    projects.change_base_directories(
        base_dir=tempdir, base_logs_dir=tempdir, project_name=project_name, update=False
    )
    projects._is_temp_dir = True
    # …

Since projects is a singleton, changing its state in the wrapper changes it for everyone. As a rule of thumb, always restore mocked or patched objects.

Probably the best way going forward is to refactor testing and use pytest's fixtures.

jsvgoncalves commented 5 months ago

Maybe this isn't the issue you tried to describe, but at least related.