clan-core/pkgs/clan-cli/tests/test_clan_uri.py

76 lines
2.3 KiB
Python
Raw Normal View History

2023-12-05 17:16:51 +00:00
from pathlib import Path
2024-03-06 19:24:36 +00:00
from clan_cli.clan_uri import ClanURI, ClanUrl
2023-12-05 17:16:51 +00:00
2023-12-05 15:17:15 +00:00
2024-03-06 19:24:36 +00:00
def test_get_url() -> None:
# Create a ClanURI object from a remote URI with parameters
2024-03-06 19:24:36 +00:00
uri = ClanURI("clan://https://example.com?password=1234#myVM")
assert uri.get_url() == "https://example.com?password=1234"
uri = ClanURI("clan://~/Downloads")
2024-03-06 19:24:36 +00:00
assert uri.get_url().endswith("/Downloads")
uri = ClanURI("clan:///home/user/Downloads")
2024-03-06 19:24:36 +00:00
assert uri.get_url() == "/home/user/Downloads"
uri = ClanURI("clan://file:///home/user/Downloads")
2024-03-06 19:24:36 +00:00
assert uri.get_url() == "/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")
2024-03-06 19:24:36 +00:00
match uri.url:
case ClanUrl.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
2024-03-06 19:24:36 +00:00
match uri.url:
case ClanUrl.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
def test_direct_local_path() -> None:
# Create a ClanURI object from a remote URI
uri = ClanURI("clan://~/Downloads")
2024-03-06 19:24:36 +00:00
assert uri.get_url().endswith("/Downloads")
def test_direct_local_path2() -> None:
# Create a ClanURI object from a remote URI
uri = ClanURI("clan:///home/user/Downloads")
2024-03-06 19:24:36 +00:00
assert uri.get_url() == "/home/user/Downloads"
def test_remote_with_clanparams() -> None:
2023-12-05 17:08:27 +00:00
# Create a ClanURI object from a remote URI with parameters
uri = ClanURI("clan://https://example.com")
2023-12-05 17:08:27 +00:00
2024-03-06 19:24:36 +00:00
assert uri.machines[0].name == "defaultVM"
2023-12-05 17:08:27 +00:00
2024-03-06 19:24:36 +00:00
match uri.url:
case ClanUrl.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
def test_remote_with_all_params() -> None:
2024-03-06 19:24:36 +00:00
uri = ClanURI("clan://https://example.com?password=12345#myVM#secondVM")
assert uri.machines[0].name == "myVM"
assert uri.machines[1].name == "secondVM"
match uri.url:
case ClanUrl.REMOTE.value(url):
assert url == "https://example.com?password=12345" # type: ignore
2023-12-05 17:08:27 +00:00
case _:
2023-12-05 17:16:51 +00:00
assert False