| 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="€") |
| 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 |
|
| 32 |
@dec.onlyif_unicode_paths |
| 33 |
def test_unicode_ipdir(): |
| 34 |
"""Check that IPython starts with non-ascii characters in the IP dir.""" |
| 35 |
ipdir = tempfile.mkdtemp(suffix="€") |
| 36 |
|
| 37 |
# Create the config file, so it tries to load it. |
| 38 |
with open(os.path.join(ipdir, "ipython_config.py"), "w", encoding="utf-8") as f: |
| 39 |
pass |
| 40 |
|
| 41 |
old_ipdir1 = os.environ.pop("IPYTHONDIR", None) |
| 42 |
old_ipdir2 = os.environ.pop("IPYTHON_DIR", None) |
| 43 |
os.environ["IPYTHONDIR"] = ipdir |
| 44 |
try: |
| 45 |
app = BaseIPythonApplication() |
| 46 |
# The lines below are copied from Application.initialize() |
| 47 |
app.init_profile_dir() |
| 48 |
app.init_config_files() |
| 49 |
app.load_config_file(suppress_errors=False) |
| 50 |
finally: |
| 51 |
if old_ipdir1: |
| 52 |
os.environ["IPYTHONDIR"] = old_ipdir1 |
| 53 |
if old_ipdir2: |
| 54 |
os.environ["IPYTHONDIR"] = old_ipdir2 |
| 55 |
|
| 56 |
|
| 57 |
def test_cli_priority(): |
| 58 |
with TemporaryDirectory() as td: |
| 59 |
|
| 60 |
class TestApp(BaseIPythonApplication): |
| 61 |
test = Unicode().tag(config=True) |
| 62 |
|
| 63 |
# Create the config file, so it tries to load it. |
| 64 |
with open(os.path.join(td, "ipython_config.py"), "w", encoding="utf-8") as f: |
| 65 |
f.write("c.TestApp.test = 'config file'") |
| 66 |
|
| 67 |
app = TestApp() |
| 68 |
app.initialize(["--profile-dir", td]) |
| 69 |
assert app.test == "config file" |
| 70 |
app = TestApp() |
| 71 |
app.initialize(["--profile-dir", td, "--TestApp.test=cli"]) |
| 72 |
assert app.test == "cli" |