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