##// END OF EJS Templates
TEST: Refactor symlink tests.
Scott Sanderson -
Show More
@@ -18,15 +18,26 b' from IPython.testing import decorators as dec'
18 from ..filemanager import FileContentsManager
18 from ..filemanager import FileContentsManager
19
19
20
20
21 def _make_dir(contents_manager, api_path):
22 """
23 Make a directory.
24 """
25 os_path = contents_manager._get_os_path(api_path)
26 try:
27 os.makedirs(os_path)
28 except OSError:
29 print("Directory already exists: %r" % os_path)
30
31
21 class TestFileContentsManager(TestCase):
32 class TestFileContentsManager(TestCase):
22
33
23 def symlink(self, src, dst):
34 def symlink(self, contents_manager, src, dst):
24 """Make a symlink to src from dst
35 """Make a symlink to src from dst
25
36
26 src and dst are api_paths
37 src and dst are api_paths
27 """
38 """
28 src_os_path = self.contents_manager._get_os_path(src)
39 src_os_path = contents_manager._get_os_path(src)
29 dst_os_path = self.contents_manager._get_os_path(dst)
40 dst_os_path = contents_manager._get_os_path(dst)
30 print(src_os_path, dst_os_path, os.path.isfile(src_os_path))
41 print(src_os_path, dst_os_path, os.path.isfile(src_os_path))
31 os.symlink(src_os_path, dst_os_path)
42 os.symlink(src_os_path, dst_os_path)
32
43
@@ -80,35 +91,37 b' class TestFileContentsManager(TestCase):'
80
91
81 @dec.skip_win32
92 @dec.skip_win32
82 def test_bad_symlink(self):
93 def test_bad_symlink(self):
83 cm = self.contents_manager
94 with TemporaryDirectory() as td:
84 path = 'test bad symlink'
95 cm = FileContentsManager(root_dir=td)
85 self.make_dir(path)
96 path = 'test bad symlink'
86
97 _make_dir(cm, path)
87 file_model = cm.new_untitled(path=path, ext='.txt')
98
88
99 file_model = cm.new_untitled(path=path, ext='.txt')
89 # create a broken symlink
100
90 self.symlink("target", '%s/%s' % (path, 'bad symlink'))
101 # create a broken symlink
91 model = cm.get(path)
102 self.symlink(cm, "target", '%s/%s' % (path, 'bad symlink'))
92 self.assertEqual(model['content'], [file_model])
103 model = cm.get(path)
104 self.assertEqual(model['content'], [file_model])
93
105
94 @dec.skip_win32
106 @dec.skip_win32
95 def test_good_symlink(self):
107 def test_good_symlink(self):
96 cm = self.contents_manager
108 with TemporaryDirectory() as td:
97 parent = 'test good symlink'
109 cm = FileContentsManager(root_dir=td)
98 name = 'good symlink'
110 parent = 'test good symlink'
99 path = '{0}/{1}'.format(parent, name)
111 name = 'good symlink'
100 self.make_dir(parent)
112 path = '{0}/{1}'.format(parent, name)
101
113 _make_dir(cm, parent)
102 file_model = cm.new(path=parent + '/zfoo.txt')
114
103
115 file_model = cm.new(path=parent + '/zfoo.txt')
104 # create a good symlink
116
105 self.symlink(file_model['path'], path)
117 # create a good symlink
106 symlink_model = cm.get(path, content=False)
118 self.symlink(cm, file_model['path'], path)
107 dir_model = cm.get(parent)
119 symlink_model = cm.get(path, content=False)
108 self.assertEqual(
120 dir_model = cm.get(parent)
109 sorted(dir_model['content'], key=lambda x: x['name']),
121 self.assertEqual(
110 [symlink_model, file_model],
122 sorted(dir_model['content'], key=lambda x: x['name']),
111 )
123 [symlink_model, file_model],
124 )
112
125
113
126
114 class TestContentsManager(TestCase):
127 class TestContentsManager(TestCase):
@@ -126,11 +139,7 b' class TestContentsManager(TestCase):'
126 def make_dir(self, api_path):
139 def make_dir(self, api_path):
127 """make subdirectory, rel_path is the relative path
140 """make subdirectory, rel_path is the relative path
128 to that directory from the location where the server started"""
141 to that directory from the location where the server started"""
129 os_path = self.contents_manager._get_os_path(api_path)
142 _make_dir(self.contents_manager, api_path)
130 try:
131 os.makedirs(os_path)
132 except OSError:
133 print("Directory already exists: %r" % os_path)
134
143
135 def add_code_cell(self, nb):
144 def add_code_cell(self, nb):
136 output = nbformat.new_output("display_data", {'application/javascript': "alert('hi');"})
145 output = nbformat.new_output("display_data", {'application/javascript': "alert('hi');"})
General Comments 0
You need to be logged in to leave comments. Login now