test_utils.py 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. """Test HTML utils"""
  2. # Copyright (c) Jupyter Development Team.
  3. # Distributed under the terms of the Modified BSD License.
  4. import ctypes
  5. import os
  6. import nose.tools as nt
  7. from traitlets.tests.utils import check_help_all_output
  8. from notebook.utils import url_escape, url_unescape, is_hidden, is_file_hidden
  9. from ipython_genutils.py3compat import cast_unicode
  10. from ipython_genutils.tempdir import TemporaryDirectory
  11. from ipython_genutils.testing.decorators import skip_if_not_win32
  12. def test_help_output():
  13. """jupyter notebook --help-all works"""
  14. # FIXME: will be notebook
  15. check_help_all_output('notebook')
  16. def test_url_escape():
  17. # changes path or notebook name with special characters to url encoding
  18. # these tests specifically encode paths with spaces
  19. path = url_escape('/this is a test/for spaces/')
  20. nt.assert_equal(path, '/this%20is%20a%20test/for%20spaces/')
  21. path = url_escape('notebook with space.ipynb')
  22. nt.assert_equal(path, 'notebook%20with%20space.ipynb')
  23. path = url_escape('/path with a/notebook and space.ipynb')
  24. nt.assert_equal(path, '/path%20with%20a/notebook%20and%20space.ipynb')
  25. path = url_escape('/ !@$#%^&* / test %^ notebook @#$ name.ipynb')
  26. nt.assert_equal(path,
  27. '/%20%21%40%24%23%25%5E%26%2A%20/%20test%20%25%5E%20notebook%20%40%23%24%20name.ipynb')
  28. def test_url_unescape():
  29. # decodes a url string to a plain string
  30. # these tests decode paths with spaces
  31. path = url_unescape('/this%20is%20a%20test/for%20spaces/')
  32. nt.assert_equal(path, '/this is a test/for spaces/')
  33. path = url_unescape('notebook%20with%20space.ipynb')
  34. nt.assert_equal(path, 'notebook with space.ipynb')
  35. path = url_unescape('/path%20with%20a/notebook%20and%20space.ipynb')
  36. nt.assert_equal(path, '/path with a/notebook and space.ipynb')
  37. path = url_unescape(
  38. '/%20%21%40%24%23%25%5E%26%2A%20/%20test%20%25%5E%20notebook%20%40%23%24%20name.ipynb')
  39. nt.assert_equal(path, '/ !@$#%^&* / test %^ notebook @#$ name.ipynb')
  40. def test_is_hidden():
  41. with TemporaryDirectory() as root:
  42. subdir1 = os.path.join(root, 'subdir')
  43. os.makedirs(subdir1)
  44. nt.assert_equal(is_hidden(subdir1, root), False)
  45. nt.assert_equal(is_file_hidden(subdir1), False)
  46. subdir2 = os.path.join(root, '.subdir2')
  47. os.makedirs(subdir2)
  48. nt.assert_equal(is_hidden(subdir2, root), True)
  49. nt.assert_equal(is_file_hidden(subdir2), True)#
  50. # root dir is always visible
  51. nt.assert_equal(is_hidden(subdir2, subdir2), False)
  52. subdir34 = os.path.join(root, 'subdir3', '.subdir4')
  53. os.makedirs(subdir34)
  54. nt.assert_equal(is_hidden(subdir34, root), True)
  55. nt.assert_equal(is_hidden(subdir34), True)
  56. subdir56 = os.path.join(root, '.subdir5', 'subdir6')
  57. os.makedirs(subdir56)
  58. nt.assert_equal(is_hidden(subdir56, root), True)
  59. nt.assert_equal(is_hidden(subdir56), True)
  60. nt.assert_equal(is_file_hidden(subdir56), False)
  61. nt.assert_equal(is_file_hidden(subdir56, os.stat(subdir56)), False)
  62. @skip_if_not_win32
  63. def test_is_hidden_win32():
  64. with TemporaryDirectory() as root:
  65. root = cast_unicode(root)
  66. subdir1 = os.path.join(root, u'subdir')
  67. os.makedirs(subdir1)
  68. assert not is_hidden(subdir1, root)
  69. r = ctypes.windll.kernel32.SetFileAttributesW(subdir1, 0x02)
  70. print(r)
  71. assert is_hidden(subdir1, root)
  72. assert is_file_hidden(subdir1)