-
Notifications
You must be signed in to change notification settings - Fork 452
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
109 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,61 +1,78 @@ | ||
from pathlib import PurePosixPath, PureWindowsPath | ||
from unittest.mock import patch | ||
|
||
import pytest | ||
|
||
from tribler.core.utilities.rest_utils import path_to_uri, scheme_from_uri, uri_is_valid_file, uri_to_path | ||
from tribler.core.utilities.rest_utils import FILE_SCHEME, HTTP_SCHEME, MAGNET_SCHEME, path_to_url, scheme_from_url, \ | ||
url_is_valid_file, \ | ||
url_to_path | ||
|
||
NIX_PATHS = [ | ||
('/path/to/file', 'file:///path/to/file'), | ||
('/path/to/file with space', 'file:///path/to/file%20with%20space'), | ||
('/path/to/%20%21file', 'file:///path/to/%2520%2521file'), # See: https://github.com/Tribler/tribler/issues/6700 | ||
POSIX_PATHS = [ | ||
('//path/to/file'), | ||
('/path/to/file'), | ||
('/path/to/file with space'), | ||
('/path/to/%20%21file'), # See: https://github.com/Tribler/tribler/issues/6700 | ||
] | ||
|
||
POSIX_URL = [ | ||
('file:/path', '/path'), | ||
('file:///path', '/path'), | ||
('file://part/path', '/path'), | ||
] | ||
|
||
WIN_PATHS = [ | ||
('C:\\path\\to\\file', 'file:///C:%5Cpath%5Cto%5Cfile'), | ||
('C:\\path\\to\\file with space', 'file:///C:%5Cpath%5Cto%5Cfile%20with%20space'), | ||
('C:\\path\\to\\%20%21file', 'file:///C:%5Cpath%5Cto%5C%2520%2521file'), | ||
(r'C:\path\to\file'), | ||
(r'C:\path\to\file with space'), | ||
(r'C:\path\to\%20%21file'), | ||
] | ||
|
||
WIN_URL = [ | ||
('file://server/share/path', r'\\server\share\path'), | ||
] | ||
|
||
SCHEMES = [ | ||
('file:///path/to/file', 'file'), | ||
('magnet:link', 'magnet'), | ||
('http://en.wikipedia.org', 'http'), | ||
('file:///path/to/file', FILE_SCHEME), | ||
('magnet:link', MAGNET_SCHEME), | ||
('http://en.wikipedia.org', HTTP_SCHEME), | ||
] | ||
|
||
|
||
# posix | ||
@pytest.mark.parametrize('path, uri', NIX_PATHS) | ||
@pytest.mark.parametrize('path', POSIX_PATHS) | ||
@patch('os.name', 'posix') | ||
def test_path_to_uri(path, uri): | ||
assert path_to_uri(path) == uri | ||
def test_round_trip_posix(path): | ||
url = path_to_url(path, _path_cls=PurePosixPath) | ||
assert url_to_path(url, _path_cls=PurePosixPath) == path | ||
|
||
|
||
@pytest.mark.parametrize('path, uri', NIX_PATHS) | ||
@pytest.mark.parametrize('url, path', POSIX_URL) | ||
@patch('os.name', 'posix') | ||
def test_uri_to_path(path, uri): | ||
assert uri_to_path(uri) == path | ||
def test_posix_url_to_path(url, path): | ||
assert url_to_path(url, _path_cls=PurePosixPath) == path | ||
|
||
|
||
# win | ||
@pytest.mark.parametrize('path, uri', WIN_PATHS) | ||
@pytest.mark.parametrize('path', WIN_PATHS) | ||
@patch('os.name', 'nt') | ||
def test_path_to_uri_win(path, uri): | ||
assert path_to_uri(path) == uri | ||
def test_round_trip_win(path): | ||
url = path_to_url(path, _path_cls=PureWindowsPath) | ||
assert url_to_path(url, _path_cls=PureWindowsPath) == path | ||
|
||
|
||
@pytest.mark.parametrize('path, uri', WIN_PATHS) | ||
@pytest.mark.parametrize('url, path', WIN_URL) | ||
@patch('os.name', 'nt') | ||
def test_uri_to_path_win(path, uri): | ||
assert uri_to_path(uri) == path | ||
def test_win_url_to_path(url, path): | ||
assert url_to_path(url, _path_cls=PureWindowsPath) == path | ||
|
||
|
||
@pytest.mark.parametrize('path, scheme', SCHEMES) | ||
def test_scheme_from_uri(path, scheme): | ||
assert scheme_from_uri(path) == scheme | ||
assert scheme_from_url(path) == scheme | ||
|
||
|
||
def test_uri_is_valid_file(tmpdir): | ||
file_path = tmpdir / '1.txt' | ||
file_path.write('test') | ||
file_uri = path_to_uri(file_path) | ||
assert uri_is_valid_file(file_uri) | ||
assert not uri_is_valid_file(file_uri + '/*') | ||
file_uri = path_to_url(file_path) | ||
assert url_is_valid_file(file_uri) | ||
assert not url_is_valid_file(file_uri + '/*') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.