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