2023-12-05 17:16:51 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2023-12-08 12:46:21 +00:00
|
|
|
from clan_cli.clan_uri import ClanParameters, ClanScheme, ClanURI
|
2023-12-05 17:16:51 +00:00
|
|
|
|
2023-12-05 15:17:15 +00:00
|
|
|
|
2023-12-08 23:04:56 +00:00
|
|
|
def test_get_internal() -> None:
|
|
|
|
# Create a ClanURI object from a remote URI with parameters
|
|
|
|
uri = ClanURI("clan://https://example.com?flake_attr=myVM&password=1234")
|
|
|
|
assert uri.get_internal() == "https://example.com?password=1234"
|
|
|
|
|
|
|
|
uri = ClanURI("clan://~/Downloads")
|
2024-01-02 15:34:10 +00:00
|
|
|
assert uri.get_internal().endswith("/Downloads")
|
2023-12-08 23:04:56 +00:00
|
|
|
|
|
|
|
uri = ClanURI("clan:///home/user/Downloads")
|
|
|
|
assert uri.get_internal() == "/home/user/Downloads"
|
|
|
|
|
|
|
|
uri = ClanURI("clan://file:///home/user/Downloads")
|
|
|
|
assert uri.get_internal() == "/home/user/Downloads"
|
|
|
|
|
|
|
|
|
2023-12-05 15:17:15 +00:00
|
|
|
def test_local_uri() -> None:
|
|
|
|
# Create a ClanURI object from a local URI
|
|
|
|
uri = ClanURI("clan://file:///home/user/Downloads")
|
2023-12-05 17:08:27 +00:00
|
|
|
match uri.scheme:
|
2024-01-02 13:55:20 +00:00
|
|
|
case ClanScheme.LOCAL.value(path):
|
2023-12-05 17:16:51 +00:00
|
|
|
assert path == Path("/home/user/Downloads") # type: ignore
|
2023-12-05 17:08:27 +00:00
|
|
|
case _:
|
|
|
|
assert False
|
2023-12-05 15:17:15 +00:00
|
|
|
|
2023-12-05 17:16:51 +00:00
|
|
|
|
2023-12-05 15:17:15 +00:00
|
|
|
def test_is_remote() -> None:
|
|
|
|
# Create a ClanURI object from a remote URI
|
|
|
|
uri = ClanURI("clan://https://example.com")
|
2023-12-05 17:08:27 +00:00
|
|
|
|
|
|
|
match uri.scheme:
|
2024-01-02 13:55:20 +00:00
|
|
|
case ClanScheme.REMOTE.value(url):
|
2023-12-05 17:16:51 +00:00
|
|
|
assert url == "https://example.com" # type: ignore
|
2023-12-05 17:08:27 +00:00
|
|
|
case _:
|
|
|
|
assert False
|
|
|
|
|
2023-12-05 17:16:51 +00:00
|
|
|
|
2023-12-08 12:46:21 +00:00
|
|
|
def test_direct_local_path() -> None:
|
|
|
|
# Create a ClanURI object from a remote URI
|
|
|
|
uri = ClanURI("clan://~/Downloads")
|
2024-01-02 15:34:10 +00:00
|
|
|
assert uri.get_internal().endswith("/Downloads")
|
2023-12-08 12:46:21 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_direct_local_path2() -> None:
|
|
|
|
# Create a ClanURI object from a remote URI
|
|
|
|
uri = ClanURI("clan:///home/user/Downloads")
|
|
|
|
assert uri.get_internal() == "/home/user/Downloads"
|
|
|
|
|
|
|
|
|
2023-12-06 12:38:22 +00:00
|
|
|
def test_remote_with_clanparams() -> None:
|
2023-12-05 17:08:27 +00:00
|
|
|
# Create a ClanURI object from a remote URI with parameters
|
2023-12-06 12:38:22 +00:00
|
|
|
uri = ClanURI("clan://https://example.com")
|
2023-12-05 17:08:27 +00:00
|
|
|
|
|
|
|
assert uri.params.flake_attr == "defaultVM"
|
|
|
|
|
|
|
|
match uri.scheme:
|
2024-01-02 13:55:20 +00:00
|
|
|
case ClanScheme.REMOTE.value(url):
|
2023-12-05 17:16:51 +00:00
|
|
|
assert url == "https://example.com" # type: ignore
|
2023-12-05 17:08:27 +00:00
|
|
|
case _:
|
|
|
|
assert False
|
|
|
|
|
2023-12-05 17:16:51 +00:00
|
|
|
|
2023-12-08 12:46:21 +00:00
|
|
|
def test_from_path_with_custom() -> None:
|
|
|
|
# Create a ClanURI object from a remote URI with parameters
|
|
|
|
uri_str = Path("/home/user/Downloads")
|
|
|
|
params = ClanParameters(flake_attr="myVM")
|
2024-01-16 16:11:26 +00:00
|
|
|
uri = ClanURI.from_path(uri_str, params=params)
|
2023-12-08 12:46:21 +00:00
|
|
|
assert uri.params.flake_attr == "myVM"
|
|
|
|
|
|
|
|
match uri.scheme:
|
2024-01-02 13:55:20 +00:00
|
|
|
case ClanScheme.LOCAL.value(path):
|
2023-12-08 12:46:21 +00:00
|
|
|
assert path == Path("/home/user/Downloads") # type: ignore
|
|
|
|
case _:
|
|
|
|
assert False
|
|
|
|
|
|
|
|
|
|
|
|
def test_from_path_with_default() -> None:
|
|
|
|
# Create a ClanURI object from a remote URI with parameters
|
|
|
|
uri_str = Path("/home/user/Downloads")
|
|
|
|
params = ClanParameters()
|
2024-01-16 16:11:26 +00:00
|
|
|
uri = ClanURI.from_path(uri_str, params=params)
|
2023-12-08 12:46:21 +00:00
|
|
|
assert uri.params.flake_attr == "defaultVM"
|
|
|
|
|
|
|
|
match uri.scheme:
|
2024-01-02 13:55:20 +00:00
|
|
|
case ClanScheme.LOCAL.value(path):
|
2023-12-08 12:46:21 +00:00
|
|
|
assert path == Path("/home/user/Downloads") # type: ignore
|
|
|
|
case _:
|
|
|
|
assert False
|
|
|
|
|
|
|
|
|
2023-12-12 20:31:47 +00:00
|
|
|
def test_from_str() -> None:
|
|
|
|
# Create a ClanURI object from a remote URI with parameters
|
|
|
|
uri_str = "https://example.com?password=asdasd&test=1234"
|
|
|
|
params = ClanParameters(flake_attr="myVM")
|
|
|
|
uri = ClanURI.from_str(url=uri_str, params=params)
|
|
|
|
assert uri.params.flake_attr == "myVM"
|
|
|
|
|
|
|
|
match uri.scheme:
|
2024-01-02 13:55:20 +00:00
|
|
|
case ClanScheme.REMOTE.value(url):
|
2023-12-12 20:31:47 +00:00
|
|
|
assert url == "https://example.com?password=asdasd&test=1234" # type: ignore
|
|
|
|
case _:
|
|
|
|
assert False
|
|
|
|
|
2024-01-16 16:11:26 +00:00
|
|
|
uri = ClanURI.from_str(url=uri_str, params={"flake_attr": "myVM"})
|
|
|
|
assert uri.params.flake_attr == "myVM"
|
|
|
|
|
|
|
|
uri = ClanURI.from_str(uri_str, "myVM")
|
|
|
|
assert uri.params.flake_attr == "myVM"
|
|
|
|
|
2023-12-12 20:31:47 +00:00
|
|
|
uri_str = "~/Downloads/democlan"
|
|
|
|
params = ClanParameters(flake_attr="myVM")
|
|
|
|
uri = ClanURI.from_str(url=uri_str, params=params)
|
|
|
|
assert uri.params.flake_attr == "myVM"
|
2024-01-02 15:34:10 +00:00
|
|
|
assert uri.get_internal().endswith("/Downloads/democlan")
|
2023-12-12 20:31:47 +00:00
|
|
|
|
|
|
|
uri_str = "~/Downloads/democlan"
|
|
|
|
uri = ClanURI.from_str(url=uri_str)
|
|
|
|
assert uri.params.flake_attr == "defaultVM"
|
2024-01-02 15:34:10 +00:00
|
|
|
assert uri.get_internal().endswith("/Downloads/democlan")
|
2023-12-12 20:31:47 +00:00
|
|
|
|
2023-12-14 17:25:42 +00:00
|
|
|
uri_str = "clan://~/Downloads/democlan"
|
|
|
|
uri = ClanURI.from_str(url=uri_str)
|
|
|
|
assert uri.params.flake_attr == "defaultVM"
|
2024-01-02 15:34:10 +00:00
|
|
|
assert uri.get_internal().endswith("/Downloads/democlan")
|
2023-12-14 17:25:42 +00:00
|
|
|
|
2023-12-12 20:31:47 +00:00
|
|
|
|
2023-12-06 12:38:22 +00:00
|
|
|
def test_remote_with_all_params() -> None:
|
2023-12-05 17:08:27 +00:00
|
|
|
# Create a ClanURI object from a remote URI with parameters
|
2023-12-06 12:38:22 +00:00
|
|
|
uri = ClanURI("clan://https://example.com?flake_attr=myVM&password=1234")
|
|
|
|
assert uri.params.flake_attr == "myVM"
|
2023-12-05 17:08:27 +00:00
|
|
|
|
|
|
|
match uri.scheme:
|
2024-01-02 13:55:20 +00:00
|
|
|
case ClanScheme.REMOTE.value(url):
|
2023-12-06 12:38:22 +00:00
|
|
|
assert url == "https://example.com?password=1234" # type: ignore
|
2023-12-05 17:08:27 +00:00
|
|
|
case _:
|
2023-12-05 17:16:51 +00:00
|
|
|
assert False
|
2024-01-16 16:11:26 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_with_hashtag() -> None:
|
|
|
|
uri = ClanURI("clan://https://example.com?flake_attr=thirdVM#myVM#secondVM")
|
|
|
|
assert uri.params.flake_attr == "myVM"
|