| 1 |
# coding: utf-8 |
| 2 |
"""Tests for profile-related functions. |
| 3 |
|
| 4 |
Currently only the startup-dir functionality is tested, but more tests should |
| 5 |
be added for: |
| 6 |
|
| 7 |
* ipython profile create |
| 8 |
* ipython profile list |
| 9 |
* ipython profile create --parallel |
| 10 |
* security dir permissions |
| 11 |
|
| 12 |
Authors |
| 13 |
------- |
| 14 |
|
| 15 |
* MinRK |
| 16 |
|
| 17 |
""" |
| 18 |
|
| 19 |
# ----------------------------------------------------------------------------- |
| 20 |
# Imports |
| 21 |
# ----------------------------------------------------------------------------- |
| 22 |
|
| 23 |
import shutil |
| 24 |
import sys |
| 25 |
import tempfile |
| 26 |
from pathlib import Path |
| 27 |
from tempfile import TemporaryDirectory |
| 28 |
from unittest import TestCase |
| 29 |
|
| 30 |
import pytest |
| 31 |
|
| 32 |
from IPython.core.profileapp import list_bundled_profiles, list_profiles_in |
| 33 |
from IPython.core.profiledir import ProfileDir |
| 34 |
from IPython.testing import decorators as dec |
| 35 |
from IPython.testing import tools as tt |
| 36 |
from IPython.utils.process import getoutput |
| 37 |
|
| 38 |
# ----------------------------------------------------------------------------- |
| 39 |
# Globals |
| 40 |
# ----------------------------------------------------------------------------- |
| 41 |
TMP_TEST_DIR = Path(tempfile.mkdtemp()) |
| 42 |
HOME_TEST_DIR = TMP_TEST_DIR / "home_test_dir" |
| 43 |
IP_TEST_DIR = HOME_TEST_DIR / ".ipython" |
| 44 |
|
| 45 |
# |
| 46 |
# Setup/teardown functions/decorators |
| 47 |
# |
| 48 |
|
| 49 |
|
| 50 |
def setup_module(): |
| 51 |
"""Setup test environment for the module: |
| 52 |
|
| 53 |
- Adds dummy home dir tree |
| 54 |
""" |
| 55 |
# Do not mask exceptions here. In particular, catching WindowsError is a |
| 56 |
# problem because that exception is only defined on Windows... |
| 57 |
(Path.cwd() / IP_TEST_DIR).mkdir(parents=True) |
| 58 |
|
| 59 |
|
| 60 |
def teardown_module(): |
| 61 |
"""Teardown test environment for the module: |
| 62 |
|
| 63 |
- Remove dummy home dir tree |
| 64 |
""" |
| 65 |
# Note: we remove the parent test dir, which is the root of all test |
| 66 |
# subdirs we may have created. Use shutil instead of os.removedirs, so |
| 67 |
# that non-empty directories are all recursively removed. |
| 68 |
shutil.rmtree(TMP_TEST_DIR) |
| 69 |
|
| 70 |
|
| 71 |
# ----------------------------------------------------------------------------- |
| 72 |
# Test functions |
| 73 |
# ----------------------------------------------------------------------------- |
| 74 |
class ProfileStartupTest(TestCase): |
| 75 |
def setUp(self): |
| 76 |
# create profile dir |
| 77 |
self.pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, "test") |
| 78 |
self.options = ["--ipython-dir", IP_TEST_DIR, "--profile", "test"] |
| 79 |
self.fname = TMP_TEST_DIR / "test.py" |
| 80 |
|
| 81 |
def tearDown(self): |
| 82 |
# We must remove this profile right away so its presence doesn't |
| 83 |
# confuse other tests. |
| 84 |
shutil.rmtree(self.pd.location) |
| 85 |
|
| 86 |
def init(self, startup_file, startup, test): |
| 87 |
# write startup python file |
| 88 |
with open(Path(self.pd.startup_dir) / startup_file, "w", encoding="utf-8") as f: |
| 89 |
f.write(startup) |
| 90 |
# write simple test file, to check that the startup file was run |
| 91 |
with open(self.fname, "w", encoding="utf-8") as f: |
| 92 |
f.write(test) |
| 93 |
|
| 94 |
def validate(self, output): |
| 95 |
tt.ipexec_validate(self.fname, output, "", options=self.options) |
| 96 |
|
| 97 |
def test_startup_py(self): |
| 98 |
self.init("00-start.py", "zzz=123\n", "print(zzz)\n") |
| 99 |
self.validate("123") |
| 100 |
|
| 101 |
def test_startup_ipy(self): |
| 102 |
self.init("00-start.ipy", "%xmode plain\n", "") |
| 103 |
self.validate("Exception reporting mode: Plain") |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.skipif( |
| 107 |
sys.implementation.name == "pypy" |
| 108 |
and ((7, 3, 13) < sys.implementation.version < (7, 3, 16)), |
| 109 |
reason="Unicode issues with scandir on PyPy, see https://github.com/pypy/pypy/issues/4860", |
| 110 |
) |
| 111 |
def test_list_profiles_in(): |
| 112 |
# No need to remove these directories and files, as they will get nuked in |
| 113 |
# the module-level teardown. |
| 114 |
td = Path(tempfile.mkdtemp(dir=TMP_TEST_DIR)) |
| 115 |
for name in ("profile_foo", "profile_hello", "not_a_profile"): |
| 116 |
Path(td / name).mkdir(parents=True) |
| 117 |
if dec.unicode_paths: |
| 118 |
Path(td / "profile_ünicode").mkdir(parents=True) |
| 119 |
|
| 120 |
with open(td / "profile_file", "w", encoding="utf-8") as f: |
| 121 |
f.write("I am not a profile directory") |
| 122 |
profiles = list_profiles_in(td) |
| 123 |
|
| 124 |
# unicode normalization can turn u'ünicode' into u'u\0308nicode', |
| 125 |
# so only check for *nicode, and that creating a ProfileDir from the |
| 126 |
# name remains valid |
| 127 |
found_unicode = False |
| 128 |
for p in list(profiles): |
| 129 |
if p.endswith("nicode"): |
| 130 |
pd = ProfileDir.find_profile_dir_by_name(td, p) |
| 131 |
profiles.remove(p) |
| 132 |
found_unicode = True |
| 133 |
break |
| 134 |
if dec.unicode_paths: |
| 135 |
assert found_unicode is True |
| 136 |
assert set(profiles) == {"foo", "hello"} |
| 137 |
|
| 138 |
|
| 139 |
def test_list_bundled_profiles(): |
| 140 |
# This variable will need to be updated when a new profile gets bundled |
| 141 |
bundled = sorted(list_bundled_profiles()) |
| 142 |
assert bundled == [] |
| 143 |
|
| 144 |
|
| 145 |
def test_profile_create_ipython_dir(): |
| 146 |
"""ipython profile create respects --ipython-dir""" |
| 147 |
with TemporaryDirectory() as td: |
| 148 |
getoutput( |
| 149 |
[ |
| 150 |
sys.executable, |
| 151 |
"-m", |
| 152 |
"IPython", |
| 153 |
"profile", |
| 154 |
"create", |
| 155 |
"foo", |
| 156 |
"--ipython-dir=%s" % td, |
| 157 |
] |
| 158 |
) |
| 159 |
profile_dir = Path(td) / "profile_foo" |
| 160 |
assert Path(profile_dir).exists() |
| 161 |
ipython_config = profile_dir / "ipython_config.py" |
| 162 |
assert Path(ipython_config).exists() |