2023-12-05 17:16:51 +00:00
|
|
|
from pathlib import Path
|
|
|
|
|
2024-03-08 16:47:27 +00:00
|
|
|
from clan_cli.clan_uri import ClanURI
|
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:
|
2023-12-08 23:04:56 +00:00
|
|
|
# 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"
|
2023-12-08 23:04:56 +00:00
|
|
|
|
|
|
|
uri = ClanURI("clan://~/Downloads")
|
2024-03-06 19:24:36 +00:00
|
|
|
assert uri.get_url().endswith("/Downloads")
|
2023-12-08 23:04:56 +00:00
|
|
|
|
|
|
|
uri = ClanURI("clan:///home/user/Downloads")
|
2024-03-06 19:24:36 +00:00
|
|
|
assert uri.get_url() == "/home/user/Downloads"
|
2023-12-08 23:04:56 +00:00
|
|
|
|
|
|
|
uri = ClanURI("clan://file:///home/user/Downloads")
|
2024-03-06 19:24:36 +00:00
|
|
|
assert uri.get_url() == "/home/user/Downloads"
|
2023-12-08 23:04:56 +00:00
|
|
|
|
|
|
|
|
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-08 16:47:27 +00:00
|
|
|
assert uri.flake_id.path == Path("/home/user/Downloads")
|
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")
|
2024-03-08 16:47:27 +00:00
|
|
|
assert uri.flake_id.url == "https://example.com"
|
2023-12-05 17:08:27 +00:00
|
|
|
|
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-03-06 19:24:36 +00:00
|
|
|
assert uri.get_url().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")
|
2024-03-06 19:24:36 +00:00
|
|
|
assert uri.get_url() == "/home/user/Downloads"
|
2023-12-08 12:46:21 +00:00
|
|
|
|
|
|
|
|
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
|
|
|
|
2024-03-07 12:04:48 +00:00
|
|
|
assert uri.machine.name == "defaultVM"
|
2024-03-08 16:47:27 +00:00
|
|
|
assert uri.flake_id.url == "https://example.com"
|
2023-12-05 17:08:27 +00:00
|
|
|
|
2023-12-05 17:16:51 +00:00
|
|
|
|
2023-12-06 12:38:22 +00:00
|
|
|
def test_remote_with_all_params() -> None:
|
2024-03-07 12:04:48 +00:00
|
|
|
uri = ClanURI("clan://https://example.com?password=12345#myVM#secondVM?dummy_opt=1")
|
|
|
|
assert uri.machine.name == "myVM"
|
|
|
|
assert uri._machines[1].name == "secondVM"
|
|
|
|
assert uri._machines[1].params.dummy_opt == "1"
|
2024-03-08 16:47:27 +00:00
|
|
|
assert uri.flake_id.url == "https://example.com?password=12345"
|
2024-03-07 12:04:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_from_str_remote() -> None:
|
|
|
|
uri = ClanURI.from_str(url="https://example.com", machine_name="myVM")
|
|
|
|
assert uri.get_url() == "https://example.com"
|
|
|
|
assert uri.get_orig_uri() == "clan://https://example.com#myVM"
|
|
|
|
assert uri.machine.name == "myVM"
|
|
|
|
assert len(uri._machines) == 1
|
2024-03-08 16:47:27 +00:00
|
|
|
assert uri.flake_id.url == "https://example.com"
|
2024-03-07 12:04:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_from_str_local() -> None:
|
|
|
|
uri = ClanURI.from_str(url="~/Projects/democlan", machine_name="myVM")
|
|
|
|
assert uri.get_url().endswith("/Projects/democlan")
|
|
|
|
assert uri.get_orig_uri() == "clan://~/Projects/democlan#myVM"
|
|
|
|
assert uri.machine.name == "myVM"
|
|
|
|
assert len(uri._machines) == 1
|
2024-03-08 16:47:27 +00:00
|
|
|
assert uri.flake_id.is_local()
|
|
|
|
assert str(uri.flake_id).endswith("/Projects/democlan") # type: ignore
|
2024-03-07 12:04:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_from_str_local_no_machine() -> None:
|
|
|
|
uri = ClanURI.from_str("~/Projects/democlan")
|
|
|
|
assert uri.get_url().endswith("/Projects/democlan")
|
|
|
|
assert uri.get_orig_uri() == "clan://~/Projects/democlan"
|
|
|
|
assert uri.machine.name == "defaultVM"
|
|
|
|
assert len(uri._machines) == 1
|
2024-03-08 16:47:27 +00:00
|
|
|
assert uri.flake_id.is_local()
|
|
|
|
assert str(uri.flake_id).endswith("/Projects/democlan") # type: ignore
|
2024-03-07 12:04:48 +00:00
|
|
|
|
|
|
|
|
|
|
|
def test_from_str_local_no_machine2() -> None:
|
|
|
|
uri = ClanURI.from_str("~/Projects/democlan#syncthing-peer1")
|
|
|
|
assert uri.get_url().endswith("/Projects/democlan")
|
|
|
|
assert uri.get_orig_uri() == "clan://~/Projects/democlan#syncthing-peer1"
|
|
|
|
assert uri.machine.name == "syncthing-peer1"
|
|
|
|
assert len(uri._machines) == 1
|
2024-03-08 16:47:27 +00:00
|
|
|
assert uri.flake_id.is_local()
|
|
|
|
assert str(uri.flake_id).endswith("/Projects/democlan") # type: ignore
|