##// END OF EJS Templates
Add test for list_profiles_in
Fernando Perez -
Show More
@@ -1,116 +1,133 b''
1 """Tests for profile-related functions.
1 """Tests for profile-related functions.
2
2
3 Currently only the startup-dir functionality is tested, but more tests should
3 Currently only the startup-dir functionality is tested, but more tests should
4 be added for:
4 be added for:
5
5
6 * ipython profile create
6 * ipython profile create
7 * ipython profile list
7 * ipython profile list
8 * ipython profile create --parallel
8 * ipython profile create --parallel
9 * security dir permissions
9 * security dir permissions
10
10
11 Authors
11 Authors
12 -------
12 -------
13
13
14 * MinRK
14 * MinRK
15
15
16 """
16 """
17 from __future__ import absolute_import
17 from __future__ import absolute_import
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 from nose import SkipTest
31 from nose import SkipTest
32
32
33 from IPython.core.profileapp import list_profiles_in, list_bundled_profiles
33 from IPython.core.profileapp import list_profiles_in, list_bundled_profiles
34 from IPython.core.profiledir import ProfileDir
34 from IPython.core.profiledir import ProfileDir
35
35
36 from IPython.testing import decorators as dec
36 from IPython.testing import decorators as dec
37 from IPython.testing import tools as tt
37 from IPython.testing import tools as tt
38 from IPython.utils import py3compat
38 from IPython.utils import py3compat
39
39
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 ProfileDirTest(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
94 # confuse other tests.
93 shutil.rmtree(self.pd.location)
95 shutil.rmtree(self.pd.location)
94
96
95 def init(self, startup_file, startup, test):
97 def init(self, startup_file, startup, test):
96 # write startup python file
98 # write startup python file
97 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:
98 f.write(startup)
100 f.write(startup)
99 # 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
100 with open(self.fname, 'w') as f:
102 with open(self.fname, 'w') as f:
101 f.write(py3compat.doctest_refactor_print(test))
103 f.write(py3compat.doctest_refactor_print(test))
102
104
103 def validate(self, output):
105 def validate(self, output):
104 # validate output
105 tt.ipexec_validate(self.fname, output, '', options=self.options)
106 tt.ipexec_validate(self.fname, output, '', options=self.options)
106
107
107 @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows")
108 @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows")
108 def test_startup_py(self):
109 def test_startup_py(self):
109 self.init('00-start.py', 'zzz=123\n',
110 self.init('00-start.py', 'zzz=123\n',
110 py3compat.doctest_refactor_print('print zzz\n'))
111 py3compat.doctest_refactor_print('print zzz\n'))
111 self.validate('123')
112 self.validate('123')
112
113
113 @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows")
114 @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows")
114 def test_startup_ipy(self):
115 def test_startup_ipy(self):
115 self.init('00-start.ipy', '%profile\n', '')
116 self.init('00-start.ipy', '%profile\n', '')
116 self.validate('test')
117 self.validate('test')
118
119
120 def test_list_profiles_in():
121 # No need to remove these directories and files, as they will get nuked in
122 # the module-level teardown.
123 prof_file = tempfile.NamedTemporaryFile(prefix='profile_', dir=IP_TEST_DIR)
124 prof_dir = tempfile.mkdtemp(prefix='profile_', dir=IP_TEST_DIR)
125 # Now, check that the profile listing doesn't get confused by files named
126 # profile_X
127 prof_name = os.path.split(prof_dir)[1].split('profile_')[1]
128 profiles = list_profiles_in(IP_TEST_DIR)
129 nt.assert_equals(profiles, [prof_name])
130
131
132 #def test_list_bundled_profiles():
133
General Comments 0
You need to be logged in to leave comments. Login now