Skip to content

Commit e9e5a93

Browse files
authored
Apply PR #6609 to 6.5.x (Fix rename_file and delete_file to handle hidden files properly) (#6660)
* Fix the path form for rename_file * Fix tests for rename_file to give values in relative paths * Fix tests to be in line with jupyter-server * Fix for determining whether a file is hidden and tests for delete_file Co-authored-by: yacchin1205 <[email protected]>
1 parent 047f69f commit e9e5a93

File tree

2 files changed

+64
-95
lines changed

2 files changed

+64
-95
lines changed

notebook/services/contents/filemanager.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -526,14 +526,14 @@ def delete_file(self, path):
526526
os_path = self._get_os_path(path)
527527
rm = os.unlink
528528

529+
if is_hidden(os_path, self.root_dir) and not self.allow_hidden:
530+
raise web.HTTPError(400, f'Cannot delete file or directory {os_path!r}')
531+
529532
four_o_four = "file or directory does not exist: %r" % path
530533

531534
if not self.exists(path):
532535
raise web.HTTPError(404, four_o_four)
533536

534-
if is_hidden(os_path, self.root_dir) and not self.allow_hidden:
535-
raise web.HTTPError(400, f'Cannot delete file or directory {os_path!r}')
536-
537537
def is_non_empty_dir(os_path):
538538
if os.path.isdir(os_path):
539539
# A directory containing only leftover checkpoints is
@@ -575,16 +575,16 @@ def rename_file(self, old_path, new_path):
575575
if new_path == old_path:
576576
return
577577

578-
if (is_hidden(old_path, self.root_dir) or is_hidden(new_path, self.root_dir)) and not self.allow_hidden:
579-
raise web.HTTPError(400, f'Cannot rename file or directory {old_path!r}')
580-
581578
# Perform path validation prior to converting to os-specific value since this
582579
# is still relative to root_dir.
583580
self._validate_path(new_path)
584581

585582
new_os_path = self._get_os_path(new_path)
586583
old_os_path = self._get_os_path(old_path)
587584

585+
if (is_hidden(old_os_path, self.root_dir) or is_hidden(new_os_path, self.root_dir)) and not self.allow_hidden:
586+
raise web.HTTPError(400, f'Cannot rename file or directory {old_path!r}')
587+
588588
# Should we proceed with the move?
589589
if os.path.exists(new_os_path) and not samefile(old_os_path, new_os_path):
590590
raise web.HTTPError(409, f'File already exists: {new_path}')

notebook/services/contents/tests/test_manager.py

Lines changed: 58 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -185,37 +185,26 @@ def test_403(self):
185185
def test_400(self):
186186
#Test Delete behavior
187187
#Test delete of file in hidden directory
188-
with self.assertRaises(HTTPError) as excinfo:
189-
with TemporaryDirectory() as td:
190-
cm = FileContentsManager(root_dir=td)
191-
hidden_dir = '.hidden'
192-
file_in_hidden_path = os.path.join(hidden_dir,'visible.txt')
193-
_make_dir(cm, hidden_dir)
194-
model = cm.new(path=file_in_hidden_path)
195-
os_path = cm._get_os_path(model['path'])
188+
with TemporaryDirectory() as td:
189+
cm = FileContentsManager(root_dir=td)
190+
hidden_dir = '.hidden'
191+
file_in_hidden_path = os.path.join(hidden_dir,'visible.txt')
192+
_make_dir(cm, hidden_dir)
193+
194+
with self.assertRaises(HTTPError) as excinfo:
195+
cm.delete_file(file_in_hidden_path)
196+
self.assertEqual(excinfo.exception.status_code, 400)
196197

197-
try:
198-
result = cm.delete_file(os_path)
199-
except HTTPError as e:
200-
self.assertEqual(e.status_code, 400)
201-
else:
202-
self.fail("Should have raised HTTPError(400)")
203198
#Test delete hidden file in visible directory
204-
with self.assertRaises(HTTPError) as excinfo:
205-
with TemporaryDirectory() as td:
206-
cm = FileContentsManager(root_dir=td)
207-
hidden_dir = 'visible'
208-
file_in_hidden_path = os.path.join(hidden_dir,'.hidden.txt')
209-
_make_dir(cm, hidden_dir)
210-
model = cm.new(path=file_in_hidden_path)
211-
os_path = cm._get_os_path(model['path'])
199+
with TemporaryDirectory() as td:
200+
cm = FileContentsManager(root_dir=td)
201+
hidden_dir = 'visible'
202+
file_in_hidden_path = os.path.join(hidden_dir,'.hidden.txt')
203+
_make_dir(cm, hidden_dir)
212204

213-
try:
214-
result = cm.delete_file(os_path)
215-
except HTTPError as e:
216-
self.assertEqual(e.status_code, 400)
217-
else:
218-
self.fail("Should have raised HTTPError(400)")
205+
with self.assertRaises(HTTPError) as excinfo:
206+
cm.delete_file(file_in_hidden_path)
207+
self.assertEqual(excinfo.exception.status_code, 400)
219208

220209
#Test Save behavior
221210
#Test save of file in hidden directory
@@ -253,76 +242,56 @@ def test_400(self):
253242

254243
#Test rename behavior
255244
#Test rename with source file in hidden directory
256-
with self.assertRaises(HTTPError) as excinfo:
257-
with TemporaryDirectory() as td:
258-
cm = FileContentsManager(root_dir=td)
259-
hidden_dir = '.hidden'
260-
file_in_hidden_path = os.path.join(hidden_dir,'visible.txt')
261-
_make_dir(cm, hidden_dir)
262-
model = cm.new(path=file_in_hidden_path)
263-
old_path = cm._get_os_path(model['path'])
264-
new_path = "new.txt"
245+
with TemporaryDirectory() as td:
246+
cm = FileContentsManager(root_dir=td)
247+
hidden_dir = '.hidden'
248+
file_in_hidden_path = os.path.join(hidden_dir,'visible.txt')
249+
_make_dir(cm, hidden_dir)
250+
old_path = file_in_hidden_path
251+
new_path = "new.txt"
265252

266-
try:
267-
result = cm.rename_file(old_path, new_path)
268-
except HTTPError as e:
269-
self.assertEqual(e.status_code, 400)
270-
else:
271-
self.fail("Should have raised HTTPError(400)")
253+
with self.assertRaises(HTTPError) as excinfo:
254+
cm.rename_file(old_path, new_path)
255+
self.assertEqual(excinfo.exception.status_code, 400)
272256

273257
#Test rename of dest file in hidden directory
274-
with self.assertRaises(HTTPError) as excinfo:
275-
with TemporaryDirectory() as td:
276-
cm = FileContentsManager(root_dir=td)
277-
hidden_dir = '.hidden'
278-
file_in_hidden_path = os.path.join(hidden_dir,'visible.txt')
279-
_make_dir(cm, hidden_dir)
280-
model = cm.new(path=file_in_hidden_path)
281-
new_path = cm._get_os_path(model['path'])
282-
old_path = "old.txt"
258+
with TemporaryDirectory() as td:
259+
cm = FileContentsManager(root_dir=td)
260+
hidden_dir = '.hidden'
261+
file_in_hidden_path = os.path.join(hidden_dir,'visible.txt')
262+
_make_dir(cm, hidden_dir)
263+
new_path = file_in_hidden_path
264+
old_path = "old.txt"
283265

284-
try:
285-
result = cm.rename_file(old_path, new_path)
286-
except HTTPError as e:
287-
self.assertEqual(e.status_code, 400)
288-
else:
289-
self.fail("Should have raised HTTPError(400)")
266+
with self.assertRaises(HTTPError) as excinfo:
267+
cm.rename_file(old_path, new_path)
268+
self.assertEqual(excinfo.exception.status_code, 400)
290269

291270
#Test rename with hidden source file in visible directory
292-
with self.assertRaises(HTTPError) as excinfo:
293-
with TemporaryDirectory() as td:
294-
cm = FileContentsManager(root_dir=td)
295-
hidden_dir = 'visible'
296-
file_in_hidden_path = os.path.join(hidden_dir,'.hidden.txt')
297-
_make_dir(cm, hidden_dir)
298-
model = cm.new(path=file_in_hidden_path)
299-
old_path = cm._get_os_path(model['path'])
300-
new_path = "new.txt"
271+
with TemporaryDirectory() as td:
272+
cm = FileContentsManager(root_dir=td)
273+
hidden_dir = 'visible'
274+
file_in_hidden_path = os.path.join(hidden_dir,'.hidden.txt')
275+
_make_dir(cm, hidden_dir)
276+
old_path = file_in_hidden_path
277+
new_path = "new.txt"
301278

302-
try:
303-
result = cm.rename_file(old_path, new_path)
304-
except HTTPError as e:
305-
self.assertEqual(e.status_code, 400)
306-
else:
307-
self.fail("Should have raised HTTPError(400)")
279+
with self.assertRaises(HTTPError) as excinfo:
280+
cm.rename_file(old_path, new_path)
281+
self.assertEqual(excinfo.exception.status_code, 400)
308282

309283
#Test rename with hidden dest file in visible directory
310-
with self.assertRaises(HTTPError) as excinfo:
311-
with TemporaryDirectory() as td:
312-
cm = FileContentsManager(root_dir=td)
313-
hidden_dir = 'visible'
314-
file_in_hidden_path = os.path.join(hidden_dir,'.hidden.txt')
315-
_make_dir(cm, hidden_dir)
316-
model = cm.new(path=file_in_hidden_path)
317-
new_path = cm._get_os_path(model['path'])
318-
old_path = "old.txt"
319-
320-
try:
321-
result = cm.rename_file(old_path, new_path)
322-
except HTTPError as e:
323-
self.assertEqual(e.status_code, 400)
324-
else:
325-
self.fail("Should have raised HTTPError(400)")
284+
with TemporaryDirectory() as td:
285+
cm = FileContentsManager(root_dir=td)
286+
hidden_dir = 'visible'
287+
file_in_hidden_path = os.path.join(hidden_dir,'.hidden.txt')
288+
_make_dir(cm, hidden_dir)
289+
new_path = file_in_hidden_path
290+
old_path = "old.txt"
291+
292+
with self.assertRaises(HTTPError) as excinfo:
293+
cm.rename_file(old_path, new_path)
294+
self.assertEqual(excinfo.exception.status_code, 400)
326295

327296
@skipIf(sys.platform.startswith('win'), "Can't test hidden files on Windows")
328297
def test_404(self):

0 commit comments

Comments
 (0)