##// END OF EJS Templates
Refactor tests to fix leftover profiles that would collide with other tests.
Fernando Perez -
Show More
@@ -1,113 +1,116 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
29
28 import nose.tools as nt
30 import nose.tools as nt
29 from nose import SkipTest
31 from nose import SkipTest
30
32
33 from IPython.core.profileapp import list_profiles_in, list_bundled_profiles
31 from IPython.core.profiledir import ProfileDir
34 from IPython.core.profiledir import ProfileDir
32
35
33 from IPython.testing import decorators as dec
36 from IPython.testing import decorators as dec
34 from IPython.testing import tools as tt
37 from IPython.testing import tools as tt
35 from IPython.utils import py3compat
38 from IPython.utils import py3compat
36
39
37
40
38 #-----------------------------------------------------------------------------
41 #-----------------------------------------------------------------------------
39 # Globals
42 # Globals
40 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
41 TMP_TEST_DIR = tempfile.mkdtemp()
44 TMP_TEST_DIR = tempfile.mkdtemp()
42 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")
43 IP_TEST_DIR = os.path.join(HOME_TEST_DIR,'.ipython')
46 IP_TEST_DIR = os.path.join(HOME_TEST_DIR,'.ipython')
44
47
45 #
48 #
46 # Setup/teardown functions/decorators
49 # Setup/teardown functions/decorators
47 #
50 #
48
51
49 def setup():
52 def setup():
50 """Setup test environment for the module:
53 """Setup test environment for the module:
51
54
52 - Adds dummy home dir tree
55 - Adds dummy home dir tree
53 """
56 """
54 # Do not mask exceptions here. In particular, catching WindowsError is a
57 # Do not mask exceptions here. In particular, catching WindowsError is a
55 # problem because that exception is only defined on Windows...
58 # problem because that exception is only defined on Windows...
56 os.makedirs(IP_TEST_DIR)
59 os.makedirs(IP_TEST_DIR)
57
60
58
61
59 def teardown():
62 def teardown():
60 """Teardown test environment for the module:
63 """Teardown test environment for the module:
61
64
62 - Remove dummy home dir tree
65 - Remove dummy home dir tree
63 """
66 """
64 # 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
65 # 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
66 # that non-empty directories are all recursively removed.
69 # that non-empty directories are all recursively removed.
67 shutil.rmtree(TMP_TEST_DIR)
70 shutil.rmtree(TMP_TEST_DIR)
68
71
69
72
70 #-----------------------------------------------------------------------------
73 #-----------------------------------------------------------------------------
71 # Test functions
74 # Test functions
72 #-----------------------------------------------------------------------------
75 #-----------------------------------------------------------------------------
73 def win32_without_pywin32():
76 def win32_without_pywin32():
74 if sys.platform == 'win32':
77 if sys.platform == 'win32':
75 try:
78 try:
76 import pywin32
79 import pywin32
77 except ImportError:
80 except ImportError:
78 return True
81 return True
79 return False
82 return False
80
83
81
84
82 @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows")
85 class ProfileDirTest(TestCase):
83 def test_startup_py():
86 def setUp(self):
84 # create profile dir
87 # create profile dir
85 pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, 'test')
88 self.pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, 'test')
86 # write startup python file
89 self.options = ['--ipython-dir', IP_TEST_DIR, '--profile', 'test']
87 with open(os.path.join(pd.startup_dir, '00-start.py'), 'w') as f:
90 self.fname = os.path.join(TMP_TEST_DIR, 'test.py')
88 f.write('zzz=123\n')
91
89 # write simple test file, to check that the startup file was run
92 def tearDown(self):
90 fname = os.path.join(TMP_TEST_DIR, 'test.py')
93 shutil.rmtree(self.pd.location)
91 with open(fname, 'w') as f:
94
92 f.write(py3compat.doctest_refactor_print('print zzz\n'))
95 def init(self, startup_file, startup, test):
93 # validate output
96 # write startup python file
94 tt.ipexec_validate(fname, '123', '',
97 with open(os.path.join(self.pd.startup_dir, startup_file), 'w') as f:
95 options=['--ipython-dir', IP_TEST_DIR, '--profile', 'test'])
98 f.write(startup)
96
99 # write simple test file, to check that the startup file was run
97 @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows")
100 with open(self.fname, 'w') as f:
98 def test_startup_ipy():
101 f.write(py3compat.doctest_refactor_print(test))
99 # create profile dir
102
100 pd = ProfileDir.create_profile_dir_by_name(IP_TEST_DIR, 'test')
103 def validate(self, output):
101 # write startup ipython file
104 # validate output
102 with open(os.path.join(pd.startup_dir, '00-start.ipy'), 'w') as f:
105 tt.ipexec_validate(self.fname, output, '', options=self.options)
103 f.write('%profile\n')
106
104 # write empty script, because we don't need anything to happen
107 @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows")
105 # after the startup file is run
108 def test_startup_py(self):
106 fname = os.path.join(TMP_TEST_DIR, 'test.py')
109 self.init('00-start.py', 'zzz=123\n',
107 with open(fname, 'w') as f:
110 py3compat.doctest_refactor_print('print zzz\n'))
108 f.write('')
111 self.validate('123')
109 # validate output
112
110 tt.ipexec_validate(fname, 'test', '',
113 @dec.skipif(win32_without_pywin32(), "Test requires pywin32 on Windows")
111 options=['--ipython-dir', IP_TEST_DIR, '--profile', 'test'])
114 def test_startup_ipy(self):
112
115 self.init('00-start.ipy', '%profile\n', '')
113
116 self.validate('test')
General Comments 0
You need to be logged in to leave comments. Login now