2023-05-17 02:21:35 +08:00
|
|
|
from __future__ import annotations
|
|
|
|
|
2023-08-21 21:54:20 +08:00
|
|
|
import json
|
2021-12-16 23:43:31 +08:00
|
|
|
import os
|
2023-02-08 00:25:24 +08:00
|
|
|
import sys
|
2021-09-14 23:28:55 +08:00
|
|
|
import warnings
|
2023-08-21 21:54:20 +08:00
|
|
|
from pathlib import Path
|
2024-02-02 03:32:40 +08:00
|
|
|
from unittest.mock import MagicMock, patch
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2023-12-06 06:18:09 +08:00
|
|
|
import httpx
|
2022-06-24 01:44:04 +08:00
|
|
|
import pytest
|
2022-07-19 22:48:46 +08:00
|
|
|
from typing_extensions import Literal
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2023-05-05 10:38:41 +08:00
|
|
|
from gradio import EventData, Request
|
2024-02-07 04:27:44 +08:00
|
|
|
from gradio.external_utils import format_ner_list
|
2022-02-10 16:12:26 +08:00
|
|
|
from gradio.utils import (
|
2023-01-21 11:52:42 +08:00
|
|
|
abspath,
|
2022-09-02 19:10:20 +08:00
|
|
|
append_unique_suffix,
|
2022-04-08 17:13:56 +08:00
|
|
|
assert_configs_are_equivalent_besides_ids,
|
2023-04-20 03:44:28 +08:00
|
|
|
check_function_inputs_match,
|
2022-02-10 16:12:26 +08:00
|
|
|
colab_check,
|
2022-04-21 01:54:44 +08:00
|
|
|
delete_none,
|
2023-06-07 02:46:07 +08:00
|
|
|
get_continuous_fn,
|
2023-11-16 14:36:39 +08:00
|
|
|
get_extension_from_file_path_or_url,
|
2023-04-20 03:44:28 +08:00
|
|
|
get_type_hints,
|
2022-02-10 16:12:26 +08:00
|
|
|
ipython_check,
|
2023-06-01 11:19:17 +08:00
|
|
|
is_in_or_equal,
|
2023-05-05 10:38:41 +08:00
|
|
|
is_special_typed_parameter,
|
2023-02-02 00:53:53 +08:00
|
|
|
kaggle_check,
|
2022-02-10 16:12:26 +08:00
|
|
|
readme_to_html,
|
2023-02-08 00:25:24 +08:00
|
|
|
sagemaker_check,
|
2022-08-23 23:31:04 +08:00
|
|
|
sanitize_list_for_csv,
|
|
|
|
sanitize_value_for_csv,
|
2023-05-02 12:59:31 +08:00
|
|
|
tex2svg,
|
2022-11-17 04:21:51 +08:00
|
|
|
validate_url,
|
2022-02-10 16:12:26 +08:00
|
|
|
)
|
2021-11-09 22:48:55 +08:00
|
|
|
|
2021-11-10 02:30:59 +08:00
|
|
|
os.environ["GRADIO_ANALYTICS_ENABLED"] = "False"
|
2021-09-03 05:01:51 +08:00
|
|
|
|
|
|
|
|
2022-11-08 08:37:55 +08:00
|
|
|
class TestUtils:
|
2024-02-02 03:32:40 +08:00
|
|
|
@patch("IPython.get_ipython")
|
2021-10-21 01:19:39 +08:00
|
|
|
def test_colab_check_no_ipython(self, mock_get_ipython):
|
|
|
|
mock_get_ipython.return_value = None
|
|
|
|
assert colab_check() is False
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2024-02-02 03:32:40 +08:00
|
|
|
@patch("IPython.get_ipython")
|
2021-10-21 01:19:39 +08:00
|
|
|
def test_ipython_check_import_fail(self, mock_get_ipython):
|
|
|
|
mock_get_ipython.side_effect = ImportError()
|
|
|
|
assert ipython_check() is False
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2024-02-02 03:32:40 +08:00
|
|
|
@patch("IPython.get_ipython")
|
2021-10-21 01:19:39 +08:00
|
|
|
def test_ipython_check_no_ipython(self, mock_get_ipython):
|
|
|
|
mock_get_ipython.return_value = None
|
|
|
|
assert ipython_check() is False
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2024-02-02 03:32:40 +08:00
|
|
|
@patch("httpx.get")
|
2021-10-27 02:20:18 +08:00
|
|
|
def test_readme_to_html_doesnt_crash_on_connection_error(self, mock_get):
|
2023-12-06 06:18:09 +08:00
|
|
|
mock_get.side_effect = httpx.ConnectError("Connection error")
|
2021-10-21 01:19:39 +08:00
|
|
|
readme_to_html("placeholder")
|
2022-01-21 21:44:12 +08:00
|
|
|
|
2021-10-22 05:30:32 +08:00
|
|
|
def test_readme_to_html_correct_parse(self):
|
2021-12-22 03:40:22 +08:00
|
|
|
readme_to_html("https://github.com/gradio-app/gradio/blob/master/README.md")
|
|
|
|
|
2023-02-08 00:25:24 +08:00
|
|
|
def test_sagemaker_check_false(self):
|
|
|
|
assert not sagemaker_check()
|
|
|
|
|
|
|
|
def test_sagemaker_check_false_if_boto3_not_installed(self):
|
2024-02-02 03:32:40 +08:00
|
|
|
with patch.dict(sys.modules, {"boto3": None}, clear=True):
|
2023-02-08 00:25:24 +08:00
|
|
|
assert not sagemaker_check()
|
|
|
|
|
2024-02-02 03:32:40 +08:00
|
|
|
@patch("boto3.session.Session.client")
|
2023-02-08 00:25:24 +08:00
|
|
|
def test_sagemaker_check_true(self, mock_client):
|
|
|
|
mock_client().get_caller_identity = MagicMock(
|
|
|
|
return_value={
|
|
|
|
"Arn": "arn:aws:sts::67364438:assumed-role/SageMaker-Datascients/SageMaker"
|
|
|
|
}
|
|
|
|
)
|
|
|
|
assert sagemaker_check()
|
|
|
|
|
2023-02-02 00:53:53 +08:00
|
|
|
def test_kaggle_check_false(self):
|
|
|
|
assert not kaggle_check()
|
|
|
|
|
|
|
|
def test_kaggle_check_true_when_run_type_set(self):
|
2024-02-02 03:32:40 +08:00
|
|
|
with patch.dict(
|
2023-02-02 00:53:53 +08:00
|
|
|
os.environ, {"KAGGLE_KERNEL_RUN_TYPE": "Interactive"}, clear=True
|
|
|
|
):
|
|
|
|
assert kaggle_check()
|
|
|
|
|
|
|
|
def test_kaggle_check_true_when_both_set(self):
|
2024-02-02 03:32:40 +08:00
|
|
|
with patch.dict(
|
2023-02-02 00:53:53 +08:00
|
|
|
os.environ,
|
|
|
|
{"KAGGLE_KERNEL_RUN_TYPE": "Interactive", "GFOOTBALL_DATA_DIR": "./"},
|
|
|
|
clear=True,
|
|
|
|
):
|
|
|
|
assert kaggle_check()
|
|
|
|
|
|
|
|
def test_kaggle_check_false_when_neither_set(self):
|
2024-02-02 03:32:40 +08:00
|
|
|
with patch.dict(
|
2023-02-02 00:53:53 +08:00
|
|
|
os.environ,
|
|
|
|
{"KAGGLE_KERNEL_RUN_TYPE": "", "GFOOTBALL_DATA_DIR": ""},
|
|
|
|
clear=True,
|
|
|
|
):
|
|
|
|
assert not kaggle_check()
|
|
|
|
|
2021-10-27 02:20:18 +08:00
|
|
|
|
2023-08-21 21:54:20 +08:00
|
|
|
def test_assert_configs_are_equivalent():
|
|
|
|
test_dir = Path(__file__).parent / "test_files"
|
|
|
|
with open(test_dir / "xray_config.json") as fp:
|
|
|
|
xray_config = json.load(fp)
|
|
|
|
with open(test_dir / "xray_config_diff_ids.json") as fp:
|
|
|
|
xray_config_diff_ids = json.load(fp)
|
|
|
|
with open(test_dir / "xray_config_wrong.json") as fp:
|
|
|
|
xray_config_wrong = json.load(fp)
|
2022-04-08 17:13:56 +08:00
|
|
|
|
2023-08-21 21:54:20 +08:00
|
|
|
assert assert_configs_are_equivalent_besides_ids(xray_config, xray_config)
|
|
|
|
assert assert_configs_are_equivalent_besides_ids(xray_config, xray_config_diff_ids)
|
2023-10-07 09:14:14 +08:00
|
|
|
with pytest.raises(ValueError):
|
2023-08-21 21:54:20 +08:00
|
|
|
assert_configs_are_equivalent_besides_ids(xray_config, xray_config_wrong)
|
2022-07-06 06:19:41 +08:00
|
|
|
|
2022-04-08 17:13:56 +08:00
|
|
|
|
2022-11-08 08:37:55 +08:00
|
|
|
class TestFormatNERList:
|
2022-03-22 01:42:53 +08:00
|
|
|
def test_format_ner_list_standard(self):
|
|
|
|
string = "Wolfgang lives in Berlin"
|
2022-03-22 02:02:17 +08:00
|
|
|
groups = [
|
|
|
|
{"entity_group": "PER", "start": 0, "end": 8},
|
|
|
|
{"entity_group": "LOC", "start": 18, "end": 24},
|
|
|
|
]
|
|
|
|
result = [
|
|
|
|
("", None),
|
|
|
|
("Wolfgang", "PER"),
|
|
|
|
(" lives in ", None),
|
|
|
|
("Berlin", "LOC"),
|
|
|
|
("", None),
|
|
|
|
]
|
2022-11-08 08:37:55 +08:00
|
|
|
assert format_ner_list(string, groups) == result
|
2022-03-22 01:42:53 +08:00
|
|
|
|
|
|
|
def test_format_ner_list_empty(self):
|
|
|
|
string = "I live in a city"
|
|
|
|
groups = []
|
|
|
|
result = [("I live in a city", None)]
|
2022-11-08 08:37:55 +08:00
|
|
|
assert format_ner_list(string, groups) == result
|
2022-03-22 01:42:53 +08:00
|
|
|
|
|
|
|
|
2022-11-08 08:37:55 +08:00
|
|
|
class TestDeleteNone:
|
2022-04-21 01:54:44 +08:00
|
|
|
"""Credit: https://stackoverflow.com/questions/33797126/proper-way-to-remove-keys-in-dictionary-with-none-values-in-python"""
|
|
|
|
|
|
|
|
def test_delete_none(self):
|
|
|
|
input = {
|
|
|
|
"a": 12,
|
|
|
|
"b": 34,
|
|
|
|
"c": None,
|
|
|
|
"k": {
|
|
|
|
"d": 34,
|
|
|
|
"t": None,
|
|
|
|
"m": [{"k": 23, "t": None}, [None, 1, 2, 3], {1, 2, None}],
|
|
|
|
None: 123,
|
|
|
|
},
|
|
|
|
}
|
2023-02-14 05:28:43 +08:00
|
|
|
truth = {
|
|
|
|
"a": 12,
|
|
|
|
"b": 34,
|
|
|
|
"k": {
|
|
|
|
"d": 34,
|
|
|
|
"t": None,
|
|
|
|
"m": [{"k": 23, "t": None}, [None, 1, 2, 3], {1, 2, None}],
|
|
|
|
None: 123,
|
|
|
|
},
|
|
|
|
}
|
2022-11-08 08:37:55 +08:00
|
|
|
assert delete_none(input) == truth
|
2022-04-21 01:54:44 +08:00
|
|
|
|
|
|
|
|
2022-08-23 23:31:04 +08:00
|
|
|
class TestSanitizeForCSV:
|
|
|
|
def test_unsafe_value(self):
|
|
|
|
assert sanitize_value_for_csv("=OPEN()") == "'=OPEN()"
|
|
|
|
assert sanitize_value_for_csv("=1+2") == "'=1+2"
|
|
|
|
assert sanitize_value_for_csv('=1+2";=1+2') == "'=1+2\";=1+2"
|
|
|
|
|
|
|
|
def test_safe_value(self):
|
|
|
|
assert sanitize_value_for_csv(4) == 4
|
|
|
|
assert sanitize_value_for_csv(-44.44) == -44.44
|
|
|
|
assert sanitize_value_for_csv("1+1=2") == "1+1=2"
|
|
|
|
assert sanitize_value_for_csv("1aaa2") == "1aaa2"
|
|
|
|
|
|
|
|
def test_list(self):
|
|
|
|
assert sanitize_list_for_csv([4, "def=", "=gh+ij"]) == [4, "def=", "'=gh+ij"]
|
|
|
|
assert sanitize_list_for_csv(
|
|
|
|
[["=abc", "def", "gh,+ij"], ["abc", "=def", "+ghij"]]
|
|
|
|
) == [["'=abc", "def", "'gh,+ij"], ["abc", "'=def", "'+ghij"]]
|
|
|
|
assert sanitize_list_for_csv([1, ["ab", "=de"]]) == [1, ["ab", "'=de"]]
|
|
|
|
|
|
|
|
|
2022-11-17 04:21:51 +08:00
|
|
|
class TestValidateURL:
|
|
|
|
@pytest.mark.flaky
|
|
|
|
def test_valid_urls(self):
|
|
|
|
assert validate_url("https://www.gradio.app")
|
|
|
|
assert validate_url("http://gradio.dev")
|
|
|
|
assert validate_url(
|
|
|
|
"https://upload.wikimedia.org/wikipedia/commons/b/b0/Bengal_tiger_%28Panthera_tigris_tigris%29_female_3_crop.jpg"
|
|
|
|
)
|
2024-02-07 03:46:20 +08:00
|
|
|
assert validate_url(
|
|
|
|
"https://huggingface.co/datasets/Xenova/transformers.js-docs/resolve/main/bread_small.png"
|
|
|
|
)
|
2022-11-17 04:21:51 +08:00
|
|
|
|
|
|
|
def test_invalid_urls(self):
|
|
|
|
assert not (validate_url("C:/Users/"))
|
|
|
|
assert not (validate_url("C:\\Users\\"))
|
|
|
|
assert not (validate_url("/home/user"))
|
|
|
|
|
|
|
|
|
2022-09-02 19:10:20 +08:00
|
|
|
class TestAppendUniqueSuffix:
|
|
|
|
def test_no_suffix(self):
|
|
|
|
name = "test"
|
|
|
|
list_of_names = ["test_1", "test_2"]
|
|
|
|
assert append_unique_suffix(name, list_of_names) == name
|
|
|
|
|
|
|
|
def test_first_suffix(self):
|
|
|
|
name = "test"
|
|
|
|
list_of_names = ["test", "test_-1"]
|
|
|
|
assert append_unique_suffix(name, list_of_names) == "test_1"
|
|
|
|
|
|
|
|
def test_later_suffix(self):
|
|
|
|
name = "test"
|
|
|
|
list_of_names = ["test", "test_1", "test_2", "test_3"]
|
|
|
|
assert append_unique_suffix(name, list_of_names) == "test_4"
|
2022-11-28 10:55:10 +08:00
|
|
|
|
|
|
|
|
2023-01-21 11:52:42 +08:00
|
|
|
class TestAbspath:
|
|
|
|
def test_abspath_no_symlink(self):
|
|
|
|
resolved_path = str(abspath("../gradio/gradio/test_data/lion.jpg"))
|
|
|
|
assert ".." not in resolved_path
|
|
|
|
|
2023-04-21 03:18:33 +08:00
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform.startswith("win"),
|
|
|
|
reason="Windows doesn't allow creation of sym links without administrative privileges",
|
|
|
|
)
|
|
|
|
def test_abspath_symlink_path(self):
|
|
|
|
os.symlink("gradio/test_data", "gradio/test_link", True)
|
|
|
|
resolved_path = str(abspath("../gradio/gradio/test_link/lion.jpg"))
|
|
|
|
os.unlink("gradio/test_link")
|
|
|
|
assert "test_link" in resolved_path
|
|
|
|
|
|
|
|
@pytest.mark.skipif(
|
|
|
|
sys.platform.startswith("win"),
|
|
|
|
reason="Windows doesn't allow creation of sym links without administrative privileges",
|
|
|
|
)
|
|
|
|
def test_abspath_symlink_dir(self):
|
|
|
|
os.symlink("gradio/test_data", "gradio/test_link", True)
|
|
|
|
full_path = os.path.join(os.getcwd(), "gradio/test_link/lion.jpg")
|
|
|
|
resolved_path = str(abspath(full_path))
|
|
|
|
os.unlink("gradio/test_link")
|
|
|
|
assert "test_link" in resolved_path
|
|
|
|
assert full_path == resolved_path
|
2023-04-20 03:44:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
class TestGetTypeHints:
|
|
|
|
def test_get_type_hints(self):
|
|
|
|
class F:
|
|
|
|
def __call__(self, s: str):
|
|
|
|
return s
|
|
|
|
|
|
|
|
class C:
|
|
|
|
def f(self, s: str):
|
|
|
|
return s
|
|
|
|
|
|
|
|
def f(s: str):
|
|
|
|
return s
|
|
|
|
|
|
|
|
class GenericObject:
|
|
|
|
pass
|
|
|
|
|
|
|
|
test_objs = [F(), C().f, f]
|
|
|
|
|
|
|
|
for x in test_objs:
|
|
|
|
hints = get_type_hints(x)
|
|
|
|
assert len(hints) == 1
|
|
|
|
assert hints["s"] == str
|
|
|
|
|
|
|
|
assert len(get_type_hints(GenericObject())) == 0
|
|
|
|
|
2023-05-05 10:38:41 +08:00
|
|
|
def test_is_special_typed_parameter(self):
|
2023-05-17 02:21:35 +08:00
|
|
|
def func(a: list[str], b: Literal["a", "b"], c, d: Request):
|
2023-05-05 10:38:41 +08:00
|
|
|
pass
|
|
|
|
|
|
|
|
hints = get_type_hints(func)
|
|
|
|
assert not is_special_typed_parameter("a", hints)
|
|
|
|
assert not is_special_typed_parameter("b", hints)
|
|
|
|
assert not is_special_typed_parameter("c", hints)
|
|
|
|
assert is_special_typed_parameter("d", hints)
|
|
|
|
|
2023-05-17 02:21:35 +08:00
|
|
|
def test_is_special_typed_parameter_with_pipe(self):
|
|
|
|
def func(a: Request, b: str | int, c: list[str]):
|
|
|
|
pass
|
|
|
|
|
|
|
|
hints = get_type_hints(func)
|
|
|
|
assert is_special_typed_parameter("a", hints)
|
|
|
|
assert not is_special_typed_parameter("b", hints)
|
|
|
|
assert not is_special_typed_parameter("c", hints)
|
|
|
|
|
2023-04-20 03:44:28 +08:00
|
|
|
|
|
|
|
class TestCheckFunctionInputsMatch:
|
|
|
|
def test_check_function_inputs_match(self):
|
|
|
|
class F:
|
|
|
|
def __call__(self, s: str, evt: EventData):
|
|
|
|
return s
|
|
|
|
|
|
|
|
class C:
|
|
|
|
def f(self, s: str, evt: EventData):
|
|
|
|
return s
|
|
|
|
|
|
|
|
def f(s: str, evt: EventData):
|
|
|
|
return s
|
|
|
|
|
|
|
|
test_objs = [F(), C().f, f]
|
|
|
|
|
|
|
|
with warnings.catch_warnings():
|
|
|
|
warnings.simplefilter("error") # Ensure there're no warnings raised here.
|
|
|
|
|
|
|
|
for x in test_objs:
|
|
|
|
check_function_inputs_match(x, [None], False)
|
2023-05-02 12:59:31 +08:00
|
|
|
|
|
|
|
|
2023-06-07 02:46:07 +08:00
|
|
|
class TestGetContinuousFn:
|
2023-11-13 16:32:00 +08:00
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_get_continuous_fn(self):
|
2023-06-07 02:46:07 +08:00
|
|
|
def int_return(x): # for origin condition
|
|
|
|
return x + 1
|
|
|
|
|
|
|
|
def int_yield(x): # new condition
|
|
|
|
for _i in range(2):
|
|
|
|
yield x
|
|
|
|
x += 1
|
|
|
|
|
|
|
|
def list_yield(x): # new condition
|
|
|
|
for _i in range(2):
|
|
|
|
yield x
|
|
|
|
x += [1]
|
|
|
|
|
2023-11-13 16:32:00 +08:00
|
|
|
agen_int_return = get_continuous_fn(fn=int_return, every=0.01)
|
|
|
|
agen_int_yield = get_continuous_fn(fn=int_yield, every=0.01)
|
|
|
|
agen_list_yield = get_continuous_fn(fn=list_yield, every=0.01)
|
|
|
|
agener_int_return = agen_int_return(1)
|
|
|
|
agener_int = agen_int_yield(1) # Primitive
|
|
|
|
agener_list = agen_list_yield([1]) # Reference
|
|
|
|
assert await agener_int_return.__anext__() == 2
|
|
|
|
assert await agener_int_return.__anext__() == 2
|
|
|
|
assert await agener_int.__anext__() == 1
|
|
|
|
assert await agener_int.__anext__() == 2
|
2023-11-14 05:12:49 +08:00
|
|
|
assert await agener_int.__anext__() == 1
|
|
|
|
assert [1] == await agener_list.__anext__()
|
|
|
|
assert [1, 1] == await agener_list.__anext__()
|
|
|
|
assert [1, 1, 1] == await agener_list.__anext__()
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_get_continuous_fn_with_async_function(self):
|
|
|
|
async def async_int_return(x): # for origin condition
|
|
|
|
return x + 1
|
|
|
|
|
|
|
|
agen_int_return = get_continuous_fn(fn=async_int_return, every=0.01)
|
|
|
|
agener_int_return = agen_int_return(1)
|
|
|
|
assert await agener_int_return.__anext__() == 2
|
|
|
|
assert await agener_int_return.__anext__() == 2
|
|
|
|
|
|
|
|
@pytest.mark.asyncio
|
|
|
|
async def test_get_continuous_fn_with_async_generator(self):
|
|
|
|
async def async_int_yield(x): # new condition
|
|
|
|
for _i in range(2):
|
|
|
|
yield x
|
|
|
|
x += 1
|
|
|
|
|
|
|
|
async def async_list_yield(x): # new condition
|
|
|
|
for _i in range(2):
|
|
|
|
yield x
|
|
|
|
x += [1]
|
|
|
|
|
|
|
|
agen_int_yield = get_continuous_fn(fn=async_int_yield, every=0.01)
|
|
|
|
agen_list_yield = get_continuous_fn(fn=async_list_yield, every=0.01)
|
|
|
|
agener_int = agen_int_yield(1) # Primitive
|
|
|
|
agener_list = agen_list_yield([1]) # Reference
|
|
|
|
assert await agener_int.__anext__() == 1
|
|
|
|
assert await agener_int.__anext__() == 2
|
2023-11-13 16:32:00 +08:00
|
|
|
assert await agener_int.__anext__() == 1
|
|
|
|
assert [1] == await agener_list.__anext__()
|
|
|
|
assert [1, 1] == await agener_list.__anext__()
|
|
|
|
assert [1, 1, 1] == await agener_list.__anext__()
|
2023-06-07 02:46:07 +08:00
|
|
|
|
|
|
|
|
2023-05-02 12:59:31 +08:00
|
|
|
def test_tex2svg_preserves_matplotlib_backend():
|
|
|
|
import matplotlib
|
|
|
|
|
|
|
|
matplotlib.use("svg")
|
|
|
|
tex2svg("1+1=2")
|
|
|
|
assert matplotlib.get_backend() == "svg"
|
|
|
|
with pytest.raises(
|
|
|
|
Exception # specifically a pyparsing.ParseException but not important here
|
|
|
|
):
|
|
|
|
tex2svg("$$$1+1=2$$$")
|
|
|
|
assert matplotlib.get_backend() == "svg"
|
2023-06-01 11:19:17 +08:00
|
|
|
|
|
|
|
|
|
|
|
def test_is_in_or_equal():
|
|
|
|
assert is_in_or_equal("files/lion.jpg", "files/lion.jpg")
|
|
|
|
assert is_in_or_equal("files/lion.jpg", "files")
|
|
|
|
assert not is_in_or_equal("files", "files/lion.jpg")
|
|
|
|
assert is_in_or_equal("/home/usr/notes.txt", "/home/usr/")
|
|
|
|
assert not is_in_or_equal("/home/usr/subdirectory", "/home/usr/notes.txt")
|
|
|
|
assert not is_in_or_equal("/home/usr/../../etc/notes.txt", "/home/usr/")
|
2023-12-19 06:30:22 +08:00
|
|
|
assert not is_in_or_equal("/safe_dir/subdir/../../unsafe_file.txt", "/safe_dir/")
|
2023-11-16 14:36:39 +08:00
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"path_or_url, extension",
|
|
|
|
[
|
|
|
|
("https://example.com/avatar/xxxx.mp4?se=2023-11-16T06:51:23Z&sp=r", "mp4"),
|
|
|
|
("/home/user/documents/example.pdf", "pdf"),
|
|
|
|
("C:\\Users\\user\\documents\\example.png", "png"),
|
|
|
|
("C:/Users/user/documents/example", ""),
|
|
|
|
],
|
|
|
|
)
|
|
|
|
def test_get_extension_from_file_path_or_url(path_or_url, extension):
|
|
|
|
assert get_extension_from_file_path_or_url(path_or_url) == extension
|