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