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