Show More
@@ -1,3 +1,4 | |||||
|
1 | # coding: utf-8 | |||
1 | """Tests for profile-related functions. |
|
2 | """Tests for profile-related functions. | |
2 |
|
3 | |||
3 | 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 | |
@@ -25,9 +26,12 import shutil | |||||
25 | import sys |
|
26 | import sys | |
26 | import tempfile |
|
27 | import tempfile | |
27 |
|
28 | |||
|
29 | from unittest import TestCase | |||
|
30 | ||||
28 | import nose.tools as nt |
|
31 | import nose.tools as nt | |
29 | from nose import SkipTest |
|
32 | from nose import SkipTest | |
30 |
|
33 | |||
|
34 | from IPython.core.profileapp import list_profiles_in, list_bundled_profiles | |||
31 | from IPython.core.profiledir import ProfileDir |
|
35 | from IPython.core.profiledir import ProfileDir | |
32 |
|
36 | |||
33 | from IPython.testing import decorators as dec |
|
37 | from IPython.testing import decorators as dec | |
@@ -79,35 +83,69 def win32_without_pywin32(): | |||||
79 | return False |
|
83 | return False | |
80 |
|
84 | |||
81 |
|
85 | |||
82 | @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows") |
|
86 | class ProfileStartupTest(TestCase): | |
83 | def test_startup_py(): |
|
87 | def setUp(self): | |
84 | # create profile dir |
|
88 | # create profile dir | |
85 | pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, 'test') |
|
89 | self.pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, 'test') | |
86 | # write startup python file |
|
90 | self.options = ['--ipython-dir', IP_TEST_DIR, '--profile', 'test'] | |
87 | with open(os.path.join(pd.startup_dir, '00-start.py'), 'w') as f: |
|
91 | self.fname = os.path.join(TMP_TEST_DIR, 'test.py') | |
88 | f.write('zzz=123\n') |
|
92 | ||
89 | # write simple test file, to check that the startup file was run |
|
93 | def tearDown(self): | |
90 | fname = os.path.join(TMP_TEST_DIR, 'test.py') |
|
94 | # We must remove this profile right away so its presence doesn't | |
91 | with open(fname, 'w') as f: |
|
95 | # confuse other tests. | |
92 | f.write(py3compat.doctest_refactor_print('print zzz\n')) |
|
96 | shutil.rmtree(self.pd.location) | |
93 | # validate output |
|
97 | ||
94 | tt.ipexec_validate(fname, '123', '', |
|
98 | def init(self, startup_file, startup, test): | |
95 | options=['--ipython-dir', IP_TEST_DIR, '--profile', 'test']) |
|
99 | # write startup python file | |
96 |
|
100 | with open(os.path.join(self.pd.startup_dir, startup_file), 'w') as f: | ||
97 | @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows") |
|
101 | f.write(startup) | |
98 | def test_startup_ipy(): |
|
102 | # write simple test file, to check that the startup file was run | |
99 | # create profile dir |
|
103 | with open(self.fname, 'w') as f: | |
100 | pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, 'test') |
|
104 | f.write(py3compat.doctest_refactor_print(test)) | |
101 | # write startup ipython file |
|
105 | ||
102 | with open(os.path.join(pd.startup_dir, '00-start.ipy'), 'w') as f: |
|
106 | def validate(self, output): | |
103 | f.write('%profile\n') |
|
107 | tt.ipexec_validate(self.fname, output, '', options=self.options) | |
104 | # write empty script, because we don't need anything to happen |
|
108 | ||
105 | # after the startup file is run |
|
109 | @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows") | |
106 | fname = os.path.join(TMP_TEST_DIR, 'test.py') |
|
110 | def test_startup_py(self): | |
107 | with open(fname, 'w') as f: |
|
111 | self.init('00-start.py', 'zzz=123\n', | |
108 | f.write('') |
|
112 | py3compat.doctest_refactor_print('print zzz\n')) | |
109 | # validate output |
|
113 | self.validate('123') | |
110 | tt.ipexec_validate(fname, 'test', '', |
|
114 | ||
111 | options=['--ipython-dir', IP_TEST_DIR, '--profile', 'test']) |
|
115 | @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows") | |
|
116 | def test_startup_ipy(self): | |||
|
117 | self.init('00-start.ipy', '%profile\n', '') | |||
|
118 | self.validate('test') | |||
|
119 | ||||
112 |
|
120 | |||
|
121 | def test_list_profiles_in(): | |||
|
122 | # No need to remove these directories and files, as they will get nuked in | |||
|
123 | # the module-level teardown. | |||
|
124 | td = tempfile.mkdtemp(dir=TMP_TEST_DIR) | |||
|
125 | td = py3compat.str_to_unicode(td) | |||
|
126 | for name in ('profile_foo', u'profile_ünicode', 'profile_hello', | |||
|
127 | 'not_a_profile'): | |||
|
128 | os.mkdir(os.path.join(td, name)) | |||
|
129 | with open(os.path.join(td, 'profile_file'), 'w') as f: | |||
|
130 | f.write("I am not a profile directory") | |||
|
131 | profiles = list_profiles_in(td) | |||
113 |
|
132 | |||
|
133 | # unicode normalization can turn u'ünicode' into u'u\0308nicode', | |||
|
134 | # so only check for *nicode, and that creating a ProfileDir from the | |||
|
135 | # name remains valid | |||
|
136 | found_unicode = False | |||
|
137 | for p in list(profiles): | |||
|
138 | if p.endswith('nicode'): | |||
|
139 | pd = ProfileDir.find_profile_dir_by_name(td, p) | |||
|
140 | profiles.remove(p) | |||
|
141 | found_unicode = True | |||
|
142 | break | |||
|
143 | nt.assert_true(found_unicode) | |||
|
144 | nt.assert_equals(set(profiles), set(['foo', 'hello'])) | |||
|
145 | ||||
|
146 | ||||
|
147 | def test_list_bundled_profiles(): | |||
|
148 | # This variable will need to be updated when a new profile gets bundled | |||
|
149 | bundled_true = [u'cluster', u'math', u'pysh', u'python3', u'sympy'] | |||
|
150 | bundled = sorted(list_bundled_profiles()) | |||
|
151 | nt.assert_equals(bundled, bundled_true) |
General Comments 0
You need to be logged in to leave comments.
Login now