| 1 |
# (C) Copyright 2007-2025 Enthought, Inc., Austin, TX |
| 2 |
# All rights reserved. |
| 3 |
# |
| 4 |
# This software is provided without warranty under the terms of the BSD |
| 5 |
# license included in LICENSE.txt and may be redistributed only under |
| 6 |
# the conditions described in the aforementioned license. The license |
| 7 |
# is also available online at http://www.enthought.com/licenses/BSD.txt |
| 8 |
# |
| 9 |
# Thanks for using Enthought open source! |
| 10 |
|
| 11 |
import os |
| 12 |
import shutil |
| 13 |
import tempfile |
| 14 |
|
| 15 |
|
| 16 |
class ETSConfigPatcher(object): |
| 17 |
""" |
| 18 |
Object that patches the directories in ETSConfig, to avoid having |
| 19 |
tests write to the home directory. |
| 20 |
""" |
| 21 |
|
| 22 |
def __init__(self): |
| 23 |
from traits.etsconfig.api import ETSConfig |
| 24 |
|
| 25 |
self.etsconfig = ETSConfig |
| 26 |
|
| 27 |
self.tmpdir = None |
| 28 |
self.old_application_data = None |
| 29 |
self.old_application_home = None |
| 30 |
self.old_user_data = None |
| 31 |
|
| 32 |
def start(self): |
| 33 |
tmpdir = self.tmpdir = tempfile.mkdtemp() |
| 34 |
|
| 35 |
self.old_application_data = self.etsconfig._application_data |
| 36 |
self.etsconfig._application_data = os.path.join( |
| 37 |
tmpdir, "application_data" |
| 38 |
) |
| 39 |
|
| 40 |
self.old_application_home = self.etsconfig._application_home |
| 41 |
self.etsconfig._application_home = os.path.join( |
| 42 |
tmpdir, "application_home" |
| 43 |
) |
| 44 |
|
| 45 |
self.old_user_data = self.etsconfig._user_data |
| 46 |
self.etsconfig._user_data = os.path.join(tmpdir, "user_home") |
| 47 |
|
| 48 |
def stop(self): |
| 49 |
if self.old_user_data is not None: |
| 50 |
self.etsconfig._user_data = self.old_user_data |
| 51 |
self.old_user_data = None |
| 52 |
|
| 53 |
if self.old_application_home is not None: |
| 54 |
self.etsconfig._application_home = self.old_application_home |
| 55 |
self.old_application_home = None |
| 56 |
|
| 57 |
if self.old_application_data is not None: |
| 58 |
self.etsconfig._application_data = self.old_application_data |
| 59 |
self.old_application_data = None |
| 60 |
|
| 61 |
if self.tmpdir is not None: |
| 62 |
shutil.rmtree(self.tmpdir) |
| 63 |
self.tmpdir = None |