import functools
import os.path
import shutil
import sys
import tempfile

import pytest


@pytest.fixture(scope="function")
def HOME(tmpdir):
    home = os.path.join(tmpdir, 'john')
    os.mkdir(home)
    # NOTE: homely._utils makes use of os.environ['HOME'], so we need to
    # destroy any homely modules that may have imported things based on this.
    # Essentially we blast away the entire module and reload it from scratch.
    for name in list(sys.modules.keys()):
        if name.startswith('homely.'):
            sys.modules.pop(name, None)
    os.environ['HOME'] = home
    return home


@pytest.fixture(scope="function")
def tmpdir(request):
    path = tempfile.mkdtemp()
    destructor = shutil.rmtree

    def destructor(path):
        print("rm -rf %s" % path)
        shutil.rmtree(path)
    request.addfinalizer(functools.partial(destructor, path))
    return os.path.realpath(path)

import functools
import os.path
import shutil
import sys
import tempfile

import pytest


@pytest.fixture(scope="function")
def HOME(tmpdir):
    old_home = os.environ['HOME']

    try:
        home = os.path.join(tmpdir, 'john')
        os.mkdir(home)
        # NOTE: homely._utils makes use of os.environ['HOME'], so we need to
        # destroy any homely modules that may have imported things based on this.
        # Essentially we blast away the entire module and reload it from scratch.
        for name in list(sys.modules.keys()):
            if name.startswith('homely.'):
                sys.modules.pop(name, None)
        os.environ['HOME'] = home
        yield home
    finally:
        os.environ['HOME'] = old_home


@pytest.fixture(scope="function")
def tmpdir(request):
    path = tempfile.mkdtemp()
    destructor = shutil.rmtree

    def destructor(path):
        print("rm -rf %s" % path)
        shutil.rmtree(path)
    request.addfinalizer(functools.partial(destructor, path))
    return os.path.realpath(path)

Rework HOME fixture so it doesn't leave os.environ corrupted
Rework HOME fixture so it doesn't leave os.environ corrupted

  import functools
  import os.path
  import shutil
  import sys
  import tempfile
  
  import pytest
  
  
  @pytest.fixture(scope="function")
  def HOME(tmpdir):
+     old_home = os.environ['HOME']
+ 
+     try:
-     home = os.path.join(tmpdir, 'john')
+         home = os.path.join(tmpdir, 'john')
-     os.mkdir(home)
+         os.mkdir(home)
-     # NOTE: homely._utils makes use of os.environ['HOME'], so we need to
+         # NOTE: homely._utils makes use of os.environ['HOME'], so we need to
-     # destroy any homely modules that may have imported things based on this.
+         # destroy any homely modules that may have imported things based on this.
-     # Essentially we blast away the entire module and reload it from scratch.
+         # Essentially we blast away the entire module and reload it from scratch.
-     for name in list(sys.modules.keys()):
+         for name in list(sys.modules.keys()):
-         if name.startswith('homely.'):
+             if name.startswith('homely.'):
-             sys.modules.pop(name, None)
+                 sys.modules.pop(name, None)
-     os.environ['HOME'] = home
+         os.environ['HOME'] = home
-     return home
+         yield home
+     finally:
+         os.environ['HOME'] = old_home
  
  
  @pytest.fixture(scope="function")
  def tmpdir(request):
      path = tempfile.mkdtemp()
      destructor = shutil.rmtree
  
      def destructor(path):
          print("rm -rf %s" % path)
          shutil.rmtree(path)
      request.addfinalizer(functools.partial(destructor, path))
      return os.path.realpath(path)
  
Rework HOME fixture so it doesn't leave os.environ corrupted
## Code Before:
import functools
import os.path
import shutil
import sys
import tempfile

import pytest


@pytest.fixture(scope="function")
def HOME(tmpdir):
    home = os.path.join(tmpdir, 'john')
    os.mkdir(home)
    # NOTE: homely._utils makes use of os.environ['HOME'], so we need to
    # destroy any homely modules that may have imported things based on this.
    # Essentially we blast away the entire module and reload it from scratch.
    for name in list(sys.modules.keys()):
        if name.startswith('homely.'):
            sys.modules.pop(name, None)
    os.environ['HOME'] = home
    return home


@pytest.fixture(scope="function")
def tmpdir(request):
    path = tempfile.mkdtemp()
    destructor = shutil.rmtree

    def destructor(path):
        print("rm -rf %s" % path)
        shutil.rmtree(path)
    request.addfinalizer(functools.partial(destructor, path))
    return os.path.realpath(path)

## Instruction:
Rework HOME fixture so it doesn't leave os.environ corrupted
## Code After:
import functools
import os.path
import shutil
import sys
import tempfile

import pytest


@pytest.fixture(scope="function")
def HOME(tmpdir):
    old_home = os.environ['HOME']

    try:
        home = os.path.join(tmpdir, 'john')
        os.mkdir(home)
        # NOTE: homely._utils makes use of os.environ['HOME'], so we need to
        # destroy any homely modules that may have imported things based on this.
        # Essentially we blast away the entire module and reload it from scratch.
        for name in list(sys.modules.keys()):
            if name.startswith('homely.'):
                sys.modules.pop(name, None)
        os.environ['HOME'] = home
        yield home
    finally:
        os.environ['HOME'] = old_home


@pytest.fixture(scope="function")
def tmpdir(request):
    path = tempfile.mkdtemp()
    destructor = shutil.rmtree

    def destructor(path):
        print("rm -rf %s" % path)
        shutil.rmtree(path)
    request.addfinalizer(functools.partial(destructor, path))
    return os.path.realpath(path)
