Format: text/x-python
⬇ Download File (2.2 KB)
   1  # coding: utf-8
   2  """Tests for IPython.core.application"""
   3  
   4  import os
   5  import tempfile
   6  
   7  from tempfile import TemporaryDirectory
   8  from traitlets import Unicode
   9  
  10  from IPython.core.application import BaseIPythonApplication
  11  from IPython.testing import decorators as dec
  12  
  13  
  14  @dec.onlyif_unicode_paths
  15  def test_unicode_cwd():
  16      """Check that IPython starts with non-ascii characters in the path."""
  17      wd = tempfile.mkdtemp(suffix=u"€")
  18      
  19      old_wd = os.getcwd()
  20      os.chdir(wd)
  21      #raise Exception(repr(os.getcwd()))
  22      try:
  23          app = BaseIPythonApplication()
  24          # The lines below are copied from Application.initialize()
  25          app.init_profile_dir()
  26          app.init_config_files()
  27          app.load_config_file(suppress_errors=False)
  28      finally:
  29          os.chdir(old_wd)
  30  
  31  @dec.onlyif_unicode_paths
  32  def test_unicode_ipdir():
  33      """Check that IPython starts with non-ascii characters in the IP dir."""
  34      ipdir = tempfile.mkdtemp(suffix=u"€")
  35      
  36      # Create the config file, so it tries to load it.
  37      with open(os.path.join(ipdir, "ipython_config.py"), "w", encoding="utf-8") as f:
  38          pass
  39      
  40      old_ipdir1 = os.environ.pop("IPYTHONDIR", None)
  41      old_ipdir2 = os.environ.pop("IPYTHON_DIR", None)
  42      os.environ["IPYTHONDIR"] = ipdir
  43      try:
  44          app = BaseIPythonApplication()
  45          # The lines below are copied from Application.initialize()
  46          app.init_profile_dir()
  47          app.init_config_files()
  48          app.load_config_file(suppress_errors=False)
  49      finally:
  50          if old_ipdir1:
  51              os.environ["IPYTHONDIR"] = old_ipdir1
  52          if old_ipdir2:
  53              os.environ["IPYTHONDIR"] = old_ipdir2
  54  
  55  def test_cli_priority():
  56      with TemporaryDirectory() as td:
  57  
  58          class TestApp(BaseIPythonApplication):
  59              test = Unicode().tag(config=True)
  60  
  61          # Create the config file, so it tries to load it.
  62          with open(os.path.join(td, "ipython_config.py"), "w", encoding="utf-8") as f:
  63              f.write("c.TestApp.test = 'config file'")
  64  
  65          app = TestApp()
  66          app.initialize(["--profile-dir", td])
  67          assert app.test == "config file"
  68          app = TestApp()
  69          app.initialize(["--profile-dir", td, "--TestApp.test=cli"])
  70          assert app.test == "cli"
  71