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