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