"Fossies" - the Fresh Open Source Software Archive

Member "http-prompt-2.1.0/tests/base.py" (5 Mar 2021, 1864 Bytes) of package /linux/www/http-prompt-2.1.0.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Python source code syntax highlighting (style: standard) with prefixed line numbers. Alternatively you can here view or download the uninterpreted source code file.

    1 import os
    2 import shutil
    3 import sys
    4 import tempfile
    5 import unittest
    6 
    7 import six
    8 
    9 
   10 class TempAppDirTestCase(unittest.TestCase):
   11     """Set up temporary app data and config directories before every test
   12     method, and delete them afterwards.
   13     """
   14     def setUp(self):
   15         # Create a temp dir that will contain data and config directories
   16         self.temp_dir = tempfile.mkdtemp()
   17 
   18         if sys.platform == 'win32':
   19             self.homes = {
   20                 # subdir_name: envvar_name
   21                 'data': 'LOCALAPPDATA',
   22                 'config': 'LOCALAPPDATA'
   23             }
   24         else:
   25             self.homes = {
   26                 # subdir_name: envvar_name
   27                 'data': 'XDG_DATA_HOME',
   28                 'config': 'XDG_CONFIG_HOME'
   29             }
   30 
   31         # Used to restore
   32         self.orig_envvars = {}
   33 
   34         for subdir_name, envvar_name in self.homes.items():
   35             if envvar_name in os.environ:
   36                 self.orig_envvars[envvar_name] = os.environ[envvar_name]
   37             os.environ[envvar_name] = os.path.join(self.temp_dir, subdir_name)
   38 
   39     def tearDown(self):
   40         # Restore envvar values
   41         for name in self.homes.values():
   42             if name in self.orig_envvars:
   43                 os.environ[name] = self.orig_envvars[name]
   44             else:
   45                 del os.environ[name]
   46 
   47         shutil.rmtree(self.temp_dir)
   48 
   49     def make_tempfile(self, data='', subdir_name=''):
   50         """Create a file under self.temp_dir and return the path."""
   51         full_tempdir = os.path.join(self.temp_dir, subdir_name)
   52         if not os.path.exists(full_tempdir):
   53             os.makedirs(full_tempdir)
   54 
   55         if isinstance(data, six.text_type):
   56             data = data.encode('utf-8')
   57 
   58         with tempfile.NamedTemporaryFile(dir=full_tempdir, delete=False) as f:
   59             f.write(data)
   60             return f.name