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