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